Selaa lähdekoodia

Some more tests for basic class members

kindlich 4 vuotta sitten
vanhempi
commit
457cb2dbec
No known key found for this signature in database

+ 12
- 0
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/definitions/JavaMemberVisitor.java Näytä tiedosto

@@ -112,6 +112,18 @@ public class JavaMemberVisitor implements MemberVisitor<Void> {
112 112
 				constructorWriter.invokeSpecial(Type.getInternalName(Object.class), "<init>", "()V");
113 113
 			}
114 114
         }
115
+    
116
+        for(IDefinitionMember membersOfSameType : member.definition.members) {
117
+            if(membersOfSameType instanceof FieldMember) {
118
+                final FieldMember fieldMember = ((FieldMember) membersOfSameType);
119
+                final Expression initializer = fieldMember.initializer;
120
+                if(initializer != null) {
121
+                    constructorWriter.loadObject(0);
122
+                    initializer.accept(statementVisitor.expressionVisitor);
123
+                    constructorWriter.putField(context.getJavaField(fieldMember));
124
+                }
125
+            }
126
+        }
115 127
 
116 128
 		for (TypeParameter typeParameter : definition.typeParameters) {
117 129
 			final JavaTypeParameterInfo typeParameterInfo = javaModule.getTypeParameterInfo(typeParameter);

+ 90
- 0
ScriptingExample/src/test/java/org/openzen/zenscript/scriptingexample/tests/actual_test/classes/AddingClasses.java Näytä tiedosto

@@ -0,0 +1,90 @@
1
+package org.openzen.zenscript.scriptingexample.tests.actual_test.classes;
2
+
3
+import org.junit.jupiter.api.*;
4
+import org.openzen.zenscript.scriptingexample.tests.helpers.*;
5
+
6
+public class AddingClasses extends ZenCodeTest {
7
+    
8
+    @Test
9
+    public void emptyClassCompiles() {
10
+        ScriptBuilder.create().add("public class SomeClass {").add("}").execute(this);
11
+        logger.assertNoErrors();
12
+    }
13
+    
14
+    @Test
15
+    public void memberGettable() {
16
+        ScriptBuilder.create()
17
+                .add("public class SomeClass {")
18
+                .add("    public var x as string;")
19
+                .add("    public this(){x = 'Hello World';}")
20
+                .add("}")
21
+                .add("")
22
+                .add("println(new SomeClass().x);")
23
+                .execute(this);
24
+        
25
+        logger.assertNoErrors();
26
+        logger.assertPrintOutputSize(1);
27
+        logger.assertPrintOutput(0, "Hello World");
28
+    }
29
+    
30
+    @Test
31
+    public void functionCallable() {
32
+        ScriptBuilder.create()
33
+                .add("public class SomeClass {")
34
+                .add("    public this(){}")
35
+                .add("    public callMeMaybe() as void {println('Hello World!');}")
36
+                .add("}")
37
+                .add("")
38
+                .add("new SomeClass().callMeMaybe();")
39
+                .execute(this);
40
+        
41
+        logger.assertPrintOutputSize(1);
42
+        logger.assertPrintOutput(0, "Hello World!");
43
+    }
44
+    
45
+    @Test
46
+    public void classAccessibleFromOtherScript() {
47
+        ScriptBuilder.create()
48
+                .add("public class SomeClass {")
49
+                .add("    public this(){}")
50
+                .add("    public callMeMaybe() as void {println('Hello World!');}")
51
+                .add("}")
52
+                .add("")
53
+                .startNewScript()
54
+                .add("new SomeClass().callMeMaybe();")
55
+                .execute(this);
56
+        
57
+        logger.assertPrintOutputSize(1);
58
+        logger.assertPrintOutput(0, "Hello World!");
59
+    }
60
+    
61
+    @Test
62
+    public void fieldInitializerSetsField() {
63
+        ScriptBuilder.create()
64
+                .add("public class SomeClass {")
65
+                .add("    public var x as string = 'Hello World';")
66
+                .add("    public this(){}")
67
+                .add("}")
68
+                .add("")
69
+                .add("println(new SomeClass().x);")
70
+                .execute(this);
71
+        
72
+        logger.assertPrintOutputSize(1);
73
+        logger.assertPrintOutput(0, "Hello World");
74
+    }
75
+    
76
+    @Test
77
+    public void constructorOverridesFieldInitializer() {
78
+        ScriptBuilder.create()
79
+                .add("public class SomeClass {")
80
+                .add("    public var x as string = 'Hello World';")
81
+                .add("    public this(){this.x = 'Goodbye World';}")
82
+                .add("}")
83
+                .add("")
84
+                .add("println(new SomeClass().x);")
85
+                .execute(this);
86
+        
87
+        logger.assertPrintOutputSize(1);
88
+        logger.assertPrintOutput(0, "Goodbye World");
89
+    }
90
+}

+ 66
- 0
ScriptingExample/src/test/java/org/openzen/zenscript/scriptingexample/tests/helpers/ScriptBuilder.java Näytä tiedosto

@@ -0,0 +1,66 @@
1
+package org.openzen.zenscript.scriptingexample.tests.helpers;
2
+
3
+import java.util.*;
4
+
5
+public class ScriptBuilder {
6
+    
7
+    private final List<String> scripts;
8
+    private StringJoiner currentScriptJoiner;
9
+    
10
+    private ScriptBuilder() {
11
+        scripts = new ArrayList<>();
12
+        startNewScript();
13
+    }
14
+    
15
+    /**
16
+     * Creates a {@link StringJoiner} that merges with newlines.
17
+     * The only real reason for this to exist is that it allows code formatting to work properly lol
18
+     */
19
+    public static ScriptBuilder create() {
20
+        return new ScriptBuilder();
21
+    }
22
+    
23
+    public ScriptBuilder add(String line) {
24
+        currentScriptJoiner.add(line);
25
+        return this;
26
+    }
27
+    
28
+    public ScriptBuilder startNewScript() {
29
+        if(currentScriptJoiner != null) {
30
+            scripts.add(currentScriptJoiner.toString());
31
+        }
32
+        
33
+        currentScriptJoiner = new StringJoiner(System.lineSeparator());
34
+        return this;
35
+    }
36
+    
37
+    public void appendScriptsToTest(ZenCodeTest test) {
38
+        startNewScript();
39
+    
40
+        for(String script : scripts) {
41
+            test.addScript(script);
42
+        }
43
+    }
44
+    
45
+    public void execute(ZenCodeTest test, LogTolerance logTolerance) {
46
+        appendScriptsToTest(test);
47
+        test.executeEngine();
48
+        switch(logTolerance) {
49
+            case NO_WARNINGS:
50
+                test.logger.assertNoWarnings();
51
+            case NO_ERRORS:
52
+                test.logger.assertNoErrors();
53
+                break;
54
+            case ALLOW_ERRORS:
55
+                break;
56
+        }
57
+    }
58
+    
59
+    public void execute(ZenCodeTest test) {
60
+        execute(test, LogTolerance.NO_WARNINGS);
61
+    }
62
+    
63
+    public enum LogTolerance {
64
+        NO_WARNINGS, NO_ERRORS, ALLOW_ERRORS
65
+    }
66
+}

+ 18
- 1
ScriptingExample/src/test/java/org/openzen/zenscript/scriptingexample/tests/helpers/ZenCodeTestLogger.java Näytä tiedosto

@@ -10,6 +10,7 @@ public class ZenCodeTestLogger extends ScriptingEngineStreamLogger {
10 10
     private static final boolean logDebug = false;
11 11
     private final List<String> printlnOutputs = new ArrayList<>();
12 12
     private final List<String> errors = new ArrayList<>();
13
+    private final List<String> warnings = new ArrayList<>();
13 14
     private boolean isEngineComplete = false;
14 15
     
15 16
     @Override
@@ -19,9 +20,21 @@ public class ZenCodeTestLogger extends ScriptingEngineStreamLogger {
19 20
         }
20 21
     }
21 22
     
23
+    @Override
24
+    public void warning(String message) {
25
+        warnings.add(message);
26
+        super.warning(message);
27
+    }
28
+    
29
+    @Override
30
+    public void throwingWarn(String message, Throwable throwable) {
31
+        warnings.add(message);
32
+        super.throwingWarn(message, throwable);
33
+    }
34
+    
22 35
     public void logPrintln(String line) {
23 36
         info(line);
24
-        this.printlnOutputs.addAll(Arrays.asList(line.split(System.lineSeparator())));
37
+        this.printlnOutputs.addAll(Arrays.asList(String.valueOf(line).split(System.lineSeparator())));
25 38
     }
26 39
     
27 40
     @Override
@@ -57,4 +70,8 @@ public class ZenCodeTestLogger extends ScriptingEngineStreamLogger {
57 70
     public void assertNoErrors() {
58 71
         Assertions.assertEquals(0, errors.size());
59 72
     }
73
+    
74
+    public void assertNoWarnings() {
75
+        Assertions.assertEquals(0, warnings.size());
76
+    }
60 77
 }

Loading…
Peruuta
Tallenna