ide/editor/src/test/java/org/apidesign/bck2brwsr/ide/editor/JsniCommentTokenizerTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 13 Feb 2013 12:08:00 +0100
branchide
changeset 717 58ce0cd13d26
permissions -rw-r--r--
Igor's dejsni files and tests. Modified to compile.
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.ide.editor;
    19 
    20 import java.io.IOException;
    21 import java.util.ArrayList;
    22 import java.util.List;
    23 import org.testng.Assert;
    24 import org.testng.annotations.Test;
    25 
    26 public class JsniCommentTokenizerTest {
    27 
    28     private static class MockSink implements JsniCommentTokenizer.Sink {
    29         final List<String> out = new ArrayList<String>();
    30 
    31         public void javascript(String s) {
    32             out.add("J " + s);
    33         }
    34 
    35         public void method(String clazz, String method, String signature) {
    36             out.add("M " + clazz + "|" + method + "|" + signature);
    37         }
    38 
    39         public void field(String clazz, String field) {
    40             out.add("F " + clazz + "|" + field);
    41         }
    42     }
    43 
    44 
    45     @Test
    46     public void testProcess_nop() throws IOException {
    47         final String in = "foo bar";
    48         final List<String> expected = new ArrayList<String>();
    49         expected.add("J foo bar");
    50 
    51         final JsniCommentTokenizer jsniCommentTokenizer = new JsniCommentTokenizer();
    52         final MockSink out = new MockSink();
    53         jsniCommentTokenizer.process(in, out);
    54 
    55         Assert.assertEquals(expected, out.out);
    56     }
    57 
    58     @Test
    59     public void testProcess_read_static_field() throws IOException {
    60         final String in = " @com.google.gwt.examples.JSNIExample::myStaticField = val + \" and stuff\";";
    61         final List<String> expected = new ArrayList<String>();
    62         expected.add("J  ");
    63         expected.add("F com.google.gwt.examples.JSNIExample|myStaticField");
    64         expected.add("J  = val + \" and stuff\";");
    65 
    66         final JsniCommentTokenizer jsniCommentTokenizer = new JsniCommentTokenizer();
    67         final MockSink out = new MockSink();
    68         jsniCommentTokenizer.process(in, out);
    69 
    70         Assert.assertEquals(expected, out.out);
    71     }
    72 
    73     @Test
    74     public void testProcess_write_instance_field() throws IOException {
    75         final String in = " x.@com.google.gwt.examples.JSNIExample::myInstanceField = val + \" and stuff\";";
    76         final List<String> expected = new ArrayList<String>();
    77         expected.add("J  x.");
    78         expected.add("F com.google.gwt.examples.JSNIExample|myInstanceField");
    79         expected.add("J  = val + \" and stuff\";");
    80 
    81         final JsniCommentTokenizer jsniCommentTokenizer = new JsniCommentTokenizer();
    82         final MockSink out = new MockSink();
    83         jsniCommentTokenizer.process(in, out);
    84 
    85         Assert.assertEquals(expected, out.out);
    86     }
    87 
    88     @Test
    89     public void testProcess_read_instance_field() throws IOException {
    90         final String in = " var val = this.@com.google.gwt.examples.JSNIExample::myInstanceField;";
    91         final List<String> expected = new ArrayList<String>();
    92         expected.add("J  var val = this.");
    93         expected.add("F com.google.gwt.examples.JSNIExample|myInstanceField");
    94         expected.add("J ;");
    95 
    96         final JsniCommentTokenizer jsniCommentTokenizer = new JsniCommentTokenizer();
    97         final MockSink out = new MockSink();
    98         jsniCommentTokenizer.process(in, out);
    99 
   100         Assert.assertEquals(expected, out.out);
   101     }
   102 
   103 
   104     @Test
   105     public void testProcess_static_method() throws IOException {
   106         final String in = " @com.google.gwt.examples.JSNIExample::staticFoo(Ljava/lang/String;)(s);";
   107         final List<String> expected = new ArrayList<String>();
   108         expected.add("J  ");
   109         expected.add("M com.google.gwt.examples.JSNIExample|staticFoo|Ljava/lang/String;");
   110         expected.add("J (s);");
   111 
   112         final JsniCommentTokenizer jsniCommentTokenizer = new JsniCommentTokenizer();
   113         final MockSink out = new MockSink();
   114         jsniCommentTokenizer.process(in, out);
   115 
   116         Assert.assertEquals(expected, out.out);
   117     }
   118 
   119 
   120     @Test
   121     public void testProcess_instance_method() throws IOException {
   122         final String in = " x.@com.google.gwt.examples.JSNIExample::instanceFoo(Ljava/lang/String;)(s);";
   123         final List<String> expected = new ArrayList<String>();
   124         expected.add("J  x.");
   125         expected.add("M com.google.gwt.examples.JSNIExample|instanceFoo|Ljava/lang/String;");
   126         expected.add("J (s);");
   127 
   128         final JsniCommentTokenizer jsniCommentTokenizer = new JsniCommentTokenizer();
   129         final MockSink out = new MockSink();
   130         jsniCommentTokenizer.process(in, out);
   131 
   132         Assert.assertEquals(expected, out.out);
   133     }
   134 
   135 
   136     @Test
   137     public void testProcess_multiline() throws IOException {
   138         final String in =
   139             " x.@com.google.gwt.examples.JSNIExample::instanceFoo(Ljava/lang/String;)(s);" +
   140             " @com.google.gwt.examples.JSNIExample::myStaticField = val + \" and stuff\";";
   141         final List<String> expected = new ArrayList<String>();
   142         expected.add("J  x.");
   143         expected.add("M com.google.gwt.examples.JSNIExample|instanceFoo|Ljava/lang/String;");
   144         expected.add("J (s); ");
   145         expected.add("F com.google.gwt.examples.JSNIExample|myStaticField");
   146         expected.add("J  = val + \" and stuff\";");
   147 
   148         final JsniCommentTokenizer jsniCommentTokenizer = new JsniCommentTokenizer();
   149         final MockSink out = new MockSink();
   150         jsniCommentTokenizer.process(in, out);
   151 
   152         Assert.assertEquals(expected, out.out);
   153     }
   154 }