Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
170 additions
and
23 deletions.
There are no files selected for viewing
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
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
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
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
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
88 changes: 88 additions & 0 deletions
88
...in/java/ch/powerunit/extensions/matchers/provideprocessor/fields/MapFieldDescription.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,88 @@ | ||
/** | ||
* | ||
*/ | ||
package ch.powerunit.extensions.matchers.provideprocessor.fields; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Optional; | ||
|
||
import javax.lang.model.type.DeclaredType; | ||
|
||
import ch.powerunit.extensions.matchers.provideprocessor.ProvidesMatchersAnnotatedElementData; | ||
import ch.powerunit.extensions.matchers.provideprocessor.ProvidesMatchersAnnotatedElementMirror; | ||
|
||
public class MapFieldDescription extends DefaultFieldDescription { | ||
|
||
private static final String EMPTY_MATCHER = "new org.hamcrest.CustomTypeSafeMatcher<%1$s>(\"map is empty\"){ public boolean matchesSafely(%1$s o) {return o.isEmpty();}}"; | ||
|
||
private static final String SIZE_MATCHER = "new org.hamcrest.CustomTypeSafeMatcher<%1$s>(\"map size is \"+other.size()){ public boolean matchesSafely(%1$s o) {return o.size()==other.size();} protected void describeMismatchSafely(%1$s item, org.hamcrest.Description mismatchDescription) {mismatchDescription.appendText(\" was size=\").appendValue(item.size());}}"; | ||
|
||
public MapFieldDescription(ProvidesMatchersAnnotatedElementData containingElementMirror, | ||
FieldDescriptionMirror mirror) { | ||
super(containingElementMirror, mirror); | ||
} | ||
|
||
@Override | ||
protected Collection<FieldDSLMethod> getSpecificFieldDslMethodFor() { | ||
String fieldType = getFieldType(); | ||
final String emptyMatcher = String.format(EMPTY_MATCHER, fieldType); | ||
|
||
Collection<FieldDSLMethod> tmp = new ArrayList<>(); | ||
tmp.add(getDslMethodBuilder().withSuffixDeclarationJavadocAndDefault("IsEmpty", "the map is empty", | ||
emptyMatcher)); | ||
if (!"".equals(generic)) { | ||
Object matchers[] = Optional.of(mirror.getFieldTypeMirror()).filter(m -> m instanceof DeclaredType) | ||
.map(DeclaredType.class::cast).filter(m -> m.getTypeArguments().size() == 2) | ||
.map(m -> m.getTypeArguments().stream().map(Object::toString) | ||
.map(o -> Optional.ofNullable(getByName(o)) | ||
.filter(ProvidesMatchersAnnotatedElementMirror::hasWithSameValue) | ||
.map(t -> t.getFullyQualifiedNameOfGeneratedClass() + "." | ||
+ t.getMethodShortClassName() + "WithSameValue") | ||
.orElse(MATCHERS + ".is")) | ||
.toArray()) | ||
.orElse(new String[] { MATCHERS + ".is", MATCHERS + ".is" }); | ||
|
||
tmp.add(generateHasSameValue(fieldType, matchers[0].toString(), matchers[1].toString())); | ||
} | ||
return tmp; | ||
} | ||
|
||
private FieldDSLMethod generateHasSameValue(String fieldType, String keyMatcher, String keyValue) { | ||
final String sizeMatcher = String.format(SIZE_MATCHER, fieldType); | ||
return getDslMethodBuilder().withDeclaration("HasSameValues", fieldType + " other") | ||
.withJavaDoc("verify that the value from the other map are exactly the once inside this map", | ||
"other the other map") | ||
.havingDefault(MATCHERS + ".both(" + sizeMatcher + ").and(" + MATCHERS | ||
+ ".allOf(other.entrySet().stream().map(kv->" + MATCHERS + ".hasEntry(" + keyMatcher | ||
+ "(kv.getKey())," + keyValue | ||
+ "(kv.getValue()))).collect(java.util.stream.Collectors.toList())))"); | ||
} | ||
|
||
@Override | ||
public String getMatcherForField() { | ||
String matcher = super.getMatcherForField(); | ||
if (!"".equals(generic)) { | ||
String localGeneric = generic.contains("?") ? "" : "<" + generic + ">"; | ||
matcher += "\n" + String.format( | ||
"private static class %1$sMatcherSameValue%2$s extends org.hamcrest.FeatureMatcher<%3$s,%4$s> {\n public %1$sMatcherSameValue(org.hamcrest.Matcher<? super %4$s> matcher) {\n super(matcher,\"%5$s\",\"%5$s\");\n }\n protected %4$s featureValueOf(%3$s actual) {\n return (java.util.Set)actual.entrySet();\n }\n}\n", | ||
mirror.getMethodFieldName(), containingElementMirror.getFullGeneric(), getFieldType(), | ||
"java.util.Set<java.util.Map.Entry" + localGeneric + ">", " [entries of] "); | ||
} | ||
return matcher; | ||
} | ||
|
||
@Override | ||
public String getFieldCopy(String lhs, String rhs) { | ||
if (!"".equals(generic)) { | ||
String fieldAccessor = getFieldAccessor(); | ||
String fieldName = getFieldName(); | ||
return "if(" + rhs + "." + fieldAccessor + "==null) {" + lhs + "." + fieldName + "(" + MATCHERS | ||
+ ".nullValue()); } else {" + lhs + "." + fieldName + "HasSameValues(" + rhs + "." + fieldAccessor | ||
+ ");}"; | ||
} | ||
return super.getFieldCopy(lhs, rhs); | ||
|
||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
src/test/java/ch/powerunit/extensions/matchers/samples/MapSample.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,22 @@ | ||
package ch.powerunit.extensions.matchers.samples; | ||
|
||
import java.util.Map; | ||
|
||
import ch.powerunit.extensions.matchers.ProvideMatchers; | ||
import ch.powerunit.extensions.matchers.samples.extension.MyTestWithoutGeneric; | ||
import ch.powerunit.extensions.matchers.samples.extension.MyTestWithoutGeneric2; | ||
|
||
@ProvideMatchers | ||
public class MapSample<E, V> { | ||
public Map<String, Integer> map1; | ||
|
||
public Map<E, V> map2; | ||
|
||
public Map map3; | ||
|
||
public Map<?, ?> map4; | ||
|
||
public Map<String, MyTestWithoutGeneric> map5; | ||
|
||
public Map<String, MyTestWithoutGeneric2> map6; | ||
} |
24 changes: 24 additions & 0 deletions
24
src/test/java/ch/powerunit/extensions/matchers/samples/MapSampleTest.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,24 @@ | ||
package ch.powerunit.extensions.matchers.samples; | ||
|
||
import java.util.Collections; | ||
|
||
import ch.powerunit.Test; | ||
import ch.powerunit.TestSuite; | ||
import ch.powerunit.extensions.matchers.samples.extension.MyTestWithoutGeneric2; | ||
|
||
public class MapSampleTest implements TestSuite { | ||
@Test | ||
public void testSameValue() { | ||
MapSample<String, Long> ms = new MapSample<>(); | ||
ms.map1 = Collections.singletonMap("x", 12); | ||
ms.map2 = Collections.singletonMap("y", 12L); | ||
ms.map3 = Collections.singletonMap("z", "a"); | ||
ms.map4 = Collections.singletonMap("a", "b"); | ||
assertThat(ms).is(MapSampleMatchers.<String, Long>mapSampleWith() | ||
.map1HasSameValues(Collections.singletonMap("x", 12)).build()); | ||
assertThat(ms).isNot( | ||
MapSampleMatchers.<String, Long>mapSampleWith().map1HasSameValues(Collections.emptyMap()).build()); | ||
assertThat(ms).isNot(MapSampleMatchers.<String, Long>mapSampleWith() | ||
.map6HasSameValues(Collections.singletonMap("x", new MyTestWithoutGeneric2("z"))).build()); | ||
} | ||
} |