def show_and_hide(p_json, p_points): """ Checks that there is a sprite that hides and shoes :param p_json: json data from file, which is the code of the scratch file. (dict) :param p_points: number of points this is worth (int) :return: The test dictionary """ from app.scratch_labs.scratch import match_string p_test = { "name": "Checking that there is hiding and showing of sprites going on (" + str(p_points) + " points)<br>", "pass": True, "pass_message": "<h5 style=\"color:green;\">Pass!</h5> " "We found hiding and showing of sprites!<br>", "fail_message": "<h5 style=\"color:red;\">Fail.</h5> " "Code does not have hiding and showing of sprites.<br>", "points": 0 } test_show = match_string(r'looks_show', p_json) if test_show['pass'] is False: p_test['fail_message'] += test_show['fail_message'] test_hide = match_string(r'looks_hide', p_json) if test_hide['pass'] is False: p_test['fail_message'] += test_hide['fail_message'] if test_show['pass'] and test_hide['pass']: p_test['points'] += p_points else: p_test['pass'] = False return p_test
def green_flag(p_scripts, p_points): from app.scratch_labs.scratch import match_string p_test = { "name": "Testing that there is a green flag. The rest of your code should be under the green flag.<br>" "There is also be a forever loop under the flag " "(" + str(p_points) + " points)", "pass": False, "pass_message": "<h5 style=\"color:green;\">Pass. <h5>" "You have a green flag with a forever.<br>", "fail_message": "<h5 style=\"color:red;\">Fail. </h5>" "Did not find a green flag AND forever under it. <br>", 'points': 0 } test_flag = match_string(r'event_whenflagclicked', p_scripts) if test_flag['pass'] is False: p_test['fail_message'] += 'Did not find a green flag at all.' if test_flag['pass'] is False: p_test['fail_message'] += 'Did not find a green flag at all.' test_flag_and_forever = match_string( r'event_whenflagclicked .+ (control_repeat|control_forever)', p_scripts) if test_flag_and_forever['pass'] is False: p_test['fail_message'] += 'There needs to be a forever under the flag.' if test_flag['pass'] and test_flag_and_forever['pass']: p_test['pass'] = True p_test['points'] += p_points return p_test
def if_ifelse(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 p_test = {"name": "Checking that the day_of_week custom block with one argument has good use of if/ifelse" " (" + str(p_points) + " points)<br>", "pass": False, "pass_message": "<h5 style=\"color:green;\">Pass!</h5> " "The day_of_week custom block with one argument has good use of if/ifelse.<br>", "fail_message": "<h5 style=\"color:red;\">Fail.</h5> " "The day_of_week custom block with one argument does not appear to have good use " "of if/ifelse.<br>", "points": 0 } if 'day_of_week %s' in p_scripts.keys(): script = p_scripts['day_of_week %s'] match_if = match_string(r"control_if'", script, num_matches=4) match_ifelse = match_string(r"control_if_else'", script, num_matches=4) if match_ifelse['pass'] is False: p_test['fail_message'] += 'Expect to see a bunch of if statements or if/else statements ' \ 'for this block to work.<br>' if match_if['pass']: p_test['fail_message'] += 'There are many if statements. This block can be written more efficiently ' \ 'with if/elses. Ask the teacher if this does not make sense.<br>' if match_if['pass'] is False and match_ifelse['pass'] is True: p_test['pass'] = True p_test['points'] += p_points return p_test
def press_zero(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 p_test = { "name": "Checking that there is a script that has 'when 0 key is pressed' along with a " "pen down, goto 0 0, clear, and point 90. (" + str(p_points) + " points)<br>", "pass": False, "pass_message": "<h5 style=\"color:green;\">Pass!</h5> " "We found the strings in the code!<br>", "fail_message": "<h5 style=\"color:red;\">Fail.</h5> " "Code does not find string we were looking for.<br>" "Be sure that there is only one 'when 0 key is pressed'<br>", "points": 0 } test_pendown = match_string( r"\['event_whenkeypressed', '0'] .+ 'pen_penDown'", p_scripts) if test_pendown['pass'] is False: p_test[ 'fail_message'] += "Did not find a when 0 pressed followed by a pen erase all.<br>" test_clear = match_string(r"\['event_whenkeypressed', '0'] .+ 'pen_clear'", p_scripts) if test_clear['pass'] is False: p_test[ 'fail_message'] += "Did not find a when 0 pressed followed by an erase all.<br>" test_point = match_string( r"\['event_whenkeypressed', '0'] .+ \['motion_pointindirection', \s '90'],", p_scripts) if test_point['pass'] is False: p_test[ 'fail_message'] += "Did not find a when 0 pressed followed by a point in direction 90.<br>" test_goto = match_string( r"\['event_whenkeypressed', '0'] .+ \['motion_gotoxy', \s '0', \s '0'],", p_scripts) if test_goto['pass'] is False: p_test[ 'fail_message'] += "Did not find a when 0 pressed followed by goto 0 0.<br>" test_color = match_string( r"\['event_whenkeypressed', '0'] .+ \['pensetPenColorToColor'", p_scripts) if test_color['pass'] is False: p_test[ 'fail_message'] += "Did not find a when 0 pressed followed by changing of pen color.<br>" if test_pendown and test_clear and test_point and test_goto and test_color: p_test['pass'] = True p_test['points'] += p_points return p_test
def test_color_change(p_scripts, p_points): """ tests to see if stage changes color if it answers 'blue :param p_scripts: the scripts given by arrange_karel :param p_points: points this is worth :return: test dictionary """ from app.scratch_labs.scratch import match_string p_test = { "name": "Testing that there is a background change if the user answers 'blue' for favorite color" " (" + str(p_points) + " points).", "pass": False, "pass_message": "<h5 style=\"color:green;\">Pass. <h5>" " There is a background change if the user answers 'blue' for favorite color.<br>", "fail_message": "<h5 style=\"color:red;\">Fail. </h5>" " Does not appear to be a background change if the user answers 'blue' for" " favorite color. <br>", 'points': 0 } test_if = match_string( r'control_if .+ operator_equals .+ VARIABLE_color .+ blue', p_scripts) if test_if['pass'] is False: p_test['fail_message'] += 'Does not appear to be an if color = blue<br>' \ 'variable color must be exactly color (spelling and capitalization count) and ' \ 'blue must be exactly "blue" (spelling and capitalization count) <br>' test_if_change_nextbackdrop = match_string( r'control_if .+ operator_equals .+ VARIABLE_color .+ blue .+' r' (looks_switchbackdropto|looks_nextbackdrop)', p_scripts) test_receive_broadcast = match_string( r'event_whenbroadcastreceived .+ ' r'(looks_switchbackdropto|looks_nextbackdrop)', p_scripts) test_if_broadcast = match_string(r'control_if .+ event_broadcast', p_scripts) if test_if['pass'] is False: p_test['fail_message'] += 'Does not appear to be an if color = blue<br>' \ 'variable color must be exactly color (spelling and capitalization count) and ' \ 'blue must be exactly "blue" (spelling and capitalization count) <br>' if not (test_if_change_nextbackdrop['pass'] or (test_receive_broadcast['pass'] and test_if_broadcast['pass'])): p_test['fail_message'] += 'You must have a switchbackdrop OR nextbackdrop inside your if.<br>' \ 'Alternatively, you can broadcast a message to the stage and have' \ ' the stage change background.<br>' if test_if['pass'] and (test_if_change_nextbackdrop['pass'] or (test_receive_broadcast['pass'] and test_if_broadcast['pass'])): p_test['pass'] = True p_test['points'] += p_points print(test_receive_broadcast['pass']) print(test_if_broadcast['pass']) 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 find_else(p_scripts, p_points): from app.scratch_labs.scratch import match_string p_test = { "name": "Testing that there is an else if you don't answer between 1-4" " (" + str(p_points) + " points).", "pass": False, "pass_message": "<h5 style=\"color:green;\">Pass. <h5>" " There is an else if you don't answer between 1-4. <br>", "fail_message": "<h5 style=\"color:red;\">Fail. </h5>" " Does not appear that there is an else if you don't answer between 1-4.<br> " "You pretty much need 5 if/else blocks to make this work." "<br>", 'points': 0 } test = match_string( r"control_if .+ looks_say .+ VARIABLE_prize1 .+ \[\[ 'control_if .+ looks_say .+ " r"VARIABLE_prize2 .+ \[\[ 'control_if .+ looks_say .+ VARIABLE_prize3 .+ " r"\[\[ 'control_if .+ looks_say .+ VARIABLE_prize4 .+? looks_say", p_scripts) if test['pass'] is False: p_test['fail_message'] += r"Looked for <br> control_if .+ looks_say .+ VARIABLE_prize1 .+ \[\[ 'control_if .+ " \ r"looks_say .+ VARIABLE_prize2 .+ \[\[ 'control_if .+ looks_say .+ VARIABLE_prize3 .+ " \ r"\[\[ 'control_if .+ looks_say .+ VARIABLE_prize4 .+? looks_say <br> inside this:" \ r" <br>" + str(p_scripts) if test['pass']: p_test['points'] += p_points p_test['pass'] = True return p_test
def press_four(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 p_test = { "name": "Checking that there is a script 'when 4 key is pressed' that calls happy birthday custom block " "with one argument. (" + str(p_points) + " points)<br>", "pass": False, "pass_message": "<h5 style=\"color:green;\">Pass!</h5> " "There is a script 'when 4 key is pressed' that calls happy_birthday custom block " "with one argument.<br>", "fail_message": "<h5 style=\"color:red;\">Fail.</h5> " "There is not a script 'when 4 key is pressed' that calls happy_birthday custom block " "with one argument.<br>" "The script must be named EXACTLY happy_birthday with exactly one input parameter.", "points": 0 } test_four = match_string( r"'event_whenkeypressed', \s* '4'], \s* 'happy_birthday \s* \%s'", p_scripts) if test_four['pass']: p_test['pass'] = True p_test['points'] += p_points return p_test
def any_conditional(p_scripts, p_points): from app.scratch_labs.scratch import match_string p_test = { "name": "Testing that at least one prize works" " (" + str(p_points) + " points).", "pass": False, "pass_message": "<h5 style=\"color:green;\">Pass. <h5>" " At least one prize works. <br>", "fail_message": "<h5 style=\"color:red;\">Fail. </h5>" " Does not appear that there at least one prize works. You need an if, an 'equals' " "operator, and a say." "<br>", 'points': 0 } test = match_string(r'control_if .+? operator_equals .+? looks_say', p_scripts) if test['pass'] is False: p_test[ 'fail_message'] += 'Looked for <br> control_if .+ operator_equals .+? looks_say .+ inside this: <br>' + str( p_scripts) if test['pass']: p_test['points'] += p_points p_test['pass'] = True return p_test
def color_variables(p_scripts, p_points): """ tests to see if set color to answer :param p_scripts: the scripts given by arrange_karel :param p_points: points this is worth :return: test dictionary """ from app.scratch_labs.scratch import match_string p_test = { "name": "Testing that you are setting a variable 'color' to an answer to a question." " (" + str(p_points) + " points).", "pass": False, "pass_message": "<h5 style=\"color:green;\">Pass. <h5>" " There is a setting a variable 'color' to an answer to a question.<br>", "fail_message": "<h5 style=\"color:red;\">Fail. </h5>" " Does not appear to be setting a variable 'color' to an answer to a question.<br>", 'points': 0 } for key in p_scripts: print("aaa script {}".format(p_scripts[key])) test_variable = match_string( r"\['data_setvariableto',\s'color',\s'sensing_answer']", p_scripts[key]) if test_variable: p_test['pass'] = True print("aaa p_test['pass'] {}".format(p_test['pass'])) if p_test['pass']: p_test['points'] += p_points return p_test
def green_flag(p_json, p_points): from app.scratch_labs.scratch import match_string p_test = { "name": "Testing that there is a green flag. The rest of your code should be under the green flag " "(" + str(p_points) + " points)", "pass": False, "pass_message": "<h5 style=\"color:green;\">Pass. <h5>" "You have a green flag with a question and variable setting under it.<br>", "fail_message": "<h5 style=\"color:red;\">Fail. </h5>" "Did not find a green flag AND a question under the green flag AND setting a " "variable setting under the green flag and question. <br>", 'points': 0 } test_flag = match_string(r'event_whenflagclicked', p_json) if test_flag['pass'] is False: p_test['fail_message'] += 'Did not find a green flag at all.' if test_flag['pass'] is False: p_test['fail_message'] += 'Did not find a green flag at all.' test_flag_and_question = match_string( r'event_whenflagclicked .+ sensing_askandwait', p_json) if test_flag_and_question['pass'] is False: p_test[ 'fail_message'] += 'There needs to be a question under the flag.' test_flag_and_question_and_variable = match_string( r'event_whenflagclicked .+ sensing_askandwait .+' r' data_setvariableto', p_json) test_flag_and_question_and_variable_2 = match_string( r'event_whenflagclicked .+ data_setvariableto .+ ' r'sensing_askandwait .+', p_json) if test_flag_and_question_and_variable[ 'pass'] is False and test_flag_and_question_and_variable_2[ 'pass'] is False: p_test[ 'fail_message'] += 'There needs to be a question and setting a variable under the flag.' if test_flag['pass'] and test_flag_and_question['pass'] and ( test_flag_and_question_and_variable['pass'] or test_flag_and_question_and_variable_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 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 one_question(p_scripts, p_points): """ Tests to see if there is one reasonable question with an if/else and two says :param p_scripts: the scripts given by arrange_karel :param p_points: points this is worth :return: test dictionary """ from app.scratch_labs.scratch import match_string p_test = { "name": "Testing that there is question being asked and a reply given depending on answer" " (" + str(p_points) + " points).", "pass": False, "pass_message": "<h5 style=\"color:green;\">Pass. <h5>" " There is question being asked and a reply given depending on answer.<br>", "fail_message": "<h5 style=\"color:red;\">Fail. </h5>" " Does not appear that there is question being asked and a reply given depending " "on answer. <br>", 'points': 0 } from app.scratch_labs.scratch import match_string test_q_and_if = match_string(r'sensing_askandwait .+ control_if_else', p_scripts) if test_q_and_if['pass'] is False: p_test['fail_message'] += "Does not appear that you have a question and an if/else.<br>" \ "Need an if/else, because you will have a different response " \ "depending on how the user answers the question.<br>" test_all_one = match_string( r'sensing_askandwait .+ control_if_else .+ \[ .+ \[ .+ looks_say .+ ] .+ ' r'\[ .+ looks_say .+ ] .+ ] ', p_scripts) if test_all_one['pass'] is False: p_test['fail_message'] += "Does not appear that you have a question AND an if/else AND two 'says' inside " \ "the if/else. <br>" if test_q_and_if['pass'] and test_all_one['pass']: p_test['pass'] = True p_test['points'] += p_points return p_test
def test_prizes_defined(p_scripts, p_points): from app.scratch_labs.scratch import match_string p_test = { "name": "Testing that 4 variables named prize1, prize2, prize3, and prize4 are set to values" " (" + str(p_points) + " points).", "pass": False, "pass_message": "<h5 style=\"color:green;\">Pass. <h5>" " There are 4 variables named prize1, prize2, prize3, and prize4 are set to values. <br>", "fail_message": "<h5 style=\"color:red;\">Fail. </h5>" " Does not appear that there are variables named prize1, prize2, prize3, and prize4 that " "are all set to values." "<br>", 'points': 0 } test_one = match_string( r'data_setvariableto .+ prize1\', \s* .+? [a-z] .+? ] ', p_scripts) if test_one['pass'] is False: p_test[ 'fail_message'] += "Value for variable prize1 does not appear to be set.<br>" test_two = match_string( r'data_setvariableto .+ prize2\', \s* .+? [a-z] .+? ] ', p_scripts) if test_two['pass'] is False: p_test[ 'fail_message'] += "Value for variable prize2 does not appear to be set.<br>" test_three = match_string( r'data_setvariableto .+ prize3\', \s* .+? [a-z] .+? ] ', p_scripts) if test_three['pass'] is False: p_test[ 'fail_message'] += "Value for variable prize3 does not appear to be set.<br>" test_four = match_string( r'data_setvariableto .+ prize4\', \s* .+? [a-z] .+? ] ', p_scripts) if test_four['pass'] is False: p_test[ 'fail_message'] += "Value for variable prize4 does not appear to be set.<br>" if test_one['pass'] and test_two['pass'] and test_three[ 'pass'] and test_four['pass']: p_test['points'] += p_points p_test['pass'] = True return p_test
def four_prizes(p_scripts, p_points): from app.scratch_labs.scratch import match_string p_test = { "name": "Testing that at all four prizes work" " (" + str(p_points) + " points).", "pass": False, "pass_message": "<h5 style=\"color:green;\">Pass. <h5>" " At least all four prizes works. <br>", "fail_message": "<h5 style=\"color:red;\">Fail. </h5>" " Does not appear that all 4 prizes work. You need an if, an 'equals' " "operator, and a say for each prize." "<br>", 'points': 0 } test_one = match_string( r"control_if .+ operator_equals .+ looks_say .+ VARIABLE_prize1 .+", p_scripts) test_two = match_string( r"control_if .+ operator_equals .+ looks_say .+ VARIABLE_prize2 .+", p_scripts) test_three = match_string( r"control_if .+ operator_equals .+ looks_say .+ VARIABLE_prize3 .+", p_scripts) test_four = match_string( r"control_if .+ operator_equals .+ looks_say .+ VARIABLE_prize4 .+", p_scripts) if test_one['pass'] is False: p_test['fail_message'] += "Did not appear that prize1 works.<br>" if test_two['pass'] is False: p_test['fail_message'] += "Did not appear that prize2 works.<br>" if test_three['pass'] is False: p_test['fail_message'] += "Did not appear that prize3 works.<br>" if test_four['pass'] is False: p_test['fail_message'] += "Did not appear that prize4 works.<br>" if test_one['pass'] and test_two['pass'] and test_three[ 'pass'] and test_four['pass']: p_test['points'] += p_points p_test['pass'] = True 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_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 name_variable_x4(p_scripts, p_points): """ Tests to see if name variable shows up 4x. :param p_scripts: the scripts given by arrange_karel :param p_points: points this is worth :return: test dictionary """ from app.scratch_labs.scratch import match_string p_test = { "name": "Testing that you using the name variable when talking to user." " (" + str(p_points) + " points).", "pass": False, "pass_message": "<h5 style=\"color:green;\">Pass. <h5>" " You are using the name variable when talking to user.<br>", "fail_message": "<h5 style=\"color:red;\">Fail. </h5>" " Does not appear you using the name variable when talking to user. <br>" "The name variable needs to show up 4 times.<br>", 'points': 0 } from app.scratch_labs.scratch import match_string test_name_x4 = match_string( r'VARIABLE_name .+ VARIABLE_name .+ VARIABLE_name .+ VARIABLE_name ', p_scripts) if test_name_x4['pass'] is False: p_test['fail_message'] += "Does not appear that you are using the name variable when asking questions.<br>" \ "Your question should be something like 'Mr. Kann, what is your favorite food?'<br>" \ "You should NOT hard-code a particular name like Mr. Kann. <br>Instead" \ " you should use a variable so if the person answering questions changes " \ "who they are, the program still works." if test_name_x4['pass']: p_test['pass'] = True p_test['points'] += p_points return p_test
def test_top_1(p_scripts, p_points): from app.scratch_labs.scratch import match_string import re p_test = { "name": "Testing that sprite goes to top of screen at beginning " "(" + str(p_points) + " points)", "pass": False, "pass_message": "<h5 style=\"color:green;\">Pass. <h5>" "Sprite goes to top of screen at beginning.<br>", "fail_message": "<h5 style=\"color:red;\">Fail. </h5>" "Sprite does NOT go to top of screen at beginning. <br>", 'points': 0 } test_flag_and_top = match_string( r'event_whenflagclicked .+ (motion_sety|motion_gotoxy)', p_scripts) if test_flag_and_top['pass'] is False: p_test['fail_message'] += 'There needs to be a block that moves the sprite to the top of the screen ' \ 'near the beginning of the script.' y = -300 for key in p_scripts.keys(): for block in p_scripts[key]: motion_sety = re.search(r"'motion_sety'", str(block), re.X | re.M | re.S) if motion_sety: print("aaa motionsety") test_high_enough = re.search( r"'motion_sety', \s+ '(-?[0-9]+)'", str(block), re.X | re.M | re.S) if test_high_enough: y = test_high_enough.group(1) break motion_gotoxy = re.search(r"'motion_gotoxy'", str(block), re.X | re.M | re.S) if motion_gotoxy: motion_gotoxy = re.search(r"'operator_random'", str(block), re.X | re.M | re.S) if motion_gotoxy: test_high_enough = re.search( r"'motion_gotoxy', \s+ \[ .+ ],\s+ '([0-9]+)", str(block), re.X | re.M | re.S) if test_high_enough: y = test_high_enough.group(1) break else: test_high_enough = re.search( r"motion_gotoxy', \s+ '-*[0-9]+', \s+ '([0-9]+)", str(block), re.X | re.M | re.S) if test_high_enough: y = test_high_enough.group(1) break print("this is y {} this is all {} wut {}".format( y, test_high_enough.group(0), test_high_enough.group(1))) if int(y) < 125: p_test[ 'fail_message'] += 'Y position of sprite needs to be higher than 125.' if test_flag_and_top['pass'] and int(y) > 125: 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 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