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

Add a map subject. #40

Merged
merged 2 commits into from
Sep 7, 2012
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
7 changes: 7 additions & 0 deletions core/src/main/java/org/truth0/TestVerb.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.truth0.subjects.BooleanSubject;
import org.truth0.subjects.ClassSubject;
Expand All @@ -27,6 +28,7 @@
import org.truth0.subjects.IntegerSubject;
import org.truth0.subjects.IterableSubject;
import org.truth0.subjects.ListSubject;
import org.truth0.subjects.MapSubject;
import org.truth0.subjects.StringSubject;
import org.truth0.subjects.Subject;

Expand Down Expand Up @@ -77,4 +79,9 @@ public <T, C extends List<T>> ListSubject<? extends ListSubject<?, T, C>, T, C>
public <T, C extends List<T>> ListSubject<? extends ListSubject<?, T, C>, T, C> that(T[] target) {
return that(Arrays.asList(target));
}

public <K, V, M extends Map<K, V>> MapSubject<? extends MapSubject<?, K, V, M>, K, V, M> that(Map<K, V> target) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such beautifully unholy generics. :)

return MapSubject.create(getFailureStrategy(), target);
}

}
101 changes: 101 additions & 0 deletions core/src/main/java/org/truth0/subjects/MapSubject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright (c) 2011 David Saff
* Copyright (c) 2011 Christian Gruber
*
* Licensed 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 org.truth0.subjects;

import java.util.Map;

import org.truth0.FailureStrategy;

import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.ImmutableList;

