2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.vm4brwsr;
20 import javax.script.Invocable;
21 import org.testng.annotations.Test;
22 import static org.testng.Assert.*;
23 import org.testng.annotations.BeforeClass;
27 * @author Jaroslav Tulach <jtulach@netbeans.org>
29 public class StringTest {
30 @Test public void firstChar() throws Exception {
32 "First char in Hello is H",
33 StringSample.class, "sayHello__CI",
38 @Test public void fromChars() throws Exception {
40 "First char in Hello is ABC",
41 StringSample.class, "fromChars__Ljava_lang_String_2CCC",
46 @Test public void concatChars() throws Exception {
48 "Composing yields ABC",
49 StringSample.class, "chars__Ljava_lang_String_2CCC",
54 @Test public void concatCharsFromInts() throws Exception {
56 "Composing yields ABC",
57 StringSample.class, "charsFromNumbers__Ljava_lang_String_2",
62 @Test public void concatCharsFromChars() throws Exception {
64 "Composing yields ABC",
65 StringSample.class, "charsFromChars__Ljava_lang_String_2",
70 @Test public void instanceOfWorks() throws Exception {
73 StringSample.class, "isStringInstance__Z",
78 @Test(timeOut=10000) public void toStringConcatenation() throws Exception {
80 "Five executions should generate 5Hello World!",
81 StringSample.class, "toStringTest__Ljava_lang_String_2I",
85 @Test public void toStringConcatenationJava() throws Exception {
86 assertEquals("Hello World!5", StringSample.toStringTest(5));
89 @Test(timeOut=10000) public void stringStringConcat() throws Exception {
91 "Composes strings OK",
92 StringSample.class, "concatStrings__Ljava_lang_String_2",
93 "Hello World!1" + "\\\n\r\t"
97 @Test public void equalsAndSubstring() throws Exception {
100 StringSample.class, "equalToHello__ZII",
104 @Test public void replaceChars() throws Exception {
106 "Can replace slashes by underscores",
107 StringSample.class, "replace__Ljava_lang_String_2Ljava_lang_String_2CC",
108 "x_y_z", "x/y/z", '/', '_'
111 @Test public void replaceIntChars() throws Exception {
113 "Can replace slashes by underscores",
114 StringSample.class, "replace__Ljava_lang_String_2Ljava_lang_String_2CC",
115 "x_y_z", "x/y/z", (int)'/', (int)'_'
119 @Test public void insertBuilder() throws Exception {
121 "Can insert something into a buffer?",
122 StringSample.class, "insertBuffer__Ljava_lang_String_2",
127 @Test public void compareHashCodeHi() throws Exception {
129 int jh = StringSample.hashCode(j);
131 "Hashcode is the same " +jh,
132 StringSample.class, "hashCode__ILjava_lang_String_2",
133 Double.valueOf(jh), j
136 @Test public void compareHashCode1() throws Exception {
137 String j = "Hello Java!";
138 int jh = StringSample.hashCode(j);
140 "Hashcode is the same " + jh,
141 StringSample.class, "hashCode__ILjava_lang_String_2",
142 Double.valueOf(jh), j
145 @Test public void stringSwitch1() throws Exception {
148 StringSample.class, "stringSwitch__ILjava_lang_String_2",
149 Double.valueOf(1), "jedna"
152 @Test public void stringSwitch2() throws Exception {
155 StringSample.class, "stringSwitch__ILjava_lang_String_2",
156 Double.valueOf(2), "dve"
159 @Test public void stringSwitchDefault() throws Exception {
162 StringSample.class, "stringSwitch__ILjava_lang_String_2",
163 Double.valueOf(-1), "none"
167 @Test public void countAB() throws Exception {
168 assertEquals(StringSample.countAB("Ahoj Bedo!"), 3, "Verify Java code is sane");
170 "One A and one B adds to 3",
171 StringSample.class, "countAB__ILjava_lang_String_2",
172 Double.valueOf(3), "Ahoj Bedo!"
177 private static CharSequence codeSeq;
178 private static Invocable code;
181 public void compileTheCode() throws Exception {
182 StringBuilder sb = new StringBuilder();
183 code = StaticMethodTest.compileClass(sb,
184 "org/apidesign/vm4brwsr/StringSample",
190 private static void assertExec(String msg,
191 Class<?> clazz, String method, Object expRes, Object... args
193 StaticMethodTest.assertExec(code, codeSeq, msg, clazz, method, expRes, args);