From b1947a5812d0795658ee1b7e112789f09f6fd0f8 Mon Sep 17 00:00:00 2001 From: "ah.jo" Date: Tue, 31 Dec 2024 20:27:14 +0900 Subject: [PATCH] Fix InnerSpec inner API (#1137) --- .../fixturemonkey/tests/kotlin/KotlinTest.kt | 26 +++++++++++++++++++ .../customizer/InnerSpecState.java | 19 +++++++++++--- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/fixture-monkey-tests/kotlin-tests/src/test/kotlin/com/navercorp/fixturemonkey/tests/kotlin/KotlinTest.kt b/fixture-monkey-tests/kotlin-tests/src/test/kotlin/com/navercorp/fixturemonkey/tests/kotlin/KotlinTest.kt index bc8206de8..bab777990 100644 --- a/fixture-monkey-tests/kotlin-tests/src/test/kotlin/com/navercorp/fixturemonkey/tests/kotlin/KotlinTest.kt +++ b/fixture-monkey-tests/kotlin-tests/src/test/kotlin/com/navercorp/fixturemonkey/tests/kotlin/KotlinTest.kt @@ -38,6 +38,7 @@ import com.navercorp.fixturemonkey.api.plugin.InterfacePlugin import com.navercorp.fixturemonkey.api.property.ConcreteTypeCandidateConcretePropertyResolver import com.navercorp.fixturemonkey.api.property.PropertyUtils import com.navercorp.fixturemonkey.api.type.Types.GeneratingWildcardType +import com.navercorp.fixturemonkey.customizer.InnerSpec import com.navercorp.fixturemonkey.customizer.Values import com.navercorp.fixturemonkey.javax.validation.plugin.JavaxValidationPlugin import com.navercorp.fixturemonkey.javax.validation.validator.JavaxArbitraryValidator @@ -1306,6 +1307,31 @@ class KotlinTest { then(actual).isNotNull } + @RepeatedTest(TEST_COUNT) + fun innerSpecInner() { + // given + data class ChildObject(val values: List) + + data class ParentObject(val list: List) + + // when + val actual = SUT.giveMeKotlinBuilder() + .setInner { + property("list") { l -> + l.size(1) + .listElement(0) { e -> + e.inner( + InnerSpec() + .property("values") { v -> v.size(5) } + ) + } + } + }.sample().list[0].values + + // then + then(actual).hasSize(5) + } + companion object { private val SUT: FixtureMonkey = FixtureMonkey.builder() .plugin(KotlinPlugin()) diff --git a/fixture-monkey/src/main/java/com/navercorp/fixturemonkey/customizer/InnerSpecState.java b/fixture-monkey/src/main/java/com/navercorp/fixturemonkey/customizer/InnerSpecState.java index 786c14932..14d08b5c4 100644 --- a/fixture-monkey/src/main/java/com/navercorp/fixturemonkey/customizer/InnerSpecState.java +++ b/fixture-monkey/src/main/java/com/navercorp/fixturemonkey/customizer/InnerSpecState.java @@ -21,6 +21,7 @@ import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; +import java.util.stream.Collectors; import javax.annotation.Nullable; @@ -28,6 +29,7 @@ import org.apiguardian.api.API.Status; import com.navercorp.fixturemonkey.tree.NextNodePredicate; +import com.navercorp.fixturemonkey.tree.StartNodePredicate; @API(since = "0.5.0", status = Status.MAINTAINED) final class InnerSpecState { @@ -70,7 +72,10 @@ InnerSpecState withPrefix(List nextNodePredicates) { if (this.objectHolder != null) { List concat = new ArrayList<>(nextNodePredicates); - concat.addAll(this.objectHolder.nextNodePredicates); + List setNextNodePredicates = this.objectHolder.nextNodePredicates.stream() + .filter(it -> !(it instanceof StartNodePredicate)) + .collect(Collectors.toList()); + concat.addAll(setNextNodePredicates); newState.objectHolder = new NodeResolverObjectHolder( this.objectHolder.sequence, concat, @@ -80,7 +85,11 @@ InnerSpecState withPrefix(List nextNodePredicates) { if (this.filterHolder != null) { List concat = new ArrayList<>(nextNodePredicates); - concat.addAll(this.filterHolder.nextNodePredicates); + List setPostConditionNextNodePredicates = + this.filterHolder.nextNodePredicates.stream() + .filter(it -> !(it instanceof StartNodePredicate)) + .collect(Collectors.toList()); + concat.addAll(setPostConditionNextNodePredicates); newState.filterHolder = new FilterHolder( this.filterHolder.sequence, concat, @@ -91,7 +100,11 @@ InnerSpecState withPrefix(List nextNodePredicates) { if (this.containerInfoHolder != null) { List concat = new ArrayList<>(nextNodePredicates); - concat.addAll(this.containerInfoHolder.nextNodePredicates); + List containerHolderNextNodePredicates = + this.containerInfoHolder.nextNodePredicates.stream() + .filter(it -> !(it instanceof StartNodePredicate)) + .collect(Collectors.toList()); + concat.addAll(containerHolderNextNodePredicates); newState.containerInfoHolder = new ContainerInfoHolder( this.containerInfoHolder.sequence, concat,