/**
* @author Christian Gruber (cgruber@israfil.net)
*/
@GwtCompatible
public class MapSubject<S extends MapSubject<S, K, V, M>, K, V, M extends Map<K, V>> extends Subject<S, M> {

public MapSubject(FailureStrategy failureStrategy, M map) {
super(failureStrategy, map);
}

/**
* Attests that the subject holds no objects, or fails.
*/
public void isEmpty() {
if (!getSubject().isEmpty()) {
fail("is empty");
}
}

/**
* Attests that the subject holds one or more objects, or fails
*/
public void isNotEmpty() {
if (getSubject().isEmpty()) {
fail("is not empty");
}
}

/**
* Attests that the subject contains the provided key or fails.
*/
public WithValue<V> hasKey(final K key) {
if (!getSubject().containsKey(key)) {
fail("has key", key);
}
return new WithValue<V>() {
@Override public void withValue(V expected) {
V actual = getSubject().get(key);
if ((actual == null && key != null) ||
!actual.equals(expected)) {
fail("has key/value pair", ImmutableList.of(key, expected),
"actually has key/value pair", ImmutableList.of(key, actual));
}
}
};
}

public void lacksKey(K key) {
if (getSubject().containsKey(key)) {
fail("lacks key", key);
}
}

public void hasValue(V key) {
if (!getSubject().containsValue(key)) {
fail("has value", key);
}
}

public void lacksValue(V key) {
if (getSubject().containsValue(key)) {
fail("lacks value", key);
}
}

public interface WithValue<V> {
void withValue(V value);
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public static <K, V, M extends Map<K, V>> MapSubject<? extends MapSubject<?, K, V, M>, K, V, M> create(
FailureStrategy failureStrategy, Map<K, V> map) {
return new MapSubject(failureStrategy, map);
}

}
163 changes: 163 additions & 0 deletions core/src/test/java/org/truth0/subjects/MapTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* Copyright (c) 2011 David Saff
* Copyright (c) 2011 Christian Gruber
*
* Licensed 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 org.truth0.subjects;

import static org.junit.Assert.fail;
import static org.truth0.Truth.ASSERT;

import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import com.google.common.collect.Maps;

/**
* Tests for Map Subjects.
*
* @author Christian Gruber (cgruber@israfil.net)
*/
@RunWith(JUnit4.class)
public class MapTest {

@Test public void mapIsEmpty() {
ASSERT.that(map(String.class, String.class)).isEmpty();
}

@Test public void mapIsEmptyWithFailure() {
try {
ASSERT.that(map(Integer.class, Integer.class, 1, 5)).isEmpty();
fail("Should have thrown.");
} catch (AssertionError e) {
ASSERT.that(e.getMessage()).contains("Not true that");
ASSERT.that(e.getMessage()).contains("is empty");
}
}

@Test public void mapIsNotEmpty() {
ASSERT.that(map(Integer.class, Integer.class, 1, 5)).isNotEmpty();
}

@Test public void mapIsNotEmptyWithFailure() {
try {
ASSERT.that(map(String.class, String.class)).isNotEmpty();
fail("Should have thrown.");
} catch (AssertionError e) {
ASSERT.that(e.getMessage()).contains("Not true that");
ASSERT.that(e.getMessage()).contains("is not empty");
}
}

@Test public void mapHasKey() {
ASSERT.that(map(String.class, Object.class, "a", "A")).hasKey("a");
}

@Test public void failMapHasKey() {
try {
ASSERT.that(map(String.class, Object.class, "a", "A")).hasKey("b");
fail("Should have thrown.");
} catch (AssertionError e) {
ASSERT.that(e.getMessage()).contains("Not true that");
ASSERT.that(e.getMessage()).contains("has key");
}
}

@Test public void failMapHasKeyWithNull() {
try {
ASSERT.that(map(String.class, Object.class, "a", "A")).hasKey(null);
fail("Should have thrown.");
} catch (AssertionError e) {
ASSERT.that(e.getMessage()).contains("Not true that");
ASSERT.that(e.getMessage()).contains("has key <null>");
}
}

@Test public void mapLacksKey() {
ASSERT.that(map(String.class, Object.class, "a", "A")).lacksKey("b");
}

@Test public void failMapLacksKey() {
try {
ASSERT.that(map(String.class, Object.class, "a", "A")).lacksKey("a");
fail("Should have thrown.");
} catch (AssertionError e) {
ASSERT.that(e.getMessage()).contains("Not true that");
ASSERT.that(e.getMessage()).contains("lacks key");
}
}

@Test public void mapHasKeyWithValue() {
ASSERT.that(map(String.class, Object.class, "a", String.class))
.hasKey("a").withValue(String.class);
}

@Test public void failMapHasKeyWithValue() {
try {
ASSERT.that(map(String.class, Object.class, "a", String.class))
.hasKey("a").withValue(Integer.class);
fail("Should have thrown.");
} catch (AssertionError e) {
ASSERT.that(e.getMessage()).contains("Not true that");
ASSERT.that(e.getMessage()).contains("has key/value pair");
}
}

@Test public void mapHasValue() {
ASSERT.that(map(String.class, Object.class, "a", "A")).hasValue("A");
}

@Test public void mapHasValueWithNull() {
ASSERT.that(map(String.class, Object.class, "a", null)).hasValue(null);
}

@Test public void failMapHasValue() {
try {
ASSERT.that(map(String.class, Object.class, "a", "A")).hasValue("B");
fail("Should have thrown.");
} catch (AssertionError e) {
ASSERT.that(e.getMessage()).contains("Not true that");
ASSERT.that(e.getMessage()).contains("has value");
}
}

@Test public void mapLacksValue() {
ASSERT.that(map(String.class, Object.class, "a", "A")).lacksValue("B");
}

@Test public void failMapLacksValue() {
try {
ASSERT.that(map(String.class, Object.class, "a", "A")).lacksValue("A");
fail("Should have thrown.");
} catch (AssertionError e) {
ASSERT.that(e.getMessage()).contains("Not true that");
ASSERT.that(e.getMessage()).contains("lacks value");
}
}


@SuppressWarnings("unchecked") // Want this to blow up if wrong.
public static <K, V> Map<K, V> map(Class<K> keyClass, Class<V> valueClass, Object ... keyval) {
Map<K, V> map = Maps.newHashMap();
if (keyval.length % 2 != 0)
throw new IllegalArgumentException("Wrong number of key/value pairs.");
for (int i = 0; i < keyval.length ; i = i + 2) {
map.put(keyClass.cast(keyval[i]), valueClass.cast(keyval[i + 1]));
}
return map;
}
}