|
| 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 | +} |
0 commit comments