From 835fac50cb91cacbbac3f6d7d156ef5b62dadec1 Mon Sep 17 00:00:00 2001 From: tobi Date: Thu, 29 Jul 2021 17:43:22 +0200 Subject: [PATCH] STCC-228 Delete all items block added [TN] --- src/scratchtocatrobat/converter/converter.py | 21 +++++++------- .../converter/test_converter.py | 29 +++---------------- .../scratch/scratch3visitor/blockmapping.py | 2 ++ .../scratch/scratch3visitor/data.py | 5 ++++ .../scratch3visitor/scratch2_json_format.py | 1 + 5 files changed, 23 insertions(+), 35 deletions(-) diff --git a/src/scratchtocatrobat/converter/converter.py b/src/scratchtocatrobat/converter/converter.py index d9ccff37..8b8618d2 100644 --- a/src/scratchtocatrobat/converter/converter.py +++ b/src/scratchtocatrobat/converter/converter.py @@ -400,6 +400,7 @@ class _ScratchToCatrobat(object): "append:toList:": catbricks.AddItemToUserListBrick, "insert:at:ofList:": catbricks.InsertItemIntoUserListBrick, "deleteLine:ofList:": catbricks.DeleteItemOfUserListBrick, + "data_deletealloflist": catbricks.ClearUserListBrick, "setLine:ofList:to:": catbricks.ReplaceItemInUserListBrick, "contentsOfList:": None, "showList:": None, @@ -2858,16 +2859,6 @@ def _convert_delete_line_of_list_block(self): deleteItemBrick.setFormulaWithBrickField(catbricks.Brick.BrickField.LIST_DELETE_ITEM, index_formula) return deleteItemBrick - if position == "all": - loop_condition_formula = catrobat.create_formula_with_value(self._converted_helper_brick_or_formula_element([list_name], "lineCountOfList:")) - catr_loop_start_brick = catbricks.RepeatBrick(loop_condition_formula) - - index_formula = catrobat.create_formula_with_value("1") # first item to be deleted for n-times! - assert index_formula is not None - deleteItemBrick.setFormulaWithBrickField(catbricks.Brick.BrickField.LIST_DELETE_ITEM, index_formula) - catr_loop_start_brick.loopBricks.add(deleteItemBrick) - - return catr_loop_start_brick index_formula = catrobat.create_formula_with_value(position) assert index_formula is not None @@ -2875,6 +2866,16 @@ def _convert_delete_line_of_list_block(self): return deleteItemBrick + @_register_handler(_block_name_to_handler_map, "data_deletealloflist") + def _convert_delete_all_of_list_block(self): + [list_name] = self.arguments + user_list = catrobat.find_global_or_sprite_user_list_by_name(self.scene, self.sprite, list_name) + assert user_list is not None + deleteItemBrick = self.CatrobatClass() + deleteItemBrick.userList = user_list + + return deleteItemBrick + @_register_handler(_block_name_to_handler_map, "setLine:ofList:to:") def _convert_set_line_of_list_to_block(self): [position, list_name, value] = self.arguments diff --git a/src/scratchtocatrobat/converter/test_converter.py b/src/scratchtocatrobat/converter/test_converter.py index 56afa664..b5b131ec 100644 --- a/src/scratchtocatrobat/converter/test_converter.py +++ b/src/scratchtocatrobat/converter/test_converter.py @@ -1444,33 +1444,12 @@ def test_can_convert_delete_last_line_from_list_block(self): assert formula_element_left_child.leftChild == None assert formula_element_left_child.rightChild == None - # deleteLine:ofList: + # data_deletealloflist def test_can_convert_delete_all_lines_from_list_block(self): - scratch_block = ["deleteLine:ofList:", "all", self._name_of_test_list] - catr_brick_list = self.block_converter._catrobat_bricks_from(scratch_block, DUMMY_CATR_SPRITE) - - assert len(catr_brick_list) == 1 - assert isinstance(catr_brick_list[0], catbricks.RepeatBrick) - assert len(catr_brick_list[0].loopBricks) == 1 - assert isinstance(catr_brick_list[0].loopBricks[0], catbricks.DeleteItemOfUserListBrick) - - formula_tree_times_to_repeat = catr_brick_list[0].getFormulaWithBrickField(catbasebrick.BrickField.TIMES_TO_REPEAT).formulaTree # @UndefinedVariable - assert formula_tree_times_to_repeat.type == catformula.FormulaElement.ElementType.FUNCTION - assert formula_tree_times_to_repeat.value == "NUMBER_OF_ITEMS" - assert formula_tree_times_to_repeat.leftChild - assert formula_tree_times_to_repeat.rightChild == None - formula_element_left_child = formula_tree_times_to_repeat.leftChild - assert formula_element_left_child.type == catformula.FormulaElement.ElementType.USER_LIST - assert formula_element_left_child.value == self._name_of_test_list - assert formula_element_left_child.leftChild == None - assert formula_element_left_child.rightChild == None - - formula_tree_list_delete_item = catr_brick_list[0].loopBricks[0].getFormulaWithBrickField(catbasebrick.BrickField.LIST_DELETE_ITEM).formulaTree # @UndefinedVariable - assert formula_tree_list_delete_item.type == catformula.FormulaElement.ElementType.NUMBER - assert formula_tree_list_delete_item.value == "1" - assert formula_tree_list_delete_item.leftChild == None - assert formula_tree_list_delete_item.rightChild == None + scratch_block = ["data_deletealloflist", self._name_of_test_list] + [catr_brick_list] = self.block_converter._catrobat_bricks_from(scratch_block, DUMMY_CATR_SPRITE) + assert isinstance(catr_brick_list, catbricks.ClearUserListBrick) # setLine:ofList:to: def test_can_convert_set_line_via_str_index_of_list_to_block(self): diff --git a/src/scratchtocatrobat/scratch/scratch3visitor/blockmapping.py b/src/scratchtocatrobat/scratch/scratch3visitor/blockmapping.py index 2a7878bb..7b931058 100644 --- a/src/scratchtocatrobat/scratch/scratch3visitor/blockmapping.py +++ b/src/scratchtocatrobat/scratch/scratch3visitor/blockmapping.py @@ -1,4 +1,5 @@ import looks, motion, event, sensing, sound, operator, control, data, pen +from scratchtocatrobat.scratch.scratch3visitor.scratch2_json_format import Scratch3_2Opcodes as opcodes visitormap = { ### event blocks #### "event_whenflagclicked" : event.visitWhenflagclicked, #tested @@ -136,6 +137,7 @@ ### data blocks #### "data_addtolist" : data.visitAddtolist,#tested "data_deleteoflist" : data.visitDeleteoflist,#tested + opcodes.DATA_DELETE_ALL_OF_LIST: data.visitDeletealloflist,#tested "data_insertatlist" : data.visitInsertatlist,#tested "data_replaceitemoflist" : data.visitReplaceitemoflist,#tested "data_itemoflist" : data.visitItemoflist,#tested diff --git a/src/scratchtocatrobat/scratch/scratch3visitor/data.py b/src/scratchtocatrobat/scratch/scratch3visitor/data.py index 711d8937..20436660 100644 --- a/src/scratchtocatrobat/scratch/scratch3visitor/data.py +++ b/src/scratchtocatrobat/scratch/scratch3visitor/data.py @@ -37,6 +37,11 @@ def visitDeleteoflist(blockcontext): index = visitGeneric(blockcontext, "INDEX") return ["deleteLine:ofList:", index, list] +def visitDeletealloflist(blockcontext): + block = blockcontext.block + list = block.fields["LIST"][0] + return ["data_deletealloflist", list] + def visitInsertatlist(blockcontext): block = blockcontext.block list = block.fields["LIST"][0] diff --git a/src/scratchtocatrobat/scratch/scratch3visitor/scratch2_json_format.py b/src/scratchtocatrobat/scratch/scratch3visitor/scratch2_json_format.py index 1b873e61..5a6e7aad 100644 --- a/src/scratchtocatrobat/scratch/scratch3visitor/scratch2_json_format.py +++ b/src/scratchtocatrobat/scratch/scratch3visitor/scratch2_json_format.py @@ -81,6 +81,7 @@ class Scratch3_2Opcodes(object): DATA_HIDE_VARIABLE = "data_hidevariable" DATA_ADD_TO_LIST = "data_addtolist" DATA_DELETE_OF_LIST = "data_deleteoflist" + DATA_DELETE_ALL_OF_LIST = "data_deletealloflist" DATA_INSERT_AT_LIST = "data_insertatlist" DATA_REPLACE_ITEM_OF_LIST = "data_replaceitemoflist" DATA_ITEM_OF_LIST = "data_itemoflist"