def day_of_week_works(p_scripts, p_points): """ :param p_scripts: json data from file, which is the code of the scratch file. (dict) :param p_points: Number of points this test is worth (int) :return: The test dictionary """ from app.scratch_labs.scratch_2_2 import brickLayer, do_sprite from app.scratch_labs.scratch import match_string p_test = {"name": "Checking that the day_of_week custom block with one input parameter works" " (" + str(p_points) + " points)<br>", "pass": False, "pass_message": "<h5 style=\"color:green;\">Pass!</h5> " "The day_of_week custom block with one input parameter works.<br>", "fail_message": "<h5 style=\"color:red;\">Fail.</h5> " "The day_of_week custom block with one input parameter does not appear to work." "Is the input parameter named day?<br>" "BE SURE INPUT PARAMETER IS NAMED day OTHERWISE THIS TEST BREAKS.<br>", "points": 0 } if 'day_of_week %s' in p_scripts.keys(): sprite = brickLayer(0, 0, 0, pendown=False, variables={"day": 1}) script = p_scripts['day_of_week %s'] day_success_1 = do_sprite(sprite, script, True) print("VALHALLA {}".format(sprite.say_history)) test_1 = match_string(r'(sunday|Sunday|SUNDAY)', sprite.say_history) if test_1['pass'] is False: p_test['fail_message'] += "Didn't find the word 'sunday' or 'Sunday' or 'SUNDAY' " \ "in say output when I called custom block day_of_week " \ "with input parameter 1<br> Got back this: <br>" \ + sprite.say_history + "<br>" sprite2 = brickLayer(0, 0, 0, pendown=False, variables={"day": 5}) script = p_scripts['day_of_week %s'] day_success_2 = do_sprite(sprite2, script, True) print("VALHALLA {}".format(sprite2.say_history)) test_2 = match_string(r'(friday|Friday|FRIDAY)', sprite2.say_history) if test_2['pass'] is False: p_test['fail_message'] += "Didn't find the word 'friday', 'Friday', or 'FRIDAY' " \ "in say output when I called custom block day_of_week " \ "with input parameter 6<br> Got back this: <br>" \ + sprite2.say_history + "<br>" if day_success_1 and day_success_2 and test_1['pass'] and test_2['pass']: p_test['pass'] = True p_test['points'] += p_points return p_test
def between_works_equal(p_scripts, p_points): """ :param p_scripts: json data from file, which is the code of the scratch file. (dict) :param p_points: Number of points this test is worth (int) :return: The test dictionary """ from app.scratch_labs.scratch_2_2 import brickLayer, do_sprite from app.scratch_labs.scratch import match_string p_test = {"name": "Checking that the between custom block with three input parameters works when number1 " "is EQUAL to number2 or number3" " (" + str(p_points) + " points)<br>", "pass": False, "pass_message": "<h5 style=\"color:green;\">Pass!</h5> " "The between custom block with three input parameters works.<br>", "fail_message": "<h5 style=\"color:red;\">Fail.</h5> " "The between custom block with three input parameters does not appear to work in this " "scenario.<br>" "Is the input parameters named 'number1', 'number2', 'number3'?<br>" "BE SURE INPUT PARAMETER IS NAMED CORRECTLY OTHERWISE THIS TEST BREAKS.<br>" "The script must say 'True' exactly (capital T, lowercase everything else).<br>", "points": 0 } if 'between %s %s %s' in p_scripts.keys(): print("ppp STARTING") sprite = brickLayer(0, 0, 0, pendown=False, variables={"number1": 5, "number2": 5, "number3": 2}) script = p_scripts['between %s %s %s'] day_success_1 = do_sprite(sprite, script, True) print("VALHALLA {}".format(sprite.say_history)) test_1 = match_string(r'^True', sprite.say_history) if test_1['pass'] is False: p_test['fail_message'] += "Called custom block 'between' with number1 = 5, number2 = 5, number3 = 2<br>" \ "Expect 'True', got this:<br>" + sprite.say_history sprite = brickLayer(0, 0, 0, pendown=False, variables={"number1": 5, "number2": 6665, "number3": 5}) day_success_2 = do_sprite(sprite, script, True) test_2 = match_string(r'^True', sprite.say_history) if test_2['pass'] is False: p_test['fail_message'] += "Called custom block 'between' with number1 = 5, number2 = 6665, number3 = 5<br>" \ "Expect 'True', got this:<br>" + sprite.say_history if day_success_1 and day_success_2 and test_1['pass'] and test_2['pass']: p_test['pass'] = True p_test['points'] += p_points return p_test
def min_works(p_scripts, p_points): """ :param p_scripts: json data from file, which is the code of the scratch file. (dict) :param p_points: Number of points this test is worth (int) :return: The test dictionary """ from app.scratch_labs.scratch_2_2 import brickLayer, do_sprite from app.scratch_labs.scratch import match_string p_test = {"name": "Checking that the min custom block with two arguments works" " (" + str(p_points) + " points)<br>", "pass": False, "pass_message": "<h5 style=\"color:green;\">Pass!</h5> " "The min custom block with two arguments works.<br>", "fail_message": "<h5 style=\"color:red;\">Fail.</h5> " "The min custom block with two arguments does not appear to work." "Is the input parameter named min?<br>" "BE SURE INPUT PARAMETERS ARE NAMED number1 and numberr" " OTHERWISE THIS TEST BREAKS.<br>", "points": 0 } if 'min %s %s' in p_scripts.keys(): sprite = brickLayer(0, 0, 0, pendown=False, variables={"number1": 5, "number2": 8}) print("aaa min test variables {}".format(sprite.variables)) script = p_scripts['min %s %s'] day_success_1 = do_sprite(sprite, script, True) print("VALHALLA {}".format(sprite.say_history)) test_1 = match_string(r'5', sprite.say_history) if test_1['pass'] is False: p_test['fail_message'] += "Called custom block 'min' with number1 = 5 and number2 = 8 <br>" \ "Expect '5', got this:<br>" + sprite.say_history + "<br>" sprite = brickLayer(0, 0, 0, pendown=False, variables={"number1": 555, "number2": -8}) day_success_2 = do_sprite(sprite, script, True) print("VALHALLA {}".format(sprite.say_history)) test_2 = match_string(r'-8', sprite.say_history) if test_2['pass'] is False: p_test['fail_message'] += "Called custom block 'min' with number1 = 555 and number2 = -8 <br>" \ "Expect '-8', got this:<br>" + sprite.say_history if day_success_1 and day_success_2 and test_1['pass'] and test_2['pass']: p_test['pass'] = True p_test['points'] += p_points return p_test
def press_two(p_scripts, p_points): """ :param p_scripts: json data from file, which is the code of the scratch file. (dict) :param p_points: Number of points this test is worth (int) :return: The test dictionary """ from app.scratch_labs.scratch import match_string, unique_coordinates, is_equilateral_triangle from app.scratch_labs.scratch_2_2 import brickLayer, do_sprite p_test = { "name": "Checking that there is a script that has 'when 2 key is pressed' that draws an " "equilateral triangle. (" + str(p_points) + " points)<br>", "pass": False, "pass_message": "<h5 style=\"color:green;\">Pass!</h5> " "There is a script that has 'when 2 key is pressed' that draws an " "equilateral triangle.<br>", "fail_message": "<h5 style=\"color:red;\">Fail.</h5> " "There is not a script that has 'when 2 key is pressed' that draws an " "equilateral triangle.<br>", "points": 0 } test_repeat = match_string(r"'control_repeat',\s'3'", p_scripts) test_two = match_string(r"'event_whenkeypressed',\s'2'", p_scripts) sprite = brickLayer(0, 0, 0, pendown=False) if test_two['pass'] is False: p_test[ 'fail_message'] += "Did not find a 'when key 2 is pressed' in the code .<br>" if test_repeat['pass'] is False: p_test[ 'fail_message'] += "Did not find a repeat block that repeats the expected number of times.<br>" move_success = False if test_two['pass']: for key in p_scripts: script = p_scripts[key] if len(script) > 1: if script[0] == ['event_whenkeypressed', '2']: move_success = do_sprite(sprite, script, True) coords = unique_coordinates(sprite.move_history) if len(coords) != 3: p_test[ 'fail_message'] += "After pressing 2, sprite should land on 3 unique coordinates, but does not.<br>" equilateral = is_equilateral_triangle(coords) if equilateral is False: p_test[ 'fail_message'] += "Failed test for equilateral triangle (all sides equal length) <br>.<br>" if test_two['pass'] and test_repeat[ 'pass'] and equilateral and move_success: p_test['pass'] = True p_test['points'] += p_points return p_test
def press_one(p_scripts, p_points): """ :param p_scripts: json data from file, which is the code of the scratch file. (dict) :param p_points: Number of points this test is worth (int) :return: The test dictionary """ from app.scratch_labs.scratch import match_string, unique_coordinates, is_square from app.scratch_labs.scratch_2_2 import brickLayer, do_sprite p_test = { "name": "Checking that there is a script that has 'when 1 key is pressed' that draws a square" " (" + str(p_points) + " points)<br>", "pass": False, "pass_message": "<h5 style=\"color:green;\">Pass!</h5> " "There is a script that has 'when 1 key is pressed' that draws a " "square.<br>", "fail_message": "<h5 style=\"color:red;\">Fail.</h5> " "There is not a script that has 'when 1 key is pressed' that draws a " "square<br>", "points": 0 } test_two = match_string(r"'event_whenkeypressed',\s'1'", p_scripts) sprite = brickLayer(0, 0, 0, pendown=False) if test_two['pass'] is False: p_test[ 'fail_message'] += "Did not find a 'when key 1 is pressed' in the code .<br>" move_success = False if test_two['pass']: for key in p_scripts: script = p_scripts[key] if len(script) > 1: if script[0] == ['event_whenkeypressed', '1']: move_success = do_sprite(sprite, script, True) coords = unique_coordinates(sprite.move_history) if len(coords) != 4: p_test[ 'fail_message'] += "After pressing 1, sprite should land on 4 unique coordinates, but does not.<br>" square = is_square(coords) if square is False: p_test['fail_message'] += "Failed test for square <br>.<br>" if test_two['pass'] and square and move_success: p_test['pass'] = True p_test['points'] += p_points return p_test
def between_works_unequal(p_scripts, p_points): """ :param p_scripts: json data from file, which is the code of the scratch file. (dict) :param p_points: Number of points this test is worth (int) :return: The test dictionary """ from app.scratch_labs.scratch_2_2 import brickLayer, do_sprite from app.scratch_labs.scratch import match_string p_test = {"name": "Checking that the between custom block with three input parameters works when number1 " "is NOT equal to number2 or number3" " (" + str(p_points) + " points)<br>", "pass": False, "pass_message": "<h5 style=\"color:green;\">Pass!</h5> " "The between custom block with three input parameters works.<br>", "fail_message": "<h5 style=\"color:red;\">Fail.</h5> " "The between custom block with three input parameters does not appear to work in this " "scenario.<br>" "Is the input parameters named 'number1', 'number2', 'number3'?<br>" "BE SURE INPUT PARAMETER IS NAMED CORRECTLY OTHERWISE THIS TEST BREAKS.<br>" "The script must say 'True' exactly (capital T, lowercase everything else)" "or else 'False' exactly (capital F, lowercase everything else).<br>", "points": 0 } if 'between %s %s %s' in p_scripts.keys(): print("ppp STARTING") sprite = brickLayer(0, 0, 0, pendown=False, variables={"number1": 1, "number2": 2, "number3": 3}) print("aaa test 1 unequals variables {}".format(sprite.variables)) script = p_scripts['between %s %s %s'] between_success_1 = do_sprite(sprite, script, True) print("VALHALLA {}".format(sprite.say_history)) test_1 = match_string(r'^False', sprite.say_history) if test_1['pass'] is False: p_test['fail_message'] += "Called custom block 'between' with number1 = 1, number2 = 2, number3 = 3<br>" \ "Expect 'False', got this:<br>" + sprite.say_history + "<br>" sprite = brickLayer(0, 0, 0, pendown=False, variables={"number1": 1, "number2": 3, "number3": 2}) print("aaa test 2 unequals variables {}".format(sprite.variables)) between_success_2 = do_sprite(sprite, script, True) test_2 = match_string(r'^False', sprite.say_history) if test_2['pass'] is False: p_test['fail_message'] += "Called custom block 'between' with number1 = 1, number2 = 3, number3 = 2<br>" \ "Expect 'False', got this:<br>" + sprite.say_history + "<br>" sprite = brickLayer(0, 0, 0, pendown=False, variables={"number1": 2, "number2": 1, "number3": 3}) print("aaa test 3 unequals variables {}".format(sprite.variables)) between_success_3 = do_sprite(sprite, script, True) test_3 = match_string(r'^True', sprite.say_history) if test_3['pass'] is False: p_test['fail_message'] += "Called custom block 'between' with number1 = 2, number2 = 1, number3 = 3<br>" \ "Expect 'True', got this:<br>" + sprite.say_history + "<br>" print("test4") sprite = brickLayer(0, 0, 0, pendown=False, variables={"number1": 2, "number2": 3, "number3": 1}) print("aaa test 4 unequals variables {}".format(sprite.variables)) between_success_4 = do_sprite(sprite, script, True) test_4 = match_string(r'^True', sprite.say_history) print("test4 say history {}".format(sprite.say_history)) if test_4['pass'] is False: p_test['fail_message'] += "Called custom block 'between' with number1 = 2, number2 = 3, number3 = 1<br>" \ "Expect 'True', got this:<br>" + sprite.say_history + "<br>" sprite = brickLayer(0, 0, 0, pendown=False, variables={"number1": 3, "number2": 1, "number3": 2}) print("aaa test 5 unequals variables {}".format(sprite.variables)) between_success_5 = do_sprite(sprite, script, True) test_5 = match_string(r'^False', sprite.say_history) if test_5['pass'] is False: p_test['fail_message'] += "Called custom block 'between' with number1 = 3, number2 = 1, number3 = 2<br>" \ "Expect 'True', got this:<br>" + sprite.say_history + "<br>" sprite = brickLayer(0, 0, 0, pendown=False, variables={"number1": 3, "number2": 2, "number3": 1}) print("aaa test 6 unequals variables {}".format(sprite.variables)) between_success_6 = do_sprite(sprite, script, True) test_6 = match_string(r'^False', sprite.say_history) if test_6['pass'] is False: p_test['fail_message'] += "Called custom block 'between' with number1 = 3, number2 = 2, number3 = 1<br>" \ "Expect 'True', got this:<br>" + sprite.say_history + "<br>" if between_success_1 and between_success_2 and between_success_3 and between_success_4 and \ between_success_5 and between_success_6 and test_1['pass'] and test_2['pass'] and test_3['pass'] and \ test_4['pass'] and test_5['pass'] and test_6['pass']: p_test['pass'] = True p_test['points'] += p_points return p_test
def happy_birthday_works(p_scripts, p_points): """ :param p_scripts: json data from file, which is the code of the scratch file. (dict) :param p_points: Number of points this test is worth (int) :return: The test dictionary """ from app.scratch_labs.scratch_2_2 import brickLayer, do_sprite from app.scratch_labs.scratch import match_string p_test = { "name": "Checking that the happy_birthday custom block with one argument works" " (" + str(p_points) + " points)<br>", "pass": False, "pass_message": "<h5 style=\"color:green;\">Pass!</h5> " "The happy_birthday custom block with one argument works.<br>", "fail_message": "<h5 style=\"color:red;\">Fail.</h5> " "The happy_birthday custom block with one argument does not appear to work." "Is the input parameter named name?<br>" "BE SURE INPUT PARAMETER IS NAMED name OTHERWISE THIS TEST BREAKS.<br>", "points": 0 } if 'happy_birthday %s' in p_scripts.keys(): sprite = brickLayer(0, 0, 0, pendown=False, variables={"name": 'McGlathery'}) script = p_scripts['happy_birthday %s'] word_success_1 = do_sprite(sprite, script, True) print("VALHALLA {}".format(sprite.say_history)) test_1a = match_string(r'McGlathery', sprite.say_history) test_1b = match_string(r'(birthday|Birthday|BIRTHDAY)', sprite.say_history) if test_1a['pass'] is False: p_test['fail_message'] += "Didn't find the word 'McGlathery' in say output when I called custom block" \ "with input parameter 'McGlathery'<br>" if test_1b['pass'] is False: p_test['fail_message'] += "Didn't find the word 'birthday', 'Birthday', or 'BIRTHDAY'" \ " in say output when I called custom block" \ "with input parameter 'McGlathery'<br>" sprite = brickLayer(0, 0, 0, pendown=False, variables={"name": 'MichaelJordan'}) script = p_scripts['happy_birthday %s'] word_success_2 = do_sprite(sprite, script, True) test_2a = match_string(r'MichaelJordan', sprite.say_history) test_2b = match_string(r'(birthday|Birthday|BIRTHDAY)', sprite.say_history) if test_2a['pass'] is False: p_test['fail_message'] += "Didn't find the word 'MichaelJordan' in say output when I called custom block " \ "with input parameter 'MichaelJordan'<br>" if test_2b['pass'] is False: p_test['fail_message'] += "Didn't find the word 'birthday', 'Birthday', or 'BIRTHDAY' " \ "in say output when I called custom block " \ "with input parameter 'MichaelJordan'<br>" if word_success_1 and word_success_2 and test_1a['pass'] and test_1b['pass'] and test_2a['pass'] and \ test_2b['pass']: p_test['pass'] = True p_test['points'] += p_points return p_test
def make_triangle_works(p_scripts, p_points): """ :param p_scripts: json data from file, which is the code of the scratch file. (dict) :param p_points: Number of points this test is worth (int) :return: The test dictionary """ from app.scratch_labs.scratch_2_2 import brickLayer, do_sprite from app.scratch_labs.scratch import unique_coordinates, is_equilateral_triangle, distance p_test = { "name": "Checking that the make_triangle custom block with one argument works" " (" + str(p_points) + " points)<br>", "pass": False, "pass_message": "<h5 style=\"color:green;\">Pass!</h5> " "The make_triangle custom block with one argument works.<br>", "fail_message": "<h5 style=\"color:red;\">Fail.</h5> " "The make_triangle custom block with one argument does not appear to work." "Is the input parameter named size?<br>" "BE SURE INPUT PARAMETER IS NAMED SIZE OTHERWISE THIS TEST BREAKS.<br>", "points": 0 } test_run_one = False test_run_two = False move_success_1 = False move_success_2 = False if 'make_triangle %s' in p_scripts.keys(): sprite = brickLayer(0, 0, 0, pendown=False, variables={"size": 60}) script = p_scripts['make_triangle %s'] print("iii script {}".format(script)) move_success_1 = do_sprite(sprite, script, True) coords = unique_coordinates(sprite.move_history) if len(coords) != 3: p_test['fail_message'] += "make_triangle custom block should land on 3 unique coordinates," \ " but does not.<br>" return p_test equilateral = is_equilateral_triangle(coords) if equilateral is False: p_test[ 'fail_message'] += "make_triangle custom block should create equilateral triangle but does not.<br>" first_move_distance = distance(sprite.move_history[0], sprite.move_history[1]) if abs(60 - first_move_distance) > 60 * 0.01: p_test['fail_message'] += "Distance between first and second move should be around 60 steps if I call " \ "block with input of 60. <br> Instead, it's " \ "this distance:" + str(first_move_distance) + "<br>" \ "This can happen if you forgot to use 'size' in your custom block and" \ " hard-coded it.<br>" else: test_run_one = True sprite = brickLayer(0, 0, 0, pendown=False, variables={"size": 100}) script = p_scripts['make_triangle %s'] move_success_2 = do_sprite(sprite, script, True) second_move_distance = distance(sprite.move_history[0], sprite.move_history[1]) if abs(100 - second_move_distance) > 100 * 0.01: p_test['fail_message'] += "Distance between first and second move should be around 100 steps if I call" \ "block with input of 100. <br> Instead, it's " \ "this distance:" + str(second_move_distance) + "<br>" \ "This can happen if you forgot to use 'size' in your custom block and" \ " hard-coded it.<br>" else: test_run_two = True else: p_test[ 'fail_message'] += "Does not look like there is a custom block make_triangle with one input parameter." if move_success_1 and move_success_2 and equilateral and test_run_one and test_run_two: p_test['pass'] = True p_test['points'] += p_points return p_test