forked from hedyorg/hedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_z_adventures.py
64 lines (48 loc) · 2.25 KB
/
test_z_adventures.py
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
import os
from website.yaml_file import YamlFile
import utils
import unittest
import hedy
from Tester import HedyTester, Snippet
from parameterized import parameterized
# file is called tests_z_ so they are executed last
# because programs are more of a priority than adventures, which change less and take longer to run
# Set the current directory to the root Hedy folder
os.chdir(os.path.join(os.getcwd(), __file__.replace(os.path.basename(__file__), '')))
def collect_snippets(path):
Hedy_snippets = []
files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f)) and f.endswith('.yaml')]
for f in files:
f = os.path.join(path, f)
yaml = YamlFile.for_file(f)
for adventure in yaml['adventures'].values():
for level_number in adventure['levels']:
if level_number > hedy.HEDY_MAX_LEVEL:
print('content above max level!')
else:
level = adventure['levels'][level_number]
adventure_name = adventure['name']
code_snippet_counter = 0
# code snippets inside story_text
for tag in utils.markdown_to_html_tags(level['story_text']):
if tag.name != 'pre' or not tag.contents[0]:
continue
code_snippet_counter += 1
code = tag.contents[0].contents[0]
Hedy_snippets.append(Snippet(f, level_number, 'story_text code snippet #' + str(code_snippet_counter), code, adventure_name))
# start_code
try:
start_code = level['start_code']
Hedy_snippets.append(Snippet(f, level_number, 'start_code', start_code, adventure_name))
except KeyError:
#TODO: create startcode not found error
pass
return Hedy_snippets
Hedy_snippets = [(s.name, s) for s in collect_snippets(path='../coursedata/adventures')]
class TestsAdventurePrograms(unittest.TestCase):
@parameterized.expand(Hedy_snippets)
def test_adventures(self, name, snippet):
if snippet is not None:
print(snippet.code)
result = HedyTester.validate_Hedy_code(snippet)
self.assertTrue(result)