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