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

Fix describeMismatchSafely in ScalarHasValue #14

Merged
merged 1 commit into from
Jun 1, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @since 0.11
* @todo #7:30min All the matchers should implement describeMismatchSafely and
* their tests must verify that the implementation of the descriptions
* are satisfactory. The matchers should not expose publicly the xxxSafely
* method and the tests should rely on actual real use with assertThat.
* See ScalarHasValueTest for an example of a satisfactory result.
*/
public final class InputHasContent extends TypeSafeMatcher<Input> {

Expand Down
20 changes: 16 additions & 4 deletions src/main/java/org/llorllale/cactoos/matchers/ScalarHasValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,27 @@ public ScalarHasValue(final Matcher<T> mtr) {
}

@Override
public boolean matchesSafely(final Scalar<T> item) {
public void describeTo(final Description description) {
description.appendText("Scalar with ");
description.appendDescriptionOf(this.matcher);
}

// @checkstyle ProtectedMethodInFinalClassCheck (1 line)
@Override
protected boolean matchesSafely(final Scalar<T> item) {
return this.matcher.matches(
new UncheckedScalar<>(item).value()
);
}

// @checkstyle ProtectedMethodInFinalClassCheck (1 line)
@Override
public void describeTo(final Description description) {
description.appendText("Scalar with ");
description.appendDescriptionOf(this.matcher);
protected void describeMismatchSafely(final Scalar<T> item,
final Description description) {
description
.appendText("was ")
.appendValue(
new UncheckedScalar<>(item).value()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,78 @@
* 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.scalar.Constant;
import org.cactoos.scalar.UncheckedScalar;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsEqual;
import org.hamcrest.core.StringContains;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

/**
* Test case for {@link org.llorllale.cactoos.matchers.ScalarHasValue}.
* Test case for {@link ScalarHasValue}.
*
* @author Alexander Menshikov ([email protected])
* @author Victor Noel ([email protected])
* @version $Id$
* @since 1.0
* @checkstyle JavadocMethodCheck (500 lines)
*/
public final class ScalarHasValueTest {

/**
* A rule for handling an exception.
*/
@Rule
public final ExpectedException exception = ExpectedException.none();

@Test
public void matchesAsExpectedWithString() {
final String expected = "some text";
MatcherAssert.assertThat(
"doesn't match a String",
new UncheckedScalar<>(() -> expected),
new ScalarHasValue<>(expected)
);
}

@Test
public void matchPositive() {
final ScalarHasValue<Integer> matcher = new ScalarHasValue<>(1);
public void matchesAsExpectedWithMatcher() {
final String expected = "text";
MatcherAssert.assertThat(
"doesn't match a Matcher",
new UncheckedScalar<>(() -> expected),
new ScalarHasValue<>(new IsEqual<>(expected))
);
}

@Test
public void hasMatcherDescriptionForFailedTest() throws Exception {
this.exception.expect(AssertionError.class);
this.exception.expectMessage(
new StringContains("Expected: Scalar with \"something\"")
);
MatcherAssert.assertThat(
"missing matcher description",
new Constant<>("something else"),
new ScalarHasValue<>(new IsEqual<>("something"))
);
}

@Test
public void hasMismatchDescriptionForFailedTest() throws Exception {
this.exception.expect(AssertionError.class);
this.exception.expectMessage(
new StringContains("but: was \"actual\"")
);
MatcherAssert.assertThat(
"Can't match equaled values",
matcher.matchesSafely(() -> 1),
new IsEqual<>(true)
"missing mismatch description",
new Constant<>("actual"),
new ScalarHasValue<>(new IsEqual<>("expected"))
);
}
}