Parcourir la source

NewAssert#assertMatch

Ranides Atterwim il y a 10 ans
Parent
commit
6d09816184

+ 4 - 0
assira.junit/pom.xml

@@ -31,5 +31,9 @@
             <groupId>net.ranides</groupId>
             <artifactId>caliper</artifactId>
         </dependency>
+        <dependency>
+            <groupId>com.google.code.findbugs</groupId>
+            <artifactId>annotations</artifactId>
+        </dependency>
     </dependencies>
 </project>

+ 186 - 163
assira.junit/src/main/java/net/ranides/assira/junit/NewAssert.java

@@ -1,163 +1,186 @@
-/*
- * @author Ranides Atterwim <ranides@gmail.com>
- * @copyright Ranides Atterwim
- * @license WTFPL
- * @url http://ranides.net/projects/assira.junit
- */
-package net.ranides.assira.junit;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-
-/**
- *
- * @author Ranides Atterwim <ranides@gmail.com>
- */
-public final class NewAssert extends org.junit.Assert {
-    
-    private NewAssert() { }
-    
-    public static void assertThrows(Class<? extends Throwable> error, Action action) {
-        try {
-            action.run();
-            fail(error + " expected");
-        } catch(Throwable cause) { // NOPMD
-            if( !error.isInstance(cause)) {
-                throw rethrow(cause);
-            }
-        }
-    }
-    
-    public static void assertSerializable(Object value) {
-        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
-        try (ObjectOutputStream ostream = new ObjectOutputStream(buffer)) {
-            ostream.writeObject(value);
-            
-            InputStream idata = new ByteArrayInputStream(buffer.toByteArray());
-            try (ObjectInputStream istream = new ObjectInputStream(idata)) {
-                assertEquals(value, istream.readObject());
-            }
-        } catch(IOException | ClassNotFoundException cause) {
-            throw rethrow(cause);
-        }
-    }
-    
-    /**
-     * Requirements (object : hash / pseudo-value)
-     * <pre>
-     *  A : 1 / 1
-     *  B : 1 / 1
-     *  C : 1 / 2
-     *  D : 2 / 3
-     * </pre>
-     * @param a1
-     * @param a2
-     * @param b
-     * @param c 
-     */
-    @SuppressWarnings("PMD.ShortVar")
-    public static void assertEquality(Object a, Object b, Object c, Object d) {
-        int ha = a.hashCode();
-        int hb = b.hashCode();
-        int hc = c.hashCode();
-        int hd = d.hashCode();
-        
-        assertTrue("hashcode: a==b", ha == hb);
-        assertTrue("hashcode: a==c", ha == hc);
-        assertTrue("hashcode: a!=d", ha != hd);
-        
-        assertTrue("a==a", a.equals(a));
-        assertTrue("b==b", b.equals(b));
-        assertTrue("c==c", c.equals(c));
-        assertTrue("d==d", d.equals(d));
-        
-        assertSymEquals("a == b", a,b);
-        
-        assertSymNotEquals("a != c", a,c);
-        assertSymNotEquals("a != d", a,d);
-        assertSymNotEquals("b != c", b,c);
-        assertSymNotEquals("b != d", b,d);
-        assertSymNotEquals("c != d", c,d);
-        
-        assertFalse("a != object", a.equals(new Object()));
-        assertFalse("b != object", b.equals(new Object()));
-        assertFalse("c != object", c.equals(new Object()));
-        assertFalse("d != object", d.equals(new Object()));
-    }
-    
-    /**
-     * Requirements (object : hash / pseudo-value)
-     * <pre>
-     *  A : 1 / 1
-     *  B : 1 / 1
-     *  D : 2 / 2
-     * </pre>
-     * @param a1
-     * @param a2
-     * @param b
-     * @param c 
-     */
-    @SuppressWarnings("PMD.ShortVar")
-    public static void assertEquality(Object a, Object b, Object d) {
-        int ha = a.hashCode();
-        int hb = b.hashCode();
-        int hd = d.hashCode();
-        
-        assertTrue("hashcode: a==b", ha == hb);
-        assertTrue("hashcode: a!=d", ha != hd);
-        
-        assertTrue("a==a", a.equals(a));
-        assertTrue("b==b", b.equals(b));
-        assertTrue("d==d", d.equals(d));
-        
-        assertSymEquals("a == b", a,b);
-        
-        assertSymNotEquals("a != d", a,d);
-        assertSymNotEquals("b != d", b,d);
-        
-        assertFalse("a != object", a.equals(new Object()));
-        assertFalse("b != object", b.equals(new Object()));
-        assertFalse("d != object", d.equals(new Object()));
-    }
-	
-	public static void assertSymEquals(Object value1, Object value2) {
-		assertEquals(value1,value2);
-        assertEquals(value2,value1);
-	}
-    
-    public static void assertSymEquals(String message, Object value1, Object value2) {
-        assertTrue(message, value1.equals(value2) );
-        assertTrue(message, value2.equals(value1) );
-    }
-    
-    private static void assertSymNotEquals(String message, Object value1, Object value2) {
-        assertFalse(message, value1.equals(value2) );
-        assertFalse(message, value2.equals(value1) );
-    }
-    
-    public interface Action {
-        
-        void run() throws Exception;
-        
-    }
-    
-    static RuntimeException rethrow(Throwable cause) {
-        new ExceptionErasure<RuntimeException>().rethrow(cause);
-        
-        // not reachable, wyjątek bez wrappowania leci linię wyżej
-        throw new RuntimeException(cause); // NOPMD
-    }
-    
-    private static class ExceptionErasure<T extends Throwable> { // NOPMD
-        
-        @SuppressWarnings("unchecked")
-        private void rethrow(Throwable exception) throws T {
-            throw (T) exception;
-        }
-
-    }
-}
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira.junit
+ */
+package net.ranides.assira.junit;
+
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.function.BiPredicate;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public final class NewAssert extends org.junit.Assert {
+    
+    private NewAssert() { }
+    
+    public static void assertThrows(Class<? extends Throwable> error, Action action) {
+        try {
+            action.run();
+            fail(error + " expected");
+        } catch(Throwable cause) { // NOPMD
+            if( !error.isInstance(cause)) {
+                throw rethrow(cause);
+            }
+        }
+    }
+    
+    public static void assertSerializable(Object value) {
+        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+        try (ObjectOutputStream ostream = new ObjectOutputStream(buffer)) {
+            ostream.writeObject(value);
+            
+            InputStream idata = new ByteArrayInputStream(buffer.toByteArray());
+            try (ObjectInputStream istream = new ObjectInputStream(idata)) {
+                assertEquals(value, istream.readObject());
+            }
+        } catch(IOException | ClassNotFoundException cause) {
+            throw rethrow(cause);
+        }
+    }
+    
+    /**
+     * Requirements (object : hash / pseudo-value)
+     * <pre>
+     *  A : 1 / 1
+     *  B : 1 / 1
+     *  C : 1 / 2
+     *  D : 2 / 3
+     * </pre>
+     * @param a1
+     * @param a2
+     * @param b
+     * @param c 
+     */
+    @SuppressFBWarnings("SA_LOCAL_SELF_COMPARISON")
+    public static void assertEquality(Object a, Object b, Object c, Object d) {
+        int ha = a.hashCode();
+        int hb = b.hashCode();
+        int hc = c.hashCode();
+        int hd = d.hashCode();
+        
+        assertTrue("hashcode: a==b", ha == hb);
+        assertTrue("hashcode: a==c", ha == hc);
+        assertTrue("hashcode: a!=d", ha != hd);
+        
+        assertTrue("a==a", a.equals(a));
+        assertTrue("b==b", b.equals(b));
+        assertTrue("c==c", c.equals(c));
+        assertTrue("d==d", d.equals(d));
+        
+        assertSymEquals("a == b", a,b);
+        
+        assertSymNotEquals("a != c", a,c);
+        assertSymNotEquals("a != d", a,d);
+        assertSymNotEquals("b != c", b,c);
+        assertSymNotEquals("b != d", b,d);
+        assertSymNotEquals("c != d", c,d);
+        
+        assertFalse("a != object", a.equals(new Object()));
+        assertFalse("b != object", b.equals(new Object()));
+        assertFalse("c != object", c.equals(new Object()));
+        assertFalse("d != object", d.equals(new Object()));
+    }
+    
+    /**
+     * Requirements (object : hash / pseudo-value)
+     * <pre>
+     *  A : 1 / 1
+     *  B : 1 / 1
+     *  D : 2 / 2
+     * </pre>
+     * @param a1
+     * @param a2
+     * @param b
+     * @param c 
+     */
+    @SuppressFBWarnings("SA_LOCAL_SELF_COMPARISON")
+    public static void assertEquality(Object a, Object b, Object d) {
+        int ha = a.hashCode();
+        int hb = b.hashCode();
+        int hd = d.hashCode();
+        
+        assertTrue("hashcode: a==b", ha == hb);
+        assertTrue("hashcode: a!=d", ha != hd);
+        
+        assertTrue("a==a", a.equals(a));
+        assertTrue("b==b", b.equals(b));
+        assertTrue("d==d", d.equals(d));
+        
+        assertSymEquals("a == b", a,b);
+        
+        assertSymNotEquals("a != d", a,d);
+        assertSymNotEquals("b != d", b,d);
+        
+        assertFalse("a != object", a.equals(new Object()));
+        assertFalse("b != object", b.equals(new Object()));
+        assertFalse("d != object", d.equals(new Object()));
+    }
+	
+	public static void assertSymEquals(Object value1, Object value2) {
+		assertEquals(value1,value2);
+        assertEquals(value2,value1);
+	}
+    
+    public static void assertSymEquals(String message, Object value1, Object value2) {
+        assertTrue(message, value1.equals(value2) );
+        assertTrue(message, value2.equals(value1) );
+    }
+    
+    private static void assertSymNotEquals(String message, Object value1, Object value2) {
+        assertFalse(message, value1.equals(value2) );
+        assertFalse(message, value2.equals(value1) );
+    }
+    
+    public static <A,B> void assertMatch(Collection<A> expected, Collection<B> values, BiPredicate<A,B> predicate) {
+        assertMatch("", expected, values, predicate);
+    }
+    
+    public static <A,B> void assertMatch(String message, Collection<A> expected, Collection<B> values, BiPredicate<A,B> predicate) {
+        assertEquals(message + ": different size", expected.size(), values.size());
+        Iterator<A> ia = expected.iterator();
+        Iterator<B> ib = values.iterator();
+        for(int i=0, n=expected.size(); i<n; i++) {
+            A a = ia.next();
+            B b = ib.next();
+            if(!predicate.test(a,b)) {
+                fail("line[" + i+"] expected '" + a +"' but was '" + b + "'");
+            }
+		}
+    }
+    
+    public interface Action {
+        
+        void run() throws Exception;
+        
+    }
+    
+    static RuntimeException rethrow(Throwable cause) {
+        new ExceptionErasure<RuntimeException>().rethrow(cause);
+        
+        // not reachable, wyjątek bez wrappowania leci linię wyżej
+        throw new RuntimeException(cause); // NOPMD
+    }
+    
+    private static class ExceptionErasure<T extends Throwable> { // NOPMD
+        
+        @SuppressWarnings("unchecked")
+        private void rethrow(Throwable exception) throws T {
+            throw (T) exception;
+        }
+
+    }
+}