|
2 | 2 | from pythonwhat.Reporter import Reporter |
3 | 3 | from pythonwhat.Test import DefinedProcessTest, InstanceProcessTest, DefinedCollProcessTest, EqualValueProcessTest |
4 | 4 | from pythonwhat.Feedback import Feedback |
5 | | -from pythonwhat.tasks import isDefinedInProcess, isInstanceInProcess, getKeysInProcess, getValueInProcess, ReprFail |
| 5 | +from pythonwhat.tasks import isDefinedInProcess, isInstanceInProcess, getKeysInProcess, getValueInProcess, isDefinedCollInProcess, ReprFail |
| 6 | +from .test_object import check_object |
| 7 | + |
| 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}`?" |
6 | 12 |
|
7 | 13 | def test_dictionary(name, |
8 | 14 | keys=None, |
9 | | - undefined_msg=None, |
10 | | - not_dictionary_msg=None, |
11 | | - key_missing_msg=None, |
12 | | - incorrect_value_msg=None, |
| 15 | + undefined_msg=MSG_UNDEFINED, |
| 16 | + not_dictionary_msg=MSG_NOT_INSTANCE, |
| 17 | + key_missing_msg=MSG_KEY_MISSING, |
| 18 | + incorrect_value_msg=MSG_INCORRECT_VAL, |
13 | 19 | state=None): |
14 | 20 | """Test the contents of a dictionary. |
15 | 21 | """ |
16 | 22 |
|
17 | 23 | rep = Reporter.active_reporter |
18 | 24 | rep.set_tag("fun", "test_dictionary") |
19 | 25 |
|
20 | | - solution_process = state.solution_process |
21 | | - student_process = state.student_process |
| 26 | + check_dict(name, undefined_msg, not_dictionary_msg, state=state) |
| 27 | + |
| 28 | + # set keys or check if manual keys are valid |
| 29 | + if not keys: |
| 30 | + keys = getKeysInProcess(name, state.solution_process) |
| 31 | + |
| 32 | + for key in keys: |
| 33 | + # check if key in dictionary |
| 34 | + test_key(name, key, incorrect_value_msg, key_missing_msg, state=state) |
22 | 35 |
|
23 | | - if not isDefinedInProcess(name, solution_process): |
24 | | - raise NameError("%r not in solution environment" % name) |
| 36 | +# Check functions ------------------------------------------------------------- |
25 | 37 |
|
26 | | - if not isInstanceInProcess(name, dict, solution_process): |
27 | | - raise ValueError("%r is not a dictionary in the solution environment" % name) |
| 38 | +def check_dict(name, undefined_msg, not_instance_msg, state=None): |
| 39 | + rep = Reporter.active_reporter |
28 | 40 |
|
29 | 41 | # Check if defined |
30 | | - if not undefined_msg: |
31 | | - undefined_msg = "Are you sure you defined the dictionary `%s`?" % name |
| 42 | + undefined_msg = undefined_msg.format(name=name) |
32 | 43 | _msg = state.build_message(undefined_msg) |
33 | | - rep.do_test(DefinedProcessTest(name, student_process, Feedback(_msg))) |
34 | 44 |
|
35 | | - if not not_dictionary_msg: |
36 | | - not_dictionary_msg = "`%s` is not a dictionary." % name |
37 | | - _msg = state.build_message(not_dictionary_msg) |
38 | | - rep.do_test(InstanceProcessTest(name, dict, student_process, Feedback(_msg))) |
| 45 | + # check but don't get solution object representation |
| 46 | + state = check_object(name, _msg, state=state) |
39 | 47 |
|
40 | | - sol_keys = getKeysInProcess(name, solution_process) |
41 | | - if sol_keys is None: |
42 | | - raise ValueError("Something went wrong in figuring out the keys for %s in the solution process" % name) |
| 48 | + is_instance(name, dict, not_instance_msg, state=state) |
43 | 49 |
|
44 | | - # set keys or check if manual keys are valid |
45 | | - if keys is None: |
46 | | - keys = sol_keys |
47 | | - elif set(keys) > set(sol_keys): |
| 50 | + return state |
| 51 | + |
| 52 | +def is_instance(name, inst, not_instance_msg, state=None): |
| 53 | + rep = Reporter.active_reporter |
| 54 | + |
| 55 | + if not isInstanceInProcess(name, inst, state.solution_process): |
| 56 | + raise ValueError("%r is not a %s in the solution environment" % (name, type(inst))) |
| 57 | + |
| 58 | + feedback = Feedback(not_instance_msg.format(name=name)) |
| 59 | + rep.do_test(InstanceProcessTest(name, inst, state.student_process, feedback)) |
| 60 | + |
| 61 | +def has_key(name, key, key_missing_msg, state=None): |
| 62 | + rep = Reporter.active_reporter |
| 63 | + |
| 64 | + if not isDefinedCollInProcess(name, key, state.solution_process): |
48 | 65 | raise NameError("Not all keys you specified are actually keys in %s in the solution process" % name) |
49 | 66 |
|
50 | | - # Check if keys and values ok |
51 | | - for key in keys: |
| 67 | + # 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))) |
| 70 | + |
| 71 | +def test_key(name, key, incorrect_value_msg, key_missing_msg, state=None): |
| 72 | + rep = Reporter.active_reporter |
| 73 | + |
| 74 | + has_key(name, key, key_missing_msg, state=state) |
| 75 | + |
| 76 | + sol_value, sol_str = getValueInProcess(name, key, state.solution_process) |
| 77 | + if isinstance(sol_value, ReprFail): |
| 78 | + raise NameError("Value from %r can't be fetched from the solution process: %s" % c(name, sol_value.info)) |
52 | 79 |
|
53 | | - # check if key available |
54 | | - if not key_missing_msg: |
55 | | - msg = "Have you specified a key `%s` inside `%s`?" % (str(key), name) |
56 | | - else: |
57 | | - msg = key_missing_msg |
58 | | - _msg = state.build_message(msg) |
59 | | - rep.do_test(DefinedCollProcessTest(name, key, student_process, Feedback(_msg))) |
60 | | - |
61 | | - |
62 | | - sol_value, sol_str = getValueInProcess(name, key, solution_process) |
63 | | - if isinstance(sol_value, ReprFail): |
64 | | - raise NameError("Value from %r can't be fetched from the solution process: %s" % c(name, sol_value.info)) |
65 | | - |
66 | | - # check if value ok |
67 | | - if not incorrect_value_msg: |
68 | | - msg = "Have you specified the correct value for the key `%s` inside `%s`?" % (str(key), name) |
69 | | - else: |
70 | | - msg = incorrect_value_msg |
71 | | - _msg = state.build_message(msg) |
72 | | - rep.do_test(EqualValueProcessTest(name, key, student_process, sol_value, Feedback(_msg))) |
| 80 | + # 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))) |
0 commit comments