2 Commits

Author SHA1 Message Date
  kindlich 5ab2f6bf08
Adds tests to check that functions in the same package can be read by each other 4 years ago
  kindlich 6b718fa47e
Two more tests 4 years ago

+ 1
- 1
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Main.java View File

36
                 .filter(File::isFile)
36
                 .filter(File::isFile)
37
                 .filter(f -> f.getName().endsWith(".zs"))
37
                 .filter(f -> f.getName().endsWith(".zs"))
38
                 .filter(f -> !f.getAbsolutePath().contains("nope"))
38
                 .filter(f -> !f.getAbsolutePath().contains("nope"))
39
-                .map(f -> new FileSourceFile(f.getName(), f))
39
+                .map(f -> new FileSourceFile(f.getAbsolutePath().substring(inputDirectory.getAbsolutePath().length() + 1), f))
40
                 .toArray(SourceFile[]::new);
40
                 .toArray(SourceFile[]::new);
41
         
41
         
42
         final PrefixedBracketParser parser = new PrefixedBracketParser(null);
42
         final PrefixedBracketParser parser = new PrefixedBracketParser(null);

+ 28
- 0
ScriptingExample/src/test/java/org/openzen/zenscript/scriptingexample/tests/actual_test/arrays/ArrayOperators.java View File

1
+package org.openzen.zenscript.scriptingexample.tests.actual_test.arrays;
2
+
3
+import org.junit.jupiter.api.*;
4
+import org.openzen.zenscript.scriptingexample.tests.helpers.*;
5
+
6
+public class ArrayOperators extends ZenCodeTest {
7
+    @Test
8
+    public void containsReturnsTrueForMatch() {
9
+        ScriptBuilder.create()
10
+                .add("var array = ['a', 'b', 'c'] as string[];")
11
+                .add("println(('a' in array) as string);")
12
+                .execute(this);
13
+        
14
+        logger.assertPrintOutputSize(1);
15
+        logger.assertPrintOutput(0, "true");
16
+    }
17
+    
18
+    @Test
19
+    public void containsReturnsFalseForNonMatch() {
20
+        ScriptBuilder.create()
21
+                .add("var array = ['a', 'b', 'c'] as string[];")
22
+                .add("println(('d' in array) as string);")
23
+                .execute(this);
24
+        
25
+        logger.assertPrintOutputSize(1);
26
+        logger.assertPrintOutput(0, "false");
27
+    }
28
+}

+ 16
- 0
ScriptingExample/src/test/java/org/openzen/zenscript/scriptingexample/tests/actual_test/expansions/StringExpansions.java View File

1
+package org.openzen.zenscript.scriptingexample.tests.actual_test.expansions;
2
+
3
+import org.junit.jupiter.api.*;
4
+import org.openzen.zenscript.scriptingexample.tests.helpers.*;
5
+
6
+public class StringExpansions extends ZenCodeTest {
7
+    @Test
8
+    public void toUpperCaseMethod() {
9
+        ScriptBuilder.create()
10
+                .add("println('hello world'.toUpperCase());")
11
+                .execute(this);
12
+    
13
+        logger.assertPrintOutputSize(1);
14
+        logger.assertPrintOutput(0, "Hello World".toUpperCase());
15
+    }
16
+}

+ 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