Skip to content

Commit

Permalink
Merge pull request #36 from peterklijn/master
Browse files Browse the repository at this point in the history
Added Collection and Map matchers
  • Loading branch information
mscharhag authored May 27, 2017
2 parents be1ccdd + 648399c commit d90f902
Show file tree
Hide file tree
Showing 6 changed files with 329 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import com.mscharhag.oleaster.matcher.matchers.*;
import com.mscharhag.oleaster.matcher.matchers.datetime.DateMatcher;

import java.util.Collection;
import java.util.Date;
import java.util.Map;

/**
* {@code Matchers} provides a set of static {@code expect()} methods that can (and should) be used to
Expand Down Expand Up @@ -186,6 +188,37 @@ public static StringMatcher expect(String value) {
return new StringMatcher(value);
}

/**
* Creates a new {@link com.mscharhag.oleaster.matcher.matchers.CollectionMatcher} initialized with
* the passed {@code value}.
* <p>The returned matcher can be used to check {@code Collection} values.
* <pre>
* expect(list).toContain("foo");
* expect(list).toBeEmpty();
* </pre>
* @see com.mscharhag.oleaster.matcher.matchers.CollectionMatcher
* @param value the value that should be checked
* @return a new {@code CollectionMatcher} initialized with {@code value}
*/
public static CollectionMatcher expect(Collection value) {
return new CollectionMatcher(value);
}

/**
* Creates a new {@link com.mscharhag.oleaster.matcher.matchers.MapMatcher} initialized with
* the passed {@code value}.
* <p>The returned matcher can be used to check {@code Map} values.
* <pre>
* expect(map).toContainValue("foo");
* expect(map).toHaveLength(3);
* </pre>
* @see com.mscharhag.oleaster.matcher.matchers.MapMatcher
* @param value the value that should be checked
* @return a new {@code MapMatcher} initialized with {@code value}
*/
public static MapMatcher expect(Map value) {
return new MapMatcher(value);
}
/**
* Creates a new {@link com.mscharhag.oleaster.matcher.matchers.ObjectMatcher} initialized with
* the passed {@code value}.
Expand Down
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());
}
}
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import org.junit.runner.RunWith;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import static com.mscharhag.oleaster.runner.StaticRunnerSupport.*;
import static com.mscharhag.oleaster.matcher.Matchers.*;
Expand Down Expand Up @@ -46,5 +50,19 @@ public class MatchersTest {{
expect(date).toBeCloseTo(dateWithinDelta, delta);
});

it("CollectionMatcher", () -> {
final List<String> list = new LinkedList<>();
list.add("one");
list.add("two");
expect(list).toContain("one");
});

it("MapMatcher", () -> {
final Map<String, Boolean> map = new HashMap<>();
map.put("one", false);
map.put("two", true);
expect(map).toContainKey("one");
expect(map).toContainValue(true);
});
});
}}
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);
});
});
});
}}
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);
});
});

});
}}

0 comments on commit d90f902

Please sign in to comment.