### Question:
ForValidator extends Validator { @Override public void validate(UastNode node) { is(UastNode.Kind.FOR); hasKeyword("for"); zeroOrOneChild(UastNode.Kind.FOR_INIT); zeroOrOneChild(UastNode.Kind.CONDITION); zeroOrOneChild(UastNode.Kind.FOR_UPDATE); singleChild(UastNode.Kind.BODY); } ForValidator(); @Override void validate(UastNode node);  }### Answer:
@Test void expected() throws Exception { UastNode forNode = buildNode(UastNode.Kind.FOR) .addChildren(keyword("for"), node(UastNode.Kind.FOR_INIT), node(UastNode.Kind.CONDITION), node(UastNode.Kind.FOR_UPDATE), node(UastNode.Kind.BODY)) .build(); validate(forNode); forNode = buildNode(UastNode.Kind.FOR) .addChildren(keyword("for"), node(UastNode.Kind.BODY)) .build(); validate(forNode); }
@Test void missing_keyword() throws Exception { UastNode forNode = buildNode(UastNode.Kind.FOR) .addChildren(node(UastNode.Kind.BODY)) .build(); Assertions.assertThatThrownBy(() -> validate(forNode)) .hasMessage("ForValidator at line N/A: No keyword found."); }
@Test void missing_body() throws Exception { UastNode forNode = buildNode(UastNode.Kind.FOR) .addChildren(keyword("for"), node(UastNode.Kind.FOR_INIT), node(UastNode.Kind.CONDITION), node(UastNode.Kind.FOR_UPDATE)) .build(); Assertions.assertThatThrownBy(() -> validate(forNode)) .hasMessage("ForValidator at line 1: Should have one single child of kind 'BODY' but got none."); }
@Test void multiple_init() throws Exception { UastNode forNode = buildNode(UastNode.Kind.FOR) .addChildren(keyword("for"), node(UastNode.Kind.FOR_INIT), node(UastNode.Kind.FOR_INIT), node(UastNode.Kind.CONDITION), node(UastNode.Kind.FOR_UPDATE)) .build(); Assertions.assertThatThrownBy(() -> validate(forNode)) .hasMessage("ForValidator at line 1: Should have one single child of kind 'FOR_INIT' but got [[FOR_INIT], [FOR_INIT]]."); }