Skip to content

Commit

Permalink
#18: Ignore order for Sets
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed Nov 13, 2024
1 parent c5bfc2b commit 58bbd51
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [0.9.0] - unreleased

## [0.8.2] - 2024-11-13

* [#18](https://github.com/itsallcode/hamcrest-auto-matcher/issues/18): Ignore order for Sets

## [0.8.1] - 2024-09-12

* [#17](https://github.com/itsallcode/hamcrest-auto-matcher/pull/17): Allow using `null` as expected value
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ repositories {
}
dependencies {
testImplementation 'org.itsallcode:hamcrest-auto-matcher:0.8.1'
testImplementation 'org.itsallcode:hamcrest-auto-matcher:0.8.2'
}
```

Expand All @@ -43,7 +43,7 @@ dependencies {
<dependency>
<groupId>org.itsallcode</groupId>
<artifactId>hamcrest-auto-matcher</artifactId>
<version>0.8.1</version>
<version>0.8.2</version>
<scope>test</scope>
</dependency>
```
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group 'org.itsallcode'
version = '0.8.1'
version = '0.8.2'

dependencies {
api 'org.hamcrest:hamcrest:3.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ private static <T> Matcher<T> createIterableContainsMatcher(final T expected) {
final Object[] elements = StreamSupport.stream(expectedIterable //
.spliterator(), false) //
.toArray();
if (expected instanceof Set) {
@SuppressWarnings("unchecked")
final Matcher<T> matcher = (Matcher<T>) AutoMatcher.containsInAnyOrder(elements);
return matcher;
}
@SuppressWarnings("unchecked")
final Matcher<T> matcher = (Matcher<T>) AutoMatcher.contains(elements);
return matcher;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void testEmptyListAndNewLinkedList() {
}

@Test
void testEmptyListAndAsListAreEqaul() {
void testEmptyListAndAsListAreEqual() {
assertValuesMatch(emptyList(), asList());
}

Expand Down
53 changes: 53 additions & 0 deletions src/test/java/org/itsallcode/matcher/auto/AutoMatcherSetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.itsallcode.matcher.auto;

import static java.util.Collections.emptySet;
import static org.itsallcode.matcher.auto.TestUtil.assertValuesDoNotMatch;
import static org.itsallcode.matcher.auto.TestUtil.assertValuesMatch;

import java.util.*;

import org.itsallcode.matcher.model.DemoAttribute;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;

class AutoMatcherSetTest {

@Test
void testEmptySet() {
assertValuesDoNotMatch(emptySet(), Set.of("value2"));
}

@Test
void testIncompatibleMemberTypes() {
assertValuesDoNotMatch(Set.of("string"), Set.of(1));
}

@Test
void testIncompatibleMemberTypesComplexTypes() {
assertValuesDoNotMatch(Set.of(new DemoAttribute("attr")), Set.of(1));
}

@Test
void testEmptySetAndNewHashSetList() {
assertValuesMatch(emptySet(), new HashSet<>());
}

@Test
void testEmptySetAndNewTreeSet() {
assertValuesMatch(emptySet(), new TreeSet<>());
}

@Test
void testEmptySetAndAsSetOfAreEqual() {
assertValuesMatch(emptySet(), Set.of());
}

@RepeatedTest(name = "ignores set order repetition {currentRepetition} of {totalRepetitions}", value = 50)
void ignoresSetOrder() {

final Set<String> set = new HashSet<>();
set.add("value1");
set.add("value2");
assertValuesMatch(Set.of("value1", "value2"), set);
}
}

0 comments on commit 58bbd51

Please sign in to comment.