### Question:
IfValidator extends Validator { @Override public void validate(UastNode node) { is(UastNode.Kind.IF); hasKeyword("if"); singleChild(UastNode.Kind.CONDITION); singleChild(UastNode.Kind.THEN); zeroOrOneChild(UastNode.Kind.ELSE).ifPresent(elseNode -> singleChild(UastNode.Kind.ELSE_KEYWORD)); } IfValidator(); @Override void validate(UastNode node);  }### Answer:
@Test void missing_else_keyword() { UastNode ifNode = node(Kind.IF, keyword("if", Kind.IF_KEYWORD), node(Kind.CONDITION), node(Kind.THEN), node(Kind.ELSE)); Validator.ValidationException exception = assertThrows(Validator.ValidationException.class, () -> validate(ifNode)); assertThat(exception.getMessage()).isEqualTo("IfValidator at line 1: Should have one single child of kind 'ELSE_KEYWORD' but got none."); }
@Test void expected() { UastNode ifNode = buildNode(Kind.IF).addChildren(keyword("if", Kind.IF_KEYWORD), node(Kind.CONDITION), node(Kind.THEN)).build(); UastNode ifElseNode = buildNode(Kind.IF).addChildren(keyword("if", Kind.IF_KEYWORD), node(Kind.CONDITION), node(Kind.THEN),keyword("if", Kind.ELSE_KEYWORD), node(Kind.ELSE)).build(); try { validate(ifNode); validate(ifElseNode); } catch (Exception e) { fail("should not have failed", e); } }
@Test void missing_if_keyword() { UastNode ifNode = node(Kind.IF, keyword("label"), node(Kind.THEN)); Validator.ValidationException exception = assertThrows(Validator.ValidationException.class, () -> validate(ifNode)); assertThat(exception.getMessage()).isEqualTo("IfValidator at line 1: Expected 'if' as keyword but got 'label'."); }