-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract Matcher classname to plugable strategy
Or, well, a function, if you want. Default implementation with support for nested records, fixes #3
- Loading branch information
1 parent
0540fce
commit 35c1403
Showing
9 changed files
with
164 additions
and
2 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
generator/src/main/java/no/rune/record/DefaultRecordMatcherClassNameResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package no.rune.record; | ||
|
||
final class DefaultRecordMatcherClassNameResolver implements RecordMatcherClassNameResolver { | ||
|
||
@Override | ||
public String resolve(Class<? extends Record> record) { | ||
var className = new StringBuilder(record.getSimpleName() + "Matcher"); | ||
|
||
for (var enclosing = record.getEnclosingClass(); enclosing != null; enclosing = enclosing.getEnclosingClass()) { | ||
className.insert(0, enclosing.getSimpleName()); | ||
} | ||
return className.toString(); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
generator/src/main/java/no/rune/record/RecordMatcherClassNameResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package no.rune.record; | ||
|
||
@FunctionalInterface | ||
public interface RecordMatcherClassNameResolver { | ||
|
||
String resolve(Class<? extends Record> record); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
generator/src/test/java/no/rune/record/DefaultRecordMatcherClassNameResolverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package no.rune.record; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static uk.co.probablyfine.matchers.Java8Matchers.where; | ||
|
||
class DefaultRecordMatcherClassNameResolverTest { | ||
|
||
DefaultRecordMatcherClassNameResolver nameResolver = new DefaultRecordMatcherClassNameResolver(); | ||
|
||
@Test | ||
void topLevelRecordResolvesMatcherClassName() { | ||
assertThat(Foo.class, where(nameResolver::resolve, is("FooMatcher"))); | ||
} | ||
|
||
@Test | ||
void firstLevelNestedMatcherClassName() { | ||
assertThat(Foo.Bar.class, where(nameResolver::resolve, is("FooBarMatcher"))); | ||
} | ||
|
||
@Test | ||
void secondLevelNestedMatcherClassName() { | ||
assertThat(Foo.Bar.Baz.class, where(nameResolver::resolve, is("FooBarBazMatcher"))); | ||
} | ||
} | ||
|
||
|
||
record Foo() { | ||
record Bar() { | ||
record Baz() {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
generator/src/test/java/no/rune/record/matcher/example/nested/NestedRecordsMatcherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package no.rune.record.matcher.example.nested; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static no.rune.record.matcher.ExpectedMatcher.expectedMatcherFor; | ||
|
||
class NestedRecordsMatcherTest { | ||
|
||
@Test | ||
void generatesExpectedMatcherForOneLevelNestedRecord() { | ||
expectedMatcherFor(TopLevel.Nested.class).assertEqualToGeneratedMatcherSourceCode(); | ||
} | ||
|
||
@Test | ||
void generatesExpectedMatcherForTwoLevelsNestedRecord() { | ||
expectedMatcherFor(TopLevel.Nested.EvenMore.class).assertEqualToGeneratedMatcherSourceCode(); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
generator/src/test/java/no/rune/record/matcher/example/nested/TopLevel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package no.rune.record.matcher.example.nested; | ||
|
||
public record TopLevel() { | ||
|
||
public record Nested(int value) { | ||
public record EvenMore() {} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...or/src/test/java/no/rune/record/matcher/example/nested/TopLevelNestedEvenMoreMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package no.rune.record.matcher.example.nested; | ||
|
||
import org.hamcrest.Description; | ||
import org.hamcrest.TypeSafeDiagnosingMatcher; | ||
|
||
public final class TopLevelNestedEvenMoreMatcher extends TypeSafeDiagnosingMatcher<TopLevel.Nested.EvenMore> { | ||
private TopLevelNestedEvenMoreMatcher() { | ||
} | ||
|
||
public static TopLevelNestedEvenMoreMatcher anEvenMore() { | ||
return new TopLevelNestedEvenMoreMatcher(); | ||
} | ||
|
||
@Override | ||
public void describeTo(Description description) { | ||
description.appendText("any ").appendText(TopLevel.Nested.EvenMore.class.getSimpleName()).appendText(" record"); | ||
} | ||
|
||
@Override | ||
protected boolean matchesSafely(TopLevel.Nested.EvenMore element, | ||
Description mismatchDescription) { | ||
boolean matches = true; | ||
return matches; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
generator/src/test/java/no/rune/record/matcher/example/nested/TopLevelNestedMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package no.rune.record.matcher.example.nested; | ||
|
||
import org.hamcrest.Description; | ||
import org.hamcrest.Matcher; | ||
import org.hamcrest.Matchers; | ||
import org.hamcrest.TypeSafeDiagnosingMatcher; | ||
import org.hamcrest.core.IsAnything; | ||
|
||
public final class TopLevelNestedMatcher extends TypeSafeDiagnosingMatcher<TopLevel.Nested> { | ||
private final Matcher<? super Integer> valueMatcher; | ||
|
||
private TopLevelNestedMatcher(Matcher<? super Integer> valueMatcher) { | ||
this.valueMatcher = valueMatcher; | ||
} | ||
|
||
public static TopLevelNestedMatcher aNested() { | ||
return new TopLevelNestedMatcher(new IsAnything<>("any value")); | ||
} | ||
|
||
public TopLevelNestedMatcher withValue(int value) { | ||
return withValue(Matchers.is(value)); | ||
} | ||
|
||
public TopLevelNestedMatcher withValue(Matcher<? super Integer> valueMatcher) { | ||
return new TopLevelNestedMatcher(valueMatcher); | ||
} | ||
|
||
@Override | ||
public void describeTo(Description description) { | ||
if (valueMatcher instanceof IsAnything) { | ||
description.appendText("any ").appendText(TopLevel.Nested.class.getSimpleName()).appendText(" record"); | ||
} | ||
else { | ||
description | ||
.appendText(TopLevel.Nested.class.getSimpleName()).appendText(" record where"); | ||
if (!(valueMatcher instanceof IsAnything)) | ||
description.appendText(" value ").appendDescriptionOf(valueMatcher); | ||
} | ||
} | ||
|
||
@Override | ||
protected boolean matchesSafely(TopLevel.Nested element, Description mismatchDescription) { | ||
boolean matches = true; | ||
if (!valueMatcher.matches(element.value())) { | ||
mismatchDescription.appendText(" value "); | ||
valueMatcher.describeMismatch(element.value(), mismatchDescription); | ||
matches = false; | ||
} | ||
return matches; | ||
} | ||
} |