Skip to content

Commit

Permalink
Fix register working on assignable type (#971)
Browse files Browse the repository at this point in the history
  • Loading branch information
seongahjo authored Apr 15, 2024
1 parent 478d1fd commit 73e681b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 26 deletions.
2 changes: 2 additions & 0 deletions docs/content/v1.0.x-kor/release-notes/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ sectionStart
### v.1.0.16
Add resolve the candidate concrete type of container type.

Fix register working on assignable type.

sectionEnd

sectionStart
Expand Down
2 changes: 2 additions & 0 deletions docs/content/v1.0.x/release-notes/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ sectionStart
### v.1.0.16
Add resolve the candidate concrete type of container type.

Fix register working on assignable type.

sectionEnd

sectionStart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import com.navercorp.fixturemonkey.kotlin.introspector.PrimaryConstructorArbitra
import com.navercorp.fixturemonkey.kotlin.maxSize
import com.navercorp.fixturemonkey.kotlin.minSize
import com.navercorp.fixturemonkey.kotlin.pushExactTypeArbitraryIntrospector
import com.navercorp.fixturemonkey.kotlin.register
import com.navercorp.fixturemonkey.kotlin.set
import com.navercorp.fixturemonkey.kotlin.setExp
import com.navercorp.fixturemonkey.kotlin.setExpGetter
Expand Down Expand Up @@ -664,6 +665,29 @@ class KotlinTest {
then(actual).isEqualTo(expected)
}

@Test
fun registerAssignableType() {
// given
open class Parent(val parent: String)

class Child(parent: String) : Parent(parent)

val expected = "registered"
val sut = FixtureMonkey.builder()
.plugin(KotlinPlugin())
.register {
it.giveMeBuilder<Parent>()
.setExp(Parent::parent, expected)
}
.build()

// when
val actual = sut.giveMeOne<Child>().parent

// then
then(actual).isEqualTo(expected)
}

@RepeatedTest(TEST_COUNT)
fun propertyCandidateResolverReturnsConcreteListType() {
val sut = FixtureMonkey.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Stream;

Expand All @@ -40,7 +39,6 @@
import com.navercorp.fixturemonkey.customizer.ArbitraryManipulator;
import com.navercorp.fixturemonkey.customizer.MonkeyManipulatorFactory;
import com.navercorp.fixturemonkey.experimental.ExperimentalArbitraryBuilder;
import com.navercorp.fixturemonkey.expression.MonkeyExpressionFactory;
import com.navercorp.fixturemonkey.resolver.ArbitraryBuilderContext;
import com.navercorp.fixturemonkey.resolver.ArbitraryResolver;
import com.navercorp.fixturemonkey.resolver.DefaultArbitraryBuilder;
Expand All @@ -54,21 +52,21 @@ public final class FixtureMonkey {
private final ManipulatorOptimizer manipulatorOptimizer;
private final MonkeyContext monkeyContext;
private final List<MatcherOperator<? extends ArbitraryBuilder<?>>> registeredArbitraryBuilders = new ArrayList<>();
private final MonkeyExpressionFactory monkeyExpressionFactory;
private final MonkeyManipulatorFactory monkeyManipulatorFactory;

public FixtureMonkey(
FixtureMonkeyOptions fixtureMonkeyOptions,
ArbitraryTraverser traverser,
ManipulatorOptimizer manipulatorOptimizer,
MonkeyContext monkeyContext,
List<MatcherOperator<Function<FixtureMonkey, ? extends ArbitraryBuilder<?>>>> registeredArbitraryBuilders,
MonkeyExpressionFactory monkeyExpressionFactory
MonkeyManipulatorFactory monkeyManipulatorFactory
) {
this.fixtureMonkeyOptions = fixtureMonkeyOptions;
this.traverser = traverser;
this.manipulatorOptimizer = manipulatorOptimizer;
this.monkeyContext = monkeyContext;
this.monkeyExpressionFactory = monkeyExpressionFactory;
this.monkeyManipulatorFactory = monkeyManipulatorFactory;
initializeRegisteredArbitraryBuilders(registeredArbitraryBuilders);
}

Expand All @@ -86,26 +84,17 @@ public <T> ArbitraryBuilder<T> giveMeBuilder(Class<T> type) {
return giveMeBuilder(typeReference);
}

@SuppressWarnings("unchecked")
public <T> ArbitraryBuilder<T> giveMeBuilder(TypeReference<T> type) {
RootProperty rootProperty = new RootProperty(type.getAnnotatedType());

ArbitraryBuilder<?> registered = registeredArbitraryBuilders.stream()
ArbitraryBuilderContext builderContext = registeredArbitraryBuilders.stream()
.filter(it -> it.match(rootProperty))
.map(MatcherOperator::getOperator)
.findAny()
.orElse(null);
.map(DefaultArbitraryBuilder.class::cast)
.map(DefaultArbitraryBuilder::getContext)
.orElse(new ArbitraryBuilderContext());

if (registered != null) {
return (ArbitraryBuilder<T>)registered.copy();
}

MonkeyManipulatorFactory monkeyManipulatorFactory = new MonkeyManipulatorFactory(
new AtomicInteger(),
monkeyExpressionFactory,
traverser,
fixtureMonkeyOptions.getDecomposedContainerValueFactory()
);
return new DefaultArbitraryBuilder<>(
fixtureMonkeyOptions,
rootProperty,
Expand All @@ -119,20 +108,14 @@ public <T> ArbitraryBuilder<T> giveMeBuilder(TypeReference<T> type) {
),
traverser,
monkeyManipulatorFactory,
new ArbitraryBuilderContext(),
builderContext.copy(),
registeredArbitraryBuilders,
monkeyContext,
fixtureMonkeyOptions.getInstantiatorProcessor()
);
}

public <T> ArbitraryBuilder<T> giveMeBuilder(T value) {
MonkeyManipulatorFactory monkeyManipulatorFactory = new MonkeyManipulatorFactory(
new AtomicInteger(),
monkeyExpressionFactory,
traverser,
fixtureMonkeyOptions.getDecomposedContainerValueFactory()
);
ArbitraryBuilderContext context = new ArbitraryBuilderContext();

ArbitraryManipulator arbitraryManipulator =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.function.UnaryOperator;

Expand Down Expand Up @@ -58,6 +59,7 @@
import com.navercorp.fixturemonkey.api.validator.ArbitraryValidator;
import com.navercorp.fixturemonkey.buildergroup.ArbitraryBuilderCandidate;
import com.navercorp.fixturemonkey.buildergroup.ArbitraryBuilderGroup;
import com.navercorp.fixturemonkey.customizer.MonkeyManipulatorFactory;
import com.navercorp.fixturemonkey.expression.ArbitraryExpressionFactory;
import com.navercorp.fixturemonkey.expression.MonkeyExpressionFactory;
import com.navercorp.fixturemonkey.resolver.ManipulatorOptimizer;
Expand Down Expand Up @@ -530,6 +532,12 @@ public FixtureMonkey build() {
defaultInterfacePlugin.accept(fixtureMonkeyOptionsBuilder);
FixtureMonkeyOptions fixtureMonkeyOptions = fixtureMonkeyOptionsBuilder.build();
ArbitraryTraverser traverser = new ArbitraryTraverser(fixtureMonkeyOptions);
MonkeyManipulatorFactory monkeyManipulatorFactory = new MonkeyManipulatorFactory(
new AtomicInteger(),
monkeyExpressionFactory,
traverser,
fixtureMonkeyOptions.getDecomposedContainerValueFactory()
);

MonkeyContext monkeyContext = monkeyContextBuilder.build();
Randoms.create(String.valueOf(seed));
Expand All @@ -539,7 +547,7 @@ public FixtureMonkey build() {
manipulatorOptimizer,
monkeyContext,
registeredArbitraryBuilders,
monkeyExpressionFactory
monkeyManipulatorFactory
);
}
}

0 comments on commit 73e681b

Please sign in to comment.