### Question:
CaseValidator extends Validator { @Override public void validate(UastNode node) { if (node.is(UastNode.Kind.DEFAULT_CASE)) { return; } is(UastNode.Kind.CASE); hasKeyword("case"); oneOrMoreChild(UastNode.Kind.CONDITION); hasAncestor(UastNode.Kind.SWITCH); } CaseValidator(); @Override void validate(UastNode node);  }### Answer:
@Test public void single_condition() throws Exception { UastNode caseNode = node(UastNode.Kind.CASE, keyword("case"), node(UastNode.Kind.CONDITION), token(":")); UastNode switchNode = node(UastNode.Kind.SWITCH, caseNode); try { validate(switchNode); } catch (Exception e) { fail("should not have failed", e); } }
@Test public void multiple_conditions() throws Exception { UastNode caseNode = node(UastNode.Kind.CASE, keyword("case"), node(UastNode.Kind.CONDITION), token(","), node(UastNode.Kind.CONDITION), token(","), node(UastNode.Kind.CONDITION), token(":")); UastNode switchNode = node(UastNode.Kind.SWITCH, caseNode); try { validate(switchNode); } catch (Exception e) { fail("should not have failed", e); } }
@Test public void ancestor_not_direct_parent() throws Exception { UastNode caseNode = node(UastNode.Kind.CASE, keyword("case"), node(UastNode.Kind.CONDITION), token(":")); UastNode parent = node(UastNode.Kind.UNSUPPORTED, caseNode); UastNode switchNode = node(UastNode.Kind.SWITCH, parent); try { validate(switchNode); } catch (Exception e) { fail("should not have failed", e); } }
@Test public void wrong_keyword() throws Exception { UastNode caseNode = node(UastNode.Kind.CASE, keyword("default"), node(UastNode.Kind.CONDITION), token(":")); UastNode switchNode = node(UastNode.Kind.SWITCH, caseNode); Validator.ValidationException exception = assertThrows(Validator.ValidationException.class, () -> validate(switchNode)); assertThat(exception.getMessage()).isEqualTo("CaseValidator at line 1: Expected 'case' as keyword but got 'default'."); }
@Test public void not_part_of_switch() throws Exception { UastNode caseNode = node(UastNode.Kind.CASE, keyword("case"), node(UastNode.Kind.CONDITION), token(":")); UastNode parent = node(UastNode.Kind.ADD, caseNode); Validator.ValidationException exception = assertThrows(Validator.ValidationException.class, () -> validate(parent)); assertThat(exception.getMessage()).isEqualTo("CaseValidator at line 1: Should have a node of kind 'SWITCH' as ancestor."); }
@Test public void should_have_condition() throws Exception { UastNode caseNode = node(UastNode.Kind.CASE, keyword("case"), token(":")); UastNode switchNode = node(UastNode.Kind.SWITCH, caseNode); Validator.ValidationException exception = assertThrows(Validator.ValidationException.class, () -> validate(switchNode)); assertThat(exception.getMessage()).isEqualTo("CaseValidator at line 1: Should have at least one child of kind 'CONDITION'."); }