Skip to content

Commit

Permalink
Fix failed cases when fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
seongahjo committed Sep 14, 2021
1 parent f12d52d commit 68fbb00
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,22 @@ private T sampleInternal() {
}, this.validator, this.validOnly).sample();
}

@SuppressWarnings("unchecked")
private T sampleInternal(List<BuilderManipulator> manipulators) {
return (T)this.tree.result(() -> {
ArbitraryTree<T> buildTree = this.tree;

this.traverser.traverse(
buildTree,
false,
this.generator
);
this.apply(manipulators);
buildTree.update(this.generator, generatorMap);
return buildTree.getArbitrary();
}, this.validator, this.validOnly).sample();
}

public List<T> sampleList(int size) {
return this.sampleStream().limit(size).collect(toList());
}
Expand Down Expand Up @@ -453,11 +469,9 @@ public ArbitraryBuilder<T> fixed() {
ArbitraryBuilder<T> copied = this.copy();

this.tree.setFixedDecomposedValue(() -> {
T sample = copied.sampleInternal();
copied.tree.setFixedDecomposedValue(() -> sample); // fix builder value
this.decomposedManipulators.forEach(it -> it.accept(sample, copied));
this.builderManipulators.removeAll(copied.builderManipulators); // remove pre-decompose manipulators
return copied.sampleInternal();
T sampled = copied.sampleInternal(this.builderManipulators); // sample with pre-fixed manipulators
this.builderManipulators.removeAll(copied.builderManipulators); // remove pre-fixed manipulators
return sampled;
});
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,10 +573,43 @@ void fixedRegisterList() {
}

@Property
void fixedWithSetArbitrary(){
void acceptIfWithSetWithRegister() {
// given
FixtureMonkey sut = FixtureMonkey.builder()
.registerGroup(FixedSetArbitraryArbitraryGroup.class)
.build();

// when
StringWrapperClass actual = sut.giveMeBuilder(StringWrapperClass.class)
.set("value", "test")
.acceptIf(it -> it.value.equals("test"), it -> it.set("value", "value"))
.sample();

then(actual.value).isEqualTo("value");
}

@Property
void fixedRegisterAcceptIf() {
// given
FixtureMonkey sut = FixtureMonkey.builder()
.registerGroup(ApplyArbitraryGroup.class)
.build();

// when
StringWrapperClass actual = sut.giveMeBuilder(StringWrapperClass.class)
.set("value", "test")
.acceptIf(it -> it.value.equals("test"), it -> it.set("value", "value"))
.fixed()
.sample();

then(actual.value).isEqualTo("value");
}

@Property
void fixedWithSetArbitrary() {
// given
ArbitraryBuilder<StringWrapperClass> arbitraryBuilder = this.sut.giveMeBuilder(StringWrapperClass.class)
.set("value",Arbitraries.strings().numeric())
.set("value", Arbitraries.strings().numeric())
.fixed();

// when
Expand All @@ -586,6 +619,42 @@ void fixedWithSetArbitrary(){
then(actual1).isEqualTo(actual2);
}

@Property
void acceptIfWithSetWithFixedOverride() {
// when
StringWrapperClass actual = this.sut.giveMeBuilder(StringWrapperClass.class)
.acceptIf(it -> true, it -> it.set("value", "value"))
.set("value", "fixed")
.fixed()
.sample();

then(actual.value).isEqualTo("fixed");
}

@Property
void acceptIfWithFixed() {
// when
StringWrapperIntegerWrapperClass actual = this.sut.giveMeBuilder(StringWrapperIntegerWrapperClass.class)
.acceptIf(it -> true, it -> it.set("value1.value", "value"))
.fixed()
.sample();

then(actual.value1.value).isEqualTo("value");
}

@Property
void acceptIfWithSetWithFixed() {
// when
StringWrapperIntegerWrapperClass actual = this.sut.giveMeBuilder(StringWrapperIntegerWrapperClass.class)
.acceptIf(it -> true, it -> it.set("value1.value", "value"))
.set("value2.value", 5)
.fixed()
.sample();

then(actual.value1.value).isEqualTo("value");
then(actual.value2.value).isEqualTo(5);
}

@Data
@NoArgsConstructor
@AllArgsConstructor
Expand Down Expand Up @@ -680,4 +749,15 @@ public ArbitraryBuilder<StringWrapperClass> string(FixtureMonkey fixture) {
.fixed();
}
}

public static class FixedSetArbitraryArbitraryGroup {
public FixedSetArbitraryArbitraryGroup() {
}

public ArbitraryBuilder<StringWrapperClass> string(FixtureMonkey fixture) {
return fixture.giveMeBuilder(StringWrapperClass.class)
.set("value", Arbitraries.strings())
.fixed();
}
}
}

0 comments on commit 68fbb00

Please sign in to comment.