From 66bf40bfbcf1d49e538447a65fd8ab2299715eff Mon Sep 17 00:00:00 2001 From: Victor Date: Tue, 28 Jun 2016 12:02:31 +0300 Subject: [PATCH] =?UTF-8?q?=D0=AE=D0=BD=D0=B8=D1=82-=D1=82=D0=B5=D1=81?= =?UTF-8?q?=D1=82=D1=8B=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D1=80=D0=BE=D0=B3?= =?UTF-8?q?=D1=80=D0=B0=D0=BC=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../annimon/ownlang/parser/ProgramsTest.java | 101 ++++++++++++++++++ .../expressions/assignmentExpression.own | 30 ++++++ .../expressions/binaryExpressionOnNumbers.own | 37 +++++++ .../expressions/binaryExpressionOnStrings.own | 8 ++ .../resources/expressions/binaryUnaryExpr.own | 27 +++++ .../expressions/functionReference.own | 6 ++ .../expressions/unaryExpressionOnStrings.own | 3 + .../resources/expressions/varFuncSameName.own | 5 + test/resources/modules/date/compareDates.own | 7 ++ test/resources/modules/date/dateFormat.own | 6 ++ test/resources/modules/date/dateParse.own | 9 ++ test/resources/modules/date/newDate.own | 12 +++ test/resources/modules/files/files.own | 28 +++++ test/resources/modules/functional/chain.own | 11 ++ test/resources/other/recursion.own | 8 ++ test/resources/other/scope.own | 10 ++ test/resources/other/types.own | 3 + 17 files changed, 311 insertions(+) create mode 100644 test/com/annimon/ownlang/parser/ProgramsTest.java create mode 100644 test/resources/expressions/assignmentExpression.own create mode 100644 test/resources/expressions/binaryExpressionOnNumbers.own create mode 100644 test/resources/expressions/binaryExpressionOnStrings.own create mode 100644 test/resources/expressions/binaryUnaryExpr.own create mode 100644 test/resources/expressions/functionReference.own create mode 100644 test/resources/expressions/unaryExpressionOnStrings.own create mode 100644 test/resources/expressions/varFuncSameName.own create mode 100644 test/resources/modules/date/compareDates.own create mode 100644 test/resources/modules/date/dateFormat.own create mode 100644 test/resources/modules/date/dateParse.own create mode 100644 test/resources/modules/date/newDate.own create mode 100644 test/resources/modules/files/files.own create mode 100644 test/resources/modules/functional/chain.own create mode 100644 test/resources/other/recursion.own create mode 100644 test/resources/other/scope.own create mode 100644 test/resources/other/types.own diff --git a/test/com/annimon/ownlang/parser/ProgramsTest.java b/test/com/annimon/ownlang/parser/ProgramsTest.java new file mode 100644 index 0000000..9811394 --- /dev/null +++ b/test/com/annimon/ownlang/parser/ProgramsTest.java @@ -0,0 +1,101 @@ +package com.annimon.ownlang.parser; + +import com.annimon.ownlang.lib.Functions; +import com.annimon.ownlang.lib.NumberValue; +import com.annimon.ownlang.lib.Variables; +import com.annimon.ownlang.parser.ast.FunctionDefineStatement; +import com.annimon.ownlang.parser.ast.Statement; +import com.annimon.ownlang.parser.ast.Visitor; +import com.annimon.ownlang.parser.visitors.AbstractVisitor; +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.junit.Assert; +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +@RunWith(value = Parameterized.class) +public class ProgramsTest { + + private static final String RES_DIR = "test/resources"; + + private final String programPath; + + public ProgramsTest(String programPath) { + this.programPath = programPath; + } + + @Parameters(name = "{index}: {0}") + public static Collection data() { + final File resDir = new File(RES_DIR); + return scanDirectory(resDir) + .map(f -> f.getPath()) + .collect(Collectors.toList()); + } + + private static Stream scanDirectory(File dir) { + return Arrays.stream(dir.listFiles()) + .flatMap(file -> { + if (file.isDirectory()) { + return scanDirectory(file); + } + return Stream.of(file); + }) + .filter(f -> f.getName().endsWith(".own")); + } + + @Before + public void initialize() { + Variables.clear(); + Functions.getFunctions().clear(); + // Let's mock junit methods as ounit functions + Functions.set("assertEquals", (args) -> { + assertEquals(args[0], args[1]); + return NumberValue.ONE; + }); + Functions.set("assertNotEquals", (args) -> { + assertNotEquals(args[0], args[1]); + return NumberValue.ONE; + }); + Functions.set("assertSameType", (args) -> { + assertEquals(args[0].type(), args[1].type()); + return NumberValue.ONE; + }); + Functions.set("assertTrue", (args) -> { + assertTrue(args[0].asInt() != 0); + return NumberValue.ONE; + }); + Functions.set("assertFalse", (args) -> { + assertFalse(args[0].asInt() == 0); + return NumberValue.ONE; + }); + } + + @Test + public void testProgram() throws IOException { + final String source = SourceLoader.readSource(programPath); + final Statement s = Parser.parse(Lexer.tokenize(source)); + try { + s.execute(); + s.accept(testFunctionsExecutor); + } catch (Exception oae) { + Assert.fail(oae.toString()); + } + } + + private static Visitor testFunctionsExecutor = new AbstractVisitor() { + @Override + public void visit(FunctionDefineStatement s) { + if (s.name.startsWith("test")) { + Functions.get(s.name).execute(); + } + } + }; +} diff --git a/test/resources/expressions/assignmentExpression.own b/test/resources/expressions/assignmentExpression.own new file mode 100644 index 0000000..8cca830 --- /dev/null +++ b/test/resources/expressions/assignmentExpression.own @@ -0,0 +1,30 @@ +def testSimpleAssignment() { + a = 8 + b = "str" + assertEquals(8, a) + assertEquals("str", b) +} + +def testPrefixIncrement() { + a = 8 + assertEquals(9, ++a) + assertEquals(9, a) +} + +def testPostfixIncrement() { + a = 8 + assertEquals(8, a++) + assertEquals(9, a) +} + +def testPrefixDecrement() { + a = 8 + assertEquals(7, --a) + assertEquals(7, a) +} + +def testPostfixDecrement() { + a = 8 + assertEquals(8, a--) + assertEquals(7, a) +} \ No newline at end of file diff --git a/test/resources/expressions/binaryExpressionOnNumbers.own b/test/resources/expressions/binaryExpressionOnNumbers.own new file mode 100644 index 0000000..0b6be70 --- /dev/null +++ b/test/resources/expressions/binaryExpressionOnNumbers.own @@ -0,0 +1,37 @@ +def testAdditionOnNumbers() { + assertEquals(4, 2 + 2) + assertEquals(6, 0 + 1 + 2 + 3) +} + +def testSubtractionOnNumbers() { + assertEquals(0, 2 - 2) + assertEquals(-6, 0 - 1 - 2 - 3) + assertEquals(110, 100 - (20 - 30)) +} + +def testMultiplicationOnNumbers() { + assertEquals(4, 2 * 2) + assertEquals(30, 5 * (-2 * -3)) +} + +def testDivisionOnNumbers() { + assertEquals(3, 6 / 2) + assertEquals(30, -900 / (60 / -2)) +} + +def testRemainder() { + assertEquals(2, 10 % 4) + assertEquals(5, 15 % (40 % 30)) +} + +def testAND() { + assertEquals(0x04, 0x04 & 0x0F) + assertEquals(0x00, 0x04 & 0x08) + assertEquals(8, 12 & 9) +} + +def testOR() { + assertEquals(12, 4 | 8) + assertEquals(0x0F, 3 | 12) + assertEquals(0x0E, 10 | 4) +} \ No newline at end of file diff --git a/test/resources/expressions/binaryExpressionOnStrings.own b/test/resources/expressions/binaryExpressionOnStrings.own new file mode 100644 index 0000000..6854ced --- /dev/null +++ b/test/resources/expressions/binaryExpressionOnStrings.own @@ -0,0 +1,8 @@ +def testStringConcatenation() { + assertEquals("22", "2" + "2") + assertEquals("22", "2" + 2) +} + +def testStringMultiplication() { + assertEquals("******", "*" * 6) +} \ No newline at end of file diff --git a/test/resources/expressions/binaryUnaryExpr.own b/test/resources/expressions/binaryUnaryExpr.own new file mode 100644 index 0000000..fdfe0d1 --- /dev/null +++ b/test/resources/expressions/binaryUnaryExpr.own @@ -0,0 +1,27 @@ +def testAdditionOnNumbers() { + assertEquals(6, 0 + 1 + 2 + 3) +} + +def testSubtractionOnNumbers() { + assertEquals(-6, 0 - 1 - 2 - 3) +} + +def testPrefixIncrement() { + a = 8 + assertEquals(9, ++a) + assertEquals(9, a) +} + +def testPostfixIncrement() { + a = 8 + assertEquals(8, a++) + assertEquals(9, a) +} + +def testStringReversing() { + assertEquals("tset", -"test") +} + +def testStringMultiplication() { + assertEquals("******", "*" * 6) +} \ No newline at end of file diff --git a/test/resources/expressions/functionReference.own b/test/resources/expressions/functionReference.own new file mode 100644 index 0000000..234def8 --- /dev/null +++ b/test/resources/expressions/functionReference.own @@ -0,0 +1,6 @@ +def func() = "function" +var = def() = "variable" +def consumer(x) = x(); + +assertEquals("function", consumer(::func)) +assertEquals("variable", consumer(var)) \ No newline at end of file diff --git a/test/resources/expressions/unaryExpressionOnStrings.own b/test/resources/expressions/unaryExpressionOnStrings.own new file mode 100644 index 0000000..06dfda0 --- /dev/null +++ b/test/resources/expressions/unaryExpressionOnStrings.own @@ -0,0 +1,3 @@ +def testStringReversing() { + assertEquals("tset", -"test") +} \ No newline at end of file diff --git a/test/resources/expressions/varFuncSameName.own b/test/resources/expressions/varFuncSameName.own new file mode 100644 index 0000000..8ecdebe --- /dev/null +++ b/test/resources/expressions/varFuncSameName.own @@ -0,0 +1,5 @@ +def function() = "function" +function = "variable" + +assertEquals("variable", function) +assertEquals("function", function()) \ No newline at end of file diff --git a/test/resources/modules/date/compareDates.own b/test/resources/modules/date/compareDates.own new file mode 100644 index 0000000..7db9f5d --- /dev/null +++ b/test/resources/modules/date/compareDates.own @@ -0,0 +1,7 @@ +use "date" + +def testCompareDates() { + assertTrue(newDate(2016, 04, 10) > newDate(2015, 03, 11)) + assertTrue(newDate(2012, 04, 10) < newDate(2015, 03, 11)) + assertTrue(newDate(2015, 03, 11, 0, 0, 0) == newDate(2015, 03, 11)) +} \ No newline at end of file diff --git a/test/resources/modules/date/dateFormat.own b/test/resources/modules/date/dateFormat.own new file mode 100644 index 0000000..6437f7a --- /dev/null +++ b/test/resources/modules/date/dateFormat.own @@ -0,0 +1,6 @@ +use "date" + +def testDateFormat() { + d = formatDate(newDate(2016, 04, 10), newFormat("yyyy/MM/dd HH:mm:ss")) + assertEquals("2016/05/10 00:00:00", d) +} \ No newline at end of file diff --git a/test/resources/modules/date/dateParse.own b/test/resources/modules/date/dateParse.own new file mode 100644 index 0000000..361cb9d --- /dev/null +++ b/test/resources/modules/date/dateParse.own @@ -0,0 +1,9 @@ +use "date" + +def testDateParse() { + d = parseDate("2016/05/10", newFormat("yyyy/MM/dd")) + assertEquals(2016, d.year) + assertEquals(4, d.month) + assertEquals(10, d.day) + assertEquals(0, d.hour) +} diff --git a/test/resources/modules/date/newDate.own b/test/resources/modules/date/newDate.own new file mode 100644 index 0000000..f33a352 --- /dev/null +++ b/test/resources/modules/date/newDate.own @@ -0,0 +1,12 @@ +use "date" + +def testNewDate() { + d = newDate(2016, 04, 10) + assertEquals(2016, d.year) + assertEquals(4, d.month) + assertEquals(10, d.day) + assertEquals(0, d.hour) + assertEquals(0, d.minute) + assertEquals(0, d.second) + assertEquals(0, d.millisecond) +} \ No newline at end of file diff --git a/test/resources/modules/files/files.own b/test/resources/modules/files/files.own new file mode 100644 index 0000000..f4591b8 --- /dev/null +++ b/test/resources/modules/files/files.own @@ -0,0 +1,28 @@ +use "files" +use "types" + +def testFiles() { + // writeLong + f = fopen("test.file", "wb") + writeLong(f, 1002003004005006007) + flush(f) + fclose(f) + + // append & writeFloat + fpNumber = 100200.3004005006007 + f = fopen("test.file", "wb+") + writeFloat(f, fpNumber) + flush(f) + fclose(f) + + f = fopen("test.file", "rb") + assertEquals(1002003004005006007, readLong(f)) + assertEquals(float(fpNumber), readFloat(f)) + assertEquals(-1, readInt(f)) // EOF + assertEquals(0, FILES_COMPARATOR(f, f)) + fclose(f) + + f = fopen("test.file", "i") + delete(f) + fclose(f) +} diff --git a/test/resources/modules/functional/chain.own b/test/resources/modules/functional/chain.own new file mode 100644 index 0000000..1969209 --- /dev/null +++ b/test/resources/modules/functional/chain.own @@ -0,0 +1,11 @@ +use "functional" + +def testFunctionalChain() { + data = [1,2,3,4,5,6,7] + result = chain(data, + ::filter, def(x) = x <= 4, + ::sortby, def(x) = -x, + ::map, def(x) = x * 2, + ) + assertEquals([8,6,4,2], result) +} diff --git a/test/resources/other/recursion.own b/test/resources/other/recursion.own new file mode 100644 index 0000000..ef20add --- /dev/null +++ b/test/resources/other/recursion.own @@ -0,0 +1,8 @@ +def testFibonacci() { + def fib(n) { + if n < 2 return n + return fib(n-2) + fib(n-1) + } + assertEquals(3, fib(4)) + assertEquals(21, fib(8)) +} \ No newline at end of file diff --git a/test/resources/other/scope.own b/test/resources/other/scope.own new file mode 100644 index 0000000..93aade7 --- /dev/null +++ b/test/resources/other/scope.own @@ -0,0 +1,10 @@ +def testScope() { + x = 5 + def func() { + assertEquals(5, x) + x += 10 + assertEquals(15, x) + } + func(); + assertEquals(15, x) +} \ No newline at end of file diff --git a/test/resources/other/types.own b/test/resources/other/types.own new file mode 100644 index 0000000..2932e42 --- /dev/null +++ b/test/resources/other/types.own @@ -0,0 +1,3 @@ +def testTypes() { + assertSameType(0, 0.0) +} \ No newline at end of file