• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.collect.testing;
18 
19 import static com.google.common.collect.testing.Helpers.NullsBeforeB;
20 import static com.google.common.collect.testing.Helpers.testComparator;
21 
22 import com.google.common.annotations.GwtCompatible;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Map;
30 import junit.framework.AssertionFailedError;
31 import junit.framework.TestCase;
32 
33 /**
34  * Unit test for {@link Helpers}.
35  *
36  * @author Chris Povirk
37  */
38 @GwtCompatible
39 public class HelpersTest extends TestCase {
testNullsBeforeB()40   public void testNullsBeforeB() {
41     testComparator(NullsBeforeB.INSTANCE, "a", "azzzzzz", null, "b", "c");
42   }
43 
testIsEmpty_iterable()44   public void testIsEmpty_iterable() {
45     List<Object> list = new ArrayList<>();
46     Helpers.assertEmpty(list);
47     Helpers.assertEmpty(
48         new Iterable<Object>() {
49           @Override
50           public Iterator<Object> iterator() {
51             return Collections.emptyList().iterator();
52           }
53         });
54 
55     list.add("a");
56     try {
57       Helpers.assertEmpty(list);
58       throw new Error();
59     } catch (AssertionFailedError expected) {
60     }
61     try {
62       Helpers.assertEmpty(
63           new Iterable<String>() {
64             @Override
65             public Iterator<String> iterator() {
66               return Collections.singleton("a").iterator();
67             }
68           });
69       throw new Error();
70     } catch (AssertionFailedError expected) {
71     }
72   }
73 
testIsEmpty_map()74   public void testIsEmpty_map() {
75     Map<Object, Object> map = new HashMap<>();
76     Helpers.assertEmpty(map);
77 
78     map.put("a", "b");
79     try {
80       Helpers.assertEmpty(map);
81       throw new Error();
82     } catch (AssertionFailedError expected) {
83     }
84   }
85 
testAssertEqualInOrder()86   public void testAssertEqualInOrder() {
87     List<?> list = Arrays.asList("a", "b", "c");
88     Helpers.assertEqualInOrder(list, list);
89 
90     List<?> fewer = Arrays.asList("a", "b");
91     try {
92       Helpers.assertEqualInOrder(list, fewer);
93       throw new Error();
94     } catch (AssertionFailedError expected) {
95     }
96 
97     try {
98       Helpers.assertEqualInOrder(fewer, list);
99       throw new Error();
100     } catch (AssertionFailedError expected) {
101     }
102 
103     List<?> differentOrder = Arrays.asList("a", "c", "b");
104     try {
105       Helpers.assertEqualInOrder(list, differentOrder);
106       throw new Error();
107     } catch (AssertionFailedError expected) {
108     }
109 
110     List<?> differentContents = Arrays.asList("a", "b", "C");
111     try {
112       Helpers.assertEqualInOrder(list, differentContents);
113       throw new Error();
114     } catch (AssertionFailedError expected) {
115     }
116   }
117 
testAssertContentsInOrder()118   public void testAssertContentsInOrder() {
119     List<?> list = Arrays.asList("a", "b", "c");
120     Helpers.assertContentsInOrder(list, "a", "b", "c");
121 
122     try {
123       Helpers.assertContentsInOrder(list, "a", "b");
124       throw new Error();
125     } catch (AssertionFailedError expected) {
126     }
127 
128     try {
129       Helpers.assertContentsInOrder(list, "a", "b", "c", "d");
130       throw new Error();
131     } catch (AssertionFailedError expected) {
132     }
133 
134     try {
135       Helpers.assertContentsInOrder(list, "a", "c", "b");
136       throw new Error();
137     } catch (AssertionFailedError expected) {
138     }
139 
140     try {
141       Helpers.assertContentsInOrder(list, "a", "B", "c");
142       throw new Error();
143     } catch (AssertionFailedError expected) {
144     }
145   }
146 
testAssertContains()147   public void testAssertContains() {
148     List<?> list = Arrays.asList("a", "b");
149     Helpers.assertContains(list, "a");
150     Helpers.assertContains(list, "b");
151 
152     try {
153       Helpers.assertContains(list, "c");
154       throw new Error();
155     } catch (AssertionFailedError expected) {
156     }
157   }
158 
testAssertContainsAllOf()159   public void testAssertContainsAllOf() {
160     List<?> list = Arrays.asList("a", "a", "b", "c");
161     Helpers.assertContainsAllOf(list, "a");
162     Helpers.assertContainsAllOf(list, "a", "a");
163     Helpers.assertContainsAllOf(list, "a", "b", "c");
164     Helpers.assertContainsAllOf(list, "a", "b", "c", "a");
165 
166     try {
167       Helpers.assertContainsAllOf(list, "d");
168       throw new Error();
169     } catch (AssertionFailedError expected) {
170     }
171 
172     try {
173       Helpers.assertContainsAllOf(list, "a", "b", "c", "d");
174       throw new Error();
175     } catch (AssertionFailedError expected) {
176     }
177 
178     try {
179       Helpers.assertContainsAllOf(list, "a", "a", "a");
180       throw new Error();
181     } catch (AssertionFailedError expected) {
182     }
183   }
184 }
185