Skip to content

Commit

Permalink
Add generating a circular-reference object (#519)
Browse files Browse the repository at this point in the history
  • Loading branch information
seongahjo authored Dec 10, 2022
1 parent 22dcf35 commit 5e240d1
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public ArbitraryNode traverse(
return this.traverse(
arbitraryProperty,
new TraverseContext(
arbitraryProperty,
new ArrayList<>(),
containerInfoManipulators
)
Expand Down Expand Up @@ -135,6 +136,10 @@ private List<ArbitraryNode> generateChildrenNodes(
for (int sequence = 0; sequence < properties.size(); sequence++) {
Property childProperty = properties.get(sequence);

if (context.isTraversed(childProperty)) {
continue;
}

ContainerPropertyGenerator containerPropertyGenerator =
this.generateOptions.getContainerPropertyGenerator(childProperty);
boolean childContainer = containerPropertyGenerator != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,28 @@
import org.apiguardian.api.API.Status;

import com.navercorp.fixturemonkey.api.generator.ArbitraryProperty;
import com.navercorp.fixturemonkey.api.property.Property;

@API(since = "0.4.0", status = Status.EXPERIMENTAL)
final class TraverseContext {
private final ArbitraryProperty rootArbitraryProperty;
private final List<ArbitraryProperty> arbitraryProperties;
private final List<ContainerInfoManipulator> containerInfoManipulators;

public TraverseContext(
ArbitraryProperty rootArbitraryProperty,
List<ArbitraryProperty> arbitraryProperties,
List<ContainerInfoManipulator> containerInfoManipulators
) {
this.rootArbitraryProperty = rootArbitraryProperty;
this.arbitraryProperties = arbitraryProperties;
this.containerInfoManipulators = containerInfoManipulators;
}

public ArbitraryProperty getRootArbitraryProperty() {
return rootArbitraryProperty;
}

public List<ArbitraryProperty> getArbitraryProperties() {
return arbitraryProperties;
}
Expand All @@ -52,6 +60,12 @@ public TraverseContext appendArbitraryProperty(
) {
List<ArbitraryProperty> arbitraryProperties = new ArrayList<>(this.arbitraryProperties);
arbitraryProperties.add(arbitraryProperty);
return new TraverseContext(arbitraryProperties, containerInfoManipulators);
return new TraverseContext(rootArbitraryProperty, arbitraryProperties, containerInfoManipulators);
}

public boolean isTraversed(Property property) {
return property.equals(rootArbitraryProperty.getObjectProperty().getProperty())
|| arbitraryProperties.stream()
.anyMatch(it -> property.equals(it.getObjectProperty().getProperty()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
import com.navercorp.fixturemonkey.test.FixtureMonkeyV04TestSpecs.InterfaceImplementation;
import com.navercorp.fixturemonkey.test.FixtureMonkeyV04TestSpecs.ListStringObject;
import com.navercorp.fixturemonkey.test.FixtureMonkeyV04TestSpecs.NullableObject;
import com.navercorp.fixturemonkey.test.FixtureMonkeyV04TestSpecs.RecursiveLeftObject;
import com.navercorp.fixturemonkey.test.FixtureMonkeyV04TestSpecs.SelfRecursiveListObject;
import com.navercorp.fixturemonkey.test.FixtureMonkeyV04TestSpecs.SelfRecursiveObject;
import com.navercorp.fixturemonkey.test.FixtureMonkeyV04TestSpecs.SimpleObject;
import com.navercorp.fixturemonkey.test.FixtureMonkeyV04TestSpecs.StaticFieldObject;
import com.navercorp.fixturemonkey.test.FixtureMonkeyV04TestSpecs.StringPair;
Expand Down Expand Up @@ -1551,4 +1554,42 @@ void setSpec() {

then(actual).isEqualTo(expected);
}

@Property
void sampleSelfRecursiveObject() {
LabMonkey sut = LabMonkey.labMonkeyBuilder()
.defaultNotNull(true)
.build();

SelfRecursiveObject actual = sut.giveMeOne(SelfRecursiveObject.class);

then(actual.getValue()).isNotNull();
then(actual.getRecursive()).isNotNull();
then(actual.getRecursive().getValue()).isNotNull();
}

@Property
void sampleSelfRecursiveList() {
LabMonkey sut = LabMonkey.labMonkeyBuilder()
.defaultNotNull(true)
.build();

SelfRecursiveListObject actual = sut.giveMeOne(SelfRecursiveListObject.class);

then(actual.getValue()).isNotNull();
then(actual.getRecursives()).isNotNull();
}

@Property
void sampleRecursiveObject() {
LabMonkey sut = LabMonkey.labMonkeyBuilder()
.defaultNotNull(true)
.build();

RecursiveLeftObject actual = sut.giveMeOne(RecursiveLeftObject.class);

then(actual.getValue()).isNotNull();
then(actual.getRecursive()).isNotNull();
then(actual.getRecursive().getValue()).isNotNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,32 @@ public static class CustomContainer extends ArrayList<String> {
public static class CustomContainerFieldObject {
CustomContainer value;
}

@Data
public static class SelfRecursiveObject {
String value;

SelfRecursiveObject recursive;
}

@Data
public static class SelfRecursiveListObject {
String value;

List<SelfRecursiveObject> recursives;
}

@Data
public static class RecursiveLeftObject {
String value;

RecursiveRightObject recursive;
}

@Data
public static class RecursiveRightObject {
int value;

RecursiveLeftObject recursive;
}
}

0 comments on commit 5e240d1

Please sign in to comment.