1 | package org.openstreetmap.josm.actions.search;
|
---|
2 |
|
---|
3 | import org.junit.Assert;
|
---|
4 | import org.junit.Before;
|
---|
5 | import org.junit.Test;
|
---|
6 | import org.openstreetmap.josm.Main;
|
---|
7 | import org.openstreetmap.josm.data.Preferences;
|
---|
8 | import org.openstreetmap.josm.data.osm.Node;
|
---|
9 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
10 |
|
---|
11 | public class SearchCompilerTest {
|
---|
12 |
|
---|
13 | @Before
|
---|
14 | public void setUp() throws Exception {
|
---|
15 | Main.pref = new Preferences();
|
---|
16 | }
|
---|
17 |
|
---|
18 | protected OsmPrimitive newPrimitive(String key, String value) {
|
---|
19 | final Node p = new Node();
|
---|
20 | p.put(key, value);
|
---|
21 | return p;
|
---|
22 | }
|
---|
23 |
|
---|
24 | @Test
|
---|
25 | public void testAny() throws Exception {
|
---|
26 | final SearchCompiler.Match c = SearchCompiler.compile("foo", false, false);
|
---|
27 | Assert.assertTrue(c.match(newPrimitive("foobar", "true")));
|
---|
28 | Assert.assertTrue(c.match(newPrimitive("name", "hello-foo-xy")));
|
---|
29 | Assert.assertFalse(c.match(newPrimitive("name", "X")));
|
---|
30 | }
|
---|
31 |
|
---|
32 | @Test
|
---|
33 | public void testEquals() throws Exception {
|
---|
34 | final SearchCompiler.Match c = SearchCompiler.compile("foo=bar", false, false);
|
---|
35 | Assert.assertFalse(c.match(newPrimitive("foobar", "true")));
|
---|
36 | Assert.assertTrue(c.match(newPrimitive("foo", "bar")));
|
---|
37 | Assert.assertFalse(c.match(newPrimitive("fooX", "bar")));
|
---|
38 | Assert.assertFalse(c.match(newPrimitive("foo", "barX")));
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Test
|
---|
42 | public void testCompare() throws Exception {
|
---|
43 | final SearchCompiler.Match c1 = SearchCompiler.compile("start_date>1950", false, false);
|
---|
44 | Assert.assertTrue(c1.match(newPrimitive("start_date", "1950-01-01")));
|
---|
45 | Assert.assertTrue(c1.match(newPrimitive("start_date", "1960")));
|
---|
46 | Assert.assertFalse(c1.match(newPrimitive("start_date", "1950")));
|
---|
47 | Assert.assertFalse(c1.match(newPrimitive("start_date", "1000")));
|
---|
48 | Assert.assertTrue(c1.match(newPrimitive("start_date", "101010")));
|
---|
49 | final SearchCompiler.Match c2 = SearchCompiler.compile("start_date<1960", false, false);
|
---|
50 | Assert.assertTrue(c2.match(newPrimitive("start_date", "1950-01-01")));
|
---|
51 | Assert.assertFalse(c2.match(newPrimitive("start_date", "1960")));
|
---|
52 | Assert.assertTrue(c2.match(newPrimitive("start_date", "1950")));
|
---|
53 | Assert.assertTrue(c2.match(newPrimitive("start_date", "1000")));
|
---|
54 | Assert.assertTrue(c2.match(newPrimitive("start_date", "200")));
|
---|
55 | final SearchCompiler.Match c3 = SearchCompiler.compile("name<I", false, false);
|
---|
56 | Assert.assertTrue(c3.match(newPrimitive("name", "Alpha")));
|
---|
57 | Assert.assertFalse(c3.match(newPrimitive("name", "Sigma")));
|
---|
58 | final SearchCompiler.Match c4 = SearchCompiler.compile("\"start_date\"<1960", false, false);
|
---|
59 | Assert.assertTrue(c4.match(newPrimitive("start_date", "1950-01-01")));
|
---|
60 | Assert.assertFalse(c4.match(newPrimitive("start_date", "2000")));
|
---|
61 |
|
---|
62 | }
|
---|
63 | }
|
---|