Add matchers for iterators.
[Sone.git] / src / test / java / net / pterodactylus / sone / Matchers.java
1 /*
2  * Sone - Matchers.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone;
19
20 import static java.util.Arrays.asList;
21 import static java.util.regex.Pattern.compile;
22
23 import java.util.Collection;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import com.google.common.base.Objects;
28 import com.google.common.collect.Lists;
29 import org.hamcrest.Description;
30 import org.hamcrest.Matcher;
31 import org.hamcrest.TypeSafeMatcher;
32
33 /**
34  * Matchers used throughout the tests.
35  *
36  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37  */
38 public class Matchers {
39
40         public static Matcher<String> matches(final String regex) {
41                 return new TypeSafeMatcher<String>() {
42                         @Override
43                         protected boolean matchesSafely(String item) {
44                                 return compile(regex).matcher(item).find();
45                         }
46
47                         @Override
48                         public void describeTo(Description description) {
49                                 description.appendText("matches: ").appendValue(regex);
50                         }
51                 };
52         }
53
54         public static <T> Matcher<Iterator<T>> contains(T... items) {
55                 return contains(asList(items));
56         }
57
58         public static <T> Matcher<Iterator<T>> contains(final Collection<T> items) {
59                 return new TypeSafeMatcher<Iterator<T>>() {
60                         @Override
61                         protected boolean matchesSafely(Iterator<T> iterator) {
62                                 for (T item : items) {
63                                         if (!iterator.hasNext()) {
64                                                 return false;
65                                         }
66                                         T nextItem = iterator.next();
67                                         if (!Objects.equal(item, nextItem)) {
68                                                 return false;
69                                         }
70                                 }
71                                 if (iterator.hasNext()) {
72                                         return false;
73                                 }
74                                 return true;
75                         }
76
77                         @Override
78                         public void describeTo(Description description) {
79                                 description.appendText("contains ").appendValue(items);
80                         }
81                 };
82         }
83
84         public static <T> Matcher<Iterator<T>> containsInAnyOrder(T... items) {
85                 return containsInAnyOrder(asList(items));
86         }
87
88         public static <T> Matcher<Iterator<T>> containsInAnyOrder(final Collection<T> items) {
89                 return new TypeSafeMatcher<Iterator<T>>() {
90                         private final List<T> remainingItems = Lists.newArrayList(items);
91                         @Override
92                         protected boolean matchesSafely(Iterator<T> iterator) {
93                                 while (iterator.hasNext()) {
94                                         T item = iterator.next();
95                                         if (remainingItems.isEmpty()) {
96                                                 return false;
97                                         }
98                                         if (!remainingItems.remove(item)) {
99                                                 return false;
100                                         }
101                                 }
102                                 if (!remainingItems.isEmpty()) {
103                                         return false;
104                                 }
105                                 return true;
106                         }
107
108                         @Override
109                         public void describeTo(Description description) {
110                                 description.appendText("contains ").appendValue(items);
111                         }
112                 };
113         }
114
115 }