Skip to content

Commit

Permalink
Add ArbitraryBuilder set value lazily (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
SooKim1110 authored May 10, 2022
1 parent 60c3c6d commit 722dab2
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import com.navercorp.fixturemonkey.arbitrary.ArbitraryNullity;
import com.navercorp.fixturemonkey.arbitrary.ArbitrarySet;
import com.navercorp.fixturemonkey.arbitrary.ArbitrarySetArbitrary;
import com.navercorp.fixturemonkey.arbitrary.ArbitrarySetLazyValue;
import com.navercorp.fixturemonkey.arbitrary.ArbitrarySetPostCondition;
import com.navercorp.fixturemonkey.arbitrary.ArbitrarySpecAny;
import com.navercorp.fixturemonkey.arbitrary.ArbitraryTraverser;
Expand Down Expand Up @@ -246,6 +247,12 @@ public ArbitraryBuilder<T> set(String expression, @Nullable Object value) {
return this;
}

public ArbitraryBuilder<T> setLazy(String expression, Supplier<T> supplier) {
ArbitraryExpression arbitraryExpression = ArbitraryExpression.from(expression);
this.builderManipulators.add(new ArbitrarySetLazyValue<>(arbitraryExpression, supplier));
return this;
}

public ArbitraryBuilder<T> set(String expression, Object value, long limit) {
ArbitraryExpression arbitraryExpression = ArbitraryExpression.from(expression);
this.builderManipulators.add(new ArbitrarySet<>(arbitraryExpression, value, limit));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Fixture Monkey
*
* Copyright (c) 2021-present NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.fixturemonkey.arbitrary;

import java.util.Objects;
import java.util.function.Supplier;

import net.jqwik.api.Arbitraries;
import net.jqwik.api.Arbitrary;

public final class ArbitrarySetLazyValue<T> extends AbstractArbitrarySet<T> {
private final Supplier<T> supplier;
private long limit;

public ArbitrarySetLazyValue(ArbitraryExpression arbitraryExpression, Supplier<T> supplier, long limit) {
super(arbitraryExpression);
this.supplier = supplier;
this.limit = limit;
}

public ArbitrarySetLazyValue(ArbitraryExpression arbitraryExpression, Supplier<T> supplier) {
this(arbitraryExpression, supplier, Long.MAX_VALUE);
}

@Override
public T getValue() {
return supplier.get();
}

@Override
public Arbitrary<T> apply(Arbitrary<T> from) {
if (this.limit > 0) {
limit--;
return Arbitraries.just(getValue());
} else {
return from;
}
}

@Override
public ArbitrarySetLazyValue<T> copy() {
return new ArbitrarySetLazyValue<>(this.getArbitraryExpression(), this.supplier, this.limit);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
if (!super.equals(obj)) {
return false;
}
ArbitrarySetLazyValue<?> that = (ArbitrarySetLazyValue<?>)obj;
return getValue().equals(that.getValue());
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import net.jqwik.api.Property;
import net.jqwik.api.domains.Domain;

import com.navercorp.fixturemonkey.ArbitraryBuilder;
import com.navercorp.fixturemonkey.customizer.ExpressionSpec;
import com.navercorp.fixturemonkey.test.SimpleManipulatorTestSpecs.IntValue;
import com.navercorp.fixturemonkey.test.SimpleManipulatorTestSpecs.IntegerList;
Expand Down Expand Up @@ -856,4 +857,32 @@ void giveMeIncludeFixedArbitrarySize() {

then(actual.getValues()).hasSizeBetween(0, 4);
}

@Property
void giveMeSetLazyValue() {
// given
ArbitraryBuilder<String> variable = SUT.giveMeBuilder(String.class);
ArbitraryBuilder<String> builder = SUT.giveMeBuilder(String.class)
.setLazy("$", () -> variable.sample());
variable.set("test");

// when
String actual = builder.sample();

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

@Property(tries = 1)
void giveMeSetLazyValueSampleGivesDifferentValue() {
// given
ArbitraryBuilder<String> variable = SUT.giveMeBuilder(String.class);
ArbitraryBuilder<String> builder = SUT.giveMeBuilder(String.class)
.setLazy("$", () -> variable.sample());
String expected = builder.sample();

// when
String actual = builder.sample();

then(actual).isNotEqualTo(expected);
}
}

0 comments on commit 722dab2

Please sign in to comment.