forked from datacamp/pythonwhat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_docs.py
More file actions
154 lines (126 loc) · 3.98 KB
/
Copy pathtest_docs.py
File metadata and controls
154 lines (126 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import pytest
import tests.helper as helper
@pytest.fixture
def data1():
return {
"DC_SOLUTION": 'x = 4\nif x > 0:\n print("x is strictly positive")',
"DC_SCT": """
Ex().check_if_else().multi(
check_test().multi(
set_env(x = -1).has_equal_value(), # chain A1
set_env(x = 1).has_equal_value(), # chain A2
set_env(x = 0).has_equal_value() # chain A3
),
check_body().check_function('print', 0).check_args('value').has_equal_value() # chain B
)
""",
}
def test_compound_statement_if_1(data1):
data1["DC_CODE"] = data1["DC_SOLUTION"]
output = helper.run(data1)
assert output["correct"]
def test_compound_statement_if_2(data1):
data1["DC_CODE"] = 'x = 4\nif 0 < x:\n print("x is strictly positive")'
output = helper.run(data1)
assert output["correct"]
def test_compount_statement_if_fail(data1):
data1["DC_CODE"] = 'x = 4\nif x >= 0:\n print("x is strictly positive")'
output = helper.run(data1)
assert not output["correct"]
@pytest.fixture
def data2():
return {
"DC_SOLUTION": """
my_dict = {'a': 1, 'b': 2}
for key, value in my_dict.items():
print(key + " - " + str(value))
""",
"DC_SCT": """
Ex().check_object('my_dict').has_equal_value()
Ex().check_for_loop().multi(
check_iter().has_equal_value(),
check_body().multi(
set_context('a', 1).has_equal_output(),
set_context('b', 2).has_equal_output()
)
)
""",
}
def test_compound_statement_for_1(data2):
data2["DC_CODE"] = data2["DC_SOLUTION"]
output = helper.run(data2)
assert output["correct"]
def test_compound_statement_for_2(data2):
data2[
"DC_CODE"
] = "my_dict = {'a': 1, 'b': 2}\nfor k, v in my_dict.items():\n print(k + ' - ' + str(v))"
output = helper.run(data2)
assert output["correct"]
def test_compount_statement_for_3(data2):
data2[
"DC_CODE"
] = "my_dict = {'a': 1, 'b': 2}\nfor first, second in my_dict.items():\n mess = first + ' - ' + str(second)\n print(mess)"
output = helper.run(data2)
assert output["correct"]
@pytest.fixture
def data3():
return {
"DC_SOLUTION": """
def shout_echo(word1, echo=1):
echo_word = word1 * echo
shout_words = echo_word + '!!!'
return shout_words
""",
"DC_SCT": """
Ex().check_function_def('shout_echo').test_correct(
multi(
check_call("f('hey', 3)").has_equal_value(),
check_call("f('hi', 2)").has_equal_value(),
check_call("f('hi')").has_equal_value()
),
check_body().set_context('test', 1).multi(
has_equal_value(name = 'echo_word'),
has_equal_value(name = 'shout_words')
)
)
""",
}
def test_compound_statement_fun_def_1(data3):
data3["DC_CODE"] = data3["DC_SOLUTION"]
output = helper.run(data3)
assert output["correct"]
def test_compound_statement_fun_def_2(data3):
data3["DC_CODE"] = "def shout_echo(w, e=1):\n ew = w * e\n return ew + '!!!'"
output = helper.run(data3)
assert output["correct"]
def test_compound_statement_fun_def_3(data3):
data3["DC_CODE"] = "def shout_echo(a, b=1):\n return a * b + '!!!'"
output = helper.run(data3)
assert output["correct"]
@pytest.fixture
def data4():
return {
"DC_SOLUTION": """
def counter(lst, key):
count = 0
for l in lst:
count += l[key]
return count
""",
"DC_SCT": """
Ex().check_function_def('counter').test_correct(
multi(
check_call("f([{'a': 1}], 'a')").has_equal_value(),
check_call("f([{'b': 1}, {'b': 2}], 'b')").has_equal_value()
),
check_body().set_context([{'a': 1}, {'a': 2}], 'a').set_env(count = 0).check_for_loop().multi(
check_iter().has_equal_value(),
check_body().set_context({'a': 1}).has_equal_value(name = 'count')
)
)
""",
}
def test_compound_statement_crazy_combo_1(data4):
data4["DC_CODE"] = data4["DC_SOLUTION"]
output = helper.run(data4)
assert output["correct"]