Exemplo n.º 1
0
    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
Exemplo n.º 2
0
    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 }
Exemplo n.º 3
0
    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 }
Exemplo n.º 4
0
    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 }