summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/funcutils/TestUtils.java
blob: bf38cbe6a13aae7189ca1f863b6b515a5cef13a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package bjc.utils.funcutils;

import static org.junit.Assert.assertEquals;

import java.util.Iterator;
import java.util.List;

/**
 * Utilities for testing.
 *
 * @author bjculkin
 *
 */
public class TestUtils {
	/**
	 * Assert an iterator provides a particular sequence of values.
	 *
	 * @param <T> The type of the values.
	 * @param src The iterator to pull values from.
	 * @param vals The values to expect from the iterator.
	 */
	@SafeVarargs
	public static <T> void assertIteratorEquals(Iterator<T> src, T... vals) {
		for (T val : vals) assertEquals(val, src.next());
	}

	/**
	 * Assert an iterator provides a particular sequence of values.
	 *
	 * @param <T> The type of the values.
	 * @param src The iterator to pull values from.
	 * @param hasMore The expected value of hasNext for the iterator.
	 * @param vals The values to expect from the iterator.
	 */
	@SafeVarargs
	public static <T> void assertIteratorEquals(boolean hasMore, Iterator<T> src,
			T... vals) {
		/*
		 * @NOTE
		 *
		 * Even though it's awkward, the boolean has to come first. Otherwise, there are
		 * cases where the compiler will get confused as to what the right value for T
		 * is, and be unable to pick an overload.
		 * 
		 * This is a case where named parameter would be rather useful.
		 */
		assertIteratorEquals(src, vals);

		assertEquals(hasMore, src.hasNext());
	}

	/**
	 * Assert that a list has a given set of contents.
	 *
	 * @param <T> The type of the values.
	 * @param src The list of actual elements.
	 * @param exps The list of expected elements.
	 */
	@SafeVarargs
	public static <T> void assertListEquals(List<T> src, T... exps) {
		assertEquals(exps.length, src.size());

		int i = 0;
		for (T act : src) {
			T exp = exps[i++];

			assertEquals(exp, act);
		}
	}
}