Skip to content

Commit a4bd752

Browse files
kaklakariadakaklakariada
andauthored
#18: Ignore order for Sets (#19)
Co-authored-by: kaklakariada <[email protected]>
1 parent c5bfc2b commit a4bd752

File tree

6 files changed

+66
-4
lines changed

6 files changed

+66
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [0.9.0] - unreleased
88

9+
## [0.8.2] - 2024-11-13
10+
11+
* [#18](https://github.com/itsallcode/hamcrest-auto-matcher/issues/18): Ignore order for Sets
12+
913
## [0.8.1] - 2024-09-12
1014

1115
* [#17](https://github.com/itsallcode/hamcrest-auto-matcher/pull/17): Allow using `null` as expected value

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ repositories {
3434
}
3535
3636
dependencies {
37-
testImplementation 'org.itsallcode:hamcrest-auto-matcher:0.8.1'
37+
testImplementation 'org.itsallcode:hamcrest-auto-matcher:0.8.2'
3838
}
3939
```
4040

@@ -43,7 +43,7 @@ dependencies {
4343
<dependency>
4444
<groupId>org.itsallcode</groupId>
4545
<artifactId>hamcrest-auto-matcher</artifactId>
46-
<version>0.8.1</version>
46+
<version>0.8.2</version>
4747
<scope>test</scope>
4848
</dependency>
4949
```

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ plugins {
99
}
1010

1111
group 'org.itsallcode'
12-
version = '0.8.1'
12+
version = '0.8.2'
1313

1414
dependencies {
1515
api 'org.hamcrest:hamcrest:3.0'

src/main/java/org/itsallcode/matcher/auto/AutoConfigBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ private static <T> Matcher<T> createIterableContainsMatcher(final T expected) {
159159
final Object[] elements = StreamSupport.stream(expectedIterable //
160160
.spliterator(), false) //
161161
.toArray();
162+
if (expected instanceof Set) {
163+
@SuppressWarnings("unchecked")
164+
final Matcher<T> matcher = (Matcher<T>) AutoMatcher.containsInAnyOrder(elements);
165+
return matcher;
166+
}
162167
@SuppressWarnings("unchecked")
163168
final Matcher<T> matcher = (Matcher<T>) AutoMatcher.contains(elements);
164169
return matcher;

src/test/java/org/itsallcode/matcher/auto/AutoMatcherListTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void testEmptyListAndNewLinkedList() {
4444
}
4545

4646
@Test
47-
void testEmptyListAndAsListAreEqaul() {
47+
void testEmptyListAndAsListAreEqual() {
4848
assertValuesMatch(emptyList(), asList());
4949
}
5050

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.itsallcode.matcher.auto;
2+
3+
import static java.util.Collections.emptySet;
4+
import static org.itsallcode.matcher.auto.TestUtil.assertValuesDoNotMatch;
5+
import static org.itsallcode.matcher.auto.TestUtil.assertValuesMatch;
6+
7+
import java.util.*;
8+
9+
import org.itsallcode.matcher.model.DemoAttribute;
10+
import org.junit.jupiter.api.RepeatedTest;
11+
import org.junit.jupiter.api.Test;
12+
13+
class AutoMatcherSetTest {
14+
15+
@Test
16+
void testEmptySet() {
17+
assertValuesDoNotMatch(emptySet(), Set.of("value2"));
18+
}
19+
20+
@Test
21+
void testIncompatibleMemberTypes() {
22+
assertValuesDoNotMatch(Set.of("string"), Set.of(1));
23+
}
24+
25+
@Test
26+
void testIncompatibleMemberTypesComplexTypes() {
27+
assertValuesDoNotMatch(Set.of(new DemoAttribute("attr")), Set.of(1));
28+
}
29+
30+
@Test
31+
void testEmptySetAndNewHashSetList() {
32+
assertValuesMatch(emptySet(), new HashSet<>());
33+
}
34+
35+
@Test
36+
void testEmptySetAndNewTreeSet() {
37+
assertValuesMatch(emptySet(), new TreeSet<>());
38+
}
39+
40+
@Test
41+
void testEmptySetAndAsSetOfAreEqual() {
42+
assertValuesMatch(emptySet(), Set.of());
43+
}
44+
45+
@RepeatedTest(name = "ignores set order repetition {currentRepetition} of {totalRepetitions}", value = 50)
46+
void ignoresSetOrder() {
47+
48+
final Set<String> set = new HashSet<>();
49+
set.add("value1");
50+
set.add("value2");
51+
assertValuesMatch(Set.of("value1", "value2"), set);
52+
}
53+
}

0 commit comments

Comments
 (0)