Proper behaviour of endsWith and associated test
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 10 Dec 2012 11:26:32 +0100
changeset 296fbf8eb98a8ef
parent 291 c6f21b56a6cf
child 297 a20721a10717
Proper behaviour of endsWith and associated test
emul/src/main/java/java/lang/String.java
javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java
vm/src/test/java/org/apidesign/vm4brwsr/CompareVMs.java
vm/src/test/java/org/apidesign/vm4brwsr/tck/CompareStringsTest.java
     1.1 --- a/emul/src/main/java/java/lang/String.java	Sun Dec 09 16:43:36 2012 +0100
     1.2 +++ b/emul/src/main/java/java/lang/String.java	Mon Dec 10 11:26:32 2012 +0100
     1.3 @@ -1426,7 +1426,7 @@
     1.4       */
     1.5      @JavaScriptBody(args = { "self", "find", "from" }, body=
     1.6          "find = find.toString();\n" +
     1.7 -        "return self.toString().substring(from, find.length) === find;\n"
     1.8 +        "return self.toString().substring(from, from + find.length) === find;\n"
     1.9      )
    1.10      public boolean startsWith(String prefix, int toffset) {
    1.11          char ta[] = toCharArray();
     2.1 --- a/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java	Sun Dec 09 16:43:36 2012 +0100
     2.2 +++ b/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java	Mon Dec 10 11:26:32 2012 +0100
     2.3 @@ -70,6 +70,10 @@
     2.4      private static void setValue(double v) {
     2.5          StringBuilder sb = new StringBuilder();
     2.6          sb.append(v);
     2.7 +        if (sb.toString().endsWith(".0")) {
     2.8 +            final int l = sb.length();
     2.9 +            sb.delete(l - 2, l);
    2.10 +        }
    2.11          Calculator.DISPLAY.setValue(sb.toString());
    2.12      }
    2.13  
     3.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/CompareVMs.java	Sun Dec 09 16:43:36 2012 +0100
     3.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/CompareVMs.java	Mon Dec 10 11:26:32 2012 +0100
     3.3 @@ -73,7 +73,7 @@
     3.4          if (v1 instanceof Number) {
     3.5              v1 = ((Number)v1).doubleValue();
     3.6          }
     3.7 -        Assert.assertEquals(v1, v2, "Comparing results");
     3.8 +        Assert.assertEquals(v2, v1, "Comparing results");
     3.9      }
    3.10      
    3.11      @Override
    3.12 @@ -130,7 +130,7 @@
    3.13                  try {
    3.14                      compileTheCode(m.getDeclaringClass());
    3.15                      Object inst = code.invokeFunction(m.getDeclaringClass().getName().replace('.', '_'), false);
    3.16 -                    value = code.invokeMethod(inst, m.getName() + "__I");
    3.17 +                    value = code.invokeMethod(inst, m.getName() + "__" + computeSignature(m));
    3.18                  } catch (Exception ex) {
    3.19                      throw new AssertionError(StaticMethodTest.dumpJS(codeSeq)).initCause(ex);
    3.20                  }
    3.21 @@ -142,5 +142,56 @@
    3.22          public String getTestName() {
    3.23              return m.getName() + (js ? "[JavaScript]" : "[Java]");
    3.24          }
    3.25 +        
    3.26 +        private static String computeSignature(Method m) {
    3.27 +            StringBuilder sb = new StringBuilder();
    3.28 +            appendType(sb, m.getReturnType());
    3.29 +            for (Class<?> c : m.getParameterTypes()) {
    3.30 +                appendType(sb, c);
    3.31 +            }
    3.32 +            return sb.toString();
    3.33 +        }
    3.34 +        
    3.35 +        private static void appendType(StringBuilder sb, Class<?> t) {
    3.36 +            if (t == null) {
    3.37 +                sb.append('V');
    3.38 +                return;
    3.39 +            }
    3.40 +            if (t.isPrimitive()) {
    3.41 +                int ch = -1;
    3.42 +                if (t == int.class) {
    3.43 +                    ch = 'I';
    3.44 +                }
    3.45 +                if (t == short.class) {
    3.46 +                    ch = 'S';
    3.47 +                }
    3.48 +                if (t == byte.class) {
    3.49 +                    ch = 'B';
    3.50 +                }
    3.51 +                if (t == boolean.class) {
    3.52 +                    ch = 'Z';
    3.53 +                }
    3.54 +                if (t == long.class) {
    3.55 +                    ch = 'J';
    3.56 +                }
    3.57 +                if (t == float.class) {
    3.58 +                    ch = 'F';
    3.59 +                }
    3.60 +                if (t == double.class) {
    3.61 +                    ch = 'D';
    3.62 +                }
    3.63 +                assert ch != -1 : "Unknown primitive type " + t;
    3.64 +                sb.append((char)ch);
    3.65 +                return;
    3.66 +            }
    3.67 +            if (t.isArray()) {
    3.68 +                sb.append("_3");
    3.69 +                appendType(sb, t.getComponentType());
    3.70 +                return;
    3.71 +            }
    3.72 +            sb.append('L');
    3.73 +            sb.append(t.getName().replace('.', '_'));
    3.74 +            sb.append("_2");
    3.75 +        }
    3.76      }
    3.77  }
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/tck/CompareStringsTest.java	Mon Dec 10 11:26:32 2012 +0100
     4.3 @@ -0,0 +1,43 @@
     4.4 +/**
     4.5 + * Back 2 Browser Bytecode Translator
     4.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4.7 + *
     4.8 + * This program is free software: you can redistribute it and/or modify
     4.9 + * it under the terms of the GNU General Public License as published by
    4.10 + * the Free Software Foundation, version 2 of the License.
    4.11 + *
    4.12 + * This program is distributed in the hope that it will be useful,
    4.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    4.15 + * GNU General Public License for more details.
    4.16 + *
    4.17 + * You should have received a copy of the GNU General Public License
    4.18 + * along with this program. Look for COPYING file in the top folder.
    4.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    4.20 + */
    4.21 +package org.apidesign.vm4brwsr.tck;
    4.22 +
    4.23 +import org.apidesign.vm4brwsr.Compare;
    4.24 +import org.apidesign.vm4brwsr.CompareVMs;
    4.25 +import org.testng.annotations.Factory;
    4.26 +
    4.27 +/**
    4.28 + *
    4.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.30 + */
    4.31 +public class CompareStringsTest {
    4.32 +    @Compare public String deleteLastTwoCharacters() {
    4.33 +        StringBuilder sb = new StringBuilder();
    4.34 +        sb.append("453.0");
    4.35 +        if (sb.toString().endsWith(".0")) {
    4.36 +            final int l = sb.length();
    4.37 +            sb.delete(l - 2, l);
    4.38 +        }
    4.39 +        return sb.toString().toString();
    4.40 +    }
    4.41 +    
    4.42 +    @Factory
    4.43 +    public static Object[] create() {
    4.44 +        return CompareVMs.create(CompareStringsTest.class);
    4.45 +    }
    4.46 +}