1- from pythonwhat .State import State
21from pythonwhat .Reporter import Reporter
3- from pythonwhat .Test import DefinedProcessTest , InstanceProcessTest , DefinedCollProcessTest , EqualValueProcessTest
2+ from pythonwhat .Test import InstanceProcessTest , DefinedCollProcessTest , EqualValueProcessTest
43from pythonwhat .Feedback import Feedback
5- from pythonwhat .tasks import isDefinedInProcess , isInstanceInProcess , getKeysInProcess , getValueInProcess , isDefinedCollInProcess , ReprFail
4+ from pythonwhat .tasks import isInstanceInProcess , getKeysInProcess , getValueInProcess , isDefinedCollInProcess , ReprFail
65from .test_object import check_object
76
8- MSG_UNDEFINED = "Are you sure you defined the dictionary `{name}`?"
9- MSG_NOT_INSTANCE = "`{name}` is not a dictionary."
10- MSG_KEY_MISSING = "Have you specified a key `{key}` inside `{name}`?"
11- MSG_INCORRECT_VAL = "Have you specified the correct value for the key `{key}` inside `{name}`?"
7+ MSG_UNDEFINED = "Are you sure you defined the dictionary `{parent[sol_part][ name] }`?"
8+ MSG_NOT_INSTANCE = "`{parent[sol_part][ name] }` is not a dictionary."
9+ MSG_KEY_MISSING = "Have you specified a key `{key}` inside `{parent[sol_part][ name] }`?"
10+ MSG_INCORRECT_VAL = "Have you specified the correct value for the key `{key}` inside `{parent[sol_part][ name] }`?"
1211
1312def test_dictionary (name ,
1413 keys = None ,
@@ -23,39 +22,33 @@ def test_dictionary(name,
2322 rep = Reporter .active_reporter
2423 rep .set_tag ("fun" , "test_dictionary" )
2524
26- check_dict (name , undefined_msg , not_dictionary_msg , state = state )
25+ child = check_dict (name , undefined_msg or MSG_UNDEFINED , not_dictionary_msg or MSG_NOT_INSTANCE , state = state )
2726
2827 # set keys or check if manual keys are valid
2928 if not keys :
3029 keys = getKeysInProcess (name , state .solution_process )
3130
3231 for key in keys :
3332 # check if key in dictionary
34- test_key (name , key , incorrect_value_msg , key_missing_msg , state = state )
33+ test_key (name , key , incorrect_value_msg or MSG_INCORRECT_VAL , key_missing_msg or MSG_KEY_MISSING , state = child )
3534
3635# Check functions -------------------------------------------------------------
3736
38- def check_dict (name , undefined_msg , not_instance_msg , state = None ):
39- rep = Reporter .active_reporter
40-
41- # Check if defined
42- undefined_msg = undefined_msg .format (name = name )
43- _msg = state .build_message (undefined_msg )
44-
45- # check but don't get solution object representation
46- state = check_object (name , _msg , state = state )
37+ def check_dict (name , undefined_msg = MSG_UNDEFINED , not_instance_msg = MSG_NOT_INSTANCE , state = None ):
4738
48- is_instance (name , dict , not_instance_msg , state = state )
39+ child = check_object (name , undefined_msg , state = state ) # test defined
40+ is_instance (name , dict , not_instance_msg , state = child ) # test instance
4941
50- return state
42+ return child
5143
5244def is_instance (name , inst , not_instance_msg , state = None ):
5345 rep = Reporter .active_reporter
5446
5547 if not isInstanceInProcess (name , inst , state .solution_process ):
5648 raise ValueError ("%r is not a %s in the solution environment" % (name , type (inst )))
5749
58- feedback = Feedback (not_instance_msg .format (name = name ))
50+ _msg = state .build_message (not_instance_msg )
51+ feedback = Feedback (_msg , state .highlight )
5952 rep .do_test (InstanceProcessTest (name , inst , state .student_process , feedback ))
6053
6154def has_key (name , key , key_missing_msg , state = None ):
@@ -65,8 +58,9 @@ def has_key(name, key, key_missing_msg, state=None):
6558 raise NameError ("Not all keys you specified are actually keys in %s in the solution process" % name )
6659
6760 # check if key available
68- msg = key_missing_msg .format (key = key , name = name )
69- rep .do_test (DefinedCollProcessTest (name , key , state .student_process , Feedback (msg )))
61+ _msg = state .build_message (key_missing_msg , {'key' : key })
62+ rep .do_test (DefinedCollProcessTest (name , key , state .student_process ,
63+ Feedback (_msg , state .highlight )))
7064
7165def test_key (name , key , incorrect_value_msg , key_missing_msg , state = None ):
7266 rep = Reporter .active_reporter
@@ -78,6 +72,5 @@ def test_key(name, key, incorrect_value_msg, key_missing_msg, state=None):
7872 raise NameError ("Value from %r can't be fetched from the solution process: %s" % c (name , sol_value .info ))
7973
8074 # check if value ok
81- msg = incorrect_value_msg .format (key = key , name = name )
82- _msg = state .build_message (msg )
83- rep .do_test (EqualValueProcessTest (name , key , state .student_process , sol_value , Feedback (_msg )))
75+ _msg = state .build_message (incorrect_value_msg , {'key' : key })
76+ rep .do_test (EqualValueProcessTest (name , key , state .student_process , sol_value , Feedback (_msg , state .highlight )))
0 commit comments