### Question:
QueryParser { public static Evaluator parse(String query) { try { QueryParser p = new QueryParser(query); return p.parse(); } catch (IllegalArgumentException e) { throw new Selector.SelectorParseException(e.getMessage()); } } private  QueryParser(String query); static Evaluator parse(String query);  }### Answer:
@Test public void testOrGetsCorrectPrecedence() { Evaluator eval = QueryParser.parse("a b, c d, e f"); assertTrue(eval instanceof CombiningEvaluator.Or); CombiningEvaluator.Or or = (CombiningEvaluator.Or) eval; assertEquals(3, or.evaluators.size()); for (Evaluator innerEval: or.evaluators) { assertTrue(innerEval instanceof CombiningEvaluator.And); CombiningEvaluator.And and = (CombiningEvaluator.And) innerEval; assertEquals(2, and.evaluators.size()); assertTrue(and.evaluators.get(0) instanceof Evaluator.Tag); assertTrue(and.evaluators.get(1) instanceof StructuralEvaluator.Parent); } }
@Test public void testParsesMultiCorrectly() { Evaluator eval = QueryParser.parse(".foo > ol, ol > li + li"); assertTrue(eval instanceof CombiningEvaluator.Or); CombiningEvaluator.Or or = (CombiningEvaluator.Or) eval; assertEquals(2, or.evaluators.size()); CombiningEvaluator.And andLeft = (CombiningEvaluator.And) or.evaluators.get(0); CombiningEvaluator.And andRight = (CombiningEvaluator.And) or.evaluators.get(1); assertEquals("ol :ImmediateParent.foo", andLeft.toString()); assertEquals(2, andLeft.evaluators.size()); assertEquals("li :prevli :ImmediateParentol", andRight.toString()); assertEquals(2, andLeft.evaluators.size()); }
@Test(expected = Selector.SelectorParseException.class) public void exceptionOnUncloseAttribute() { Evaluator parse = QueryParser.parse("section > a[href=\"]"); }
@Test(expected = Selector.SelectorParseException.class) public void testParsesSingleQuoteInContains() { Evaluator parse = QueryParser.parse("p:contains(One \" One)"); }