/
CastTest.java
97 lines (83 loc) · 3.2 KB
/
CastTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package org.ternlang.compile;
import junit.framework.TestCase;
public class CastTest extends TestCase {
private static final String SOURCE_1 =
"var builder = new StringBuilder();\n"+
"\n"+
"builder.append(97 as Character);\n"+
"builder.append(98 as Character);\n"+
"builder.append(99 as Character);\n"+
"builder.append(100 as Character);\n"+
"builder.append(101 as Character);\n"+
"builder.append(102 as Character);\n"+
"\n"+
"var text = builder.toString();\n"+
"println(text);\n"+
"assert text == 'abcdef';\n";
private static final String SOURCE_2 =
"const x = 11 as Character;\n"+
"const y = 22.0 as Integer;\n"+
"\n"+
"assert x.class == Character.class;\n"+
"assert y.class == Integer.class;\n";
private static final String SOURCE_3 =
"function foo(x): String{\n"+
" return x as String;\n"+
"}\n"+
"assert foo(1).class == String.class;\n";
private static final String SOURCE_4 =
"function foo(){\n"+
" for(i in 0..10)\n" +
" if(i % 2 == 0)\n"+
" yield i as String;\n"+
" else\n"+
" yield i as Integer;\n"+
"}\n"+
"var it = foo();\n"+
"var iterator = it.iterator();\n"+
"assert iterator.next().class == String.class;\n"+
"assert iterator.next().class == Integer.class;\n"+
"assert iterator.next().class == String.class;\n"+
"assert iterator.next().class == Integer.class;\n"+
"assert iterator.next() == `4`;\n"+
"assert iterator.next() == 5;\n";
private static final String SOURCE_5 =
"let x = 'a'.charAt(0) as Integer;\n"+
"assert x == 97;\n";
private static final String SOURCE_6 =
"let x = 'a' as Character;\n"+
"assert x == 'a';\n";
public void testCharacterCast() throws Exception {;
Compiler compiler = ClassPathCompilerBuilder.createCompiler();
Executable executable = compiler.compile(SOURCE_1);
executable.execute();
}
public void testDeclarationCast() throws Exception {;
Compiler compiler = ClassPathCompilerBuilder.createCompiler();
Executable executable = compiler.compile(SOURCE_2);
executable.execute();
}
public void testReturnCast() throws Exception {
Compiler compiler = ClassPathCompilerBuilder.createCompiler();
Executable executable = compiler.compile(SOURCE_3);
executable.execute();
}
public void testYieldCast() throws Exception {
Compiler compiler = ClassPathCompilerBuilder.createCompiler();
System.err.println(SOURCE_4);
Executable executable = compiler.compile(SOURCE_4);
executable.execute();
}
public void testCharacterToInteger() throws Exception {
Compiler compiler = ClassPathCompilerBuilder.createCompiler();
System.err.println(SOURCE_5);
Executable executable = compiler.compile(SOURCE_5);
executable.execute();
}
public void testStringToCharacter() throws Exception {
Compiler compiler = ClassPathCompilerBuilder.createCompiler();
System.err.println(SOURCE_6);
Executable executable = compiler.compile(SOURCE_6);
executable.execute();
}
}