jaroslav@106
|
1 |
/**
|
jaroslav@106
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@106
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@106
|
4 |
*
|
jaroslav@106
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@106
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@106
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@106
|
8 |
*
|
jaroslav@106
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@106
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@106
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@106
|
12 |
* GNU General Public License for more details.
|
jaroslav@106
|
13 |
*
|
jaroslav@106
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@106
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@106
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@106
|
17 |
*/
|
jaroslav@34
|
18 |
package org.apidesign.vm4brwsr;
|
jaroslav@34
|
19 |
|
jaroslav@34
|
20 |
import javax.script.Invocable;
|
jaroslav@34
|
21 |
import javax.script.ScriptException;
|
jaroslav@34
|
22 |
import org.testng.annotations.Test;
|
jaroslav@34
|
23 |
import static org.testng.Assert.*;
|
jaroslav@103
|
24 |
import org.testng.annotations.BeforeClass;
|
jaroslav@34
|
25 |
|
jaroslav@34
|
26 |
/**
|
jaroslav@34
|
27 |
*
|
jaroslav@34
|
28 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@34
|
29 |
*/
|
jaroslav@34
|
30 |
public class StringTest {
|
jaroslav@34
|
31 |
@Test public void firstChar() throws Exception {
|
jaroslav@34
|
32 |
assertExec(
|
jaroslav@34
|
33 |
"First char in Hello is H",
|
jaroslav@34
|
34 |
"org_apidesign_vm4brwsr_StringSample_sayHelloCI",
|
jaroslav@35
|
35 |
"H", 0
|
jaroslav@34
|
36 |
);
|
jaroslav@34
|
37 |
}
|
jaroslav@36
|
38 |
|
jaroslav@45
|
39 |
@Test public void fromChars() throws Exception {
|
jaroslav@45
|
40 |
assertExec(
|
jaroslav@45
|
41 |
"First char in Hello is ABC",
|
jaroslav@45
|
42 |
"org_apidesign_vm4brwsr_StringSample_fromCharsLjava_lang_StringCCC",
|
jaroslav@45
|
43 |
"ABC", 'A', 'B', 'C'
|
jaroslav@45
|
44 |
);
|
jaroslav@45
|
45 |
}
|
jaroslav@45
|
46 |
|
jaroslav@171
|
47 |
@Test public void concatChars() throws Exception {
|
jaroslav@171
|
48 |
assertExec(
|
jaroslav@171
|
49 |
"Composing yields ABC",
|
jaroslav@171
|
50 |
"org_apidesign_vm4brwsr_StringSample_charsLjava_lang_StringCCC",
|
jaroslav@171
|
51 |
"ABC", 'A', 'B', 'C'
|
jaroslav@171
|
52 |
);
|
jaroslav@171
|
53 |
}
|
jaroslav@171
|
54 |
|
jaroslav@176
|
55 |
@Test public void concatCharsFromInts() throws Exception {
|
jaroslav@176
|
56 |
assertExec(
|
jaroslav@176
|
57 |
"Composing yields ABC",
|
jaroslav@176
|
58 |
"org_apidesign_vm4brwsr_StringSample_charsFromNumbersLjava_lang_String",
|
jaroslav@176
|
59 |
"ABC"
|
jaroslav@176
|
60 |
);
|
jaroslav@176
|
61 |
}
|
jaroslav@176
|
62 |
|
jaroslav@176
|
63 |
@Test public void concatCharsFromChars() throws Exception {
|
jaroslav@176
|
64 |
assertExec(
|
jaroslav@176
|
65 |
"Composing yields ABC",
|
jaroslav@176
|
66 |
"org_apidesign_vm4brwsr_StringSample_charsFromCharsLjava_lang_String",
|
jaroslav@176
|
67 |
"ABC"
|
jaroslav@176
|
68 |
);
|
jaroslav@176
|
69 |
}
|
jaroslav@176
|
70 |
|
jaroslav@93
|
71 |
@Test(timeOut=10000) public void toStringConcatenation() throws Exception {
|
jaroslav@36
|
72 |
assertExec(
|
jaroslav@36
|
73 |
"Five executions should generate 5Hello World!",
|
jaroslav@36
|
74 |
"org_apidesign_vm4brwsr_StringSample_toStringTestLjava_lang_StringI",
|
jaroslav@94
|
75 |
"Hello World!5", 5
|
jaroslav@36
|
76 |
);
|
jaroslav@36
|
77 |
}
|
jaroslav@94
|
78 |
@Test public void toStringConcatenationJava() throws Exception {
|
jaroslav@94
|
79 |
assertEquals("Hello World!5", StringSample.toStringTest(5));
|
jaroslav@94
|
80 |
}
|
jaroslav@104
|
81 |
|
jaroslav@104
|
82 |
@Test(timeOut=10000) public void stringStringConcat() throws Exception {
|
jaroslav@104
|
83 |
assertExec(
|
jaroslav@104
|
84 |
"Composes strings OK",
|
jaroslav@104
|
85 |
"org_apidesign_vm4brwsr_StringSample_concatStringsLjava_lang_String",
|
jaroslav@137
|
86 |
"Hello World!1" + "\\\n\r\t"
|
jaroslav@104
|
87 |
);
|
jaroslav@104
|
88 |
}
|
jaroslav@116
|
89 |
|
jaroslav@175
|
90 |
@Test public void equalsAndSubstring() throws Exception {
|
jaroslav@116
|
91 |
assertExec(
|
jaroslav@116
|
92 |
"Composes are OK",
|
jaroslav@116
|
93 |
"org_apidesign_vm4brwsr_StringSample_equalToHelloZII",
|
jaroslav@175
|
94 |
true, 0, 5
|
jaroslav@116
|
95 |
);
|
jaroslav@116
|
96 |
}
|
jaroslav@104
|
97 |
|
jaroslav@103
|
98 |
private static CharSequence codeSeq;
|
jaroslav@103
|
99 |
private static Invocable code;
|
jaroslav@34
|
100 |
|
jaroslav@103
|
101 |
@BeforeClass
|
jaroslav@103
|
102 |
public void compileTheCode() throws Exception {
|
jaroslav@34
|
103 |
StringBuilder sb = new StringBuilder();
|
jaroslav@103
|
104 |
code = StaticMethodTest.compileClass(sb,
|
jaroslav@34
|
105 |
"org/apidesign/vm4brwsr/StringSample",
|
jaroslav@34
|
106 |
"java/lang/String"
|
jaroslav@34
|
107 |
);
|
jaroslav@103
|
108 |
codeSeq = sb;
|
jaroslav@103
|
109 |
}
|
jaroslav@103
|
110 |
|
jaroslav@103
|
111 |
private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
|
jaroslav@34
|
112 |
Object ret = null;
|
jaroslav@34
|
113 |
try {
|
jaroslav@103
|
114 |
ret = code.invokeFunction(methodName, args);
|
jaroslav@34
|
115 |
} catch (ScriptException ex) {
|
jaroslav@103
|
116 |
fail("Execution failed in\n" + codeSeq, ex);
|
jaroslav@34
|
117 |
} catch (NoSuchMethodException ex) {
|
jaroslav@103
|
118 |
fail("Cannot find method in\n" + codeSeq, ex);
|
jaroslav@34
|
119 |
}
|
jaroslav@34
|
120 |
if (ret == null && expRes == null) {
|
jaroslav@34
|
121 |
return;
|
jaroslav@34
|
122 |
}
|
jaroslav@34
|
123 |
if (expRes.equals(ret)) {
|
jaroslav@34
|
124 |
return;
|
jaroslav@34
|
125 |
}
|
jaroslav@103
|
126 |
assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
|
jaroslav@34
|
127 |
|
jaroslav@34
|
128 |
}
|
jaroslav@34
|
129 |
|
jaroslav@34
|
130 |
}
|