Ignore:
Timestamp:
2015-10-11T19:16:00+02:00 (9 years ago)
Author:
Don-vip
Message:

improve search compiler unit test

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/actions/search/SearchCompilerTest.java

    r8857 r8859  
    22package org.openstreetmap.josm.actions.search;
    33
     4import static org.junit.Assert.assertFalse;
     5import static org.junit.Assert.assertThat;
     6import static org.junit.Assert.assertTrue;
     7
    48import org.hamcrest.CoreMatchers;
    5 import org.junit.Assert;
    69import org.junit.Before;
    710import org.junit.Test;
    811import org.openstreetmap.josm.JOSMFixture;
     12import org.openstreetmap.josm.actions.search.SearchCompiler.Match;
     13import org.openstreetmap.josm.actions.search.SearchCompiler.ParseError;
    914import org.openstreetmap.josm.data.coor.LatLon;
    1015import org.openstreetmap.josm.data.osm.DataSet;
    1116import org.openstreetmap.josm.data.osm.Node;
    1217import org.openstreetmap.josm.data.osm.OsmPrimitive;
     18import org.openstreetmap.josm.data.osm.Relation;
     19import org.openstreetmap.josm.data.osm.RelationData;
     20import org.openstreetmap.josm.data.osm.RelationMember;
    1321import org.openstreetmap.josm.data.osm.Way;
     22import org.openstreetmap.josm.data.osm.WayData;
    1423
    1524/**
     
    2635    }
    2736
     37    private static final class SearchContext {
     38        final DataSet ds = new DataSet();
     39        final Node n1 = new Node(new LatLon(0, 0));
     40        final Node n2 = new Node(new LatLon(5, 5));
     41        final Way w1 = new Way();
     42        final Way w2 = new Way();
     43        final Relation r1 = new Relation();
     44        final Relation r2 = new Relation();
     45
     46        private final Match m;
     47        private final Match n;
     48
     49        private SearchContext(String state) throws ParseError {
     50            m = SearchCompiler.compile(state);
     51            n = SearchCompiler.compile('-' + state);
     52            ds.addPrimitive(n1);
     53            ds.addPrimitive(n2);
     54            w1.addNode(n1);
     55            w1.addNode(n2);
     56            w2.addNode(n1);
     57            w2.addNode(n2);
     58            ds.addPrimitive(w1);
     59            ds.addPrimitive(w2);
     60            r1.addMember(new RelationMember("", w1));
     61            r1.addMember(new RelationMember("", w2));
     62            r2.addMember(new RelationMember("", w1));
     63            r2.addMember(new RelationMember("", w2));
     64            ds.addPrimitive(r1);
     65            ds.addPrimitive(r2);
     66        }
     67
     68        private void match(OsmPrimitive p, boolean cond) {
     69            if (cond) {
     70                assertTrue(p.toString(), m.match(p));
     71                assertFalse(p.toString(), n.match(p));
     72            } else {
     73                assertFalse(p.toString(), m.match(p));
     74                assertTrue(p.toString(), n.match(p));
     75            }
     76        }
     77    }
     78
    2879    protected OsmPrimitive newPrimitive(String key, String value) {
    2980        final Node p = new Node();
     
    3283    }
    3384
    34     @Test
    35     public void testAny() throws Exception {
     85    /**
     86     * Search anything.
     87     * @throws ParseError if an error has been encountered while compiling
     88     */
     89    @Test
     90    public void testAny() throws ParseError {
    3691        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 {
     92        assertTrue(c.match(newPrimitive("foobar", "true")));
     93        assertTrue(c.match(newPrimitive("name", "hello-foo-xy")));
     94        assertFalse(c.match(newPrimitive("name", "X")));
     95    }
     96
     97    /**
     98     * Search by equality key=value.
     99     * @throws ParseError if an error has been encountered while compiling
     100     */
     101    @Test
     102    public void testEquals() throws ParseError {
    44103        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 {
     104        assertFalse(c.match(newPrimitive("foobar", "true")));
     105        assertTrue(c.match(newPrimitive("foo", "bar")));
     106        assertFalse(c.match(newPrimitive("fooX", "bar")));
     107        assertFalse(c.match(newPrimitive("foo", "barX")));
     108    }
     109
     110    /**
     111     * Search by comparison.
     112     * @throws ParseError if an error has been encountered while compiling
     113     */
     114    @Test
     115    public void testCompare() throws ParseError {
    53116        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")));
     117        assertTrue(c1.match(newPrimitive("start_date", "1950-01-01")));
     118        assertTrue(c1.match(newPrimitive("start_date", "1960")));
     119        assertFalse(c1.match(newPrimitive("start_date", "1950")));
     120        assertFalse(c1.match(newPrimitive("start_date", "1000")));
     121        assertTrue(c1.match(newPrimitive("start_date", "101010")));
    59122
    60123        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")));
     124        assertTrue(c2.match(newPrimitive("start_date", "1950-01-01")));
     125        assertFalse(c2.match(newPrimitive("start_date", "1960")));
     126        assertTrue(c2.match(newPrimitive("start_date", "1950")));
     127        assertTrue(c2.match(newPrimitive("start_date", "1000")));
     128        assertTrue(c2.match(newPrimitive("start_date", "200")));
    66129
    67130        final SearchCompiler.Match c3 = SearchCompiler.compile("name<I");
    68         Assert.assertTrue(c3.match(newPrimitive("name", "Alpha")));
    69         Assert.assertFalse(c3.match(newPrimitive("name", "Sigma")));
     131        assertTrue(c3.match(newPrimitive("name", "Alpha")));
     132        assertFalse(c3.match(newPrimitive("name", "Sigma")));
    70133
    71134        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")));
     135        assertTrue(c4.match(newPrimitive("start_date", "1950-01-01")));
     136        assertFalse(c4.match(newPrimitive("start_date", "2000")));
    74137
    75138        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")));
     139        assertTrue(c5.match(newPrimitive("height", "200")));
     140        assertTrue(c5.match(newPrimitive("height", "99999")));
     141        assertFalse(c5.match(newPrimitive("height", "50")));
     142        assertFalse(c5.match(newPrimitive("height", "-9999")));
     143        assertFalse(c5.match(newPrimitive("height", "fixme")));
    81144
    82145        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 {
     146        assertTrue(c6.match(newPrimitive("name", "Delta")));
     147        assertFalse(c6.match(newPrimitive("name", "Alpha")));
     148    }
     149
     150    /**
     151     * Search by nth.
     152     * @throws ParseError if an error has been encountered while compiling
     153     */
     154    @Test
     155    public void testNth() throws ParseError {
    89156        final DataSet dataSet = new DataSet();
    90157        final Way way = new Way();
     
    99166        way.addNode(node1);
    100167        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}"));
     168        assertFalse(SearchCompiler.compile("nth:2").match(node1));
     169        assertTrue(SearchCompiler.compile("nth:1").match(node1));
     170        assertFalse(SearchCompiler.compile("nth:0").match(node1));
     171        assertTrue(SearchCompiler.compile("nth:0").match(node0));
     172        assertTrue(SearchCompiler.compile("nth:2").match(node2));
     173        assertTrue(SearchCompiler.compile("nth:-1").match(node2));
     174        assertTrue(SearchCompiler.compile("nth:-2").match(node1));
     175        assertTrue(SearchCompiler.compile("nth:-3").match(node0));
     176    }
     177
     178    /**
     179     * Search by negative nth.
     180     * @throws ParseError if an error has been encountered while compiling
     181     */
     182    @Test
     183    public void testNthParseNegative() throws ParseError {
     184        assertThat(SearchCompiler.compile("nth:-1").toString(), CoreMatchers.is("Nth{nth=-1, modulo=false}"));
     185    }
     186
     187    /**
     188     * Search by modified status.
     189     * @throws ParseError if an error has been encountered while compiling
     190     */
     191    @Test
     192    public void testModified() throws ParseError {
     193        SearchContext sc = new SearchContext("modified");
     194        // Not modified but new
     195        for (OsmPrimitive p : new OsmPrimitive[]{sc.n1, sc.w1, sc.r1}) {
     196            assertFalse(p.toString(), p.isModified());
     197            assertTrue(p.toString(), p.isNewOrUndeleted());
     198            sc.match(p, true);
     199        }
     200        // Modified and new
     201        for (OsmPrimitive p : new OsmPrimitive[]{sc.n1, sc.w1, sc.r1}) {
     202            p.setModified(true);
     203            assertTrue(p.toString(), p.isModified());
     204            assertTrue(p.toString(), p.isNewOrUndeleted());
     205            sc.match(p, true);
     206        }
     207        // Modified but not new
     208        for (OsmPrimitive p : new OsmPrimitive[]{sc.n1, sc.w1, sc.r1}) {
     209            p.setOsmId(1, 1);
     210            assertTrue(p.toString(), p.isModified());
     211            assertFalse(p.toString(), p.isNewOrUndeleted());
     212            sc.match(p, true);
     213        }
     214        // Not modified nor new
     215        for (OsmPrimitive p : new OsmPrimitive[]{sc.n2, sc.w2, sc.r2}) {
     216            p.setOsmId(2, 2);
     217            assertFalse(p.toString(), p.isModified());
     218            assertFalse(p.toString(), p.isNewOrUndeleted());
     219            sc.match(p, false);
     220        }
     221    }
     222
     223    /**
     224     * Search by selected status.
     225     * @throws ParseError if an error has been encountered while compiling
     226     */
     227    @Test
     228    public void testSelected() throws ParseError {
     229        SearchContext sc = new SearchContext("selected");
     230        // Not selected
     231        for (OsmPrimitive p : new OsmPrimitive[]{sc.n1, sc.w1, sc.r1}) {
     232            assertFalse(p.toString(), p.isSelected());
     233            sc.match(p, false);
     234        }
     235        // Selected
     236        for (OsmPrimitive p : new OsmPrimitive[]{sc.n2, sc.w2, sc.r2}) {
     237            sc.ds.addSelected(p);
     238            assertTrue(p.toString(), p.isSelected());
     239            sc.match(p, true);
     240        }
     241    }
     242
     243    /**
     244     * Search by incomplete status.
     245     * @throws ParseError if an error has been encountered while compiling
     246     */
     247    @Test
     248    public void testIncomplete() throws ParseError {
     249        SearchContext sc = new SearchContext("incomplete");
     250        // Not incomplete
     251        for (OsmPrimitive p : new OsmPrimitive[]{sc.n1, sc.w1, sc.r1}) {
     252            assertFalse(p.toString(), p.isIncomplete());
     253            sc.match(p, false);
     254        }
     255        // Incomplete
     256        sc.n2.setCoor(null);
     257        WayData wd = new WayData();
     258        wd.setIncomplete(true);
     259        sc.w2.load(wd);
     260        RelationData rd = new RelationData();
     261        rd.setIncomplete(true);
     262        sc.r2.load(rd);
     263        for (OsmPrimitive p : new OsmPrimitive[]{sc.n2, sc.w2, sc.r2}) {
     264            assertTrue(p.toString(), p.isIncomplete());
     265            sc.match(p, true);
     266        }
     267    }
     268
     269    /**
     270     * Search by untagged status.
     271     * @throws ParseError if an error has been encountered while compiling
     272     */
     273    @Test
     274    public void testUntagged() throws ParseError {
     275        SearchContext sc = new SearchContext("untagged");
     276        // Untagged
     277        for (OsmPrimitive p : new OsmPrimitive[]{sc.n1, sc.w1, sc.r1}) {
     278            assertFalse(p.toString(), p.isTagged());
     279            sc.match(p, true);
     280        }
     281        // Tagged
     282        for (OsmPrimitive p : new OsmPrimitive[]{sc.n2, sc.w2, sc.r2}) {
     283            p.put("foo", "bar");
     284            assertTrue(p.toString(), p.isTagged());
     285            sc.match(p, false);
     286        }
     287    }
     288
     289    /**
     290     * Search by closed status.
     291     * @throws ParseError if an error has been encountered while compiling
     292     */
     293    @Test
     294    public void testClosed() throws ParseError {
     295        SearchContext sc = new SearchContext("closed");
     296        // Closed
     297        sc.w1.addNode(sc.n1);
     298        for (Way w : new Way[]{sc.w1}) {
     299            assertTrue(w.toString(), w.isClosed());
     300            sc.match(w, true);
     301        }
     302        // Unclosed
     303        for (OsmPrimitive p : new OsmPrimitive[]{sc.n1, sc.n2, sc.w2, sc.r1, sc.r2}) {
     304            sc.match(p, false);
     305        }
     306    }
     307
     308    /**
     309     * Search by new status.
     310     * @throws ParseError if an error has been encountered while compiling
     311     */
     312    @Test
     313    public void testNew() throws ParseError {
     314        SearchContext sc = new SearchContext("new");
     315        // New
     316        for (OsmPrimitive p : new OsmPrimitive[]{sc.n1, sc.w1, sc.r1}) {
     317            assertTrue(p.toString(), p.isNew());
     318            sc.match(p, true);
     319        }
     320        // Not new
     321        for (OsmPrimitive p : new OsmPrimitive[]{sc.n2, sc.w2, sc.r2}) {
     322            p.setOsmId(2, 2);
     323            assertFalse(p.toString(), p.isNew());
     324            sc.match(p, false);
     325        }
    114326    }
    115327}
Note: See TracChangeset for help on using the changeset viewer.