Changeset 9930 in josm


Ignore:
Timestamp:
2016-03-05T16:25:52+01:00 (8 years ago)
Author:
simon04
Message:

Search: fix string representation of boolean connectives (parentheses)

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/search/SearchCompiler.java

    r9701 r9930  
    453453        @Override
    454454        public String toString() {
    455             return lhs + " && " + rhs;
     455            return (lhs instanceof BinaryMatch && !(lhs instanceof And) ? "(" + lhs + ")" : lhs) + " && "
     456                    + (rhs instanceof BinaryMatch && !(rhs instanceof And) ? "(" + rhs + ")" : rhs);
    456457        }
    457458    }
     
    477478        @Override
    478479        public String toString() {
    479             return lhs + " || " + rhs;
     480            return (lhs instanceof BinaryMatch && !(lhs instanceof Or) ? "(" + lhs + ")" : lhs) + " || "
     481                    + (rhs instanceof BinaryMatch && !(rhs instanceof Or) ? "(" + rhs + ")" : rhs);
    480482        }
    481483    }
     
    501503        @Override
    502504        public String toString() {
    503             return lhs + " ^ " + rhs;
     505            return (lhs instanceof BinaryMatch && !(lhs instanceof Xor) ? "(" + lhs + ")" : lhs) + " ^ "
     506                    + (rhs instanceof BinaryMatch && !(rhs instanceof Xor) ? "(" + rhs + ")" : rhs);
    504507        }
    505508    }
  • trunk/test/unit/org/openstreetmap/josm/actions/search/SearchCompilerTest.java

    r9701 r9930  
    44import static org.junit.Assert.assertEquals;
    55import static org.junit.Assert.assertFalse;
    6 import static org.junit.Assert.assertThat;
    76import static org.junit.Assert.assertTrue;
    87
    9 import org.hamcrest.CoreMatchers;
    108import org.junit.Before;
    119import org.junit.Test;
     
    9795        assertTrue(c.match(newPrimitive("name", "hello-foo-xy")));
    9896        assertFalse(c.match(newPrimitive("name", "X")));
     97        assertEquals("foo", c.toString());
    9998    }
    10099
     
    110109        assertFalse(c.match(newPrimitive("fooX", "bar")));
    111110        assertFalse(c.match(newPrimitive("foo", "barX")));
     111        assertEquals("foo=bar", c.toString());
    112112    }
    113113
     
    186186    @Test
    187187    public void testNthParseNegative() throws ParseError {
    188         assertThat(SearchCompiler.compile("nth:-1").toString(), CoreMatchers.is("Nth{nth=-1, modulo=false}"));
     188        assertEquals("Nth{nth=-1, modulo=false}", SearchCompiler.compile("nth:-1").toString());
    189189    }
    190190
     
    408408        assertFalse(search.match(n1));
    409409    }
     410
     411    /**
     412     * Tests the implementation of the Boolean logic.
     413     * @throws ParseError if an error has been encountered while compiling
     414     */
     415    @Test
     416    public void testBooleanLogic() throws ParseError {
     417        final SearchCompiler.Match c1 = SearchCompiler.compile("foo AND bar AND baz");
     418        assertTrue(c1.match(newPrimitive("foobar", "baz")));
     419        assertEquals("foo && bar && baz", c1.toString());
     420        final SearchCompiler.Match c2 = SearchCompiler.compile("foo AND (bar OR baz)");
     421        assertTrue(c2.match(newPrimitive("foobar", "yes")));
     422        assertTrue(c2.match(newPrimitive("foobaz", "yes")));
     423        assertEquals("foo && (bar || baz)", c2.toString());
     424        final SearchCompiler.Match c3 = SearchCompiler.compile("foo OR (bar baz)");
     425        assertEquals("foo || (bar && baz)", c3.toString());
     426        final SearchCompiler.Match c4 = SearchCompiler.compile("foo1 OR (bar1 bar2 baz1 XOR baz2) OR foo2");
     427        assertEquals("foo1 || (bar1 && bar2 && (baz1 ^ baz2)) || foo2", c4.toString());
     428    }
    410429}
Note: See TracChangeset for help on using the changeset viewer.