Parcourir la source

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

kindlich il y a 4 ans
Parent
révision
5ab2f6bf08
Aucune clé connue n'a été trouvée dans la base pour cette signature

+ 27
- 0
ScriptingExample/src/test/java/org/openzen/zenscript/scriptingexample/tests/actual_test/packages/SamePackage.java Voir le fichier

@@ -0,0 +1,27 @@
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 Voir le fichier

@@ -4,11 +4,12 @@ import java.util.*;
4 4
 
5 5
 public class ScriptBuilder {
6 6
     
7
-    private final List<String> scripts;
7
+    private final Map<String, String> scriptNameToScript;
8 8
     private StringJoiner currentScriptJoiner;
9
+    private String currentScriptName;
9 10
     
10 11
     private ScriptBuilder() {
11
-        scripts = new ArrayList<>();
12
+        scriptNameToScript = new HashMap<>();
12 13
         startNewScript();
13 14
     }
14 15
     
@@ -26,20 +27,29 @@ public class ScriptBuilder {
26 27
     }
27 28
     
28 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 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 44
         return this;
35 45
     }
36 46
     
37 47
     public void appendScriptsToTest(ZenCodeTest test) {
38 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 55
     public void execute(ZenCodeTest test, LogTolerance logTolerance) {

+ 8
- 2
ScriptingExample/src/test/java/org/openzen/zenscript/scriptingexample/tests/helpers/ZenCodeTest.java Voir le fichier

@@ -10,6 +10,7 @@ import org.openzen.zenscript.parser.*;
10 10
 import org.openzen.zenscript.scriptingexample.tests.*;
11 11
 
12 12
 import java.util.*;
13
+import java.util.stream.*;
13 14
 
14 15
 
15 16
 public abstract class ZenCodeTest {
@@ -28,6 +29,7 @@ public abstract class ZenCodeTest {
28 29
     public void beforeEach() throws CompileException {
29 30
         this.logger = new ZenCodeTestLogger();
30 31
         this.engine = new ScriptingEngine(logger);
32
+        engine.debug = true;
31 33
         this.testModule = engine.createNativeModule("test_module", "org.openzen.zenscript.scripting_tests");
32 34
         SharedGlobals.currentlyActiveLogger = logger;
33 35
         
@@ -65,8 +67,12 @@ public abstract class ZenCodeTest {
65 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…
Annuler
Enregistrer