Selaa lähdekoodia

Started with Unit tests

kindlich 4 vuotta sitten
vanhempi
commit
6a77f27c9f
No known key found for this signature in database

+ 9
- 0
ScriptingExample/build.gradle Näytä tiedosto

@@ -15,4 +15,13 @@ if (!hasProperty('mainClass')) {
15 15
 
16 16
 dependencies {
17 17
     compile project(':JavaIntegration')
18
+    testCompile group: "org.junit.jupiter", name: "junit-jupiter", version: "5.4.2"
19
+    testRuntime group: "org.junit.jupiter", name: "junit-jupiter", version: "5.4.2"
18 20
 }
21
+
22
+test {
23
+    useJUnitPlatform()
24
+    testLogging{
25
+        events "PASSED", "FAILED", "SKIPPED"
26
+    }
27
+}

+ 14
- 0
ScriptingExample/src/test/java/org/openzen/zenscript/scriptingexample/tests/SharedGlobals.java Näytä tiedosto

@@ -0,0 +1,14 @@
1
+package org.openzen.zenscript.scriptingexample.tests;
2
+
3
+import org.openzen.zencode.java.*;
4
+import org.openzen.zenscript.scriptingexample.tests.helpers.*;
5
+
6
+public class SharedGlobals {
7
+    
8
+    public static ZenCodeTestLogger currentlyActiveLogger;
9
+    
10
+    @ZenCodeGlobals.Global
11
+    public static void println(String s) {
12
+        currentlyActiveLogger.logPrintln(s);
13
+    }
14
+}

+ 16
- 0
ScriptingExample/src/test/java/org/openzen/zenscript/scriptingexample/tests/actual_test/HelloWorldTest.java Näytä tiedosto

@@ -0,0 +1,16 @@
1
+package org.openzen.zenscript.scriptingexample.tests.actual_test;
2
+
3
+import org.junit.jupiter.api.*;
4
+import org.openzen.zenscript.scriptingexample.tests.helpers.*;
5
+
6
+public class HelloWorldTest extends ZenCodeTest {
7
+    
8
+    @Test
9
+    public void helloWorld() {
10
+        addScript("println('hello world');");
11
+        executeEngine();
12
+        
13
+        logger.assertPrintOutputSize(1);
14
+        logger.assertPrintOutput(0, "hello world");
15
+    }
16
+}

+ 28
- 0
ScriptingExample/src/test/java/org/openzen/zenscript/scriptingexample/tests/actual_test/arithmethic_operators/AdditionTest.java Näytä tiedosto

@@ -0,0 +1,28 @@
1
+package org.openzen.zenscript.scriptingexample.tests.actual_test.arithmethic_operators;
2
+
3
+import org.junit.jupiter.api.*;
4
+import org.junit.jupiter.params.*;
5
+import org.junit.jupiter.params.provider.*;
6
+import org.openzen.zenscript.scriptingexample.tests.helpers.*;
7
+
8
+public class AdditionTest extends ZenCodeTest {
9
+    
10
+    @ParameterizedTest(name = "{0} + {1}")
11
+    @CsvSource({"0,0", "0,1", "10,10", "-1,1", "815,4711"})
12
+    public void testSomeAdditions(int i, int j) {
13
+        addScript(String.format("println(%d + %d);", i, j));
14
+        executeEngine();
15
+        
16
+        logger.assertPrintOutputSize(1);
17
+        logger.assertPrintOutput(0, String.valueOf(i + j));
18
+    }
19
+    
20
+    @Test
21
+    public void testChainedAdditions() {
22
+        addScript("println(1+2+3+4+5+6+7+8+9+10);");
23
+        executeEngine();
24
+        
25
+        logger.assertPrintOutputSize(1);
26
+        logger.assertPrintOutput(0, String.valueOf(55));
27
+    }
28
+}

+ 18
- 0
ScriptingExample/src/test/java/org/openzen/zenscript/scriptingexample/tests/actual_test/control_structures/LoopTest.java Näytä tiedosto

@@ -0,0 +1,18 @@
1
+package org.openzen.zenscript.scriptingexample.tests.actual_test.control_structures;
2
+
3
+import org.junit.jupiter.api.*;
4
+import org.openzen.zenscript.scriptingexample.tests.helpers.*;
5
+
6
+public class LoopTest extends ZenCodeTest {
7
+    
8
+    @Test
9
+    public void validateOutput() {
10
+        addScript("for i in 0 .. 100 println(i);");
11
+        executeEngine();
12
+        
13
+        logger.assertPrintOutputSize(100);
14
+        for(int i = 0; i < 100; i++) {
15
+            logger.assertPrintOutput(i, String.valueOf(i));
16
+        }
17
+    }
18
+}

+ 33
- 0
ScriptingExample/src/test/java/org/openzen/zenscript/scriptingexample/tests/helpers/FunctionParameterList.java Näytä tiedosto

@@ -0,0 +1,33 @@
1
+package org.openzen.zenscript.scriptingexample.tests.helpers;
2
+
3
+
4
+import org.openzen.zenscript.codemodel.*;
5
+
6
+import java.util.*;
7
+
8
+public class FunctionParameterList {
9
+    private final List<FunctionParameter> parameters;
10
+    private final List<Object> values;
11
+    
12
+    public FunctionParameterList() {
13
+        this.parameters = new ArrayList<>();
14
+        this.values = new ArrayList<>();
15
+    }
16
+    
17
+    public void addParameter(FunctionParameter parameter, Object value) {
18
+        parameters.add(parameter);
19
+        values.add(value);
20
+    }
21
+    
22
+    public FunctionParameter[] getParameters() {
23
+        return parameters.toArray(FunctionParameter.NONE);
24
+    }
25
+    
26
+    public Map<FunctionParameter, Object> getParameterMap() {
27
+        final Map<FunctionParameter, Object> results = new HashMap<>();
28
+        for(int i = 0; i < parameters.size(); i++) {
29
+            results.put(parameters.get(i), values.get(i));
30
+        }
31
+        return results;
32
+    }
33
+}

+ 80
- 0
ScriptingExample/src/test/java/org/openzen/zenscript/scriptingexample/tests/helpers/ZenCodeTest.java Näytä tiedosto

@@ -0,0 +1,80 @@
1
+package org.openzen.zenscript.scriptingexample.tests.helpers;
2
+
3
+import org.junit.jupiter.api.*;
4
+import org.openzen.zencode.java.*;
5
+import org.openzen.zencode.shared.*;
6
+import org.openzen.zenscript.codemodel.*;
7
+import org.openzen.zenscript.codemodel.type.*;
8
+import org.openzen.zenscript.lexer.*;
9
+import org.openzen.zenscript.parser.*;
10
+import org.openzen.zenscript.scriptingexample.tests.*;
11
+import org.openzen.zenscript.scriptingexample.tests.helpers.*;
12
+
13
+import java.util.*;
14
+
15
+
16
+public abstract class ZenCodeTest {
17
+    
18
+    private final List<SourceFile> sourceFiles;
19
+    protected ScriptingEngine engine;
20
+    protected JavaNativeModule testModule;
21
+    protected ZenCodeTestLogger logger;
22
+    
23
+    protected ZenCodeTest() {
24
+        sourceFiles = new ArrayList<>();
25
+    }
26
+    
27
+    
28
+    @BeforeEach
29
+    public void beforeEach() throws CompileException {
30
+        this.logger = new ZenCodeTestLogger();
31
+        this.engine = new ScriptingEngine(logger);
32
+        this.testModule = engine.createNativeModule("test_module", "org.openzen.zenscript.scripting_tests");
33
+        SharedGlobals.currentlyActiveLogger = logger;
34
+        
35
+        getRequiredClasses().stream().distinct().forEach(requiredClass -> {
36
+            testModule.addGlobals(requiredClass);
37
+            testModule.addClass(requiredClass);
38
+        });
39
+        engine.registerNativeProvided(testModule);
40
+    }
41
+    
42
+    public void executeEngine() {
43
+        try {
44
+            final FunctionParameterList parameters = getParameters();
45
+            final SemanticModule script_tests = engine.createScriptedModule("script_tests", sourceFiles
46
+                    .toArray(new SourceFile[0]), getBEP(), parameters.getParameters());
47
+            
48
+            Assertions.assertTrue(script_tests.isValid(), "Scripts are not valid!");
49
+            engine.registerCompiled(script_tests);
50
+            engine.run(parameters.getParameterMap());
51
+        } catch(ParseException e) {
52
+            e.printStackTrace();
53
+            Assertions.fail("Error in Engine execution", e);
54
+        }
55
+        logger.setEngineComplete();
56
+    }
57
+    
58
+    public void addScript(String context) {
59
+        sourceFiles.add(new LiteralSourceFile("test_script_" + sourceFiles.size() + ".zs", context));
60
+    }
61
+    
62
+    
63
+    public BracketExpressionParser getBEP() {
64
+        return null;
65
+    }
66
+    
67
+    public FunctionParameterList getParameters() {
68
+        final FunctionParameterList functionParameterList = new FunctionParameterList();
69
+        final StoredType stringArrayType = engine.registry.getArray(StringTypeID.AUTO, 1).stored();
70
+        FunctionParameter args = new FunctionParameter(stringArrayType, "args");
71
+        functionParameterList.addParameter(args, new String[]{"a", "b", "c"});
72
+        return functionParameterList;
73
+    }
74
+    
75
+    public List<Class<?>> getRequiredClasses() {
76
+        final ArrayList<Class<?>> result = new ArrayList<>();
77
+        result.add(SharedGlobals.class);
78
+        return result;
79
+    }
80
+}

+ 43
- 0
ScriptingExample/src/test/java/org/openzen/zenscript/scriptingexample/tests/helpers/ZenCodeTestLogger.java Näytä tiedosto

@@ -0,0 +1,43 @@
1
+package org.openzen.zenscript.scriptingexample.tests.helpers;
2
+
3
+import org.junit.jupiter.api.*;
4
+import org.openzen.zencode.java.logger.*;
5
+
6
+import java.util.*;
7
+
8
+public class ZenCodeTestLogger extends ScriptingEngineStreamLogger {
9
+    
10
+    private static final boolean logDebug = false;
11
+    private final List<String> printlnOutputs = new ArrayList<>();
12
+    private boolean isEngineComplete = false;
13
+    
14
+    @Override
15
+    public void debug(String message) {
16
+        if(logDebug) {
17
+            super.debug(message);
18
+        }
19
+    }
20
+    
21
+    public void logPrintln(String line) {
22
+        info(line);
23
+        this.printlnOutputs.addAll(Arrays.asList(line.split(System.lineSeparator())));
24
+    }
25
+    
26
+    void setEngineComplete() {
27
+        isEngineComplete = true;
28
+    }
29
+    
30
+    public void assertPrintOutput(int line, String content) {
31
+        if(!isEngineComplete) {
32
+            Assertions.fail("Trying to call an assertion before the engine ran, probably a fault in the test!");
33
+        }
34
+        Assertions.assertEquals(content, printlnOutputs.get(line));
35
+    }
36
+    
37
+    public void assertPrintOutputSize(int size) {
38
+        if(!isEngineComplete) {
39
+            Assertions.fail("Trying to call an assertion before the engine ran, probably a fault in the test!");
40
+        }
41
+        Assertions.assertEquals(size, printlnOutputs.size());
42
+    }
43
+}

Loading…
Peruuta
Tallenna