-
Notifications
You must be signed in to change notification settings - Fork 260
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,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); | ||
} | ||
|
||
} |
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,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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Such beautifully unholy generics. :)