rt/flow/src/test/java/org/apidesign/bck2brwsr/flow/LoopControlTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 11 Mar 2015 18:58:39 +0100
branchflow
changeset 1812 4fef6b767f61
child 1815 fbe883b5a793
permissions -rw-r--r--
Defining API for registration of a Flow.Analyzer and getting ready for use the one from Graal
     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 org.apidesign.vm4brwsr.Bck2Brwsr.Flow;
    21 import static org.testng.Assert.*;
    22 import org.testng.annotations.AfterClass;
    23 import org.testng.annotations.AfterMethod;
    24 import org.testng.annotations.BeforeClass;
    25 import org.testng.annotations.BeforeMethod;
    26 import org.testng.annotations.Test;
    27 
    28 /**
    29  *
    30  * @author Jaroslav Tulach
    31  */
    32 public class LoopControlTest {
    33     private static TestVM vm;
    34     
    35     public LoopControlTest() {
    36     }
    37 
    38     @BeforeClass
    39     public static void setUpClass() throws Exception {
    40         class MyFlow implements Flow.Analyzer {
    41             boolean called;
    42             @Override
    43             public boolean analyze(Flow request) {
    44                 called = true;
    45                 return GraalFlowAnalyzer.getDefault().analyze(request);
    46             }
    47         }
    48         MyFlow flow = new MyFlow();
    49         vm = TestVM.compileClass(null, flow, LoopControl.class.getName().replace('.', '/'));
    50         assertTrue(flow.called, "We have been consuted about at least one method");
    51     }
    52 
    53     @AfterClass
    54     public static void tearDownClass() throws Exception {
    55         vm = null;
    56     }
    57 
    58     @BeforeMethod
    59     public void setUpMethod() throws Exception {
    60     }
    61 
    62     @AfterMethod
    63     public void tearDownMethod() throws Exception {
    64     }
    65 
    66     @Test
    67     public void testExecute() throws Exception {
    68         String code = vm.codeSeq().toString();
    69         int begin = code.indexOf("simpleLoopTestWithExit__II = function");
    70         assertNotEquals(begin, -1, "Control loop defined" + code);
    71         int end = code.indexOf("m.access = ", begin);
    72         assertNotEquals(end, -1, "Control loop end defined" + code);
    73         final String body = code.substring(begin, end);
    74         assertFalse(body.contains("gt"), "No gt control flow used: " + body);
    75     }
    76     
    77 }