Skip to content

Commit 66bf40b

Browse files
committed
Юнит-тесты для программ
1 parent 51c6439 commit 66bf40b

17 files changed

+311
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.annimon.ownlang.parser;
2+
3+
import com.annimon.ownlang.lib.Functions;
4+
import com.annimon.ownlang.lib.NumberValue;
5+
import com.annimon.ownlang.lib.Variables;
6+
import com.annimon.ownlang.parser.ast.FunctionDefineStatement;
7+
import com.annimon.ownlang.parser.ast.Statement;
8+
import com.annimon.ownlang.parser.ast.Visitor;
9+
import com.annimon.ownlang.parser.visitors.AbstractVisitor;
10+
import java.io.File;
11+
import java.io.IOException;
12+
import java.util.Arrays;
13+
import java.util.Collection;
14+
import java.util.stream.Collectors;
15+
import java.util.stream.Stream;
16+
import org.junit.Assert;
17+
import static org.junit.Assert.*;
18+
import org.junit.Before;
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
import org.junit.runners.Parameterized;
22+
import org.junit.runners.Parameterized.Parameters;
23+
24+
@RunWith(value = Parameterized.class)
25+
public class ProgramsTest {
26+
27+
private static final String RES_DIR = "test/resources";
28+
29+
private final String programPath;
30+
31+
public ProgramsTest(String programPath) {
32+
this.programPath = programPath;
33+
}
34+
35+
@Parameters(name = "{index}: {0}")
36+
public static Collection<String> data() {
37+
final File resDir = new File(RES_DIR);
38+
return scanDirectory(resDir)
39+
.map(f -> f.getPath())
40+
.collect(Collectors.toList());
41+
}
42+
43+
private static Stream<File> scanDirectory(File dir) {
44+
return Arrays.stream(dir.listFiles())
45+
.flatMap(file -> {
46+
if (file.isDirectory()) {
47+
return scanDirectory(file);
48+
}
49+
return Stream.of(file);
50+
})
51+
.filter(f -> f.getName().endsWith(".own"));
52+
}
53+
54+
@Before
55+
public void initialize() {
56+
Variables.clear();
57+
Functions.getFunctions().clear();
58+
// Let's mock junit methods as ounit functions
59+
Functions.set("assertEquals", (args) -> {
60+
assertEquals(args[0], args[1]);
61+
return NumberValue.ONE;
62+
});
63+
Functions.set("assertNotEquals", (args) -> {
64+
assertNotEquals(args[0], args[1]);
65+
return NumberValue.ONE;
66+
});
67+
Functions.set("assertSameType", (args) -> {
68+
assertEquals(args[0].type(), args[1].type());
69+
return NumberValue.ONE;
70+
});
71+
Functions.set("assertTrue", (args) -> {
72+
assertTrue(args[0].asInt() != 0);
73+
return NumberValue.ONE;
74+
});
75+
Functions.set("assertFalse", (args) -> {
76+
assertFalse(args[0].asInt() == 0);
77+
return NumberValue.ONE;
78+
});
79+
}
80+
81+
@Test
82+
public void testProgram() throws IOException {
83+
final String source = SourceLoader.readSource(programPath);
84+
final Statement s = Parser.parse(Lexer.tokenize(source));
85+
try {
86+
s.execute();
87+
s.accept(testFunctionsExecutor);
88+
} catch (Exception oae) {
89+
Assert.fail(oae.toString());
90+
}
91+
}
92+
93+
private static Visitor testFunctionsExecutor = new AbstractVisitor() {
94+
@Override
95+
public void visit(FunctionDefineStatement s) {
96+
if (s.name.startsWith("test")) {
97+
Functions.get(s.name).execute();
98+
}
99+
}
100+
};
101+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
def testSimpleAssignment() {
2+
a = 8
3+
b = "str"
4+
assertEquals(8, a)
5+
assertEquals("str", b)
6+
}
7+
8+
def testPrefixIncrement() {
9+
a = 8
10+
assertEquals(9, ++a)
11+
assertEquals(9, a)
12+
}
13+
14+
def testPostfixIncrement() {
15+
a = 8
16+
assertEquals(8, a++)
17+
assertEquals(9, a)
18+
}
19+
20+
def testPrefixDecrement() {
21+
a = 8
22+
assertEquals(7, --a)
23+
assertEquals(7, a)
24+
}
25+
26+
def testPostfixDecrement() {
27+
a = 8
28+
assertEquals(8, a--)
29+
assertEquals(7, a)
30+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
def testAdditionOnNumbers() {
2+
assertEquals(4, 2 + 2)
3+
assertEquals(6, 0 + 1 + 2 + 3)
4+
}
5+
6+
def testSubtractionOnNumbers() {
7+
assertEquals(0, 2 - 2)
8+
assertEquals(-6, 0 - 1 - 2 - 3)
9+
assertEquals(110, 100 - (20 - 30))
10+
}
11+
12+
def testMultiplicationOnNumbers() {
13+
assertEquals(4, 2 * 2)
14+
assertEquals(30, 5 * (-2 * -3))
15+
}
16+
17+
def testDivisionOnNumbers() {
18+
assertEquals(3, 6 / 2)
19+
assertEquals(30, -900 / (60 / -2))
20+
}
21+
22+
def testRemainder() {
23+
assertEquals(2, 10 % 4)
24+
assertEquals(5, 15 % (40 % 30))
25+
}
26+
27+
def testAND() {
28+
assertEquals(0x04, 0x04 & 0x0F)
29+
assertEquals(0x00, 0x04 & 0x08)
30+
assertEquals(8, 12 & 9)
31+
}
32+
33+
def testOR() {
34+
assertEquals(12, 4 | 8)
35+
assertEquals(0x0F, 3 | 12)
36+
assertEquals(0x0E, 10 | 4)
37+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def testStringConcatenation() {
2+
assertEquals("22", "2" + "2")
3+
assertEquals("22", "2" + 2)
4+
}
5+
6+
def testStringMultiplication() {
7+
assertEquals("******", "*" * 6)
8+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
def testAdditionOnNumbers() {
2+
assertEquals(6, 0 + 1 + 2 + 3)
3+
}
4+
5+
def testSubtractionOnNumbers() {
6+
assertEquals(-6, 0 - 1 - 2 - 3)
7+
}
8+
9+
def testPrefixIncrement() {
10+
a = 8
11+
assertEquals(9, ++a)
12+
assertEquals(9, a)
13+
}
14+
15+
def testPostfixIncrement() {
16+
a = 8
17+
assertEquals(8, a++)
18+
assertEquals(9, a)
19+
}
20+
21+
def testStringReversing() {
22+
assertEquals("tset", -"test")
23+
}
24+
25+
def testStringMultiplication() {
26+
assertEquals("******", "*" * 6)
27+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def func() = "function"
2+
var = def() = "variable"
3+
def consumer(x) = x();
4+
5+
assertEquals("function", consumer(::func))
6+
assertEquals("variable", consumer(var))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def testStringReversing() {
2+
assertEquals("tset", -"test")
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def function() = "function"
2+
function = "variable"
3+
4+
assertEquals("variable", function)
5+
assertEquals("function", function())
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use "date"
2+
3+
def testCompareDates() {
4+
assertTrue(newDate(2016, 04, 10) > newDate(2015, 03, 11))
5+
assertTrue(newDate(2012, 04, 10) < newDate(2015, 03, 11))
6+
assertTrue(newDate(2015, 03, 11, 0, 0, 0) == newDate(2015, 03, 11))
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use "date"
2+
3+
def testDateFormat() {
4+
d = formatDate(newDate(2016, 04, 10), newFormat("yyyy/MM/dd HH:mm:ss"))
5+
assertEquals("2016/05/10 00:00:00", d)
6+
}

0 commit comments

Comments
 (0)