vm/src/test/java/org/apidesign/vm4brwsr/VMinVMTest.java
branchjavap
changeset 170 2336c52d3ee5
parent 169 6f2aef4cf160
child 173 2f0205599623
     1.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/VMinVMTest.java	Fri Nov 16 08:26:55 2012 +0100
     1.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/VMinVMTest.java	Sat Nov 17 11:21:17 2012 +0100
     1.3 @@ -17,6 +17,10 @@
     1.4   */
     1.5  package org.apidesign.vm4brwsr;
     1.6  
     1.7 +import java.io.File;
     1.8 +import java.io.FileWriter;
     1.9 +import java.io.IOException;
    1.10 +import java.io.InputStream;
    1.11  import static org.testng.Assert.*;
    1.12  import javax.script.Invocable;
    1.13  import org.testng.annotations.BeforeClass;
    1.14 @@ -32,24 +36,47 @@
    1.15      private static Invocable code;
    1.16      
    1.17      @Test public void compareTheGeneratedCode() throws Exception {
    1.18 -        StringBuilder hotspot = new StringBuilder();
    1.19 -        GenJS.compile(hotspot, "org/apidesign/vm4brwsr/Array");
    1.20 +        byte[] arr = readClass("/org/apidesign/vm4brwsr/Array.class");
    1.21 +        String ret1 = VMinVM.toJavaScript(arr);
    1.22          
    1.23 -        Object ret = code.invokeFunction(
    1.24 -            "org_apidesign_vm4brwsr_GenJS_toStringLjava_lang_StringLjava_lang_String",
    1.25 -            "org/apidesign/vm4brwsr/Array"
    1.26 -        );
    1.27 +        Object ret;
    1.28 +        try {
    1.29 +            ret = code.invokeFunction(
    1.30 +                "org_apidesign_vm4brwsr_VMinVM_toJavaScriptLjava_lang_StringAB",
    1.31 +                arr
    1.32 +            );
    1.33 +        } catch (Exception ex) {
    1.34 +            File f = File.createTempFile("execution", ".js");
    1.35 +            FileWriter w = new FileWriter(f);
    1.36 +            w.append(codeSeq);
    1.37 +            w.close();
    1.38 +            throw new Exception(ex.getMessage() + " file: " + f, ex);
    1.39 +        }
    1.40 +
    1.41 +        
    1.42          assertTrue(ret instanceof String, "It is string: " + ret);
    1.43          
    1.44 -        assertEquals((String)ret, hotspot.toString(), "The code is the same");
    1.45 +        assertEquals((String)ret, ret1.toString(), "The code is the same");
    1.46      }
    1.47      
    1.48      @BeforeClass
    1.49      public void compileTheCode() throws Exception {
    1.50          StringBuilder sb = new StringBuilder();
    1.51          code = StaticMethodTest.compileClass(sb, 
    1.52 -            "org/apidesign/vm4brwsr/GenJS"
    1.53 +            "org/apidesign/vm4brwsr/VMinVM"
    1.54          );
    1.55          codeSeq = sb;
    1.56      }
    1.57 +    
    1.58 +    private static byte[] readClass(String res) throws IOException {
    1.59 +        InputStream is1 = VMinVMTest.class.getResourceAsStream(res);
    1.60 +        assertNotNull(is1, "Stream found");
    1.61 +        byte[] arr = new byte[is1.available()];
    1.62 +        int len = is1.read(arr);
    1.63 +        is1.close();
    1.64 +        if (len != arr.length) {
    1.65 +            throw new IOException("Wrong len " + len + " for arr: " + arr.length);
    1.66 +        }
    1.67 +        return arr;
    1.68 +    }
    1.69  }