### Question:
ReferringCodeGenerator { String generateEndingReferringBodyWhenChaining(ReferMetadata referMetadata) { Type referType = referMetadata.getRefType(); String ref = referMetadata.getRef(); return isCollection(referType) && referMetadata.getPathPart().contains("[*]") ? generateEndingLoop(getCollectionGeneric(referType).getTypeName(), ref) : generateEndingAssignment(getRawType(referType).getTypeName(), ref, "result.add(%s(%s));"); }    }### Answer:
@Test void shouldCreateEndingLoopWhenChainingAndGroupingOperator() { String ref = "ref[*]"; Type type = new ParameterizedType() { @Override public Type[] getActualTypeArguments() { return new Type[]{String.class}; } @Override public Type getRawType() { return List.class; } @Override public Type getOwnerType() { return null; } }; ReferMetadata referMetadata = new ReferMetadata(type, "ref", ref); String body = new ReferringCodeGenerator().generateEndingReferringBodyWhenChaining(referMetadata); assertThat(body).isEqualToNormalizingWhitespace("" + "if(" + BoxingUtils.class.getName() + ".box(v0.ref) == null) { \n" + " return null; \n" + "} \n" + "java.util.Iterator i0 = v0.ref.iterator(); \n" + "while(i0.hasNext()) { \n" + " java.lang.String v1 = (java.lang.String) i0.next(); \n" + " result.add(" + BoxingUtils.class.getName() + ".box(v1)); \n" + "} \n"); }
@Test void shouldCreateEndingLoopWhenChainingAndNoGroupingOperator() { String ref = "ref"; Type type = new ParameterizedType() { @Override public Type[] getActualTypeArguments() { return new Type[]{String.class}; } @Override public Type getRawType() { return List.class; } @Override public Type getOwnerType() { return null; } }; ReferMetadata referMetadata = new ReferMetadata(type, ref, ref); String body = new ReferringCodeGenerator().generateEndingReferringBodyWhenChaining(referMetadata); assertThat(body).isEqualToIgnoringWhitespace("" + "java.util.List v1 = (java.util.List) v0.ref; \n" + "result.add(" + BoxingUtils.class.getName() + ".box(v1));"); }
@Test void shouldCreateEndingAssignmentWhenChaining() { String ref = "ref"; Type type = String.class; ReferMetadata referMetadata = new ReferMetadata(type, ref, ref); String body = new ReferringCodeGenerator().generateEndingReferringBodyWhenChaining(referMetadata); assertThat(body).isEqualToIgnoringWhitespace("" + "java.lang.String v1 = (java.lang.String) v0.ref; \n" + "result.add(" + BoxingUtils.class.getName() + ".box(v1));"); }