rt/vm/src/test/java/org/apidesign/vm4brwsr/StackMapperTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 27 Feb 2015 10:41:40 +0100
changeset 1794 df885b256382
permissions -rw-r--r--
First StackMapper test
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 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.vm4brwsr;
    19 
    20 import javax.script.ScriptEngine;
    21 import javax.script.ScriptEngineManager;
    22 import static org.testng.Assert.*;
    23 import org.testng.annotations.Test;
    24 
    25 /**
    26  *
    27  * @author Jaroslav Tulach
    28  */
    29 public class StackMapperTest {
    30     
    31     public StackMapperTest() {
    32     }
    33     
    34     @Test public void replaceValueThatAnotherReplaceDependsOn()
    35     throws Exception {
    36         StringBuilder sb = new StringBuilder();
    37         
    38         StackMapper smapper = new StackMapper();
    39         
    40         smapper.assign(sb, VarType.INTEGER, "0");
    41         
    42         smapper.assign(sb, VarType.REFERENCE, "arr");
    43         smapper.assign(sb, VarType.INTEGER, "33");
    44         smapper.replace(sb, VarType.INTEGER, "@2[@1]", 
    45             smapper.popI(), smapper.getA(0)
    46         );
    47 
    48         smapper.replace(sb, VarType.INTEGER, "(@1) + (@2)", 
    49             smapper.getI(1), smapper.popI()
    50         );
    51         
    52         smapper.assign(sb, VarType.REFERENCE, "arr");
    53         smapper.assign(sb, VarType.INTEGER, "22");
    54         smapper.replace(sb, VarType.INTEGER, "@2[@1]", 
    55             smapper.popI(), smapper.getA(0)
    56         );
    57         
    58         smapper.replace(sb, VarType.INTEGER, "(@1) + (@2)", 
    59             smapper.getI(1), smapper.popI()
    60         );
    61         
    62         smapper.flush(sb);
    63         
    64         ScriptEngineManager sem = new ScriptEngineManager();
    65         ScriptEngine em = sem.getEngineByMimeType("text/javascript");
    66         Object ret = em.eval("var arr= []; arr[33] = 40; arr[22] = 2; " + sb + "; stI0;");
    67         
    68         assertTrue(ret instanceof Number, "Result is number: " + ret);
    69         assertEquals(((Number)ret).intValue(), 42, "Right value");
    70     }
    71     
    72 }