Browse Source

Adds tests to check that functions in the same package can be read by each other

kindlich 4 years ago
parent
commit
5ab2f6bf08
No known key found for this signature in database

+ 27
- 0
ScriptingExample/src/test/java/org/openzen/zenscript/scriptingexample/tests/actual_test/packages/SamePackage.java View File

1
+package org.openzen.zenscript.scriptingexample.tests.actual_test.packages;
2
+
3
+import org.junit.jupiter.api.*;
4
+import org.openzen.zenscript.scriptingexample.tests.helpers.*;
5
+
6
+public class SamePackage extends ZenCodeTest {
7
+    
8
+    @Test
9
+    public void definitionsInSamePackageAreAccessible() {
10
+        addScript("public function doSomething() as string => 'Hello World';", "a.zs");
11
+        addScript("println(doSomething());", "b.zs");
12
+        
13
+        executeEngine();
14
+        logger.assertPrintOutputSize(1);
15
+        logger.assertPrintOutput(0, "Hello World");
16
+    }
17
+    
18
+    @Test
19
+    public void definitionsInSamePackageAreAccessible_2() {
20
+        addScript("public function doSomething() as string => 'Hello World';", "some/test/package/a.zs");
21
+        addScript("println(doSomething());", "some/test/package/b.zs");
22
+    
23
+        executeEngine();
24
+        logger.assertPrintOutputSize(1);
25
+        logger.assertPrintOutput(0, "Hello World");
26
+    }
27
+}

+ 18
- 8
ScriptingExample/src/test/java/org/openzen/zenscript/scriptingexample/tests/helpers/ScriptBuilder.java View File

4
 
4
 
5
 public class ScriptBuilder {
5
 public class ScriptBuilder {
6
     
6
     
7
-    private final List<String> scripts;
7
+    private final Map<String, String> scriptNameToScript;
8
     private StringJoiner currentScriptJoiner;
8
     private StringJoiner currentScriptJoiner;
9
+    private String currentScriptName;
9
     
10
     
10
     private ScriptBuilder() {
11
     private ScriptBuilder() {
11
-        scripts = new ArrayList<>();
12
+        scriptNameToScript = new HashMap<>();
12
         startNewScript();
13
         startNewScript();
13
     }
14
     }
14
     
15
     
26
     }
27
     }
27
     
28
     
28
     public ScriptBuilder startNewScript() {
29
     public ScriptBuilder startNewScript() {
29
-        if(currentScriptJoiner != null) {
30
-            scripts.add(currentScriptJoiner.toString());
30
+        return startNewScript(null);
31
+    }
32
+    
33
+    public ScriptBuilder startNewScript(String fileName) {
34
+        if(currentScriptJoiner != null && currentScriptJoiner.length() != 0) {
35
+            scriptNameToScript.put(currentScriptName, currentScriptJoiner.toString());
31
         }
36
         }
32
-        
37
+    
33
         currentScriptJoiner = new StringJoiner(System.lineSeparator());
38
         currentScriptJoiner = new StringJoiner(System.lineSeparator());
39
+        if(fileName == null) {
40
+            currentScriptName = "test_script_" + (scriptNameToScript.size() + 1) + ".zs";
41
+        } else {
42
+            currentScriptName = fileName;
43
+        }
34
         return this;
44
         return this;
35
     }
45
     }
36
     
46
     
37
     public void appendScriptsToTest(ZenCodeTest test) {
47
     public void appendScriptsToTest(ZenCodeTest test) {
38
         startNewScript();
48
         startNewScript();
39
     
49
     
40
-        for(String script : scripts) {
41
-            test.addScript(script);
42
-        }
50
+        scriptNameToScript.forEach((name, content) -> {
51
+            test.addScript(content, name);
52
+        });
43
     }
53
     }
44
     
54
     
45
     public void execute(ZenCodeTest test, LogTolerance logTolerance) {
55
     public void execute(ZenCodeTest test, LogTolerance logTolerance) {

+ 8
- 2
ScriptingExample/src/test/java/org/openzen/zenscript/scriptingexample/tests/helpers/ZenCodeTest.java View File

10
 import org.openzen.zenscript.scriptingexample.tests.*;
10
 import org.openzen.zenscript.scriptingexample.tests.*;
11
 
11
 
12
 import java.util.*;
12
 import java.util.*;
13
+import java.util.stream.*;
13
 
14
 
14
 
15
 
15
 public abstract class ZenCodeTest {
16
 public abstract class ZenCodeTest {
28
     public void beforeEach() throws CompileException {
29
     public void beforeEach() throws CompileException {
29
         this.logger = new ZenCodeTestLogger();
30
         this.logger = new ZenCodeTestLogger();
30
         this.engine = new ScriptingEngine(logger);
31
         this.engine = new ScriptingEngine(logger);
32
+        engine.debug = true;
31
         this.testModule = engine.createNativeModule("test_module", "org.openzen.zenscript.scripting_tests");
33
         this.testModule = engine.createNativeModule("test_module", "org.openzen.zenscript.scripting_tests");
32
         SharedGlobals.currentlyActiveLogger = logger;
34
         SharedGlobals.currentlyActiveLogger = logger;
33
         
35
         
65
         logger.setEngineComplete();
67
         logger.setEngineComplete();
66
     }
68
     }
67
     
69
     
68
-    public void addScript(String context) {
69
-        sourceFiles.add(new LiteralSourceFile("test_script_" + sourceFiles.size() + ".zs", context));
70
+    public void addScript(String content) {
71
+        addScript(content, "test_script_" + sourceFiles.size() + ".zs");
72
+    }
73
+    
74
+    public void addScript(String content, String name) {
75
+        sourceFiles.add(new LiteralSourceFile(name, content));
70
     }
76
     }
71
     
77
     
72
     
78
     

Loading…
Cancel
Save