Skip to content

Commit 5aae3e2

Browse files
committed
Модуль юнит-тестирования
1 parent 8eb25ef commit 5aae3e2

File tree

4 files changed

+186
-1
lines changed

4 files changed

+186
-1
lines changed

src/com/annimon/ownlang/Main.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ private static void run(String input, boolean showTokens, boolean showAst, boole
9494
} finally {
9595
if (showMeasurements) {
9696
measurement.stop("Execution time");
97+
System.out.println("======================");
9798
System.out.println(measurement.summary(TimeUnit.MILLISECONDS, true));
9899
}
99100
}

src/com/annimon/ownlang/lib/Functions.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
public final class Functions {
1212

1313
private static final Map<String, Function> functions;
14-
1514
static {
1615
functions = new HashMap<>();
1716
}
1817

18+
public static Map<String, Function> getFunctions() {
19+
return functions;
20+
}
21+
1922
public static boolean isExists(String key) {
2023
return functions.containsKey(key);
2124
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
}

tests.own

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use "ounit"
2+
3+
def testAdditionOnNumbers() {
4+
assertEquals(6, 0 + 1 + 2 + 3)
5+
}
6+
7+
def testSubtractionOnNumbers() {
8+
assertEquals(-6, 0 - 1 - 2 - 3)
9+
}
10+
11+
def testPrefixIncrement() {
12+
a = 8
13+
assertEquals(9, ++a)
14+
assertEquals(9, a)
15+
}
16+
17+
def testPostfixIncrement() {
18+
a = 8
19+
assertEquals(8, a++)
20+
assertEquals(9, a)
21+
}
22+
23+
def testStringReversing() {
24+
assertEquals("tset", -"test")
25+
}
26+
27+
def testStringMultiplication() {
28+
assertEquals("******", "*" * 6)
29+
}
30+
31+
def testTypes() {
32+
assertSameType(0, 0.0)
33+
}
34+
35+
def testFail() {
36+
assertTrue(false)
37+
}
38+
39+
println runTests()

0 commit comments

Comments
 (0)