-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Breaking change:
stream()
semantics when used with stateful generat…
…ors. With this commit, all root objects created by `stream()` are independent of each other. This affects the behaviour of `stream()` when used with stateful generators such as `emit()`, `intSeq()`, `longSeq()`. Example: ```java List<String> results = Instancio.of(String.class) .generate(allStrings(), gen -> gen.emit().items("foo", "bar", "baz").ignoreUnused()) .stream() .limit(3) .toList(); // Original behaviour assertThat(results).containsExactly("foo", "bar", "baz"); // New behaviour assertThat(results).containsExactly("foo", "foo", "foo"); ```
- Loading branch information
Showing
9 changed files
with
396 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...feature-tests/src/test/java/org/instancio/test/features/generator/misc/EmitModelTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2022-2023 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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 org.instancio.test.features.generator.misc; | ||
|
||
import org.instancio.Instancio; | ||
import org.instancio.Model; | ||
import org.instancio.junit.InstancioExtension; | ||
import org.instancio.test.support.tags.Feature; | ||
import org.instancio.test.support.tags.FeatureTag; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.instancio.Select.allStrings; | ||
|
||
@FeatureTag({Feature.EMIT_GENERATOR, Feature.MODEL}) | ||
@ExtendWith(InstancioExtension.class) | ||
class EmitModelTest { | ||
|
||
@Test | ||
void emitWithTwoModels() { | ||
final String expected = "foo"; | ||
|
||
final Model<String> model = Instancio.of(String.class) | ||
.generate(allStrings(), gen -> gen.emit().items(expected)) | ||
.toModel(); | ||
|
||
// Each root object should have its own copy of items to emit | ||
// even though they share the same instance of a model | ||
final String result1 = Instancio.create(model); | ||
final String result2 = Instancio.create(model); | ||
|
||
assertThat(result1).isEqualTo(result2).isEqualTo(expected); | ||
} | ||
|
||
} |
91 changes: 91 additions & 0 deletions
91
...eature-tests/src/test/java/org/instancio/test/features/generator/misc/EmitStreamTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright 2022-2023 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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 org.instancio.test.features.generator.misc; | ||
|
||
import org.instancio.Instancio; | ||
import org.instancio.TypeToken; | ||
import org.instancio.exception.InstancioApiException; | ||
import org.instancio.junit.InstancioExtension; | ||
import org.instancio.test.support.tags.Feature; | ||
import org.instancio.test.support.tags.FeatureTag; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.instancio.Select.all; | ||
|
||
/** | ||
* {@code stream()} creates a new root object each time. | ||
* Therefore, each root object gets its own copy of values to emit. | ||
*/ | ||
@FeatureTag({Feature.EMIT_GENERATOR, Feature.STREAM}) | ||
@ExtendWith(InstancioExtension.class) | ||
class EmitStreamTest { | ||
|
||
@Test | ||
void throwsAnExceptionOnUnusedItems() { | ||
final String[] items = {"foo", "bar", "baz"}; | ||
|
||
final Stream<String> stream = Instancio.of(String.class) | ||
.generate(all(String.class), gen -> gen.emit().items(items)) | ||
.stream() | ||
.limit(items.length); | ||
|
||
assertThatThrownBy(() -> stream.collect(toList())) // NOSONAR | ||
.isExactlyInstanceOf(InstancioApiException.class) | ||
.hasMessageContainingAll( | ||
"not all the items provided via the 'emit()' method have been consumed", | ||
"Remaining items: [bar, baz]"); | ||
} | ||
|
||
@Test | ||
void ignoreUnusedItems() { | ||
final String[] items = {"foo", "bar", "baz"}; | ||
|
||
final Stream<String> results = Instancio.of(String.class) | ||
.generate(all(String.class), gen -> gen.emit().items(items).ignoreUnused()) | ||
.stream() | ||
.limit(items.length); | ||
|
||
assertThat(results) | ||
.as("Should pick the first value from items for each root objectm and ignore the rest") | ||
.containsExactly("foo", "foo", "foo"); | ||
} | ||
|
||
@Test | ||
void withMultipleItems() { | ||
final String[] items = {"foo", "bar", "baz"}; | ||
|
||
final int outerListSize = 10; | ||
|
||
final List<List<String>> results = Instancio.of(new TypeToken<List<String>>() {}) | ||
.generate(all(List.class), gen -> gen.collection().size(items.length)) // inner list | ||
.generate(all(String.class), gen -> gen.emit().items(items)) | ||
.stream() | ||
.limit(outerListSize) | ||
.collect(toList()); | ||
|
||
assertThat(results) | ||
.hasSize(outerListSize) | ||
.allMatch(r -> r.equals(Arrays.asList(items))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.