You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by br...@apache.org on 2013/04/01 19:09:24 UTC

svn commit: r1463207 - in /commons/proper/csv/trunk/src/test/java/org/apache/commons/csv: TokenMatchers.java TokenMatchersTest.java

Author: britter
Date: Mon Apr  1 17:09:24 2013
New Revision: 1463207

URL: http://svn.apache.org/r1463207
Log:
Add some matcher implementations as discussed on the ML http://markmail.org/message/k7gzqhbgfyiszyph

Added:
    commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/TokenMatchers.java   (with props)
    commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/TokenMatchersTest.java   (with props)

Added: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/TokenMatchers.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/TokenMatchers.java?rev=1463207&view=auto
==============================================================================
--- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/TokenMatchers.java (added)
+++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/TokenMatchers.java Mon Apr  1 17:09:24 2013
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.csv;
+
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import static org.hamcrest.core.AllOf.allOf;
+
+/**
+ * Collection of matchers for asserting the type and content of tokens.
+ */
+final class TokenMatchers {
+
+    public static Matcher<Token> hasType(final Token.Type expectedType) {
+        return new TypeSafeDiagnosingMatcher<Token>() {
+
+            public void describeTo(Description description) {
+                description.appendText("token has type ");
+                description.appendValue(expectedType);
+            }
+
+            @Override
+            protected boolean matchesSafely(Token item,
+                    Description mismatchDescription) {
+                mismatchDescription.appendText("token type is ");
+                mismatchDescription.appendValue(item.type);
+                if (item.type == expectedType) {
+                    return true;
+                }
+                return false;
+            }
+        };
+    }
+
+    public static Matcher<Token> hasContent(final String expectedContent) {
+        return new TypeSafeDiagnosingMatcher<Token>() {
+
+            public void describeTo(Description description) {
+                description.appendText("token has content ");
+                description.appendValue(expectedContent);
+            }
+
+            @Override
+            protected boolean matchesSafely(Token item,
+                    Description mismatchDescription) {
+                mismatchDescription.appendText("token content is ");
+                mismatchDescription.appendValue(item.content.toString());
+                if (expectedContent.equals(item.content.toString())) {
+                    return true;
+                }
+                return false;
+            }
+        };
+    }
+
+    public static Matcher<Token> isReady() {
+        return new TypeSafeDiagnosingMatcher<Token>() {
+
+            public void describeTo(Description description) {
+                description.appendText("token is ready ");
+            }
+
+            @Override
+            protected boolean matchesSafely(Token item,
+                    Description mismatchDescription) {
+                mismatchDescription.appendText("token is not ready ");
+                return item.isReady;
+            }
+        };
+    }
+
+    public static Matcher<Token> matches(final Token.Type expectedType, final String expectedContent) {
+        return allOf(hasType(expectedType), hasContent(expectedContent));
+    }
+
+}

Propchange: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/TokenMatchers.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/TokenMatchersTest.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/TokenMatchersTest.java?rev=1463207&view=auto
==============================================================================
--- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/TokenMatchersTest.java (added)
+++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/TokenMatchersTest.java Mon Apr  1 17:09:24 2013
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.csv;
+
+import static org.apache.commons.csv.TokenMatchers.hasContent;
+import static org.apache.commons.csv.TokenMatchers.hasType;
+import static org.apache.commons.csv.TokenMatchers.isReady;
+import static org.apache.commons.csv.TokenMatchers.matches;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class TokenMatchersTest {
+
+    private Token token;
+
+    @Before
+    public void setUp() {
+        token = new Token();
+        token.type = Token.Type.TOKEN;
+        token.isReady = true;
+        token.content.append("content");
+    }
+
+    @Test
+    public void testHasType() {
+        assertFalse(hasType(Token.Type.COMMENT).matches(token));
+        assertFalse(hasType(Token.Type.EOF).matches(token));
+        assertFalse(hasType(Token.Type.EORECORD).matches(token));
+        assertTrue(hasType(Token.Type.TOKEN).matches(token));
+    }
+
+    @Test
+    public void testHasContent() {
+        assertFalse(hasContent("This is not the token's content").matches(token));
+        assertTrue(hasContent("content").matches(token));
+    }
+
+    @Test
+    public void testIsReady() {
+        assertTrue(isReady().matches(token));
+        token.isReady = false;
+        assertFalse(isReady().matches(token));
+    }
+
+    @Test
+    public void testMatches() {
+        assertTrue(matches(Token.Type.TOKEN, "content").matches(token));
+        assertFalse(matches(Token.Type.EOF, "content").matches(token));
+        assertFalse(matches(Token.Type.TOKEN, "not the content").matches(token));
+        assertFalse(matches(Token.Type.EORECORD, "not the content").matches(token));
+    }
+
+}

Propchange: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/TokenMatchersTest.java
------------------------------------------------------------------------------
    svn:eol-style = native