rt/flow/src/test/java/org/apidesign/bck2brwsr/flow/LoopControlTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 13 Mar 2015 11:59:26 +0100
branchflow
changeset 1818 21089a85f02b
parent 1815 fbe883b5a793
child 1842 dd4dabfead82
permissions -rw-r--r--
First method with flow analyser generated
     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.bck2brwsr.flow;
    19 
    20 import javax.script.ScriptEngine;
    21 import javax.script.ScriptEngineManager;
    22 import javax.script.ScriptException;
    23 import org.apidesign.vm4brwsr.Bck2Brwsr.Flow;
    24 import static org.testng.Assert.*;
    25 import org.testng.annotations.AfterClass;
    26 import org.testng.annotations.AfterMethod;
    27 import org.testng.annotations.BeforeClass;
    28 import org.testng.annotations.BeforeMethod;
    29 import org.testng.annotations.Test;
    30 
    31 /**
    32  *
    33  * @author Jaroslav Tulach
    34  */
    35 public class LoopControlTest {
    36     private static TestVM vm;
    37     
    38     public LoopControlTest() {
    39     }
    40 
    41     @BeforeClass
    42     public static void setUpClass() throws Exception {
    43         class MyFlow implements Flow.Analyzer {
    44             boolean called;
    45             @Override
    46             public boolean analyze(Flow request) {
    47                 if (
    48                     request.getMethodName().equals("simpleLoopTest") ||
    49                     request.getMethodName().equals("simpleLoopTestWithExit") ||
    50                     false
    51                 ) {
    52                     called = true;
    53                     return GraalFlowAnalyzer.getDefault().analyze(request);
    54                 } else {
    55                     return false;
    56                 }
    57             }
    58         }
    59         MyFlow flow = new MyFlow();
    60         vm = TestVM.compileClass(null, flow, LoopControl.class.getName().replace('.', '/'));
    61         assertTrue(flow.called, "We have been consuted about at least one method");
    62     }
    63 
    64     @AfterClass
    65     public static void tearDownClass() throws Exception {
    66         vm = null;
    67     }
    68 
    69     @BeforeMethod
    70     public void setUpMethod() throws Exception {
    71     }
    72 
    73     @AfterMethod
    74     public void tearDownMethod() throws Exception {
    75     }
    76 
    77     @Test
    78     public void testSimpleLoopTest() throws Exception {
    79         assertFlowControl("simpleLoopTest__II", LoopControl.simpleLoopTest(123));
    80     }
    81 
    82 //   David, this test is broken due to wrong offsets of instructions
    83 //    @Test
    84 //    public void testSimpleLoopWithExitTest() throws Exception {
    85 //        assertFlowControl("simpleLoopTestWithExit__II", LoopControl.simpleLoopTestWithExit(123));
    86 //    }
    87     
    88     private void assertFlowControl(final String mangledSig, int exp) throws Exception {
    89         String code = vm.codeSeq().toString();
    90         int begin = code.indexOf(mangledSig + " = function");
    91         assertNotEquals(begin, -1, "Control loop defined for " + mangledSig + " in:\n" + code);
    92         int end = code.indexOf("m.access = ", begin);
    93         assertNotEquals(end, -1, "Control loop end defined" + code);
    94         final String body = code.substring(begin, end);
    95         assertFalse(body.contains("gt"), "No gt control flow used: " + body);
    96         
    97         ScriptEngine eng = new ScriptEngineManager().getEngineByMimeType("text/javascript");
    98         try {
    99             eng.eval(body);
   100         } catch (ScriptException ex) {
   101             fail("Cannot parse:\n" + body, ex);
   102         }
   103         
   104         vm.assertExec("Is the code compilable?", LoopControl.class, mangledSig, exp, 123);
   105     }
   106     
   107 }