1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.truth0.subjects; |
18 | |
|
19 | |
import java.util.Arrays; |
20 | |
import java.util.Map; |
21 | |
|
22 | |
import org.truth0.FailureStrategy; |
23 | |
|
24 | |
import com.google.common.annotations.GwtCompatible; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
@GwtCompatible |
30 | |
public class MapSubject<S extends MapSubject<S, K, V, M>, K, V, M extends Map<K, V>> extends Subject<S, M> { |
31 | |
|
32 | |
public MapSubject(FailureStrategy failureStrategy, M map) { |
33 | 19 | super(failureStrategy, map); |
34 | 19 | } |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public void isEmpty() { |
40 | 2 | if (!getSubject().isEmpty()) { |
41 | 1 | fail("is empty"); |
42 | |
} |
43 | 1 | } |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public void isNotEmpty() { |
49 | 2 | if (getSubject().isEmpty()) { |
50 | 1 | fail("is not empty"); |
51 | |
} |
52 | 1 | } |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public WithValue<V> hasKey(final K key) { |
58 | 8 | if (!getSubject().containsKey(key)) { |
59 | 2 | fail("has key", key); |
60 | |
} |
61 | 6 | return new WithValue<V>() { |
62 | |
@Override public void withValue(V expected) { |
63 | 5 | V actual = getSubject().get(key); |
64 | 5 | if ((actual == null && expected != null) || |
65 | |
!(actual == expected || actual.equals(expected))) { |
66 | 3 | fail("has key/value pair", Arrays.asList(key, expected), |
67 | |
"actually has key/value pair", Arrays.asList(key, actual)); |
68 | |
} |
69 | 2 | } |
70 | |
}; |
71 | |
} |
72 | |
|
73 | |
public void lacksKey(K key) { |
74 | 2 | if (getSubject().containsKey(key)) { |
75 | 1 | fail("lacks key", key); |
76 | |
} |
77 | 1 | } |
78 | |
|
79 | |
public void hasValue(V key) { |
80 | 3 | if (!getSubject().containsValue(key)) { |
81 | 1 | fail("has value", key); |
82 | |
} |
83 | 2 | } |
84 | |
|
85 | |
public void lacksValue(V key) { |
86 | 2 | if (getSubject().containsValue(key)) { |
87 | 1 | fail("lacks value", key); |
88 | |
} |
89 | 1 | } |
90 | |
|
91 | |
public interface WithValue<V> { |
92 | |
void withValue(V value); |
93 | |
} |
94 | |
|
95 | |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
96 | |
public static <K, V, M extends Map<K, V>> MapSubject<? extends MapSubject<?, K, V, M>, K, V, M> create( |
97 | |
FailureStrategy failureStrategy, Map<K, V> map) { |
98 | 19 | return new MapSubject(failureStrategy, map); |
99 | |
} |
100 | |
|
101 | |
} |