ide/editor/src/test/java/org/apidesign/bck2brwsr/ide/editor/JsniCommentTokenizerTest.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/ide/editor/src/test/java/org/apidesign/bck2brwsr/ide/editor/JsniCommentTokenizerTest.java Mon Jan 13 12:37:03 2014 +0100
1.3 @@ -0,0 +1,154 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.bck2brwsr.ide.editor;
1.22 +
1.23 +import java.io.IOException;
1.24 +import java.util.ArrayList;
1.25 +import java.util.List;
1.26 +import org.testng.Assert;
1.27 +import org.testng.annotations.Test;
1.28 +
1.29 +public class JsniCommentTokenizerTest {
1.30 +
1.31 + private static class MockSink implements JsniCommentTokenizer.Sink {
1.32 + final List<String> out = new ArrayList<String>();
1.33 +
1.34 + public void javascript(String s) {
1.35 + out.add("J " + s);
1.36 + }
1.37 +
1.38 + public void method(String clazz, String method, String signature) {
1.39 + out.add("M " + clazz + "|" + method + "|" + signature);
1.40 + }
1.41 +
1.42 + public void field(String clazz, String field) {
1.43 + out.add("F " + clazz + "|" + field);
1.44 + }
1.45 + }
1.46 +
1.47 +
1.48 + @Test
1.49 + public void testProcess_nop() throws IOException {
1.50 + final String in = "foo bar";
1.51 + final List<String> expected = new ArrayList<String>();
1.52 + expected.add("J foo bar");
1.53 +
1.54 + final JsniCommentTokenizer jsniCommentTokenizer = new JsniCommentTokenizer();
1.55 + final MockSink out = new MockSink();
1.56 + jsniCommentTokenizer.process(in, out);
1.57 +
1.58 + Assert.assertEquals(expected, out.out);
1.59 + }
1.60 +
1.61 + @Test
1.62 + public void testProcess_read_static_field() throws IOException {
1.63 + final String in = " @com.google.gwt.examples.JSNIExample::myStaticField = val + \" and stuff\";";
1.64 + final List<String> expected = new ArrayList<String>();
1.65 + expected.add("J ");
1.66 + expected.add("F com.google.gwt.examples.JSNIExample|myStaticField");
1.67 + expected.add("J = val + \" and stuff\";");
1.68 +
1.69 + final JsniCommentTokenizer jsniCommentTokenizer = new JsniCommentTokenizer();
1.70 + final MockSink out = new MockSink();
1.71 + jsniCommentTokenizer.process(in, out);
1.72 +
1.73 + Assert.assertEquals(expected, out.out);
1.74 + }
1.75 +
1.76 + @Test
1.77 + public void testProcess_write_instance_field() throws IOException {
1.78 + final String in = " x.@com.google.gwt.examples.JSNIExample::myInstanceField = val + \" and stuff\";";
1.79 + final List<String> expected = new ArrayList<String>();
1.80 + expected.add("J x.");
1.81 + expected.add("F com.google.gwt.examples.JSNIExample|myInstanceField");
1.82 + expected.add("J = val + \" and stuff\";");
1.83 +
1.84 + final JsniCommentTokenizer jsniCommentTokenizer = new JsniCommentTokenizer();
1.85 + final MockSink out = new MockSink();
1.86 + jsniCommentTokenizer.process(in, out);
1.87 +
1.88 + Assert.assertEquals(expected, out.out);
1.89 + }
1.90 +
1.91 + @Test
1.92 + public void testProcess_read_instance_field() throws IOException {
1.93 + final String in = " var val = this.@com.google.gwt.examples.JSNIExample::myInstanceField;";
1.94 + final List<String> expected = new ArrayList<String>();
1.95 + expected.add("J var val = this.");
1.96 + expected.add("F com.google.gwt.examples.JSNIExample|myInstanceField");
1.97 + expected.add("J ;");
1.98 +
1.99 + final JsniCommentTokenizer jsniCommentTokenizer = new JsniCommentTokenizer();
1.100 + final MockSink out = new MockSink();
1.101 + jsniCommentTokenizer.process(in, out);
1.102 +
1.103 + Assert.assertEquals(expected, out.out);
1.104 + }
1.105 +
1.106 +
1.107 + @Test
1.108 + public void testProcess_static_method() throws IOException {
1.109 + final String in = " @com.google.gwt.examples.JSNIExample::staticFoo(Ljava/lang/String;)(s);";
1.110 + final List<String> expected = new ArrayList<String>();
1.111 + expected.add("J ");
1.112 + expected.add("M com.google.gwt.examples.JSNIExample|staticFoo|Ljava/lang/String;");
1.113 + expected.add("J (s);");
1.114 +
1.115 + final JsniCommentTokenizer jsniCommentTokenizer = new JsniCommentTokenizer();
1.116 + final MockSink out = new MockSink();
1.117 + jsniCommentTokenizer.process(in, out);
1.118 +
1.119 + Assert.assertEquals(expected, out.out);
1.120 + }
1.121 +
1.122 +
1.123 + @Test
1.124 + public void testProcess_instance_method() throws IOException {
1.125 + final String in = " x.@com.google.gwt.examples.JSNIExample::instanceFoo(Ljava/lang/String;)(s);";
1.126 + final List<String> expected = new ArrayList<String>();
1.127 + expected.add("J x.");
1.128 + expected.add("M com.google.gwt.examples.JSNIExample|instanceFoo|Ljava/lang/String;");
1.129 + expected.add("J (s);");
1.130 +
1.131 + final JsniCommentTokenizer jsniCommentTokenizer = new JsniCommentTokenizer();
1.132 + final MockSink out = new MockSink();
1.133 + jsniCommentTokenizer.process(in, out);
1.134 +
1.135 + Assert.assertEquals(expected, out.out);
1.136 + }
1.137 +
1.138 +
1.139 + @Test
1.140 + public void testProcess_multiline() throws IOException {
1.141 + final String in =
1.142 + " x.@com.google.gwt.examples.JSNIExample::instanceFoo(Ljava/lang/String;)(s);" +
1.143 + " @com.google.gwt.examples.JSNIExample::myStaticField = val + \" and stuff\";";
1.144 + final List<String> expected = new ArrayList<String>();
1.145 + expected.add("J x.");
1.146 + expected.add("M com.google.gwt.examples.JSNIExample|instanceFoo|Ljava/lang/String;");
1.147 + expected.add("J (s); ");
1.148 + expected.add("F com.google.gwt.examples.JSNIExample|myStaticField");
1.149 + expected.add("J = val + \" and stuff\";");
1.150 +
1.151 + final JsniCommentTokenizer jsniCommentTokenizer = new JsniCommentTokenizer();
1.152 + final MockSink out = new MockSink();
1.153 + jsniCommentTokenizer.process(in, out);
1.154 +
1.155 + Assert.assertEquals(expected, out.out);
1.156 + }
1.157 +}