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

Use OO matchers instead of statics #928

Merged
merged 1 commit into from
Sep 20, 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
19 changes: 12 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,22 @@ The MIT License (MIT)
<artifactId>forbiddenapis</artifactId>
<version>2.5</version>
<configuration>
<signaturesFiles>
<signaturesFile>./src/test/resources/forbidden-apis.txt</signaturesFile>
</signaturesFiles>
<!--
@todo #588:30min In the continuation of #588, all the calls
@todo #903:30min In the continuation of #588, all the calls
to Matchers should be replaced with their OO counterparts.
This todo should be updated with a new one until everything is
done. At the end the configuration property below should be
removed so that calls to forbidden APIs fail the build.
done. The newly covered classes should be added to the include
configuration property below. At the end, the configuration
property below should be completely removed so that calls
to forbidden APIs always fail the build for every classes.
-->
<failOnViolation>false</failOnViolation>
<signaturesFiles>
<signaturesFile>./src/test/resources/forbidden-apis.txt</signaturesFile>
</signaturesFiles>
<includes>
<include>org/cactoos/bytes/*.class</include>
<include>org/cactoos/map/*.class</include>
</includes>
</configuration>
<executions>
<execution>
Expand Down
35 changes: 30 additions & 5 deletions src/test/java/org/cactoos/map/BehavesAsMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
import java.util.Map;
import org.hamcrest.Description;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.TypeSafeMatcher;
import org.hamcrest.collection.IsMapContaining;
import org.hamcrest.core.IsAnything;
import org.hamcrest.core.IsCollectionContaining;
import org.hamcrest.core.IsEqual;

/**
* Matcher for collection.
Expand Down Expand Up @@ -61,10 +64,32 @@ public BehavesAsMap(final K akey, final V val) {

@Override
public boolean matchesSafely(final Map<K, V> map) {
MatcherAssert.assertThat(map, Matchers.hasKey(this.key));
MatcherAssert.assertThat(map, Matchers.hasValue(this.value));
MatcherAssert.assertThat(map.keySet(), Matchers.hasItem(this.key));
MatcherAssert.assertThat(map.values(), Matchers.hasItem(this.value));
MatcherAssert.assertThat(
"Doesn't contain the key",
map,
new IsMapContaining<>(
new IsEqual<>(this.key),
new IsAnything<>()
)
);
MatcherAssert.assertThat(
"Doesn't contain the value",
map,
new IsMapContaining<>(
new IsAnything<>(),
new IsEqual<>(this.value)
)
);
MatcherAssert.assertThat(
"Doesn't contain the key in #keySet()",
map.keySet(),
new IsCollectionContaining<>(new IsEqual<>(this.key))
);
MatcherAssert.assertThat(
"Doesn't contain the value in #values()",
map.values(),
new IsCollectionContaining<>(new IsEqual<>(this.value))
);
return true;
}

Expand Down
5 changes: 3 additions & 2 deletions src/test/java/org/cactoos/map/ClearDeletesAllValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import java.util.Map;
import org.hamcrest.Description;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.TypeSafeMatcher;
import org.hamcrest.core.IsEqual;

/**
* Check a clear method.
Expand All @@ -43,7 +43,8 @@ public final class ClearDeletesAllValues<K, V> extends
public boolean matchesSafely(final Map<K, V> map) {
map.clear();
MatcherAssert.assertThat(
map.isEmpty(), Matchers.is(true)
"Can't be cleared",
map.isEmpty(), new IsEqual<>(true)
);
return true;
}
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/org/cactoos/map/GroupedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.cactoos.iterable.IterableOf;
import org.cactoos.list.ListOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.collection.IsMapContaining;
import org.hamcrest.core.IsEqual;
import org.junit.Test;

Expand Down Expand Up @@ -77,9 +77,9 @@ public void groupedByOneHasEntries() throws Exception {
number -> number,
Object::toString
),
Matchers.hasEntry(
1,
new ListOf<>("1", "1", "1")
new IsMapContaining<>(
new IsEqual<>(1),
new IsEqual<>(new ListOf<>("1", "1", "1"))
)
);
}
Expand Down
9 changes: 4 additions & 5 deletions src/test/java/org/cactoos/map/MapEntryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package org.cactoos.map;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.core.IsEqual;
import org.junit.Test;

Expand All @@ -43,7 +42,7 @@ public void getKey() {
MatcherAssert.assertThat(
"Can't get key in the map entry",
new MapEntry<>(key, value).getKey(),
Matchers.equalTo(key)
new IsEqual<>(key)
);
}

Expand All @@ -54,7 +53,7 @@ public void getValue() {
MatcherAssert.assertThat(
"Can't get value in the map entry",
new MapEntry<>(key, value).getValue(),
Matchers.equalTo(value)
new IsEqual<>(value)
);
}

Expand All @@ -70,7 +69,7 @@ public void equalsTo() {
MatcherAssert.assertThat(
"MapEntries are not equals",
new MapEntry<>(key, value).equals(new MapEntry<>(key, value)),
Matchers.equalTo(true)
new IsEqual<>(true)
);
}

Expand All @@ -80,7 +79,7 @@ public void compareHash() {
"the hash code are not equals",
new MapEntry<>("elegant", "objects").hashCode(),
// @checkstyle MagicNumber (1 line)
Matchers.equalTo(32739498)
new IsEqual<>(32739498)
);
}

Expand Down
13 changes: 6 additions & 7 deletions src/test/java/org/cactoos/map/MapEnvelopeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.Map;
import org.cactoos.func.FuncOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.core.IsEqual;
import org.hamcrest.core.IsNot;
import org.junit.Rule;
Expand Down Expand Up @@ -142,7 +141,7 @@ public void mapIsEmptyTrue() {
new MapNoNulls<>(
new MapOf<Integer, Integer>()
).isEmpty(),
Matchers.is(true)
new IsEqual<>(true)
);
}

Expand All @@ -155,7 +154,7 @@ public void mapIsEmptyFalse() {
new MapEntry<>(1, 0)
)
).isEmpty(),
Matchers.is(false)
new IsEqual<>(false)
);
}

Expand All @@ -168,7 +167,7 @@ public void mapContainsKeyTrue() {
new MapEntry<>(1, 0)
)
).containsKey(1),
Matchers.is(true)
new IsEqual<>(true)
);
}

Expand All @@ -181,7 +180,7 @@ public void mapContainsKeyFalse() {
new MapEntry<>(1, 0)
)
).containsKey(0),
Matchers.is(false)
new IsEqual<>(false)
);
}

Expand All @@ -194,7 +193,7 @@ public void mapContainsValueTrue() {
new MapEntry<>(1, 0)
)
).containsValue(0),
Matchers.is(true)
new IsEqual<>(true)
);
}

Expand All @@ -207,7 +206,7 @@ public void mapContainsValueFalse() {
new MapEntry<>(1, 0)
)
).containsValue(1),
Matchers.is(false)
new IsEqual<>(false)
);
}

Expand Down
13 changes: 9 additions & 4 deletions src/test/java/org/cactoos/map/MapNoNullsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
package org.cactoos.map;

import java.util.HashMap;
import org.cactoos.iterable.IterableOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.collection.IsMapContaining;
import org.hamcrest.core.AllOf;
import org.hamcrest.core.IsEqual;
import org.junit.Ignore;
import org.junit.Rule;
Expand Down Expand Up @@ -349,6 +351,7 @@ public void clear() {
}

@Test
@SuppressWarnings("unchecked")
public void entrySet() {
MatcherAssert.assertThat(
"Can't call #entrySet()",
Expand All @@ -360,9 +363,11 @@ public void entrySet() {
}
}
),
Matchers.allOf(
Matchers.hasEntry(1, 1),
Matchers.hasEntry(0, 0)
new AllOf<>(
new IterableOf<>(
new IsMapContaining<>(new IsEqual<>(1), new IsEqual<>(1)),
new IsMapContaining<>(new IsEqual<>(0), new IsEqual<>(0))
)
)
);
}
Expand Down
43 changes: 27 additions & 16 deletions src/test/java/org/cactoos/map/MapOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterator.Repeated;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.collection.IsMapContaining;
import org.hamcrest.core.AllOf;
import org.hamcrest.core.IsAnything;
import org.hamcrest.core.IsEqual;
import org.hamcrest.core.IsNot;
import org.hamcrest.core.StringStartsWith;
import org.junit.Test;

/**
Expand Down Expand Up @@ -66,9 +71,9 @@ public void convertsIterableToMap() {
new MapEntry<>(0, "hello, "),
new MapEntry<>(1, "world!")
),
Matchers.hasEntry(
Matchers.equalTo(0),
Matchers.startsWith("hello")
new IsMapContaining<>(
new IsEqual<>(0),
new StringStartsWith("hello")
)
);
}
Expand All @@ -87,7 +92,7 @@ public void sensesChangesInMap() throws Exception {
MatcherAssert.assertThat(
"Can't sense the changes in the underlying map",
map.size(),
Matchers.not(Matchers.equalTo(map.size()))
new IsNot<>(new IsEqual<>(map.size()))
);
}

Expand All @@ -104,7 +109,7 @@ public void createsMapWithFunctions() {
}
)
),
Matchers.hasKey(0)
new IsMapContaining<>(new IsEqual<>(0), new IsAnything<>())
);
}

Expand All @@ -116,7 +121,7 @@ public void integersToString() {
new MapEntry<>(-1, 0),
new MapEntry<>(1, 2)
).toString(),
Matchers.equalTo("{-1=0, 1=2}")
new IsEqual<>("{-1=0, 1=2}")
);
}

Expand All @@ -140,7 +145,7 @@ public void mapsToString() {
)
)
).toString(),
Matchers.equalTo("{-1={4=7, first=second}, 1={green=red, 2.7=3.1}}")
new IsEqual<>("{-1={4=7, first=second}, 1={green=red, 2.7=3.1}}")
);
}

Expand All @@ -149,11 +154,12 @@ public void emptyToString() {
MatcherAssert.assertThat(
"Can't convert empty map to string",
new MapOf<Integer, Map<String, String>>().toString(),
Matchers.equalTo("{}")
new IsEqual<>("{}")
);
}

@Test
@SuppressWarnings("unchecked")
public void createsMapFromMapAndMapEntries() {
MatcherAssert.assertThat(
"Can't create a map from map and map entries",
Expand All @@ -163,9 +169,11 @@ public void createsMapFromMapAndMapEntries() {
),
new MapEntry<Integer, Integer>(1, 1)
),
Matchers.allOf(
Matchers.hasEntry(0, 0),
Matchers.hasEntry(1, 1)
new AllOf<>(
new IterableOf<>(
new IsMapContaining<>(new IsEqual<>(0), new IsEqual<>(0)),
new IsMapContaining<>(new IsEqual<>(1), new IsEqual<>(1))
)
)
);
}
Expand All @@ -179,11 +187,12 @@ public void createsMapFromFunctionsAndIterable() {
new FuncOf<Integer, Integer>(0),
new IterableOf<Integer>(0)
),
Matchers.hasEntry(0, 0)
new IsMapContaining<>(new IsEqual<>(0), new IsEqual<>(0))
);
}

@Test
@SuppressWarnings("unchecked")
public void createsMapFromMapFunctionsAndIterable() {
MatcherAssert.assertThat(
"Can't create a map from map, functions and iterable.",
Expand All @@ -195,9 +204,11 @@ public void createsMapFromMapFunctionsAndIterable() {
),
new IterableOf<>(0)
),
Matchers.allOf(
Matchers.hasEntry(0, 0),
Matchers.hasEntry(1, 1)
new AllOf<>(
new IterableOf<>(
new IsMapContaining<>(new IsEqual<>(0), new IsEqual<>(0)),
new IsMapContaining<>(new IsEqual<>(1), new IsEqual<>(1))
)
)
);
}
Expand Down
Loading