Sfoglia il codice sorgente

new: assira.junit.TestContractRunner#function
new: assira.junit.TestContractRunner#supplier
new: assira.junit.TestContractRunner - we can pass any generator, not only int[]

Ranides Atterwim 10 anni fa
parent
commit
e46e82f1e5

+ 60 - 65
assira.junit/src/main/java/net/ranides/assira/junit/TestContractRunner.java

@@ -37,17 +37,24 @@ import org.junit.Ignore;
  */
 public final class TestContractRunner {
 	
+	private static final boolean DEBUG = "true".equals(System.getProperty("assira.junit.debug"));
+	
 	private final SortedSet<MethodRunner> methods = new TreeSet<>();
     
     private final List<TesterWrapper> testers = new ArrayList<>();
     
     private final List<Pattern> ignored = new ArrayList<>();
-    
+        
     private final PrintStream output;
-    
-    private static final boolean DEBUG = "true".equals(System.getProperty("assira.junit.debug"));
 	
+	private final Generator generator = new Generator();
+    
     private boolean debug = DEBUG;
+	
+	private Function<Object, Object> function;
+	
+	private Supplier<Object> supplier;
+	
 
     public TestContractRunner(PrintStream output) {
         this.output = output;
@@ -133,25 +140,33 @@ public final class TestContractRunner {
         });
         return this;
     }
