-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from peterklijn/master
Added Collection and Map matchers
- Loading branch information
Showing
6 changed files
with
329 additions
and
0 deletions.
There are no files selected for viewing
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
46 changes: 46 additions & 0 deletions
46
...ster-matcher/src/main/java/com/mscharhag/oleaster/matcher/matchers/CollectionMatcher.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,46 @@ | ||
package com.mscharhag.oleaster.matcher.matchers; | ||
|
||
import com.mscharhag.oleaster.matcher.util.Arguments; | ||
|
||
import java.util.Collection; | ||
|
||
import static com.mscharhag.oleaster.matcher.util.Expectations.expectTrue; | ||
|
||
/** | ||
* Matcher class to validate Collections | ||
*/ | ||
public class CollectionMatcher extends ObjectMatcher<Collection> { | ||
public CollectionMatcher(final Collection value) { | ||
super(value); | ||
Arguments.ensureNotNull(value, "Collection cannot be null"); | ||
} | ||
|
||
/** | ||
* Checks if the stored {@code Collection} contains the provided item. | ||
* <p>This method throws an {@code AssertionError} if | ||
* <ul> | ||
* <li>the stored {@code Collection} does not contain the passed {@code item}</li> | ||
* </ul> | ||
* </p> | ||
* @param item the {@code Object} which the {@code Collection} should contain. | ||
*/ | ||
public void toContain(final Object item) { | ||
expectTrue(this.getValue().contains(item), "Expected '%s' to contain '%s'", this.getValue(), item); | ||
} | ||
|
||
public void toNotContain(final Object item) { | ||
expectTrue(!this.getValue().contains(item), "Expected '%s' to not contain '%s'", this.getValue(), item); | ||
} | ||
|
||
public void toBeEmpty() { | ||
expectTrue(this.getValue().size() == 0, "Expected '%s' to be empty", this.getValue()); | ||
} | ||
|
||
public void toNotBeEmpty() { | ||
expectTrue(this.getValue().size() > 0, "Expected '%s' to not be empty", this.getValue()); | ||
} | ||
|
||
public void toHaveLength(final int length) { | ||
expectTrue(this.getValue().size() == length, "Expected '%s' to have a length of %d, instead has a length of %d", this.getValue(), length, this.getValue().size()); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
oleaster-matcher/src/main/java/com/mscharhag/oleaster/matcher/matchers/MapMatcher.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,42 @@ | ||
package com.mscharhag.oleaster.matcher.matchers; | ||
|
||
import java.util.Map; | ||
|
||
import com.mscharhag.oleaster.matcher.util.Arguments; | ||
|
||
import static com.mscharhag.oleaster.matcher.util.Expectations.expectTrue; | ||
|
||
public class MapMatcher extends ObjectMatcher<Map> { | ||
public MapMatcher(final Map value) { | ||
super(value); | ||
Arguments.ensureNotNull(value, "Map cannot be null"); | ||
} | ||
|
||
public void toBeEmpty() { | ||
expectTrue(this.getValue().size() == 0, "Expected '%s' to be empty", this.getValue()); | ||
} | ||
|
||
public void toNotBeEmpty() { | ||
expectTrue(this.getValue().size() > 0, "Expected '%s' to not be empty", this.getValue()); | ||
} | ||
|
||
public void toContainKey(final Object key) { | ||
expectTrue(this.getValue().containsKey(key), "Expected '%s' to contain key '%s'", this.getValue(), key); | ||
} | ||
|
||
public void toNotContainKey(final Object key) { | ||
expectTrue(!this.getValue().containsKey(key), "Expected '%s' to not contain key '%s'", this.getValue(), key); | ||
} | ||
|
||
public void toContainValue(final Object value) { | ||
expectTrue(this.getValue().containsValue(value), "Expected '%s' to contain value '%s'", this.getValue(), value); | ||
} | ||
|
||
public void toNotContainValue(final Object value) { | ||
expectTrue(!this.getValue().containsValue(value), "Expected '%s' to not contain value '%s'", this.getValue(), value); | ||
} | ||
|
||
public void toHaveLength(final int length) { | ||
expectTrue(this.getValue().size() == length, "Expected '%s' to have a length of %d, instead has a length of %d", this.getValue(), length, this.getValue().size()); | ||
} | ||
} |
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
92 changes: 92 additions & 0 deletions
92
...-matcher/src/test/java/com/mscharhag/oleaster/matcher/matchers/CollectionMatcherTest.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,92 @@ | ||
package com.mscharhag.oleaster.matcher.matchers; | ||
|
||
import com.mscharhag.oleaster.runner.OleasterRunner; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.util.*; | ||
|
||
import static com.mscharhag.oleaster.matcher.TestUtil.expectAssertionError; | ||
import static com.mscharhag.oleaster.runner.StaticRunnerSupport.*; | ||
|
||
@RunWith(OleasterRunner.class) | ||
public class CollectionMatcherTest {{ | ||
describe("CollectionMatcher test", () -> { | ||
final List<String> list = new LinkedList<>(); | ||
list.add("one"); | ||
list.add("two"); | ||
|
||
final Set<String> set = new HashSet<>(); | ||
set.add("one"); | ||
set.add("two"); | ||
|
||
describe("when toContain() is called", () -> { | ||
it("fails if the stored collection does not contain the expected value", () -> { | ||
expectAssertionError(() -> new CollectionMatcher(list).toContain("three"), | ||
"Expected '[one, two]' to contain 'three'"); | ||
expectAssertionError(() -> new CollectionMatcher(set).toContain("four"), | ||
"Expected '[one, two]' to contain 'four'"); | ||
}); | ||
|
||
it("is ok if the stored collection contains the expected value", () -> { | ||
new CollectionMatcher(list).toContain("one"); | ||
new CollectionMatcher(set).toContain("two"); | ||
}); | ||
}); | ||
|
||
describe("when toNotContain() is called", () -> { | ||
it("fails if hte stored collection does contain the expected value", () -> { | ||
expectAssertionError(() -> new CollectionMatcher(list).toNotContain("one"), | ||
"Expected '[one, two]' to not contain 'one'"); | ||
expectAssertionError(() -> new CollectionMatcher(set).toNotContain("one"), | ||
"Expected '[one, two]' to not contain 'one'"); | ||
}); | ||
|
||
it("is ok if the stored collection not contains the expected value", () -> { | ||
new CollectionMatcher(list).toNotContain("three"); | ||
new CollectionMatcher(set).toNotContain("three"); | ||
}); | ||
}); | ||
|
||
describe("when toBeEmpty() is called", () -> { | ||
it("fails if the stored collection is not empty", () -> { | ||
expectAssertionError(() -> new CollectionMatcher(list).toBeEmpty(), | ||
"Expected '[one, two]' to be empty"); | ||
expectAssertionError(() -> new CollectionMatcher(set).toBeEmpty(), | ||
"Expected '[one, two]' to be empty"); | ||
}); | ||
|
||
it("is ok if the stored collection is empty", () -> { | ||
new CollectionMatcher(new LinkedList()).toBeEmpty(); | ||
new CollectionMatcher(new HashSet()).toBeEmpty(); | ||
}); | ||
}); | ||
|
||
describe("when toNotBeEmpty() is called", () -> { | ||
it("fails if the stored collection is empty", () -> { | ||
expectAssertionError(() -> new CollectionMatcher(new LinkedList()).toNotBeEmpty(), | ||
"Expected '[]' to not be empty"); | ||
expectAssertionError(() -> new CollectionMatcher(new HashSet()).toNotBeEmpty(), | ||
"Expected '[]' to not be empty"); | ||
}); | ||
|
||
it("is ok if the stored collection is not empty", () -> { | ||
new CollectionMatcher(list).toNotBeEmpty(); | ||
new CollectionMatcher(set).toNotBeEmpty(); | ||
}); | ||
}); | ||
|
||
describe("when toHaveLength() is called", () -> { | ||
it("fails if the stored collection does not have the provided length", () -> { | ||
expectAssertionError(() -> new CollectionMatcher(list).toHaveLength(1), | ||
"Expected '[one, two]' to have a length of 1, instead has a length of 2"); | ||
expectAssertionError(() -> new CollectionMatcher(set).toHaveLength(-1), | ||
"Expected '[one, two]' to have a length of -1, instead has a length of 2"); | ||
}); | ||
|
||
it("is ok if hte stored collection has the provided length", () -> { | ||
new CollectionMatcher(list).toHaveLength(2); | ||
new CollectionMatcher(set).toHaveLength(2); | ||
}); | ||
}); | ||
}); | ||
}} |
98 changes: 98 additions & 0 deletions
98
oleaster-matcher/src/test/java/com/mscharhag/oleaster/matcher/matchers/MapMatcherTest.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,98 @@ | ||
package com.mscharhag.oleaster.matcher.matchers; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import com.mscharhag.oleaster.runner.OleasterRunner; | ||
|
||
import org.junit.runner.RunWith; | ||
|
||
import static com.mscharhag.oleaster.matcher.TestUtil.expectAssertionError; | ||
import static com.mscharhag.oleaster.runner.StaticRunnerSupport.*; | ||
|
||
@RunWith(OleasterRunner.class) | ||
public class MapMatcherTest {{ | ||
describe("MapMatcher test", () -> { | ||
final Map<String, Integer> map = new HashMap<>(); | ||
map.put("one", 1); | ||
map.put("two", 2); | ||
|
||
describe("when toBeEmpty() is called", () -> { | ||
it("fails if the stored map is not empty", () -> { | ||
expectAssertionError(() -> new MapMatcher(map).toBeEmpty(), | ||
"Expected '{one=1, two=2}' to be empty"); | ||
}); | ||
|
||
it("is ok if the stored map is empty", () -> { | ||
new MapMatcher(new HashMap()).toBeEmpty(); | ||
}); | ||
}); | ||
|
||
describe("when toNotBeEmpty() is called", () -> { | ||
it("fails if the stored map is empty", () -> { | ||
expectAssertionError(() -> new MapMatcher(new HashMap()).toNotBeEmpty(), | ||
"Expected '{}' to not be empty"); | ||
}); | ||
|
||
it("is ok if the stored map is not empty", () -> { | ||
new MapMatcher(map).toNotBeEmpty(); | ||
}); | ||
}); | ||
|
||
describe("when toContainKey() is called", () -> { | ||
it("fails if the stored map does not contain the expected key", () -> { | ||
expectAssertionError(() -> new MapMatcher(map).toContainKey("three"), | ||
"Expected '{one=1, two=2}' to contain key 'three'"); | ||
}); | ||
|
||
it("is ok if the stored map contains the expected key", () -> { | ||
new MapMatcher(map).toContainKey("one"); | ||
}); | ||
}); | ||
|
||
describe("when toNotContainKey() is called", () -> { | ||
it("fails if the stored map does not contain the expected key", () -> { | ||
expectAssertionError(() -> new MapMatcher(map).toNotContainKey("one"), | ||
"Expected '{one=1, two=2}' to not contain key 'one'"); | ||
}); | ||
|
||
it("is ok if the stored map contains the expected key", () -> { | ||
new MapMatcher(map).toNotContainKey("three"); | ||
}); | ||
}); | ||
|
||
describe("when toContainValue() is called", () -> { | ||
it("fails if the stored map does not contain the expected value", () -> { | ||
expectAssertionError(() -> new MapMatcher(map).toContainValue(3), | ||
"Expected '{one=1, two=2}' to contain value '3'"); | ||
}); | ||
|
||
it("is ok if the stored map contains the expected value", () -> { | ||
new MapMatcher(map).toContainValue(2); | ||
}); | ||
}); | ||
|
||
describe("when toNotContainValue() is called", () -> { | ||
it("fails if the stored value does not contain the expected value", () -> { | ||
expectAssertionError(() -> new MapMatcher(map).toNotContainValue(1), | ||
"Expected '{one=1, two=2}' to not contain value '1'"); | ||
}); | ||
|
||
it("is ok if the stored value contains the expected value", () -> { | ||
new MapMatcher(map).toNotContainValue(3); | ||
}); | ||
}); | ||
|
||
describe("when toHaveLength() is called", () -> { | ||
it("fails if the stored value does not have the provided length", () -> { | ||
expectAssertionError(() -> new MapMatcher(map).toHaveLength(3), | ||
"Expected '{one=1, two=2}' to have a length of 3, instead has a length of 2"); | ||
}); | ||
|
||
it("is ok if the stored value does have the provided length", () -> { | ||
new MapMatcher(map).toHaveLength(2); | ||
}); | ||
}); | ||
|
||
}); | ||
}} |