From 4db980f1fd6753151dc1d7996db39ad9721a7e5f Mon Sep 17 00:00:00 2001 From: Ken Soh Date: Tue, 14 May 2019 01:44:18 +0800 Subject: [PATCH] #423 - enable check step within for loop https://github.com/kelaberetiv/TagUI/issues/423 update summary - check step was not originally designed to work in for loops. this update allows check step to work in for loops, and also adds test cases to verify that check step can work within for loops. technical details - check step will insert an if block into the code block that immediately closes without normal processing line by line. this will cause TagUI to be mistaken that parsing is still within an if block and not handling a for loop block as it should be handled. in this change, a helper function check_intent_clear_injected_if_block() is added and called during parsing of check step to update TagUI internal tracking that the if block has been closed at end of the check step. that allows for loops to function correctly when check step is used within a loop. --- src/tagui_parse.php | 10 ++++++++-- src/test/positive_test | 9 +++++++++ src/test/positive_test.signature | 27 +++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/tagui_parse.php b/src/tagui_parse.php index 0bce2b5a..43522329 100755 --- a/src/tagui_parse.php +++ b/src/tagui_parse.php @@ -857,6 +857,10 @@ function mouse_intent($raw_intent) { else if (strtolower($params) == "up") return "casper.then(function() {".call_sikuli($raw_intent,"up"); else echo "ERROR - " . current_line() . " cannot understand step " . $raw_intent . "\n";} +// helper function as check_intent() adds an if block that immediately closes without going through closure handling +function check_intent_clear_injected_if_block() {$last_delimiter_pos = strrpos($GLOBALS['code_block_tracker'],"|"); +$GLOBALS['code_block_tracker']=substr($GLOBALS['code_block_tracker'],0,$last_delimiter_pos); return "";} + function check_intent($raw_intent) { $params = trim(substr($raw_intent." ",1+strpos($raw_intent." "," "))); $params = str_replace("||"," JAVASCRIPT_OR ",$params); // to handle conflict with "|" delimiter @@ -867,8 +871,10 @@ function check_intent($raw_intent) { if (substr_count($params,"|")!=2) echo "ERROR - " . current_line() . " if/true/false missing for " . $raw_intent . "\n"; else if (getenv('tagui_test_mode') == 'true') return "casper.then(function() {"."{".parse_condition("if ".$param1). -"\ntest.assert(true,".add_concat($param2).");\nelse test.assert(false,".add_concat($param3).");}".end_fi()."});"."\n\n"; -else return "casper.then(function() {"."{".parse_condition("if ".$param1)."\nthis.echo(".add_concat($param2).");\nelse this.echo(".add_concat($param3).");}".end_fi()."});"."\n\n";} +"\ntest.assert(true,".add_concat($param2).");\nelse test.assert(false,".add_concat($param3).");}". +check_intent_clear_injected_if_block().end_fi()."});"."\n\n"; +else return "casper.then(function() {"."{".parse_condition("if ".$param1)."\nthis.echo(".add_concat($param2). +");\nelse this.echo(".add_concat($param3).");}".check_intent_clear_injected_if_block().end_fi()."});"."\n\n";} function test_intent($raw_intent) { echo "ERROR - " . current_line() . " use CasperJS tester module to professionally " . $raw_intent . "\n"; diff --git a/src/test/positive_test b/src/test/positive_test index 535024fc..1efa7b01 100644 --- a/src/test/positive_test +++ b/src/test/positive_test @@ -438,6 +438,15 @@ check eggs lesser than 10|"eggs lesser than 10"|"eggs not lesser than 10" check eggs lesser than 10 | 'eggs lesser than 10' | 'eggs not lesser than 10' check eggs lesser than 10|'eggs lesser than 10'|'eggs not lesser than 10' +// test check within for loop +for n from 1 to 10 +check eggs lesser than 10 | "eggs lesser than 10" | "eggs not lesser than 10" + +for n from 1 to 10 +{ +check eggs lesser than 10 | "eggs lesser than 10" | "eggs not lesser than 10" +} + // test frame frame mainframe echo "test mainframe" diff --git a/src/test/positive_test.signature b/src/test/positive_test.signature index 21402865..ec5d2dab 100644 --- a/src/test/positive_test.signature +++ b/src/test/positive_test.signature @@ -2733,6 +2733,33 @@ casper.then(function() {{if ((eggs < 10)) this.echo('eggs lesser than 10'); else this.echo('eggs not lesser than 10');}}); +// test check within for loop +casper.then(function() {for (n=1; n<=10; n++) +{casper.then(function() {for_loop_signal = '[CONTINUE_SIGNAL][n]';}); +(function (n) { // start of IIFE pattern +{ // start of code block + +casper.then(function() {{if ((eggs < 10)) +this.echo("eggs lesser than 10"); +else this.echo("eggs not lesser than 10");}}); + +} // end of code block +})(n); // end of IIFE pattern, with dummy marker for break step +casper.then(function() {for_loop_signal = '[BREAK_SIGNAL][n]';});}}); + +casper.then(function() {for (n=1; n<=10; n++) +{casper.then(function() {for_loop_signal = '[CONTINUE_SIGNAL][n]';}); +(function (n) { // start of IIFE pattern +{ // start of code block + +casper.then(function() {{if ((eggs < 10)) +this.echo("eggs lesser than 10"); +else this.echo("eggs not lesser than 10");}}); + +} // end of code block +})(n); // end of IIFE pattern, with dummy marker for break step +casper.then(function() {for_loop_signal = '[BREAK_SIGNAL][n]';});}}); + // test frame casper.then(function() {techo('frame mainframe');}); casper.withFrame('mainframe', function() {