-    
-    public boolean run(Function<int[], ?> generator) {
-        return irun(new TGeneratorF(generator));
-    }
-    
-    public boolean run(Supplier<?> generator) {
-        return irun(new TGeneratorS(generator));
-    }
-    
-    private boolean irun(TGenerator generator) { //NOPMD
+	
+	@SuppressWarnings("unchecked")
+	public TestContractRunner supplier(Supplier<?> supplier) {
+		this.supplier = (Supplier)supplier;
+		return this;
+	}
+	
+	@SuppressWarnings("unchecked")
+	public <P> TestContractRunner function(P ident, Function<P, ?> function) {
+		this.function = (Function)function;
+		this.supplier = () -> this.function.apply(ident);
+		return this;
+	}
+
+    public boolean run() {
         for(TesterWrapper tester : testers) {
             tester.bind(this);
         }
-        Class<?> type = generator.get().getClass();
+		if(null == supplier) {
+			throw new AssertionError("Supplier is not set.");
+		}
+        Class<?> type = supplier.get().getClass();
         Counter counter = new Counter();
         List<String> failed = new ArrayList<>();
         methods.stream().filter((m)->m.accept(type)).forEach((m)->{
             try {
-                m.run(counter, generator);
+                m.run(counter);
             } catch(Throwable cause) {
                 failed.add(m.label);
                 cause.printStackTrace(output);
@@ -173,6 +188,8 @@ public final class TestContractRunner {
     private void ireset() {
 		debug = DEBUG;
         ignored.clear();
+		function = null;
+		supplier = null;
         for(TesterWrapper tester : testers) {
             tester.resetParams();
         }
@@ -195,8 +212,8 @@ public final class TestContractRunner {
         }
 
         @Override
-        protected void invoke(TGenerator argument) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
-            method.invoke(that, argument.get());
+        protected void invoke() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+            method.invoke(that, supplier.get());
         }
         
     }
@@ -208,8 +225,8 @@ public final class TestContractRunner {
         }
         
         @Override
-        protected void invoke(TGenerator argument) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
-            method.invoke(that, argument);
+        protected void invoke() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+            method.invoke(that, generator);
         }
         
     }
@@ -245,9 +262,9 @@ public final class TestContractRunner {
             return type.isAssignableFrom(param);
         }
         
-        protected abstract void invoke(TGenerator argument) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException;
+        protected abstract void invoke() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException;
         
-        public void run(Counter counter, TGenerator param) throws RuntimeException {
+        public void run(Counter counter) throws RuntimeException {
 			for(Pattern p : ignored) {
 				if(p.matcher(nid).matches()) {
 					if(debug) {
@@ -258,7 +275,7 @@ public final class TestContractRunner {
 			}
             int index = counter.next();
             try {
-                invoke(param);
+                invoke();
 				if(debug) {
 					printf(type, index, method, "");
 				}
@@ -313,9 +330,9 @@ public final class TestContractRunner {
     
     private static Class<?> typeofFunction(Method method) throws AssertionError {
         ParameterizedType gparam = (ParameterizedType)method.getGenericParameterTypes()[0];
-        if(!gparam.getActualTypeArguments()[0].equals(int[].class)) {
-            throw new AssertionError("Function must accept int[] in method: " + method, causeForMethod(method));
-        }
+//        if(!gparam.getActualTypeArguments()[0].equals(int[].class)) {
+//            throw new AssertionError("Function must accept int[] in method: " + method, causeForMethod(method));
+//        }
         Type tparam = gparam.getActualTypeArguments()[1];
         if(tparam instanceof Class<?>) {
             return (Class<?>)tparam;
@@ -392,48 +409,26 @@ public final class TestContractRunner {
         }
         
     }
-    
-    private interface TGenerator extends Function<int[], Object>, Supplier<Object>  { }
-    
-    private static class TGeneratorF implements TGenerator {
-        
-        private final Function<int[], ?> function;
-
-        public TGeneratorF(Function<int[], ?> function) {
-            this.function = function;
-        }
-
-        @Override
-        public Object get() {
-            return function.apply(new int[0]);
-        }
-
-        @Override
-        public Object apply(int[] t) {
-            return function.apply(t);
-        }
-        
-    }
-    
-    private static class TGeneratorS implements TGenerator {
-        
-        private final Supplier<?> function;
-
-        public TGeneratorS(Supplier<?> function) {
-            this.function = function;
-        }
+	
+	private final class Generator implements Function<Object, Object>, Supplier<Object> {
 
-        @Override
-        public Object get() {
-            return function.get();
-        }
+		@Override
+		public Object apply(Object params) {
+			if( null == function) {
+				throw new AssertionError("Function is not set.");
+			}
+			return function.apply(params);
+		}
 
-        @Override
-        public Object apply(int[] t) {
-            throw new UnsupportedOperationException("Generator function not supplied.");
-        }
-        
-    }
+		@Override
+		public Object get() {
+			if(null == supplier) {
+				throw new AssertionError("Supplier is not set.");
+			}
+			return supplier.get();
+		}
+		
+	}
     
     private static final class IParam {
         private final Object object;

+ 17 - 11
assira.junit/src/test/java/net/ranides/assira/junit/TestContractRunnerTest.java

@@ -88,9 +88,9 @@ public class TestContractRunnerTest {
             .append(new Testers.ForA2());
 
         TesterObjects.OUT.clear();
-        suite.run(() -> new ObjectABC());
-        suite.run(() -> new ObjectA());
-        suite.run(() -> new AbstractAB(){});
+		suite.supplier(() -> new ObjectABC()).run();
+		suite.supplier(() -> new ObjectA()).run();
+		suite.supplier(() -> new AbstractAB(){}).run();
         assertEquals(EXPECTED, TesterObjects.OUT);
     }
     
@@ -107,7 +107,7 @@ public class TestContractRunnerTest {
             .append(new Testers.Correct());
 
         TesterObjects.OUT.clear();
-        suite.run(() -> new ObjectA());
+        suite.supplier(() -> new ObjectA()).run();
         assertEquals(EXPECTED_A, TesterObjects.OUT);
     }
     
@@ -121,7 +121,7 @@ public class TestContractRunnerTest {
 			.debug(true);
 
         TesterObjects.OUT.clear();
-        suite.run(() -> new ObjectA());
+        suite.supplier(() -> new ObjectA()).run();
         assertEquals(EXPECTED_OUT, out.toString());
         assertEquals(EXPECTED_A, TesterObjects.OUT);
     }
@@ -132,7 +132,8 @@ public class TestContractRunnerTest {
         
         new TestContractRunner(System.err)
             .append(new Testers.ForMap())
-            .run(() -> new TreeMap<>());
+            .supplier(() -> new TreeMap<>())
+			.run();
         assertEquals(Arrays.asList("map"), TesterObjects.OUT);
     }
     
@@ -141,7 +142,8 @@ public class TestContractRunnerTest {
         assertThrows(AssertionError.class, ()->{
             new TestContractRunner(System.err)
                 .append(new Testers.ForWildcard())
-                .run(() -> new TreeMap<>());
+                .supplier(() -> new TreeMap<>())
+				.run();
         });
     }
     
@@ -150,7 +152,8 @@ public class TestContractRunnerTest {
         TesterObjects.OUT.clear();
         new TestContractRunner(System.err)
             .append(new Testers.ForFunction())
-            .run((int[] args) -> new ArrayList<>());
+            .function(new int[0], (args) -> new ArrayList<>())
+			.run();
         assertEquals(EXPECTED_FUNCTION, TesterObjects.OUT);
     }
     
@@ -164,13 +167,16 @@ public class TestContractRunnerTest {
         suite
             .param("name", "ranides")
             .param("count!", 33)
-            .run(() -> new Object());
+            .supplier(() -> new Object())
+			.run();
         suite
             .param("count!", 34)
-            .run(() -> new Object());
+            .supplier(() -> new Object())
+			.run();
         suite
             .param("name", "ot")
-            .run(() -> new Object());
+            .supplier(() -> new Object())
+			.run();
         
         assertEquals(EXPECTED_RES, TesterObjects.OUT);