Skip to content

Commit

Permalink
Fix for compatible 0.4.3 (#484)
Browse files Browse the repository at this point in the history
  • Loading branch information
seongahjo authored Nov 11, 2022
1 parent 6b06935 commit f82b838
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@

import com.navercorp.fixturemonkey.api.context.MonkeyGeneratorContext;
import com.navercorp.fixturemonkey.api.customizer.FixtureCustomizer;
import com.navercorp.fixturemonkey.api.lazy.LazyArbitrary;
import com.navercorp.fixturemonkey.api.matcher.MatcherOperator;
import com.navercorp.fixturemonkey.api.property.CompositeProperty;
import com.navercorp.fixturemonkey.api.property.Property;

@API(since = "0.4.0", status = Status.EXPERIMENTAL)
Expand All @@ -57,6 +59,8 @@ public final class ArbitraryGeneratorContext {

private final MonkeyGeneratorContext monkeyGeneratorContext;

private final LazyArbitrary<Property> pathProperty = LazyArbitrary.lazy(this::initPathProperty);

@SuppressWarnings("rawtypes")
public ArbitraryGeneratorContext(
ArbitraryProperty property,
Expand Down Expand Up @@ -140,4 +144,16 @@ public synchronized boolean isUniqueAndCheck(Property property, Object value) {
public void evictUnique(Property property) {
monkeyGeneratorContext.evictUnique(property);
}

public Property getPathProperty() {
return pathProperty.getValue();
}

private Property initPathProperty() {
if (ownerContext == null) {
return property.getObjectProperty().getProperty();
}

return new CompositeProperty(property.getObjectProperty().getProperty(), ownerContext.getPathProperty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public ArbitraryIntrospectorResult introspect(ArbitraryGeneratorContext context)
new FilteredMonkeyArbitrary<>(
arbitraries.get(0),
it -> context.isUniqueAndCheck(
mapEntryProperty,
context.getOwnerContext().getPathProperty(),
it
),
MAX_TRIES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public ArbitraryIntrospectorResult introspect(ArbitraryGeneratorContext context)
}

return new ArbitraryIntrospectorResult(builderCombinator.build(map -> {
context.evictUnique(property);
context.evictUnique(context.getPathProperty());
return map;
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public ArbitraryIntrospectorResult introspect(ArbitraryGeneratorContext context)
new FilteredMonkeyArbitrary<>(
arbitrary,
it -> context.isUniqueAndCheck(
property,
context.getPathProperty(),
it
),
MAX_TRIES
Expand All @@ -87,7 +87,7 @@ public ArbitraryIntrospectorResult introspect(ArbitraryGeneratorContext context)

return new ArbitraryIntrospectorResult(
builderCombinator.build(set -> {
context.evictUnique(property);
context.evictUnique(context.getPathProperty());
return set;
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public <T> DefaultArbitraryBuilder<T> giveMeBuilder(Class<T> type) {
return giveMeBuilder(typeReference);
}

@SuppressWarnings("unchecked")
@Override
public <T> DefaultArbitraryBuilder<T> giveMeBuilder(TypeReference<T> type) {
ManipulateOptions manipulateOptions = manipulateOptionsBuilder.build();
Expand All @@ -106,11 +105,7 @@ public <T> DefaultArbitraryBuilder<T> giveMeBuilder(TypeReference<T> type) {
.findAny()
.orElse(null);

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

return new DefaultArbitraryBuilder<>(
DefaultArbitraryBuilder<T> arbitraryBuilder = new DefaultArbitraryBuilder<>(
manipulateOptions,
rootProperty,
new ArbitraryResolver(
Expand All @@ -124,6 +119,12 @@ public <T> DefaultArbitraryBuilder<T> giveMeBuilder(TypeReference<T> type) {
this.validator,
new ArbitraryBuilderContext()
);

if (registered != null) {
arbitraryBuilder.setLazy("$", registered::sample);
}

return arbitraryBuilder;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ void sizeBiggerThanRegisterSized() {
@Property
void registerObjectNotFixed() {
LabMonkey sut = LabMonkey.labMonkeyBuilder()
.register(String.class, it -> it.giveMeBuilder(String.class).set("$", Arbitraries.strings().alpha()))
.register(String.class, it -> it.giveMeBuilder(String.class).set("$", Arbitraries.strings().ofLength(10)))
.build();

List<String> sampled = sut.giveMeBuilder(new TypeReference<List<String>>() {
Expand Down Expand Up @@ -1120,4 +1120,46 @@ void applySizeWhenRegisteredWithSize() {

then(actual).hasSize(10);
}

@Property
void sizeWhenRegisterSizeInApply() {
// given
LabMonkey sut = LabMonkey.labMonkeyBuilder()
.register(
ListStringObject.class,
fixture -> fixture.giveMeBuilder(ListStringObject.class)
.apply((it, builder) -> builder.size("values", 1))
)
.build();

// when
List<String> actual = sut.giveMeBuilder(ListStringObject.class)
.size("values", 2)
.sample()
.getValues();

then(actual).hasSize(2);
}

@Property
void sizeWhenRegisterApply() {
// given
LabMonkey sut = LabMonkey.labMonkeyBuilder()
.register(
ListStringObject.class,
fixture -> fixture.giveMeBuilder(ListStringObject.class)
.size("values", 1)
.apply((it, builder) -> {
})
)
.build();

// when
List<String> actual = sut.giveMeBuilder(ListStringObject.class)
.size("values", 2)
.sample()
.getValues();

then(actual).hasSize(2);
}
}

0 comments on commit f82b838

Please sign in to comment.