2 Commits

Author SHA1 Message Date
  kindlich f4639923f8
Check and escape more special Characters 4 years ago
  kindlich cfbaaf9f9b
Tests: Make ScriptBuilder use a LinkedHashMap to preserve file orde 4 years ago

+ 13
- 2
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaCompiler.java View File

@@ -17,7 +17,9 @@ import org.openzen.zenscript.javabytecode.compiler.definitions.*;
17 17
 import org.openzen.zenscript.javashared.*;
18 18
 import org.openzen.zenscript.javashared.prepare.*;
19 19
 
20
+import java.io.*;
20 21
 import java.util.*;
22
+import java.util.stream.*;
21 23
 
22 24
 /**
23 25
  * @author Hoofdgebruiker
@@ -139,8 +141,17 @@ public class JavaCompiler {
139 141
 		if (filename == null) {
140 142
 			return "generatedBlock" + (generatedScriptBlockCounter++);
141 143
 		} else {
142
-			// TODO: remove special characters
143
-			return filename.substring(0, filename.lastIndexOf('.')).replace("/", "_");
144
+            // TODO: find all special characters
145
+            final String specialCharRegex = Stream.of('/', '\\', '.', ';')
146
+                    .filter(character -> character != File.separatorChar)
147
+                    .map(String::valueOf)
148
+                    .collect(Collectors.joining("", "[", "]"));
149
+            
150
+			return filename
151
+                    .substring(0, filename.lastIndexOf('.')) //remove the .zs part
152
+                    .replaceAll(specialCharRegex, "_")
153
+                    .replace('[', '_')
154
+                    .replace(File.separatorChar, '/');
144 155
 		}
145 156
 	}
146 157
 

+ 29
- 0
ScriptingExample/src/test/java/org/openzen/zenscript/scriptingexample/tests/actual_test/file_names/FileNamesAndDigits.java View File

@@ -0,0 +1,29 @@
1
+package org.openzen.zenscript.scriptingexample.tests.actual_test.file_names;
2
+
3
+import org.junit.jupiter.api.*;
4
+import org.openzen.zenscript.scriptingexample.tests.helpers.*;
5
+
6
+public class FileNamesAndDigits extends ZenCodeTest {
7
+    
8
+    @Test
9
+    public void TestThatAFileMayConsistOnlyOfDigits() {
10
+        ScriptBuilder.create()
11
+                .startNewScript("123456789.zs")
12
+                .add("println('Hello World');")
13
+                .execute(this);
14
+        
15
+        logger.assertPrintOutputSize(1);
16
+        logger.assertPrintOutput(0, "Hello World");
17
+    }
18
+    
19
+    @Test
20
+    public void TestThatAFileMayConsistDotsAndDigits() {
21
+        ScriptBuilder.create()
22
+                .startNewScript("12345678.9.zs")
23
+                .add("println('Hello World');")
24
+                .execute(this);
25
+        
26
+        logger.assertPrintOutputSize(1);
27
+        logger.assertPrintOutput(0, "Hello World");
28
+    }
29
+}

+ 75
- 0
ScriptingExample/src/test/java/org/openzen/zenscript/scriptingexample/tests/actual_test/file_names/FileNamesAndSpecialCharacters.java View File

@@ -0,0 +1,75 @@
1
+package org.openzen.zenscript.scriptingexample.tests.actual_test.file_names;
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
+import java.io.*;
9
+
10
+public class FileNamesAndSpecialCharacters extends ZenCodeTest {
11
+    
12
+    /**
13
+     * All the characters that we test.
14
+     */
15
+    public static char[] specialCharsToTest() {
16
+        return new char[]{'.', '1', '%', '$', '_', '!', '/', '\\', '\0', '\1', ' ', ';', ':', '-', '|', '[', ']'};
17
+    }
18
+    
19
+    /**
20
+     * We disable the debug mode here because some of the filenames are invalid filenames in Windows/Unix.
21
+     * Since debug mode would try to write them out we disable it here.
22
+     */
23
+    @BeforeEach
24
+    public void disableDebug() {
25
+        engine.debug = false;
26
+    }
27
+    
28
+    @ParameterizedTest(name = "[{index}] Checking special Character '{0}'")
29
+    @MethodSource("specialCharsToTest")
30
+    public void TestThatFilenameCanContainCharacter(char characterToTest) {
31
+        
32
+        ScriptBuilder.create()
33
+                .startNewScript(String.format("t%1$se%1$ss%1$st.zs", characterToTest))
34
+                .add("println('Hello World');")
35
+                .execute(this);
36
+        
37
+        logger.assertNoWarnings();
38
+        logger.assertNoErrors();
39
+        logger.assertPrintOutputSize(1);
40
+        logger.assertPrintOutput(0, "Hello World");
41
+    }
42
+    
43
+    @ParameterizedTest
44
+    @MethodSource(value = "specialCharsToTest")
45
+    public void TestThatFilesWithSpecialCharacterFileNameAreAccessibleInsideOtherScripts(char characterToTest) {
46
+        ScriptBuilder.create()
47
+                .startNewScript(String.format("t%1$se%1$ss%1$st_1.zs", characterToTest))
48
+                .add("public function getTheString() as string => 'Hello World';")
49
+                .startNewScript(String.format("t%1$se%1$ss%1$st_2.zs", characterToTest))
50
+                .add("println(getTheString());")
51
+                .execute(this);
52
+        
53
+        logger.assertNoWarnings();
54
+        logger.assertNoErrors();
55
+        logger.assertPrintOutputSize(1);
56
+        logger.assertPrintOutput(0, "Hello World");
57
+    }
58
+    
59
+    @ParameterizedTest
60
+    @MethodSource(value = "specialCharsToTest")
61
+    public void TestThatFilesWithSpecialCharactersCanBePutInSubFolders(char characterToTest){
62
+        //noinspection SpellCheckingInspection
63
+        ScriptBuilder.create()
64
+                .startNewScript(String.format("some%2$sfolder%2$st%1$se%1$ss%1$st_1.zs", characterToTest, File.separatorChar))
65
+                .add("public function getTheString() as string => 'Hello World';")
66
+                .startNewScript(String.format("some%2$sfolder%2$st%1$se%1$ss%1$st_2.zs", characterToTest, File.separatorChar))
67
+                .add("println(getTheString());")
68
+                .execute(this);
69
+    
70
+        logger.assertNoWarnings();
71
+        logger.assertNoErrors();
72
+        logger.assertPrintOutputSize(1);
73
+        logger.assertPrintOutput(0, "Hello World");
74
+    }
75
+}

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

@@ -9,7 +9,7 @@ public class ScriptBuilder {
9 9
     private String currentScriptName;
10 10
     
11 11
     private ScriptBuilder() {
12
-        scriptNameToScript = new HashMap<>();
12
+        scriptNameToScript = new LinkedHashMap<>();
13 13
         startNewScript();
14 14
     }
15 15
     

Loading…
Cancel
Save