Skip to content

Commit aef7130

Browse files
committed
Add some matcher implementations as discussed on the ML http://markmail.org/message/k7gzqhbgfyiszyph
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1463207 13f79535-47bb-0310-9956-ffa450edef68
1 parent 8f43655 commit aef7130

2 files changed

Lines changed: 161 additions & 0 deletions

File tree

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.csv;
18+
19+
import org.hamcrest.Description;
20+
import org.hamcrest.Matcher;
21+
import org.hamcrest.TypeSafeDiagnosingMatcher;
22+
import static org.hamcrest.core.AllOf.allOf;
23+
24+
/**
25+
* Collection of matchers for asserting the type and content of tokens.
26+
*/
27+
final class TokenMatchers {
28+
29+
public static Matcher<Token> hasType(final Token.Type expectedType) {
30+
return new TypeSafeDiagnosingMatcher<Token>() {
31+
32+
public void describeTo(Description description) {
33+
description.appendText("token has type ");
34+
description.appendValue(expectedType);
35+
}
36+
37+
@Override
38+
protected boolean matchesSafely(Token item,
39+
Description mismatchDescription) {
40+
mismatchDescription.appendText("token type is ");
41+
mismatchDescription.appendValue(item.type);
42+
if (item.type == expectedType) {
43+
return true;
44+
}
45+
return false;
46+
}
47+
};
48+
}
49+
50+
public static Matcher<Token> hasContent(final String expectedContent) {
51+
return new TypeSafeDiagnosingMatcher<Token>() {
52+
53+
public void describeTo(Description description) {
54+
description.appendText("token has content ");
55+
description.appendValue(expectedContent);
56+
}
57+
58+
@Override
59+
protected boolean matchesSafely(Token item,
60+
Description mismatchDescription) {
61+
mismatchDescription.appendText("token content is ");
62+
mismatchDescription.appendValue(item.content.toString());
63+
if (expectedContent.equals(item.content.toString())) {
64+
return true;
65+
}
66+
return false;
67+
}
68+
};
69+
}
70+
71+
public static Matcher<Token> isReady() {
72+
return new TypeSafeDiagnosingMatcher<Token>() {
73+
74+
public void describeTo(Description description) {
75+
description.appendText("token is ready ");
76+
}
77+
78+
@Override
79+
protected boolean matchesSafely(Token item,
80+
Description mismatchDescription) {
81+
mismatchDescription.appendText("token is not ready ");
82+
return item.isReady;
83+
}
84+
};
85+
}
86+
87+
public static Matcher<Token> matches(final Token.Type expectedType, final String expectedContent) {
88+
return allOf(hasType(expectedType), hasContent(expectedContent));
89+
}
90+
91+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.csv;
18+
19+
import static org.apache.commons.csv.TokenMatchers.hasContent;
20+
import static org.apache.commons.csv.TokenMatchers.hasType;
21+
import static org.apache.commons.csv.TokenMatchers.isReady;
22+
import static org.apache.commons.csv.TokenMatchers.matches;
23+
import static org.junit.Assert.assertFalse;
24+
import static org.junit.Assert.assertTrue;
25+
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
29+
public class TokenMatchersTest {
30+
31+
private Token token;
32+
33+
@Before
34+
public void setUp() {
35+
token = new Token();
36+
token.type = Token.Type.TOKEN;
37+
token.isReady = true;
38+
token.content.append("content");
39+
}
40+
41+
@Test
42+
public void testHasType() {
43+
assertFalse(hasType(Token.Type.COMMENT).matches(token));
44+
assertFalse(hasType(Token.Type.EOF).matches(token));
45+
assertFalse(hasType(Token.Type.EORECORD).matches(token));
46+
assertTrue(hasType(Token.Type.TOKEN).matches(token));
47+
}
48+
49+
@Test
50+
public void testHasContent() {
51+
assertFalse(hasContent("This is not the token's content").matches(token));
52+
assertTrue(hasContent("content").matches(token));
53+
}
54+
55+
@Test
56+
public void testIsReady() {
57+
assertTrue(isReady().matches(token));
58+
token.isReady = false;
59+
assertFalse(isReady().matches(token));
60+
}
61+
62+
@Test
63+
public void testMatches() {
64+
assertTrue(matches(Token.Type.TOKEN, "content").matches(token));
65+
assertFalse(matches(Token.Type.EOF, "content").matches(token));
66+
assertFalse(matches(Token.Type.TOKEN, "not the content").matches(token));
67+
assertFalse(matches(Token.Type.EORECORD, "not the content").matches(token));
68+
}
69+
70+
}

0 commit comments

Comments
 (0)