2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.bck2brwsr.flow;
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;
33 * @author Jaroslav Tulach
35 public class LoopControlTest {
36 private static TestVM vm;
38 public LoopControlTest() {
42 public static void setUpClass() throws Exception {
43 class MyFlow implements Flow.Analyzer {
46 public boolean analyze(Flow request) {
48 request.getMethodName().equals("simpleLoopTest") ||
49 request.getMethodName().equals("simpleLoopTestWithExit") ||
53 return GraalFlowAnalyzer.getDefault().analyze(request);
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");
65 public static void tearDownClass() throws Exception {
70 public void setUpMethod() throws Exception {
74 public void tearDownMethod() throws Exception {
78 public void testSimpleLoopTest() throws Exception {
79 assertFlowControl("simpleLoopTest__II", LoopControl.simpleLoopTest(123));
82 // David, this test is broken due to wrong offsets of instructions
84 // public void testSimpleLoopWithExitTest() throws Exception {
85 // assertFlowControl("simpleLoopTestWithExit__II", LoopControl.simpleLoopTestWithExit(123));
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);
97 ScriptEngine eng = new ScriptEngineManager().getEngineByMimeType("text/javascript");
100 } catch (ScriptException ex) {
101 fail("Cannot parse:\n" + body, ex);
104 vm.assertExec("Is the code compilable?", LoopControl.class, mangledSig, exp, 123);