source: josm/trunk/test/unit/org/openstreetmap/josm/actions/search/SearchCompilerTest.java@ 8857

Last change on this file since 8857 was 8857, checked in by Don-vip, 9 years ago

improve/cleanup unit tests

  • Property svn:eol-style set to native
File size: 4.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.search;
3
4import org.hamcrest.CoreMatchers;
5import org.junit.Assert;
6import org.junit.Before;
7import org.junit.Test;
8import org.openstreetmap.josm.JOSMFixture;
9import org.openstreetmap.josm.data.coor.LatLon;
10import org.openstreetmap.josm.data.osm.DataSet;
11import org.openstreetmap.josm.data.osm.Node;
12import org.openstreetmap.josm.data.osm.OsmPrimitive;
13import org.openstreetmap.josm.data.osm.Way;
14
15/**
16 * Unit tests for class {@link SearchCompiler}.
17 */
18public class SearchCompilerTest {
19
20 /**
21 * Setup test.
22 */
23 @Before
24 public void setUp() {
25 JOSMFixture.createUnitTestFixture().init();
26 }
27
28 protected OsmPrimitive newPrimitive(String key, String value) {
29 final Node p = new Node();
30 p.put(key, value);
31 return p;
32 }
33
34 @Test
35 public void testAny() throws Exception {
36 final SearchCompiler.Match c = SearchCompiler.compile("foo");
37 Assert.assertTrue(c.match(newPrimitive("foobar", "true")));
38 Assert.assertTrue(c.match(newPrimitive("name", "hello-foo-xy")));
39 Assert.assertFalse(c.match(newPrimitive("name", "X")));
40 }
41
42 @Test
43 public void testEquals() throws Exception {
44 final SearchCompiler.Match c = SearchCompiler.compile("foo=bar");
45 Assert.assertFalse(c.match(newPrimitive("foobar", "true")));
46 Assert.assertTrue(c.match(newPrimitive("foo", "bar")));
47 Assert.assertFalse(c.match(newPrimitive("fooX", "bar")));
48 Assert.assertFalse(c.match(newPrimitive("foo", "barX")));
49 }
50
51 @Test
52 public void testCompare() throws Exception {
53 final SearchCompiler.Match c1 = SearchCompiler.compile("start_date>1950");
54 Assert.assertTrue(c1.match(newPrimitive("start_date", "1950-01-01")));
55 Assert.assertTrue(c1.match(newPrimitive("start_date", "1960")));
56 Assert.assertFalse(c1.match(newPrimitive("start_date", "1950")));
57 Assert.assertFalse(c1.match(newPrimitive("start_date", "1000")));
58 Assert.assertTrue(c1.match(newPrimitive("start_date", "101010")));
59
60 final SearchCompiler.Match c2 = SearchCompiler.compile("start_date<1960");
61 Assert.assertTrue(c2.match(newPrimitive("start_date", "1950-01-01")));
62 Assert.assertFalse(c2.match(newPrimitive("start_date", "1960")));
63 Assert.assertTrue(c2.match(newPrimitive("start_date", "1950")));
64 Assert.assertTrue(c2.match(newPrimitive("start_date", "1000")));
65 Assert.assertTrue(c2.match(newPrimitive("start_date", "200")));
66
67 final SearchCompiler.Match c3 = SearchCompiler.compile("name<I");
68 Assert.assertTrue(c3.match(newPrimitive("name", "Alpha")));
69 Assert.assertFalse(c3.match(newPrimitive("name", "Sigma")));
70
71 final SearchCompiler.Match c4 = SearchCompiler.compile("\"start_date\"<1960");
72 Assert.assertTrue(c4.match(newPrimitive("start_date", "1950-01-01")));
73 Assert.assertFalse(c4.match(newPrimitive("start_date", "2000")));
74
75 final SearchCompiler.Match c5 = SearchCompiler.compile("height>180");
76 Assert.assertTrue(c5.match(newPrimitive("height", "200")));
77 Assert.assertTrue(c5.match(newPrimitive("height", "99999")));
78 Assert.assertFalse(c5.match(newPrimitive("height", "50")));
79 Assert.assertFalse(c5.match(newPrimitive("height", "-9999")));
80 Assert.assertFalse(c5.match(newPrimitive("height", "fixme")));
81
82 final SearchCompiler.Match c6 = SearchCompiler.compile("name>C");
83 Assert.assertTrue(c6.match(newPrimitive("name", "Delta")));
84 Assert.assertFalse(c6.match(newPrimitive("name", "Alpha")));
85 }
86
87 @Test
88 public void testNth() throws Exception {
89 final DataSet dataSet = new DataSet();
90 final Way way = new Way();
91 final Node node0 = new Node(new LatLon(1, 1));
92 final Node node1 = new Node(new LatLon(2, 2));
93 final Node node2 = new Node(new LatLon(3, 3));
94 dataSet.addPrimitive(way);
95 dataSet.addPrimitive(node0);
96 dataSet.addPrimitive(node1);
97 dataSet.addPrimitive(node2);
98 way.addNode(node0);
99 way.addNode(node1);
100 way.addNode(node2);
101 Assert.assertFalse(SearchCompiler.compile("nth:2").match(node1));
102 Assert.assertTrue(SearchCompiler.compile("nth:1").match(node1));
103 Assert.assertFalse(SearchCompiler.compile("nth:0").match(node1));
104 Assert.assertTrue(SearchCompiler.compile("nth:0").match(node0));
105 Assert.assertTrue(SearchCompiler.compile("nth:2").match(node2));
106 Assert.assertTrue(SearchCompiler.compile("nth:-1").match(node2));
107 Assert.assertTrue(SearchCompiler.compile("nth:-2").match(node1));
108 Assert.assertTrue(SearchCompiler.compile("nth:-3").match(node0));
109 }
110
111 @Test
112 public void testNthParseNegative() throws Exception {
113 Assert.assertThat(SearchCompiler.compile("nth:-1").toString(), CoreMatchers.is("Nth{nth=-1, modulo=false}"));
114 }
115}
Note: See TracBrowser for help on using the repository browser.