Skip to content

Commit

Permalink
Fix not applying registered child manipulations if parent uses thenApply
Browse files Browse the repository at this point in the history
  • Loading branch information
seongahjo committed Dec 31, 2024
1 parent 2bab10b commit f4cc94d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,26 @@ class KotlinTest {
then(actual).isNotNull
}

@Test
fun parentRegisterThenApplyAndSizeReturnsChildRegister() {
data class ChildObject(val value: String)

data class ParentObject(val values: List<ChildObject>)

val sut = FixtureMonkey.builder()
.register { it.giveMeKotlinBuilder<ParentObject>().thenApply { _, _ -> } }
.register { it.giveMeKotlinBuilder<ChildObject>().set(ChildObject::value, "1") }
.plugin(KotlinPlugin())
.build()

val actual = sut.giveMeKotlinBuilder<ParentObject>()
.size(ParentObject::values, 10)
.sample()
.values

then(actual).allMatch { it.value == "1" }
}

companion object {
private val SUT: FixtureMonkey = FixtureMonkey.builder()
.plugin(KotlinPlugin())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
package com.navercorp.fixturemonkey.tree;

import static com.navercorp.fixturemonkey.api.type.Types.nullSafe;
import static java.util.stream.Collectors.toMap;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import javax.annotation.Nullable;
Expand All @@ -29,6 +33,7 @@
import org.apiguardian.api.API.Status;

import com.navercorp.fixturemonkey.api.generator.ArbitraryProperty;
import com.navercorp.fixturemonkey.api.generator.ObjectProperty;
import com.navercorp.fixturemonkey.api.lazy.LazyArbitrary;
import com.navercorp.fixturemonkey.api.property.Property;
import com.navercorp.fixturemonkey.api.property.PropertyPath;
Expand Down Expand Up @@ -83,19 +88,23 @@ public boolean expand() {
public void forceExpand() {
this.traverseNode.forceExpand();
this.setChildren(
nullSafe(this.traverseNode.getChildren()).asList().stream()
.map(it -> new ObjectNode(it, generateFixtureContext.newChildNodeContext()))
.collect(Collectors.toList())
this.mergeWithNewChildren(
nullSafe(this.traverseNode.getChildren()).asList().stream()
.map(it -> new ObjectNode(it, generateFixtureContext.newChildNodeContext()))
.collect(Collectors.toList())
)
);
}

@Override
public void forceExpand(TypeDefinition typeDefinition) {
this.traverseNode.forceExpand(typeDefinition);
this.setChildren(
nullSafe(this.traverseNode.getChildren()).asList().stream()
.map(it -> new ObjectNode(it, generateFixtureContext.newChildNodeContext()))
.collect(Collectors.toList())
this.mergeWithNewChildren(
nullSafe(this.traverseNode.getChildren()).asList().stream()
.map(it -> new ObjectNode(it, generateFixtureContext.newChildNodeContext()))
.collect(Collectors.toList())
)
);
}

Expand Down Expand Up @@ -191,4 +200,34 @@ public TreeNodeManipulator getAppliedTreeNodeManipulator() {
public GenerateFixtureContext getObjectNodeContext() {
return generateFixtureContext;
}

private List<ObjectNode> mergeWithNewChildren(List<ObjectNode> newChildren) {
if (this.children == null) {
return newChildren;
}

boolean shrinkChildNodes = this.children.size() > newChildren.size();
if (shrinkChildNodes) {
return this.children.subList(0, newChildren.size());
}

boolean expandChildNodes = this.children.size() < newChildren.size();
if (expandChildNodes) {
Map<ObjectProperty, ObjectNode> existingNodesByObjectProperty = this.children.stream()
.collect(toMap(it -> it.getMetadata().getTreeProperty().getObjectProperty(), Function.identity()));

List<ObjectNode> concatNewChildren = new ArrayList<>();
for (ObjectNode newChild : newChildren) {
ObjectNode existingNode =
existingNodesByObjectProperty.get(newChild.getMetadata().getTreeProperty().getObjectProperty());
if (existingNode != null) {
concatNewChildren.add(existingNode);
} else {
concatNewChildren.add(newChild);
}
}
return concatNewChildren;
}
return this.children;
}
}

0 comments on commit f4cc94d

Please sign in to comment.