|
| 1 | +package com.annimon.ownlang.lib.modules; |
| 2 | + |
| 3 | +import com.annimon.ownlang.lib.*; |
| 4 | +import java.text.DecimalFormat; |
| 5 | +import java.util.List; |
| 6 | +import java.util.stream.Collectors; |
| 7 | + |
| 8 | +/** |
| 9 | + * |
| 10 | + * @author aNNiMON |
| 11 | + */ |
| 12 | +public final class ounit implements Module { |
| 13 | + |
| 14 | + @Override |
| 15 | + public void init() { |
| 16 | + Functions.set("assertEquals", new assertEquals()); |
| 17 | + Functions.set("assertNotEquals", new assertNotEquals()); |
| 18 | + Functions.set("assertSameType", new assertSameType()); |
| 19 | + Functions.set("assertTrue", new assertTrue()); |
| 20 | + Functions.set("assertFalse", new assertFalse()); |
| 21 | + Functions.set("runTests", new runTests()); |
| 22 | + } |
| 23 | + |
| 24 | + private static String microsToSeconds(long micros) { |
| 25 | + return new DecimalFormat("#0.0000").format(micros / 1000d / 1000d) + " sec"; |
| 26 | + } |
| 27 | + |
| 28 | + private static class assertEquals implements Function { |
| 29 | + @Override |
| 30 | + public Value execute(Value... args) { |
| 31 | + if (args[0].equals(args[1])) return NumberValue.ONE; |
| 32 | + throw new OUnitAssertionException("Values are not equals: " |
| 33 | + + "1: " + args[0] + ", 2: " + args[1]); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private static class assertNotEquals implements Function { |
| 38 | + @Override |
| 39 | + public Value execute(Value... args) { |
| 40 | + if (!args[0].equals(args[1])) return NumberValue.ONE; |
| 41 | + throw new OUnitAssertionException("Values are equals: " + args[0]); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private static class assertSameType implements Function { |
| 46 | + @Override |
| 47 | + public Value execute(Value... args) { |
| 48 | + if (args[0].type() == args[1].type()) return NumberValue.ONE; |
| 49 | + throw new OUnitAssertionException("Types mismatch. " |
| 50 | + + "1: " + Types.typeToString(args[0].type()) |
| 51 | + + ", 2: " + Types.typeToString(args[1].type())); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private static class assertTrue implements Function { |
| 56 | + @Override |
| 57 | + public Value execute(Value... args) { |
| 58 | + if (args[0].asInt() != 0) return NumberValue.ONE; |
| 59 | + throw new OUnitAssertionException("Expected true, but found false."); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private static class assertFalse implements Function { |
| 64 | + @Override |
| 65 | + public Value execute(Value... args) { |
| 66 | + if (args[0].asInt() == 0) return NumberValue.ONE; |
| 67 | + throw new OUnitAssertionException("Expected false, but found true."); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private static class runTests implements Function { |
| 72 | + |
| 73 | + @Override |
| 74 | + public Value execute(Value... args) { |
| 75 | + List<TestInfo> tests = Functions.getFunctions().entrySet().stream() |
| 76 | + .filter(e -> e.getKey().toLowerCase().startsWith("test")) |
| 77 | + .map(e -> runTest(e.getKey(), e.getValue())) |
| 78 | + .collect(Collectors.toList()); |
| 79 | + |
| 80 | + int failures = 0; |
| 81 | + long summaryTime = 0; |
| 82 | + final StringBuilder result = new StringBuilder(); |
| 83 | + for (TestInfo test : tests) { |
| 84 | + if (!test.isPassed) failures++; |
| 85 | + summaryTime += test.elapsedTimeInMicros; |
| 86 | + result.append(System.lineSeparator()); |
| 87 | + result.append(test.info()); |
| 88 | + } |
| 89 | + result.append(System.lineSeparator()); |
| 90 | + result.append(String.format("Tests run: %d, Failures: %d, Time elapsed: %s", |
| 91 | + tests.size(), failures, |
| 92 | + microsToSeconds(summaryTime))); |
| 93 | + return new StringValue(result.toString()); |
| 94 | + } |
| 95 | + |
| 96 | + private TestInfo runTest(String name, Function f) { |
| 97 | + final long startTime = System.nanoTime(); |
| 98 | + boolean isSuccessfull; |
| 99 | + String failureDescription; |
| 100 | + try { |
| 101 | + f.execute(); |
| 102 | + isSuccessfull = true; |
| 103 | + failureDescription = ""; |
| 104 | + } catch (OUnitAssertionException oae) { |
| 105 | + isSuccessfull = false; |
| 106 | + failureDescription = oae.getMessage(); |
| 107 | + } |
| 108 | + final long elapsedTime = System.nanoTime() - startTime; |
| 109 | + return new TestInfo(name, isSuccessfull, failureDescription, elapsedTime / 1000); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private static class OUnitAssertionException extends RuntimeException { |
| 114 | + |
| 115 | + public OUnitAssertionException(String message) { |
| 116 | + super(message); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private static class TestInfo { |
| 121 | + String name; |
| 122 | + boolean isPassed; |
| 123 | + String failureDescription; |
| 124 | + long elapsedTimeInMicros; |
| 125 | + |
| 126 | + public TestInfo(String name, boolean isPassed, String failureDescription, long elapsedTimeInMicros) { |
| 127 | + this.name = name; |
| 128 | + this.isPassed = isPassed; |
| 129 | + this.failureDescription = failureDescription; |
| 130 | + this.elapsedTimeInMicros = elapsedTimeInMicros; |
| 131 | + } |
| 132 | + |
| 133 | + public String info() { |
| 134 | + return String.format("%s [%s]\n%sElapsed: %s\n", |
| 135 | + name, |
| 136 | + isPassed ? "passed" : "FAILED", |
| 137 | + isPassed ? "" : (failureDescription + "\n"), |
| 138 | + microsToSeconds(elapsedTimeInMicros) |
| 139 | + ); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments