View Javadoc
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.hamcrest.core.AllOf.allOf;
20  
21  import org.hamcrest.Description;
22  import org.hamcrest.Matcher;
23  import org.hamcrest.TypeSafeDiagnosingMatcher;
24  
25  /**
26   * Collection of matchers for asserting the type and content of tokens.
27   */
28  final class TokenMatchers {
29  
30      public static Matcher<Token> hasContent(final String expectedContent) {
31          return new TypeSafeDiagnosingMatcher<Token>() {
32  
33              @Override
34              public void describeTo(final Description description) {
35                  description.appendText("token has content ");
36                  description.appendValue(expectedContent);
37              }
38  
39              @Override
40              protected boolean matchesSafely(final Token item,
41                      final Description mismatchDescription) {
42                  mismatchDescription.appendText("token content is ");
43                  mismatchDescription.appendValue(item.content.toString());
44                  return expectedContent.equals(item.content.toString());
45              }
46          };
47      }
48  
49      public static Matcher<Token> hasType(final Token.Type expectedType) {
50          return new TypeSafeDiagnosingMatcher<Token>() {
51  
52              @Override
53              public void describeTo(final Description description) {
54                  description.appendText("token has type ");
55                  description.appendValue(expectedType);
56              }
57  
58              @Override
59              protected boolean matchesSafely(final Token item,
60                      final Description mismatchDescription) {
61                  mismatchDescription.appendText("token type is ");
62                  mismatchDescription.appendValue(item.type);
63                  return item.type == expectedType;
64              }
65          };
66      }
67  
68      public static Matcher<Token> isReady() {
69          return new TypeSafeDiagnosingMatcher<Token>() {
70  
71              @Override
72              public void describeTo(final Description description) {
73                  description.appendText("token is ready ");
74              }
75  
76              @Override
77              protected boolean matchesSafely(final Token item,
78                      final Description mismatchDescription) {
79                  mismatchDescription.appendText("token is not ready ");
80                  return item.isReady;
81              }
82          };
83      }
84  
85      public static Matcher<Token> matches(final Token.Type expectedType, final String expectedContent) {
86          return allOf(hasType(expectedType), hasContent(expectedContent));
87      }
88  
89  }