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

Allow using null as expected value #17

Merged
merged 4 commits into from
Sep 12, 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
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ jobs:
fetch-depth: 0
- uses: actions/setup-java@v4
with:
cache: gradle
distribution: temurin
java-version: |
11
17
21
- uses: gradle/actions/wrapper-validation@v3
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v4
- name: Build with Java ${{ matrix.java }}
run: ./gradlew build --info --warning-mode=summary -PjavaVersion=${{ matrix.java }}
- name: Sonar analysis
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ jobs:

- uses: actions/setup-java@v4
with:
cache: gradle
distribution: temurin
java-version: |
11
17

- uses: gradle/actions/wrapper-validation@v3
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v4

- name: Initialize CodeQL
uses: github/codeql-action/init@v3
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ jobs:
with:
distribution: "temurin"
java-version: 11
cache: "gradle"

- name: Setup Gradle
uses: gradle/actions/setup-gradle@v4

- name: Build
run: ./gradlew build --warning-mode all
Expand Down
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.1] - 2024-09-12

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

## [0.8.0] - 2024-09-01

### Changes
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.0'
testImplementation 'org.itsallcode:hamcrest-auto-matcher:0.8.1'
}
```

Expand All @@ -43,7 +43,7 @@ dependencies {
<dependency>
<groupId>org.itsallcode</groupId>
<artifactId>hamcrest-auto-matcher</artifactId>
<version>0.8.0</version>
<version>0.8.1</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.0'
version = '0.8.1'

dependencies {
api 'org.hamcrest:hamcrest:3.0'
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.1-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ MatcherConfig<T> build() {
return configBuilder.build();
}

@SuppressWarnings("unchecked")
static <T> Matcher<T> createEqualToMatcher(final T expected) {
if (expected == null) {
return (Matcher<T>) Matchers.nullValue();
}
final Class<? extends Object> type = expected.getClass();
if (type.isArray()) {
return createArrayMatcher(expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ void testStringArrayEmpty() {
assertValuesDoNotMatch(new String[] { "a" }, new String[0]);
}

@Test
void testObjectArrayEmpty() {
assertValuesDoNotMatch(new Object[] { "a" }, new Object[0]);
}

@Test
void testObjectArrayMixed() {
assertValuesDoNotMatch(new Object[] { "a", null, 1 }, new Object[0]);
}

@Test
void testComplexObjectArrayMatch() {
assertValuesMatch(new ArrayElement[] { new ArrayElement(1, "a") },
Expand Down
24 changes: 22 additions & 2 deletions src/test/java/org/itsallcode/matcher/auto/AutoMatcherListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import static org.itsallcode.matcher.auto.TestUtil.assertValuesDoNotMatch;
import static org.itsallcode.matcher.auto.TestUtil.assertValuesMatch;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.*;

import org.itsallcode.matcher.model.DemoAttribute;
import org.junit.jupiter.api.Test;
Expand All @@ -24,6 +23,11 @@ void testIncompatibleMemberTypes() {
assertValuesDoNotMatch(asList("string"), asList(1));
}

@Test
void testActualNull() {
assertValuesDoNotMatch(asList((String) null), asList("not null"));
}

@Test
void testIncompatibleMemberTypesComplexTypes() {
assertValuesDoNotMatch(asList(new DemoAttribute("attr")), asList(1));
Expand Down Expand Up @@ -67,4 +71,20 @@ void testAsListAndNewArrayListWith1Entry() {
list.add("value1");
assertValuesMatch(asList("value1"), list);
}

@Test
void testListOfAndNewArrayListWith1Entry() {
final ArrayList<String> list = new ArrayList<>();
list.add("value1");
assertValuesMatch(List.of("value1"), list);
}

@Test
void testListWithMultipleEntries() {
final ArrayList<String> list = new ArrayList<>();
list.add("value1");
list.add(null);
list.add("value2");
assertValuesMatch(asList("value1", null, "value2"), list);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ void testSingletonMap() {
assertValuesDoNotMatch(singletonMap("key1", "value1"), singletonMap("key2", "value2"));
}

@Test
void testSingletonMapAndEmptyMapOf() {
assertValuesDoNotMatch(singletonMap("key1", "value1"), Map.of());
}
@Test
void testSingletonMapAndEmptyMap() {
assertValuesDoNotMatch(singletonMap("key1", "value1"), emptyMap());
Expand All @@ -43,6 +47,11 @@ void testSingletonMapAndNewHashMap() {
assertValuesDoNotMatch(singletonMap("key1", "value1"), new HashMap<>());
}

@Test
void testMapWithNullEntry() {
assertValuesDoNotMatch(singletonMap("key1", null), new HashMap<>());
}

@Test
void testIncompatibleTypesClassNotPublic() {
final List<String> actual = singletonList("value1");
Expand All @@ -57,10 +66,17 @@ void testAutoMatcherWorksForSingletonMapAndNewHashMapWith1Entry() {
assertValuesMatch(singletonMap("key1", "value1"), map);
}

@Test
void testHashMapWithNullEntry() {
final HashMap<Object, Object> map = new HashMap<>();
map.put("key1", null);
assertValuesMatch(singletonMap("key1", null), map);
}

@Test
void testSingletonMapAndNewHashMapWith1Entry() {
final HashMap<Object, Object> map = new HashMap<>();
map.put("key1", "value1");
assertValuesMatch(singletonMap("key1", "value1"), map);
assertValuesMatch(Map.of("key1", "value1"), map);
}
}
15 changes: 10 additions & 5 deletions src/test/java/org/itsallcode/matcher/auto/AutoMatcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ void setup() {
value2Equal = model("m", 2);
}

@Test
void testNull() {
assertThat(null, AutoMatcher.equalTo(null));
}

@Test
void testNotNull() {
assertThat("null", not(AutoMatcher.equalTo(null)));
}

@Test
@SuppressWarnings("unchecked")
void testIncompatibleTypes() {
Expand Down Expand Up @@ -81,11 +91,6 @@ void testEqualToNotEqual() {
assertThat(value1, not(AutoMatcher.equalTo(value2)));
}

@Test
void testEqualToNullThrowsNullPointerException() {
assertThrows(NullPointerException.class, () -> AutoMatcher.equalTo(null));
}

@Test
void testContainsActualEmptyListSuccess() {
assertThat(emptyList(), AutoMatcher.contains());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ public abstract class MatcherTestBase<T> {

protected abstract Function<T, Matcher<T>> createNewSUT();

@Test
void testNullObject() {
assertThrows(NullPointerException.class, () -> createMatcher(null));
}

@Test
void subjectUnderTestMustBeNotNull() {
assertNotNull(createNewSUT());
Expand Down