Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
IterableSubject |
|
| 2.142857142857143;2.143 |
1 | /* | |
2 | * Copyright (C) 2011 Google, Inc. | |
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 | package org.truth0.subjects; | |
17 | ||
18 | import com.google.common.annotations.GwtCompatible; | |
19 | ||
20 | import org.truth0.FailureStrategy; | |
21 | ||
22 | import java.util.Arrays; | |
23 | import java.util.Iterator; | |
24 | ||
25 | /** | |
26 | * @author Kevin Bourrillion | |
27 | */ | |
28 | @GwtCompatible | |
29 | public class IterableSubject<S extends IterableSubject<S, T, C>, T, C extends Iterable<T>> extends Subject<S, C> { | |
30 | ||
31 | @SuppressWarnings({ "unchecked", "rawtypes" }) | |
32 | public static <T, C extends Iterable<T>> IterableSubject<? extends IterableSubject<?, T, C>, T, C> create( | |
33 | FailureStrategy failureStrategy, Iterable<T> list) { | |
34 | 0 | return new IterableSubject(failureStrategy, list); |
35 | } | |
36 | ||
37 | // TODO: Arguably this should even be package private | |
38 | protected IterableSubject(FailureStrategy failureStrategy, C list) { | |
39 | 0 | super(failureStrategy, list); |
40 | 0 | } |
41 | ||
42 | /** | |
43 | * Attests that the subject holds no more objects, or fails. | |
44 | */ | |
45 | public void isEmpty() { | |
46 | 0 | if (getSubject().iterator().hasNext()) { |
47 | 0 | fail("is empty"); |
48 | } | |
49 | 0 | } |
50 | ||
51 | /** | |
52 | * Attests that the subject holds one or more objects, or fails | |
53 | */ | |
54 | public void isNotEmpty() { | |
55 | 0 | if (!getSubject().iterator().hasNext()) { |
56 | 0 | fail("is not empty"); |
57 | } | |
58 | 0 | } |
59 | ||
60 | /** | |
61 | * Asserts that the items are supplied in the order given by the iterable. If | |
62 | * the iterable under test and/or the {@code expectedItems} do not provide | |
63 | * iteration order guarantees (say, {@link Set<T>}s), this method may provide | |
64 | * unexpected results. Consider using {@link #is(T)} in such cases, or using | |
65 | * collections and iterables that provide strong order guarantees. | |
66 | */ | |
67 | public void iteratesAs(Iterable<T> expectedItems) { | |
68 | 0 | Iterator<T> actualItems = getSubject().iterator(); |
69 | 0 | for (Object expected : expectedItems) { |
70 | 0 | if (!actualItems.hasNext()) { |
71 | 0 | fail("iterates through", expectedItems); |
72 | } else { | |
73 | 0 | Object actual = actualItems.next(); |
74 | 0 | if (actual == expected || actual != null && actual.equals(expected)) { |
75 | 0 | continue; |
76 | } else { | |
77 | 0 | fail("iterates through", expectedItems); |
78 | } | |
79 | } | |
80 | 0 | } |
81 | 0 | if (actualItems.hasNext()) { |
82 | 0 | fail("iterates through", expectedItems); |
83 | } | |
84 | 0 | } |
85 | ||
86 | /** | |
87 | * @deprecated use {@link #iteratesAs(T...)} | |
88 | */ | |
89 | @Deprecated | |
90 | public void iteratesOverSequence(T... expectedItems) { | |
91 | 0 | iteratesAs(expectedItems); |
92 | 0 | } |
93 | ||
94 | /** | |
95 | * Asserts that the items are supplied in the order given by the iterable. If | |
96 | * the iterable under test does not provide iteration order guarantees (say, | |
97 | * a {@link Set<T>}), this method is not suitable for asserting that order. | |
98 | * Consider using {@link #is(T)} | |
99 | */ | |
100 | public void iteratesAs(T... expectedItems) { | |
101 | 0 | iteratesAs(Arrays.asList(expectedItems)); |
102 | 0 | } |
103 | } |