|
| 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 | +} |
0 commit comments