Skip to content

Commit

Permalink
Prefer usages of containsOnlyKeys when asserting keySets of a map
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedsamehsalah authored and mohamedsamehsalah committed Apr 16, 2023
1 parent 929f1dd commit 1fbff8c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import org.assertj.core.api.AbstractAssert;
Expand Down Expand Up @@ -220,6 +221,19 @@ MapAssert<K, V> after(Map<K, V> map, K key) {
}
}

static final class AssertThatMapContainsOnlyKeys<K, V> {
@BeforeTemplate
AbstractAssert<?, ?> before(Map<K, V> map, Set<K> keys) {
return assertThat(map.keySet()).hasSameElementsAs(keys);
}

@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractAssert<?, ?> after(Map<K, V> map, Set<K> keys) {
return assertThat(map).containsOnlyKeys(keys);
}
}

static final class AssertThatMapContainsValue<K, V> {
@BeforeTemplate
AbstractBooleanAssert<?> before(Map<K, V> map, V value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ MapAssert<Integer, Integer> testAbstractMapAssertHasSameSizeAs() {
return assertThat(ImmutableMap.of(1, 2).containsKey(3)).isFalse();
}

AbstractAssert<?, ?> testAssertThatMapContainsOnlyKeys() {
return assertThat(ImmutableMap.of(1, 2).keySet()).hasSameElementsAs(ImmutableSet.of(1));
}

AbstractAssert<?, ?> testAssertThatMapContainsValue() {
return assertThat(ImmutableMap.of(1, 2).containsValue(3)).isTrue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ MapAssert<Integer, Integer> testAbstractMapAssertHasSameSizeAs() {
return assertThat(ImmutableMap.of(1, 2)).doesNotContainKey(3);
}

AbstractAssert<?, ?> testAssertThatMapContainsOnlyKeys() {
return assertThat(ImmutableMap.of(1, 2)).containsOnlyKeys(ImmutableSet.of(1));
}

AbstractAssert<?, ?> testAssertThatMapContainsValue() {
return assertThat(ImmutableMap.of(1, 2)).containsValue(3);
}
Expand Down

0 comments on commit 1fbff8c

Please sign in to comment.