Commit 5d89bfb5 authored by Stein Eldar Johnsen's avatar Stein Eldar Johnsen 💬

new matcher: matches regex

And lots of test coverage.
parent ae3d632a
......@@ -23,10 +23,12 @@ import org.hamcrest.Description;
import org.hamcrest.Matcher;
import java.util.Collection;
import java.util.Objects;
/**
* All items in a collection matches the given matcher.
*
* This is equivalent with a loop over all items in the collection
* and calling <code>assertThat(item, matcher)</code>
*/
public class AllItemsMatch<T> extends BaseMatcher<Collection<T>> {
private final Matcher<T> matcher;
......@@ -48,27 +50,9 @@ public class AllItemsMatch<T> extends BaseMatcher<Collection<T>> {
return true;
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o == null || !(getClass().equals(o.getClass()))) {
return false;
}
AllItemsMatch other = (AllItemsMatch) o;
return Objects.equals(matcher, other.matcher);
}
@Override
public int hashCode() {
return Objects.hash(AllItemsMatch.class, matcher);
}
@Override
public void describeTo(Description description) {
description.appendText("all match ");
description.appendText("all items match: ");
matcher.describeTo(description);
}
......
......@@ -48,24 +48,6 @@ public class EqualIgnoreIndent extends BaseMatcher<String> {
return noIndentActual.equals(noIndentExpected);
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o == null || !(getClass().equals(o.getClass()))) {
return false;
}
EqualIgnoreIndent other = (EqualIgnoreIndent) o;
return Objects.equals(other.expected, expected);
}
@Override
public int hashCode() {
return Objects.hash(EqualIgnoreIndent.class, expected);
}
@Override
public void describeTo(Description description) {
description.appendText("equalIgnoreIndent(" + expected + ")");
......
......@@ -21,8 +21,6 @@ package net.morimekta.testing.matchers;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import java.util.Objects;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
......@@ -51,25 +49,6 @@ public class InRange<T extends Number> extends BaseMatcher<T> {
actual.doubleValue() < higherExclusive.doubleValue();
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o == null || !(getClass().equals(o.getClass()))) {
return false;
}
InRange other = (InRange) o;
return Objects.equals(lowerInclusive, other.lowerInclusive) &&
Objects.equals(higherExclusive, other.higherExclusive);
}
@Override
public int hashCode() {
return Objects.hash(InRange.class, lowerInclusive, higherExclusive);
}
@Override
public void describeTo(Description description) {
description.appendText("in range [" + lowerInclusive + " .. " + higherExclusive + ")");
......
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package net.morimekta.testing.matchers;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import java.util.Collection;
import java.util.regex.Pattern;
/**
* Checks that the matching string matches the regex.
*/
public class MatchesRegex extends BaseMatcher<Collection<String>> {
private final Pattern pattern;
public MatchesRegex(String regex) {
this(Pattern.compile(regex));
}
public MatchesRegex(Pattern pattern) {
this.pattern = pattern;
}
@Override
public boolean matches(Object item) {
if (!(item instanceof CharSequence)) {
return false;
}
return pattern.matcher(item.toString()).matches();
}
@Override
public void describeTo(Description description) {
description.appendText("matches /" + pattern.toString() + "/");
}
@Override
@SuppressWarnings("unchecked")
public void describeMismatch(Object item, Description description) {
if (!(item instanceof CharSequence)) {
description.appendText("was not a string: ")
.appendValue(item);
return;
}
description.appendText("was ").appendValue(item);
}
}
package net.morimekta.testing.matchers;
import com.google.common.collect.ImmutableList;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.StringDescription;
import org.junit.Test;
import java.util.Collection;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public class AllItemsMatchTest {
@Test
public void testMatches() {
Matcher<String> matcher = is("a");
Matcher<Collection<String>> sut = new AllItemsMatch<>(matcher);
assertThat(sut.matches(ImmutableList.of("a", "a")), is(true));
assertThat(sut.matches(ImmutableList.of("a", "b")), is(false));
// If no entries, all (none) items match.
assertThat(sut.matches(ImmutableList.of()), is(true));
assertThat(sut.matches(null), is(false));
assertThat(sut.matches("a"), is(false));
}
@Test
public void testDescribeTo() {
Matcher<String> matcher = is("a");
Matcher<Collection<String>> sut = new AllItemsMatch<>(matcher);
Description description = new StringDescription();
sut.describeTo(description);
assertThat(description.toString(), is("all items match: is \"a\""));
}
@Test
public void testDescribeMismatch() {
Matcher<String> matcher = is("a");
Matcher<Collection<String>> sut = new AllItemsMatch<>(matcher);
Description description = new StringDescription();
sut.describeMismatch("a", description);
assertThat(description.toString(), is("not a collection: \"a\""));
description = new StringDescription();
sut.describeMismatch(ImmutableList.of("a", "b"), description);
assertThat(description.toString(), is("was [\"a\", \"b\"]"));
}
}
package net.morimekta.testing.matchers;
import org.hamcrest.Description;
import org.hamcrest.StringDescription;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public class MatchesRegexTest {
@Test
public void testMatches() {
assertThat(new MatchesRegex("a.*").matches("aa"), is(true));
assertThat(new MatchesRegex("a.*").matches("baa"), is(false));
assertThat(new MatchesRegex("a.*b[1-6]").matches("ayb"), is(false));
assertThat(new MatchesRegex("a.*b[1-6]").matches("ayb5"), is(true));
assertThat(new MatchesRegex("a.*b").matches(44), is(false));
}
@Test
public void testDescribeTo() {
Description description = new StringDescription();
new MatchesRegex("a.*b[1-6]").describeTo(description);
assertThat(description.toString(), is("matches /a.*b[1-6]/"));
}
@Test
public void testDescribeMismatch() {
Description description = new StringDescription();
new MatchesRegex("a.*b[1-6]").describeMismatch("ayb", description);
assertThat(description.toString(), is("was \"ayb\""));
description = new StringDescription();
new MatchesRegex("a.*b[1-6]").describeMismatch(44, description);
assertThat(description.toString(), is("was not a string: <44>"));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment