Skip to content

Commit

Permalink
(#106) Introduces Mismatches Matcher to test Matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Jul 14, 2019
1 parent 313d2d5 commit 3a33a27
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 9 deletions.
148 changes: 148 additions & 0 deletions src/main/java/org/llorllale/cactoos/matchers/Mismatches.java
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;
}
}
21 changes: 12 additions & 9 deletions src/test/java/org/llorllale/cactoos/matchers/HasLinesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

package org.llorllale.cactoos.matchers;

import org.cactoos.text.Joined;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand Down Expand Up @@ -63,16 +64,18 @@ public void matches() {
*/
@Test
public void failed() {
this.exception.expect(AssertionError.class);
this.exception.expectMessage(
String.format(
"Expected: Lines are <[Tom, Mike]>%n but was: <[Tom, John]>"
)
);
new Assertion<>(
"does not match lines that do not contain the given strings",
String.format("Tom%nJohn%n"),
new HasLines("Tom", "Mike")
"must not match lines that do not contain the given strings",
new HasLines("Tom", "Mike"),
new Mismatches<>(
String.format("Tom%nJohn%n"),
new Joined(
"\n",
"",
"Expected: Lines are <[Tom, Mike]>",
" but was: <[Tom, John]>"
)
)
).affirm();
}

Expand Down
69 changes: 69 additions & 0 deletions src/test/java/org/llorllale/cactoos/matchers/MismatchesTest.java
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();
}
}

3 comments on commit 3a33a27

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 3a33a27 Jul 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 106-7cce08fb discovered in src/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.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 3a33a27 Jul 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 106-33b58c37 discovered in src/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.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 3a33a27 Jul 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 106-02263f65 discovered in src/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.

Please sign in to comment.