def test_timer_block(self): cls = self.__class__ script_data = [0, 0, [["whenGreenFlag"], ["wait:elapsed:from:", ["timer"]]]] cls.TIMER_HELPER_OBJECTS_DATA_TEMPLATE["children"][0]["scripts"] = [script_data] raw_project = scratch.RawProject(cls.TIMER_HELPER_OBJECTS_DATA_TEMPLATE) expected_background_object_script_data = [0, 0, [['whenGreenFlag'], ['doForever', [ ['changeVar:by:', scratch.S2CC_TIMER_VARIABLE_NAME, 0.03], ['wait:elapsed:from:', 0.03] ]] ]] expected_first_object_script_data = [0, 0, [["whenGreenFlag"], ['wait:elapsed:from:', ['readVariable', scratch.S2CC_TIMER_VARIABLE_NAME]] ]] # validate assert len(raw_project.objects) == 2 [background_object, first_object] = raw_project.objects # background object assert len(background_object.scripts) == 1 script = background_object.scripts[0] expected_script = scratch.Script(expected_background_object_script_data) assert script == expected_script # first object assert len(first_object.scripts) == 1 script = first_object.scripts[0] expected_script = scratch.Script(expected_first_object_script_data) assert script == expected_script # global variables global_variables = background_object._dict_object["variables"] assert len(global_variables) == 1 assert global_variables[0] == { "name": scratch.S2CC_TIMER_VARIABLE_NAME, "value": 0, "isPersistent": False }
def test_two_distance_blocks_in_different_objects(self): cls = self.__class__ script_data = [0, 0, [["whenGreenFlag"], ["forward:", ["distanceTo:", "Sprite2"]]]] cls.DISTANCE_HELPER_OBJECTS_DATA_TEMPLATE["scripts"] = [script_data] cls.DISTANCE_HELPER_OBJECTS_DATA_TEMPLATE["children"][0]["scripts"] = [script_data] cls.DISTANCE_HELPER_OBJECTS_DATA_TEMPLATE["children"][1]["scripts"] = [] raw_project = scratch.RawProject(cls.DISTANCE_HELPER_OBJECTS_DATA_TEMPLATE) position_x_var_name = scratch.S2CC_POSITION_X_VARIABLE_NAME_PREFIX + "Sprite2" position_y_var_name = scratch.S2CC_POSITION_Y_VARIABLE_NAME_PREFIX + "Sprite2" expected_background_and_first_object_script_data = [0, 0, [["whenGreenFlag"], ['forward:', ["computeFunction:of:", "sqrt", ["+", ["*", ["()", ["-", ["xpos"], ["readVariable", position_x_var_name]]], ["()", ["-", ["xpos"], ["readVariable", position_x_var_name]]] ], ["*", ["()", ["-", ["ypos"], ["readVariable", position_y_var_name]]], ["()", ["-", ["ypos"], ["readVariable", position_y_var_name]]] ] ]] ]]] expected_second_object_script_data = [0, 0, [['whenGreenFlag'], ["doForever", [ ["setVar:to:", position_x_var_name, ["xpos"]], ["setVar:to:", position_y_var_name, ["ypos"]], ["wait:elapsed:from:", 0.03] ]] ]] # validate assert len(raw_project.objects) == 3 [background_object, first_object, second_object] = raw_project.objects # background object assert len(background_object.scripts) == 1 script = background_object.scripts[0] expected_script = scratch.Script(expected_background_and_first_object_script_data) assert script == expected_script # first object assert len(first_object.scripts) == 1 script = first_object.scripts[0] expected_script = scratch.Script(expected_background_and_first_object_script_data) assert script == expected_script # second object assert len(second_object.scripts) == 1 script = second_object.scripts[0] expected_script = scratch.Script(expected_second_object_script_data) assert script == expected_script # global variables global_variables = background_object._dict_object["variables"] assert len(global_variables) == 2 assert global_variables[0] == { "name": position_x_var_name, "value": 0, "isPersistent": False } assert global_variables[1] == { "name": position_y_var_name, "value": 0, "isPersistent": False }
def test_reset_timer_block_with_existant_timer_block_in_other_object_must_NOT_be_ignored(self): cls = self.__class__ background_script_data = [0, 0, [["whenGreenFlag"], ["timerReset"]]] cls.TIMER_HELPER_OBJECTS_DATA_TEMPLATE["scripts"] = [background_script_data] expected_background_object_scripts_data = [[0, 0, [['whenGreenFlag'], ["doBroadcastAndWait", scratch.S2CC_TIMER_RESET_BROADCAST_MESSAGE] ]], [0, 0, [['whenGreenFlag'], ['doForever', [ ['changeVar:by:', scratch.S2CC_TIMER_VARIABLE_NAME, 0.03], ['wait:elapsed:from:', 0.03] ]]] ], [0, 0, [['whenIReceive', scratch.S2CC_TIMER_RESET_BROADCAST_MESSAGE], ['setVar:to:', scratch.S2CC_TIMER_VARIABLE_NAME, 0] ]]] # first object scripts first_object_script_data = [0, 0, [["whenGreenFlag"], ["wait:elapsed:from:", ["timer"]]]] cls.TIMER_HELPER_OBJECTS_DATA_TEMPLATE["children"][0]["scripts"] = [first_object_script_data] expected_first_object_script_data = [0, 0, [["whenGreenFlag"], ['wait:elapsed:from:', ['readVariable', scratch.S2CC_TIMER_VARIABLE_NAME]] ]] # create project raw_project = scratch.RawProject(cls.TIMER_HELPER_OBJECTS_DATA_TEMPLATE) # validate assert len(raw_project.objects) == 2 [background_object, first_object] = raw_project.objects # background object assert len(background_object.scripts) == 3 script = background_object.scripts[0] expected_script = scratch.Script(expected_background_object_scripts_data[0]) assert script == expected_script script = background_object.scripts[1] expected_script = scratch.Script(expected_background_object_scripts_data[1]) assert script == expected_script script = background_object.scripts[2] expected_script = scratch.Script(expected_background_object_scripts_data[2]) assert script == expected_script # first object assert len(first_object.scripts) == 1 script = first_object.scripts[0] expected_script = scratch.Script(expected_first_object_script_data) assert script == expected_script # global variables global_variables = background_object._dict_object["variables"] assert len(global_variables) == 1 assert global_variables[0] == { "name": scratch.S2CC_TIMER_VARIABLE_NAME, "value": 0, "isPersistent": False }
def test_reset_timer_block_with_non_existant_timer_block_should_be_ignored(self): cls = self.__class__ script_data = [0, 0, [["whenGreenFlag"], ["timerReset"]]] cls.TIMER_HELPER_OBJECTS_DATA_TEMPLATE["children"][0]["scripts"] = [script_data] expected_first_object_script_data = [0, 0, [["whenGreenFlag"], ['doBroadcastAndWait', scratch.S2CC_TIMER_RESET_BROADCAST_MESSAGE] ]] # create project raw_project = scratch.RawProject(cls.TIMER_HELPER_OBJECTS_DATA_TEMPLATE) # validate assert len(raw_project.objects) == 2 [background_object, first_object] = raw_project.objects # background object assert len(background_object.scripts) == 0 # first object assert len(first_object.scripts) == 1 script = first_object.scripts[0] expected_script = scratch.Script(expected_first_object_script_data) assert script == expected_script # global variables global_variables = background_object._dict_object["variables"] assert len(global_variables) == 0
def test_verify_simple_formula_in_script_element_tree(self): script_data = [0, 0, [["whenGreenFlag"], ["wait:elapsed:from:", ["randomFrom:to:", -100, 100]]]] expected_raw_script_data = [["whenGreenFlag"], ["wait:elapsed:from:", ["randomFrom:to:", -100, 100]]] script = scratch.Script(script_data) assert script.type == scratch.SCRIPT_GREEN_FLAG assert len(script.arguments) == 0 assert script.raw_script == expected_raw_script_data assert script.blocks == expected_raw_script_data[1:] assert isinstance(script.script_element, scratch.BlockList) assert script.script_element.name == '<LIST>' assert len(script.script_element.children) == 1 script_element_child = script.script_element.children[0] assert script_element_child.name == 'wait:elapsed:from:' assert len(script_element_child.children) == 1 script_element_child = script_element_child.children[0] assert script_element_child.name == 'randomFrom:to:' assert len(script_element_child.children) == 2 left_child = script_element_child.children[0] right_child = script_element_child.children[1] assert left_child.name == -100 assert len(left_child.children) == 0 assert right_child.name == 100 assert len(right_child.children) == 0
def test_can_construct_on_correct_input(self): expected_values = ( (4, (2, 2, 2, 1)), (1, (2,)), (1, (2,)), ) for script_input, [expected_length, expected_block_children_number] in zip(EASY_SCRIPTS, expected_values): assert len(scratch.Script(script_input).blocks) == expected_length blocks = scratch.ScriptElement.from_raw_script(script_input) assert isinstance(blocks, scratch.BlockList) and len(blocks.children) == expected_length nested_blocks = blocks.children assert all(isinstance(block, scratch.ScriptElement) for block in nested_blocks) assert [len(block.children) for block in nested_blocks] == list(expected_block_children_number)
def test_verify_complex_script_element_tree(self): script_data = [0, 0, [["whenGreenFlag"], ["wait:elapsed:from:", ["+", \ 0, ["randomFrom:to:", 0, 100]]], ['changeVar:by:', "temp", 0.1]]] expected_raw_script_data = [["whenGreenFlag"], ["wait:elapsed:from:", ["+", \ 0, ["randomFrom:to:", 0, 100]]], ['changeVar:by:', "temp", 0.1]] script = scratch.Script(script_data) assert script.type == scratch.SCRIPT_GREEN_FLAG assert len(script.arguments) == 0 assert script.raw_script == expected_raw_script_data assert script.blocks == expected_raw_script_data[1:] assert isinstance(script.script_element, scratch.BlockList) assert script.script_element.name == '<LIST>' assert len(script.script_element.children) == 2 script_element_child = script.script_element.children[0] assert script_element_child.name == 'wait:elapsed:from:' assert len(script_element_child.children) == 1 script_element_child = script_element_child.children[0] assert script_element_child.name == '+' assert len(script_element_child.children) == 2 left_child = script_element_child.children[0] right_child = script_element_child.children[1] assert left_child.name == 0 assert len(left_child.children) == 0 assert right_child.name == 'randomFrom:to:' assert len(right_child.children) == 2 left_child = right_child.children[0] right_child = right_child.children[1] assert left_child.name == 0 assert len(left_child.children) == 0 assert right_child.name == 100 assert len(right_child.children) == 0 script_element_child = script.script_element.children[1] assert script_element_child.name == 'changeVar:by:' assert len(script_element_child.children) == 2 left_child = script_element_child.children[0] right_child = script_element_child.children[1] assert left_child.name == 'temp' assert len(left_child.children) == 0 assert right_child.name == 0.1 assert len(right_child.children) == 0
def setUp(self): unittest.TestCase.setUp(self) self.input_data = EASY_SCRIPTS[0] self.script = scratch.Script(self.input_data)
def test_can_construct_on_correct_input(self): for script_input in EASY_SCRIPTS: script = scratch.Script(script_input) assert script
def test_can_construct_on_correct_input(self): for script_input in EASY_SCRIPTS: script = scratch.Script(script_input) assert script def test_fail_on_wrong_input(self): faulty_script_structures = [ [], "a string is no correct input", ["the first 2 params", "must be integers", []], [0101, 444, "further the third one must be a list"] ] for faulty_input in faulty_script_structures: assert not scratch.Script.is_valid_script_input(faulty_input) with self.assertRaises(scratch.ScriptError): scratch.Script(faulty_input) class TestScriptFunc(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) self.input_data = EASY_SCRIPTS[0] self.script = scratch.Script(self.input_data) def test_can_access_blocks_from_scratch_script(self): assert self.script.get_type() == 'whenGreenFlag' expected_block_names = ["say:duration:elapsed:from:", "doRepeat", "changeGraphicEffect:by:", "say:"] assert [block[0] for block in self.script.blocks] == expected_block_names assert self.script.raw_script == self.input_data[2]