diff --git a/generators/lua/lists.js b/generators/lua/lists.js index a122c9f2e..5c3831a02 100644 --- a/generators/lua/lists.js +++ b/generators/lua/lists.js @@ -17,7 +17,7 @@ goog.require('Blockly.Lua'); Blockly.Lua['lists_create_empty'] = function(block) { // Create an empty list. - return ['{}', Blockly.Lua.ORDER_ATOMIC]; + return ['{}', Blockly.Lua.ORDER_HIGH]; }; Blockly.Lua['lists_create_with'] = function(block) { @@ -28,7 +28,7 @@ Blockly.Lua['lists_create_with'] = function(block) { Blockly.Lua.ORDER_NONE) || 'None'; } var code = '{' + elements.join(', ') + '}'; - return [code, Blockly.Lua.ORDER_ATOMIC]; + return [code, Blockly.Lua.ORDER_HIGH]; }; Blockly.Lua['lists_repeat'] = function(block) { @@ -126,7 +126,7 @@ Blockly.Lua['lists_getIndex'] = function(block) { var mode = block.getFieldValue('MODE') || 'GET'; var where = block.getFieldValue('WHERE') || 'FROM_START'; var list = Blockly.Lua.valueToCode(block, 'VALUE', Blockly.Lua.ORDER_HIGH) || - '{}'; + '({})'; var getIndex_ = Blockly.Lua.lists.getIndex_; // If `list` would be evaluated more than once (which is the case for LAST, diff --git a/tests/generators/golden/generated.dart b/tests/generators/golden/generated.dart index 6a1311cbd..4d438a074 100644 --- a/tests/generators/golden/generated.dart +++ b/tests/generators/golden/generated.dart @@ -1027,17 +1027,29 @@ void test_get_lists_simple() { unittest_assertequals(list[list.length - (0 + 3)], 'Kirk', 'get #-end order simple'); } +dynamic lists_get_from_end(List my_list, num x) { + x = my_list.length - x; + return my_list[x]; +} + +/// Tests the "get" block with create list call. +void test_get_lists_create_list() { + unittest_assertequals(['Kirk', 'Spock', 'McCoy'].first, 'Kirk', 'get first create list'); + unittest_assertequals(['Kirk', 'Spock', 'McCoy'].last, 'McCoy', 'get last simple'); + unittest_assertequals(['Kirk', 'Spock', 'McCoy'].indexOf(lists_get_random_item(['Kirk', 'Spock', 'McCoy'])) + 1 > 0, true, 'get random simple'); + unittest_assertequals(['Kirk', 'Spock', 'McCoy'][1], 'Spock', 'get # simple'); + unittest_assertequals(['Kirk', 'Spock', 'McCoy'][((true ? 2 : null) - 1)], 'Spock', 'get # order simple'); + unittest_assertequals(lists_get_from_end(['Kirk', 'Spock', 'McCoy'], 3), 'Kirk', 'get #-end simple'); + // The order for index for #-end is addition because this will catch errors in generators where most perform the operation ... - index. + unittest_assertequals(lists_get_from_end(['Kirk', 'Spock', 'McCoy'], 0 + 3), 'Kirk', 'get #-end order simple'); +} + /// Creates a list for use with the get test. dynamic get_star_wars() { number_of_calls = (number_of_calls is num ? number_of_calls : 0) + 1; return ['Kirk', 'Spock', 'McCoy']; } -dynamic lists_get_from_end(List my_list, num x) { - x = my_list.length - x; - return my_list[x]; -} - /// Tests the "get" block with a function call. void test_get_lists_complex() { list = ['Kirk', 'Spock', 'McCoy']; @@ -1623,6 +1635,7 @@ main() { test_find_lists_simple(); test_find_lists_complex(); test_get_lists_simple(); + test_get_lists_create_list(); test_get_lists_complex(); test_getRemove(); test_remove(); diff --git a/tests/generators/golden/generated.js b/tests/generators/golden/generated.js index c9b80a7a7..424793112 100644 --- a/tests/generators/golden/generated.js +++ b/tests/generators/golden/generated.js @@ -1018,6 +1018,18 @@ function test_get_lists_simple() { assertEquals(list.slice((-(0 + 3)))[0], 'Kirk', 'get #-end order simple'); } +// Tests the "get" block with create list call. +function test_get_lists_create_list() { + assertEquals(['Kirk', 'Spock', 'McCoy'][0], 'Kirk', 'get first create list'); + assertEquals(['Kirk', 'Spock', 'McCoy'].slice(-1)[0], 'McCoy', 'get last simple'); + assertEquals(['Kirk', 'Spock', 'McCoy'].indexOf(listsGetRandomItem(['Kirk', 'Spock', 'McCoy'], false)) + 1 > 0, true, 'get random simple'); + assertEquals(['Kirk', 'Spock', 'McCoy'][1], 'Spock', 'get # simple'); + assertEquals(['Kirk', 'Spock', 'McCoy'][((true ? 2 : null) - 1)], 'Spock', 'get # order simple'); + assertEquals(['Kirk', 'Spock', 'McCoy'].slice(-3)[0], 'Kirk', 'get #-end simple'); + // The order for index for #-end is addition because this will catch errors in generators where most perform the operation ... - index. + assertEquals(['Kirk', 'Spock', 'McCoy'].slice((-(0 + 3)))[0], 'Kirk', 'get #-end order simple'); +} + // Creates a list for use with the get test. function get_star_wars() { number_of_calls = (typeof number_of_calls == 'number' ? number_of_calls : 0) + 1; @@ -1557,6 +1569,7 @@ test_lists_length(); test_find_lists_simple(); test_find_lists_complex(); test_get_lists_simple(); +test_get_lists_create_list(); test_get_lists_complex(); test_getRemove(); test_remove(); diff --git a/tests/generators/golden/generated.lua b/tests/generators/golden/generated.lua index 9b6acfa8b..d96ad64a9 100644 --- a/tests/generators/golden/generated.lua +++ b/tests/generators/golden/generated.lua @@ -1123,13 +1123,6 @@ function test_get_lists_simple() end --- Creates a list for use with the get test. -function get_star_wars() - number_of_calls = number_of_calls + 1 - return {'Kirk', 'Spock', 'McCoy'} -end - - function list_get_last(t) return t[#t] end @@ -1142,6 +1135,26 @@ function list_get_from_end(t, at) return t[#t + 1 - at] end +-- Tests the "get" block with create list call. +function test_get_lists_create_list() + assertEquals(({'Kirk', 'Spock', 'McCoy'})[1], 'Kirk', 'get first create list') + assertEquals(list_get_last(({'Kirk', 'Spock', 'McCoy'})), 'McCoy', 'get last simple') + assertEquals(first_index({'Kirk', 'Spock', 'McCoy'}, list_get_random(({'Kirk', 'Spock', 'McCoy'}))) > 0, true, 'get random simple') + assertEquals(({'Kirk', 'Spock', 'McCoy'})[2], 'Spock', 'get # simple') + assertEquals(({'Kirk', 'Spock', 'McCoy'})[true and 2 or nil], 'Spock', 'get # order simple') + assertEquals(list_get_from_end(({'Kirk', 'Spock', 'McCoy'}), 3), 'Kirk', 'get #-end simple') + -- The order for index for #-end is addition because this will catch errors in generators where most perform the operation ... - index. + assertEquals(list_get_from_end(({'Kirk', 'Spock', 'McCoy'}), 0 + 3), 'Kirk', 'get #-end order simple') +end + + +-- Creates a list for use with the get test. +function get_star_wars() + number_of_calls = number_of_calls + 1 + return {'Kirk', 'Spock', 'McCoy'} +end + + -- Tests the "get" block with a function call. function test_get_lists_complex() list = {'Kirk', 'Spock', 'McCoy'} @@ -1826,6 +1839,7 @@ test_lists_length() test_find_lists_simple() test_find_lists_complex() test_get_lists_simple() +test_get_lists_create_list() test_get_lists_complex() test_getRemove() test_remove() diff --git a/tests/generators/golden/generated.php b/tests/generators/golden/generated.php index a2d470de4..1b968b90d 100644 --- a/tests/generators/golden/generated.php +++ b/tests/generators/golden/generated.php @@ -1039,6 +1039,19 @@ function test_get_lists_simple() { assertEquals(array_slice($list2, (-(0 + 3)), 1)[0], 'Kirk', 'get #-end order simple'); } +// Tests the "get" block with create list call. +function test_get_lists_create_list() { + global $test_name, $naked, $proc_x, $proc_y, $func_x, $func_y, $func_a, $n, $ok, $log, $count, $varToChange, $rand, $item, $text, $number_of_calls, $list2, $proc_z, $func_z, $x, $proc_w, $func_c, $if2, $i, $loglist, $changing_list, $list_copy, $unittestResults; + assertEquals(array('Kirk', 'Spock', 'McCoy')[0], 'Kirk', 'get first create list'); + assertEquals(end(array('Kirk', 'Spock', 'McCoy')), 'McCoy', 'get last simple'); + assertEquals(indexOf(array('Kirk', 'Spock', 'McCoy'), lists_get_random_item(array('Kirk', 'Spock', 'McCoy'))) > 0, true, 'get random simple'); + assertEquals(array('Kirk', 'Spock', 'McCoy')[1], 'Spock', 'get # simple'); + assertEquals(array('Kirk', 'Spock', 'McCoy')[((true ? 2 : null) - 1)], 'Spock', 'get # order simple'); + assertEquals(array_slice(array('Kirk', 'Spock', 'McCoy'), -3, 1)[0], 'Kirk', 'get #-end simple'); + // The order for index for #-end is addition because this will catch errors in generators where most perform the operation ... - index. + assertEquals(array_slice(array('Kirk', 'Spock', 'McCoy'), (-(0 + 3)), 1)[0], 'Kirk', 'get #-end order simple'); +} + // Creates a list for use with the get test. function get_star_wars() { global $test_name, $naked, $proc_x, $proc_y, $func_x, $func_y, $func_a, $n, $ok, $log, $count, $varToChange, $rand, $item, $text, $number_of_calls, $list2, $proc_z, $func_z, $x, $proc_w, $func_c, $if2, $i, $loglist, $changing_list, $list_copy, $unittestResults; @@ -1650,6 +1663,7 @@ test_lists_length(); test_find_lists_simple(); test_find_lists_complex(); test_get_lists_simple(); +test_get_lists_create_list(); test_get_lists_complex(); test_getRemove(); test_remove(); diff --git a/tests/generators/golden/generated.py b/tests/generators/golden/generated.py index b20142bb6..8120ae899 100644 --- a/tests/generators/golden/generated.py +++ b/tests/generators/golden/generated.py @@ -872,6 +872,18 @@ def test_get_lists_simple(): # The order for index for #-end is addition because this will catch errors in generators where most perform the operation ... - index. assertEquals(list2[-int(0 + 3)], 'Kirk', 'get #-end order simple') +# Tests the "get" block with create list call. +def test_get_lists_create_list(): + global test_name, naked, proc_x, proc_y, func_x, func_y, func_a, n, ok, log, count, varToChange, rand, item, text, number_of_calls, list2, proc_z, func_z, x, proc_w, func_c, if2, i, loglist, changing_list, list_copy, unittestResults + assertEquals(['Kirk', 'Spock', 'McCoy'][0], 'Kirk', 'get first create list') + assertEquals(['Kirk', 'Spock', 'McCoy'][-1], 'McCoy', 'get last simple') + assertEquals(first_index(['Kirk', 'Spock', 'McCoy'], random.choice(['Kirk', 'Spock', 'McCoy'])) > 0, True, 'get random simple') + assertEquals(['Kirk', 'Spock', 'McCoy'][1], 'Spock', 'get # simple') + assertEquals(['Kirk', 'Spock', 'McCoy'][int((2 if True else None) - 1)], 'Spock', 'get # order simple') + assertEquals(['Kirk', 'Spock', 'McCoy'][-3], 'Kirk', 'get #-end simple') + # The order for index for #-end is addition because this will catch errors in generators where most perform the operation ... - index. + assertEquals(['Kirk', 'Spock', 'McCoy'][-int(0 + 3)], 'Kirk', 'get #-end order simple') + # Creates a list for use with the get test. def get_star_wars(): global test_name, naked, proc_x, proc_y, func_x, func_y, func_a, n, ok, log, count, varToChange, rand, item, text, number_of_calls, list2, proc_z, func_z, x, proc_w, func_c, if2, i, loglist, changing_list, list_copy, unittestResults @@ -1395,6 +1407,7 @@ test_lists_length() test_find_lists_simple() test_find_lists_complex() test_get_lists_simple() +test_get_lists_create_list() test_get_lists_complex() test_getRemove() test_remove() diff --git a/tests/generators/lists.xml b/tests/generators/lists.xml index ccb76a946..ce8a14369 100644 --- a/tests/generators/lists.xml +++ b/tests/generators/lists.xml @@ -21,43 +21,48 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + + + + + + @@ -97,7 +102,7 @@ - + @@ -133,7 +138,7 @@ - + test create lists Tests the "create list with" and "create empty list" blocks. @@ -289,7 +294,7 @@ - + get empty list Creates an empty list for use with the empty test. @@ -297,7 +302,7 @@ - + test lists empty Tests the "is empty" block. @@ -390,7 +395,7 @@ - + test lists length Tests the "length" block. @@ -524,7 +529,7 @@ - + test find lists simple Tests the "find" block with a variable. @@ -657,7 +662,7 @@ - + get names Creates a list for use with the find test. @@ -696,7 +701,7 @@ - + test find lists complex Tests the "find" block with a function call. @@ -1066,7 +1071,7 @@ - + test get lists simple Tests the "get" block with a variable. @@ -1356,7 +1361,392 @@ - + + test get lists create list + Tests the "get" block with create list call. + + + + + get first create list + + + + + + GET + FIRST + + + + + + Kirk + + + + + Spock + + + + + McCoy + + + + + + + + + Kirk + + + + + + + get last simple + + + + + + GET + LAST + + + + + + Kirk + + + + + Spock + + + + + McCoy + + + + + + + + + McCoy + + + + + TRUE + + + get random simple + + + + + GT + + + FIRST + + + + + + Kirk + + + + + Spock + + + + + McCoy + + + + + + + + GET + RANDOM + + + + + + Kirk + + + + + Spock + + + + + McCoy + + + + + + + + + + + + + -1 + + + + + + + + + + + get # simple + + + + + + GET + FROM_START + + + + + + Kirk + + + + + Spock + + + + + McCoy + + + + + + + + + 1 + + + + + + + + + Spock + + + + + + + get # order simple + + + + + + GET + FROM_START + + + + + + Kirk + + + + + Spock + + + + + McCoy + + + + + + + + + TRUE + + + + + + + 1 + + + + + + + + + + + + + + Spock + + + + + + + get #-end simple + + + + + + GET + FROM_END + + + + + + Kirk + + + + + Spock + + + + + McCoy + + + + + + + + + 2 + + + + + + + + + Kirk + + + + + + + get #-end order simple + + + + + + GET + FROM_END + The order for index for #-end is addition because this will catch errors in generators where most perform the operation ... - index. + + + + + + Kirk + + + + + Spock + + + + + McCoy + + + + + + + ADD + + + 0 + + + + + + + 2 + + + + + + + + + + + Kirk + + + + + + + + + + + + + + + + + + get star wars Creates a list for use with the get test. @@ -1390,7 +1780,7 @@ - + test get lists complex Tests the "get" block with a function call. @@ -2058,7 +2448,7 @@ - + test getRemove Tests the "get and remove" block. @@ -3009,7 +3399,7 @@ - + test remove Tests the "remove" block. @@ -3780,7 +4170,7 @@ - + test set Tests the "set" block. @@ -4641,7 +5031,7 @@ - + test insert Tests the "insert" block. @@ -5542,7 +5932,7 @@ - + test sublist simple Tests the "get sub-list" block with a variable. @@ -6417,7 +6807,7 @@ - + get space shuttles Creates a list for use with the sublist test. @@ -6461,7 +6851,7 @@ - + test sublist complex Tests the "get sub-list" block with a function call. @@ -7557,7 +7947,7 @@ - + test join Tests the "join" block. @@ -7658,7 +8048,7 @@ - + test split Tests the "split" block. @@ -7774,7 +8164,7 @@ - + test sort alphabetic Tests the "alphabetic sort" block. @@ -7895,7 +8285,7 @@ - + test sort ignoreCase Tests the "alphabetic sort ignore case" block. @@ -8016,7 +8406,7 @@ - + test sort numeric Tests the "numeric sort" block. @@ -8137,7 +8527,7 @@ - + test lists reverse Tests the "list reverse" block. @@ -8282,4 +8672,4 @@ - + \ No newline at end of file