Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'InnerSpec.keys()' Method with Collection Parameter Support #934

Merged
merged 4 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
Expand Down Expand Up @@ -166,6 +167,16 @@ public InnerSpec key(Object key) {
return this;
}

/**
* Sets multiple keys in the currently referred map property from a Collection.
*
* @param keys The Collection of keys to set in the map. Can be empty.
*/
public InnerSpec keys(Collection<?> keys) {
keys.forEach(this::key);
return this;
}

/**
* Sets multiple keys in the currently referred map property.
*
Expand All @@ -188,6 +199,16 @@ public InnerSpec key(Consumer<InnerSpec> consumer) {
return this;
}

/**
* Sets a value in the currently referred map property from a Collection.
*
* @param values The Collection of values to set in the map. Can be empty.
*/
public InnerSpec values(Collection<?> values) {
values.forEach(this::value);
return this;
}

/**
* Sets a value in the currently referred map property.
*
Expand Down Expand Up @@ -233,6 +254,23 @@ public InnerSpec entry(Object key, @Nullable Object value) {
return this;
}

/**
* Sets multiple key-value pairs in the map from a Collection.
*
* @param entries The entries to be added to the map. Should be entered in key, value order. Can be empty.
*/
public InnerSpec entries(Collection<?> entries) {
if (entries.size() % 2 != 0) {
throw new IllegalArgumentException("key-value pairs for the Map should be entered");
}

IntStream.range(0, entries.size())
.filter(i -> i % 2 == 0)
.forEach(i -> entry(entries.toArray()[i], entries.toArray()[i + 1]));

return this;
}

/**
* Sets multiple key-value pairs in the map.
*
Expand All @@ -245,9 +283,7 @@ public InnerSpec entries(Object... entries) {

IntStream.range(0, entries.length)
.filter(i -> i % 2 == 0)
.forEach(i -> {
entry(entries[i], entries[i + 1]);
});
.forEach(i -> entry(entries[i], entries[i + 1]));

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.assertj.core.api.BDDAssertions.thenThrownBy;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -827,4 +828,50 @@ void setNotNull() {

then(actual).isNotNull();
}

@Property
void keysForCollection() {
List<String> keyList = Arrays.asList("key1", "key2", "key3");

Map<String, String> actual = SUT.giveMeBuilder(MapObject.class)
.setInner(
new InnerSpec()
.property("strMap", it -> it.keys(keyList).size(3))
)
.sample()
.getStrMap();

then(actual.keySet()).containsAll(keyList);
}

@Property
void valuesForCollection() {
List<String> valueList = Arrays.asList("value1", "value2", "value3");

Map<String, String> actual = SUT.giveMeBuilder(MapObject.class)
.setInner(
new InnerSpec()
.property("strMap", it -> it.values(valueList).size(3))
)
.sample()
.getStrMap();

then(actual.values()).containsAll(valueList);
}

@Property
void entriesForCollection() {
List<String> entries = Arrays.asList("key1", "value1", "key2", "value2");

Map<String, String> actual = SUT.giveMeBuilder(MapObject.class)
.setInner(
new InnerSpec()
.property("strMap", it -> it.entries(entries).size(2))
)
.sample()
.getStrMap();

then(actual.get("key1")).isEqualTo("value1");
then(actual.get("key2")).isEqualTo("value2");
}
}
Loading