-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#106) Introduces Mismatches Matcher to test Matchers
- Loading branch information
1 parent
313d2d5
commit 3a33a27
Showing
3 changed files
with
229 additions
and
9 deletions.
There are no files selected for viewing
148 changes: 148 additions & 0 deletions
148
src/main/java/org/llorllale/cactoos/matchers/Mismatches.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,148 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) for portions of project cactoos-matchers are held by | ||
* Yegor Bugayenko, 2017-2018, as part of project cactoos. | ||
* All other copyright for project cactoos-matchers are held by | ||
* George Aristy, 2018. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included | ||
* in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package org.llorllale.cactoos.matchers; | ||
|
||
import org.cactoos.Text; | ||
import org.cactoos.text.Joined; | ||
import org.cactoos.text.TextOf; | ||
import org.cactoos.text.UncheckedText; | ||
import org.hamcrest.Description; | ||
import org.hamcrest.Matcher; | ||
import org.hamcrest.TypeSafeDiagnosingMatcher; | ||
|
||
/** | ||
* Matcher to test {@link org.hamcrest.Matcher} objects. | ||
* | ||
* <p>Here is an example:</p> | ||
* <pre>{@code | ||
* new Assertion<>( | ||
* "Can't describe a mismatch", | ||
* new HasLines("Tom", "Mike"), | ||
* new Mismatches<>( | ||
* String.format("Tom%nJohn%n"), | ||
* "Lines are <[Tom, Mike]>", | ||
* "<[Tom, John]>" | ||
* ) | ||
* ).affirm(); | ||
* }</pre> | ||
* | ||
* @param <X> Type of item. | ||
* @since 1.0.0 | ||
* @todo #106:30min Convert all the Matcher tests to use Mismatches | ||
* instead of checking directly the output of mismatch methods or | ||
* the exception of Assertion. | ||
* @todo #106:30min Add extra tests for this class to validate all | ||
* the different constructors (also add a constructor taking message | ||
* as a String) and ensure they are coherent with how Assertion is | ||
* working and throwing errors. | ||
* @checkstyle ProtectedMethodInFinalClassCheck (200 lines) | ||
*/ | ||
public final class Mismatches<X> extends TypeSafeDiagnosingMatcher<Matcher<X>> { | ||
|
||
/** | ||
* The testing arguments for the target matcher. | ||
*/ | ||
private final X args; | ||
|
||
/** | ||
* The expected mismatch message. | ||
*/ | ||
private final Text message; | ||
|
||
/** | ||
* Ctor. | ||
* @param args The testing arguments for the matcher. | ||
* @param expected The expected portion of the mismatch message. | ||
* @param actual The actual portion of the mismatch message. | ||
*/ | ||
public Mismatches( | ||
final X args, | ||
final String expected, | ||
final String actual | ||
) { | ||
this(args, new TextOf(expected), new TextOf(actual)); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* @param args The testing arguments for the matcher. | ||
* @param expected The expected portion of the mismatch message. | ||
* @param actual The actual portion of the mismatch message. | ||
*/ | ||
public Mismatches( | ||
final X args, | ||
final Text expected, | ||
final Text actual | ||
) { | ||
this( | ||
args, | ||
new Joined( | ||
new TextOf("\n"), | ||
new TextOf(""), | ||
new Joined(new TextOf(""), new TextOf("Expected: "), expected), | ||
new Joined(new TextOf(""), new TextOf(" but was: "), actual) | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* @param args The testing arguments for the matcher. | ||
* @param message The expected mismatch message. | ||
*/ | ||
public Mismatches(final X args, final Text message) { | ||
super(); | ||
this.args = args; | ||
this.message = message; | ||
} | ||
|
||
@Override | ||
public void describeTo(final Description desc) { | ||
desc.appendText("Mismatches ") | ||
.appendValue(this.args) | ||
.appendText(" with message ") | ||
.appendValue(this.message); | ||
} | ||
|
||
// @todo #106:30min Add a description in case the mismatch fails. | ||
// And then introduce some tests to validate that the description | ||
// is properly constructed. | ||
@Override | ||
protected boolean matchesSafely(final Matcher<X> matcher, | ||
final Description dsc) { | ||
boolean mismatch; | ||
try { | ||
new Assertion<>("", this.args, matcher).affirm(); | ||
mismatch = false; | ||
} catch (final AssertionError err) { | ||
mismatch = err.getMessage().equals( | ||
new UncheckedText(this.message).asString() | ||
); | ||
} | ||
return mismatch; | ||
} | ||
} |
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
69 changes: 69 additions & 0 deletions
69
src/test/java/org/llorllale/cactoos/matchers/MismatchesTest.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,69 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) for portions of project cactoos-matchers are held by | ||
* Yegor Bugayenko, 2017-2018, as part of project cactoos. | ||
* All other copyright for project cactoos-matchers are held by | ||
* George Aristy, 2018. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included | ||
* in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package org.llorllale.cactoos.matchers; | ||
|
||
import org.cactoos.text.TextOf; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Test case for {@link Mismatches}. | ||
* | ||
* @since 1.0.0 | ||
* @checkstyle JavadocMethodCheck (500 lines) | ||
*/ | ||
public final class MismatchesTest { | ||
|
||
/** | ||
* Example of {@link Mismatches} usage. | ||
*/ | ||
@Test | ||
public void mismatches() { | ||
new Assertion<>( | ||
"Must mismatch properly", | ||
new TextIs("abc"), | ||
new Mismatches<>( | ||
new TextOf("def"), | ||
"Text with value \"abc\"", | ||
"Text is \"def\"" | ||
) | ||
).affirm(); | ||
} | ||
|
||
@Test | ||
public void matches() { | ||
new Assertion<>( | ||
"Must fail to mismatch", | ||
new Mismatches<>(new TextOf("a"), new TextOf("expected")), | ||
new Mismatches<>( | ||
new TextIs("a"), | ||
"Mismatches <a> with message <expected>", | ||
"" | ||
) | ||
).affirm(); | ||
} | ||
} |
3a33a27
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
106-7cce08fb
discovered insrc/main/java/org/llorllale/cactoos/matchers/Mismatches.java
and submitted as #136. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.3a33a27
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
106-33b58c37
discovered insrc/main/java/org/llorllale/cactoos/matchers/Mismatches.java
and submitted as #137. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.3a33a27
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
106-02263f65
discovered insrc/main/java/org/llorllale/cactoos/matchers/Mismatches.java
and submitted as #138. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.