Skip to content

Commit

Permalink
Add more innerSpec support methods (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
seongahjo authored Dec 6, 2022
1 parent 508fe82 commit 9432d84
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static com.navercorp.fixturemonkey.Constants.DEFAULT_ELEMENT_MAX_SIZE;
import static com.navercorp.fixturemonkey.Constants.DEFAULT_ELEMENT_MIN_SIZE;
import static com.navercorp.fixturemonkey.Constants.NO_OR_ALL_INDEX_INTEGER_VALUE;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -259,6 +260,14 @@ public InnerSpec listElement(int index, Consumer<InnerSpec> consumer) {
return this;
}

public InnerSpec allListElement(@Nullable Object value) {
return listElement(NO_OR_ALL_INDEX_INTEGER_VALUE, value);
}

public InnerSpec allListElement(Consumer<InnerSpec> consumer) {
return listElement(NO_OR_ALL_INDEX_INTEGER_VALUE, consumer);
}

public InnerSpec property(String property, @Nullable Object value) {
NodeResolver nextResolver = new CompositeNodeResolver(
this.treePathResolver,
Expand Down Expand Up @@ -347,6 +356,24 @@ public InnerSpec entryLazy(Supplier<?> keySupplier, Supplier<?> valueSupplier) {
return this;
}

public InnerSpec allKey(Consumer<InnerSpec> consumer) {
if (consumer == null) {
setKeyAll(null);
return this;
}

NodeResolver nextResolver = new CompositeNodeResolver(
this.treePathResolver,
new DefaultNodeResolver(new NodeAllEntryPredicate()),
new DefaultNodeResolver(new NodeKeyValuePredicate(true))
);
InnerSpec innerSpec = new InnerSpec(traverser, manipulateOptions, nextResolver);
consumer.accept(innerSpec);
arbitraryManipulators.addAll(innerSpec.arbitraryManipulators);
containerInfoManipulators.addAll(innerSpec.containerInfoManipulators);
return this;
}

public InnerSpec allKeyLazy(Supplier<?> supplier) {
LazyArbitrary<?> lazyArbitrary = LazyArbitrary.lazy(supplier);
arbitraryManipulators.add(new ArbitraryManipulator(
Expand All @@ -361,6 +388,39 @@ public InnerSpec allKeyLazy(Supplier<?> supplier) {
return this;
}

public InnerSpec allValue(@Nullable Object value) {
this.arbitraryManipulators.add(
new ArbitraryManipulator(
new CompositeNodeResolver(
this.treePathResolver,
new DefaultNodeResolver(new NodeAllEntryPredicate()),
new DefaultNodeResolver(new NodeKeyValuePredicate(false))
),
convertToNodeManipulator(value)
)
);

return this;
}

public InnerSpec allValue(Consumer<InnerSpec> consumer) {
if (consumer == null) {
allValue((Object)null);
return this;
}

NodeResolver nextResolver = new CompositeNodeResolver(
this.treePathResolver,
new DefaultNodeResolver(new NodeAllEntryPredicate()),
new DefaultNodeResolver(new NodeKeyValuePredicate(false))
);
InnerSpec innerSpec = new InnerSpec(traverser, manipulateOptions, nextResolver);
consumer.accept(innerSpec);
arbitraryManipulators.addAll(innerSpec.arbitraryManipulators);
containerInfoManipulators.addAll(innerSpec.containerInfoManipulators);
return this;
}

public InnerSpec allValueLazy(Supplier<?> supplier) {
LazyArbitrary<?> lazyArbitrary = LazyArbitrary.lazy(supplier);
arbitraryManipulators.add(new ArbitraryManipulator(
Expand All @@ -375,9 +435,8 @@ public InnerSpec allValueLazy(Supplier<?> supplier) {
return this;
}

public InnerSpec allEntryLazy(Supplier<?> keySupplier, Supplier<?> valueSupplier) {
public InnerSpec allEntry(Supplier<?> keySupplier, Object value) {
LazyArbitrary<?> keyLazyArbitrary = LazyArbitrary.lazy(keySupplier);
LazyArbitrary<?> valueLazyArbitrary = LazyArbitrary.lazy(valueSupplier);

this.arbitraryManipulators.add(
new ArbitraryManipulator(
Expand All @@ -397,12 +456,16 @@ public InnerSpec allEntryLazy(Supplier<?> keySupplier, Supplier<?> valueSupplier
new DefaultNodeResolver(new NodeAllEntryPredicate()),
new DefaultNodeResolver(new NodeKeyValuePredicate(false))
),
convertToNodeManipulator(valueLazyArbitrary)
convertToNodeManipulator(value)
)
);
return this;
}

public InnerSpec allEntryLazy(Supplier<?> keySupplier, Supplier<?> valueSupplier) {
return this.allEntry(keySupplier, valueSupplier);
}

public List<ArbitraryManipulator> getArbitraryManipulators() {
return arbitraryManipulators;
}
Expand All @@ -426,6 +489,19 @@ private void setValue(@Nullable Object value) {
);
}

private void setKeyAll(@Nullable Object value) {
this.arbitraryManipulators.add(
new ArbitraryManipulator(
new CompositeNodeResolver(
this.treePathResolver,
new DefaultNodeResolver(new NodeAllEntryPredicate()),
new DefaultNodeResolver(new NodeKeyValuePredicate(true))
),
convertToNodeManipulator(value)
)
);
}

private void setEntry(Object key, @Nullable Object value) {
if (key == null) {
throw new IllegalArgumentException(
Expand Down Expand Up @@ -466,6 +542,13 @@ private NodeManipulator convertToNodeManipulator(@Nullable Object value) {
LazyArbitrary.lazy(() -> ((Arbitrary<?>)value).sample()),
false
);
} else if (value instanceof Supplier) {
return new NodeSetLazyManipulator<>(
traverser,
manipulateOptions,
LazyArbitrary.lazy((Supplier<?>)value),
false
);
} else if (value instanceof LazyArbitrary) {
return new NodeSetLazyManipulator<>(
traverser,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.api.BDDAssertions.thenThrownBy;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -32,6 +33,7 @@
import com.navercorp.fixturemonkey.ArbitraryBuilder;
import com.navercorp.fixturemonkey.LabMonkey;
import com.navercorp.fixturemonkey.api.generator.ArbitraryContainerInfo;
import com.navercorp.fixturemonkey.api.type.TypeReference;
import com.navercorp.fixturemonkey.test.InnerSpecTestSpecs.ComplexObjectObject;
import com.navercorp.fixturemonkey.test.InnerSpecTestSpecs.IntegerMapObject;
import com.navercorp.fixturemonkey.test.InnerSpecTestSpecs.MapObject;
Expand Down Expand Up @@ -410,6 +412,19 @@ void allValueLazy() {
then(actual.getIntegerMap().values()).allMatch(it -> it >= 0 && it <= 100);
}

@Property
void allEntry() {
IntegerMapObject actual = SUT.giveMeBuilder(IntegerMapObject.class)
.setInner("integerMap", m -> m.allEntry(
() -> Arbitraries.integers().between(0, 100),
100
))
.sample();

then(actual.getIntegerMap().keySet()).allMatch(it -> it >= 0 && it <= 100);
then(actual.getIntegerMap().values()).allMatch(it -> it == 100);
}

@Property
void allEntryLazy() {
IntegerMapObject actual = SUT.giveMeBuilder(IntegerMapObject.class)
Expand All @@ -422,4 +437,82 @@ void allEntryLazy() {
then(actual.getIntegerMap().keySet()).allMatch(it -> it >= 0 && it <= 100);
then(actual.getIntegerMap().values()).allMatch(it -> it >= 0 && it <= 100);
}

@Property
void allKey() {
String expected = "test";

List<String> actual = SUT.giveMeBuilder(MapObject.class)
.setInner("objectKeyMap", m -> m.allKey(v ->
v.property("str", expected)
))
.sample()
.getObjectKeyMap()
.keySet()
.stream()
.map(SimpleObject::getStr)
.collect(Collectors.toList());

then(actual).allMatch(expected::equals);
}

@Property
void allValue() {
String expected = "test";

Collection<String> actual = SUT.giveMeBuilder(MapObject.class)
.setInner("strMap", m -> m.allValue(expected))
.sample()
.getStrMap()
.values();

then(actual).allMatch(expected::equals);
}

@Property
void allValueInner() {
String expected = "test";

List<String> actual = SUT.giveMeBuilder(MapObject.class)
.setInner("objectValueMap", m -> m.allValue(v ->
v.property("str", expected)
))
.sample()
.getObjectValueMap()
.values()
.stream()
.map(SimpleObject::getStr)
.collect(Collectors.toList());

then(actual).allMatch(expected::equals);
}

@Property
void allListElement() {
String expected = "test";

// when
List<String> actual = SUT.giveMeBuilder(new TypeReference<List<String>>() {
})
.setInner("$", l -> l.allListElement(expected))
.sample();

then(actual).allMatch(expected::equals);
}

@Property
void allListElementInnerSpec() {
String expected = "test";

// when
List<String> actual = SUT.giveMeBuilder(new TypeReference<List<List<String>>>() {
})
.setInner("$", l -> l.allListElement(inner -> inner.allListElement(expected)))
.sample()
.stream()
.flatMap(Collection::stream)
.collect(Collectors.toList());

then(actual).allMatch(expected::equals);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static class MapObject {
private Map<String, List<String>> listValueMap;
private Map<String, List<List<String>>> listListValueMap;
private Map<String, SimpleObject> objectValueMap;
private Map<SimpleObject, String> objectKeyMap;
}

@Data
Expand Down

0 comments on commit 9432d84

Please sign in to comment.