1.1 --- a/rt/flow/src/test/java/org/apidesign/bck2brwsr/flow/LoopControlTest.java Thu Mar 12 12:07:54 2015 +0100
1.2 +++ b/rt/flow/src/test/java/org/apidesign/bck2brwsr/flow/LoopControlTest.java Fri Mar 13 11:59:26 2015 +0100
1.3 @@ -17,6 +17,9 @@
1.4 */
1.5 package org.apidesign.bck2brwsr.flow;
1.6
1.7 +import javax.script.ScriptEngine;
1.8 +import javax.script.ScriptEngineManager;
1.9 +import javax.script.ScriptException;
1.10 import org.apidesign.vm4brwsr.Bck2Brwsr.Flow;
1.11 import static org.testng.Assert.*;
1.12 import org.testng.annotations.AfterClass;
1.13 @@ -41,7 +44,11 @@
1.14 boolean called;
1.15 @Override
1.16 public boolean analyze(Flow request) {
1.17 - if (request.getMethodName().equals("simpleLoopTestWithExit")) {
1.18 + if (
1.19 + request.getMethodName().equals("simpleLoopTest") ||
1.20 + request.getMethodName().equals("simpleLoopTestWithExit") ||
1.21 + false
1.22 + ) {
1.23 called = true;
1.24 return GraalFlowAnalyzer.getDefault().analyze(request);
1.25 } else {
1.26 @@ -68,17 +75,33 @@
1.27 }
1.28
1.29 @Test
1.30 - public void testExecute() throws Exception {
1.31 + public void testSimpleLoopTest() throws Exception {
1.32 + assertFlowControl("simpleLoopTest__II", LoopControl.simpleLoopTest(123));
1.33 + }
1.34 +
1.35 +// David, this test is broken due to wrong offsets of instructions
1.36 +// @Test
1.37 +// public void testSimpleLoopWithExitTest() throws Exception {
1.38 +// assertFlowControl("simpleLoopTestWithExit__II", LoopControl.simpleLoopTestWithExit(123));
1.39 +// }
1.40 +
1.41 + private void assertFlowControl(final String mangledSig, int exp) throws Exception {
1.42 String code = vm.codeSeq().toString();
1.43 - int begin = code.indexOf("simpleLoopTestWithExit__II = function");
1.44 - assertNotEquals(begin, -1, "Control loop defined" + code);
1.45 + int begin = code.indexOf(mangledSig + " = function");
1.46 + assertNotEquals(begin, -1, "Control loop defined for " + mangledSig + " in:\n" + code);
1.47 int end = code.indexOf("m.access = ", begin);
1.48 assertNotEquals(end, -1, "Control loop end defined" + code);
1.49 final String body = code.substring(begin, end);
1.50 assertFalse(body.contains("gt"), "No gt control flow used: " + body);
1.51
1.52 - int exp = LoopControl.simpleLoopTestWithExit(123);
1.53 - vm.assertExec("Is the code compilable?", LoopControl.class, "simpleLoopTestWithExit__II", exp, 123);
1.54 + ScriptEngine eng = new ScriptEngineManager().getEngineByMimeType("text/javascript");
1.55 + try {
1.56 + eng.eval(body);
1.57 + } catch (ScriptException ex) {
1.58 + fail("Cannot parse:\n" + body, ex);
1.59 + }
1.60 +
1.61 + vm.assertExec("Is the code compilable?", LoopControl.class, mangledSig, exp, 123);
1.62 }
1.63
1.64 }