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