2 Коміти

Автор SHA1 Повідомлення Дата
  kindlich 5ab2f6bf08
Adds tests to check that functions in the same package can be read by each other 4 роки тому
  kindlich 6b718fa47e
Two more tests 4 роки тому

+ 1
- 1
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Main.java Переглянути файл

@@ -36,7 +36,7 @@ public class Main {
36 36
                 .filter(File::isFile)
37 37
                 .filter(f -> f.getName().endsWith(".zs"))
38 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 40
                 .toArray(SourceFile[]::new);
41 41
         
42 42
         final PrefixedBracketParser parser = new PrefixedBracketParser(null);

+ 28
- 0
ScriptingExample/src/test/java/org/openzen/zenscript/scriptingexample/tests/actual_test/arrays/ArrayOperators.java Переглянути файл

@@ -0,0 +1,28 @@
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 Переглянути файл

@@ -0,0 +1,16 @@
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 Переглянути файл

@@ -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 Переглянути файл

@@ -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 Переглянути файл

@@ -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
     

Завантаження…
Відмінити
Зберегти