summaryrefslogtreecommitdiff
path: root/python/edu/course-creator/resources/fileTemplates/internal/test_helper.py.ft
blob: 1182b7815c067a393b186b5a797e3adef23e23dc (plain)
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
import sys

def get_file_text(path):
    """ get file text by path"""
    file_io = open(path, "r")
    text = file_io.read()
    file_io.close()
    return text

def get_file_output(path):
    # TODO: get file output by path
    return ""

def test_file_importable():
    """ tests there is no obvious syntax errors"""
    path = sys.argv[-1]
    try:
        import_file(path)
    except ImportError:
        failed("File contains syntax errors")
        return
    except SyntaxError:
        failed("File contains syntax errors")
        return
    except NameError:
        failed("File contains syntax errors")
        return

    passed()

def import_file(path):
    """ returns imported file """
    import imp
    tmp = imp.load_source('tmp', path)
    return tmp

def import_task_file():
    """ returns imported file """
    path = sys.argv[-1]
    return import_file(path)

def test_is_not_empty():
    path = sys.argv[-1]
    file_text = get_file_text(path)

    if len(file_text) > 0:
        passed()
    else:
        failed("The file is empty. Please, reload the task and try again.")

def test_is_initial_text(error_text="You should modify the file"):
    path = sys.argv[-1]
    text = get_initial_text(path)
    file_text = get_file_text(path)

    if file_text.strip() == text:
        failed(error_text)
    else:
        passed()

def get_initial_text(path):
    course_lib = sys.argv[-2]

    import os
    # path format is "project_root/lessonX/taskY/file.py"
    task_index = path.rfind(os.sep, 0, path.rfind(os.sep))
    index = path.rfind(os.sep, 0, task_index)
    relative_path = path[index+1:]
    initial_file_path = os.path.join(course_lib, relative_path)
    return get_file_text(initial_file_path)


def test_text_equals(text, error_text):
    path = sys.argv[-1]
    file_text = get_file_text(path)

    if file_text.strip() == text:
        passed()
    else:
        failed(error_text)

def test_window_text_deleted(error_text="Don't just delete task text"):
    windows = get_task_windows()

    for window in windows:
        if len(window) == 0:
            failed(error_text)
            return
    passed()


def failed(message="Please, reload the task and try again."):
    print("#study_plugin FAILED + " + message)

def passed():
    print("#study_plugin test OK")

def get_task_windows():
    prefix = "#study_plugin_window = "
    path = sys.argv[-1]
    import os
    windows_path = os.path.splitext(path)[0] + "_windows"
    windows = []
    f = open(windows_path, "r")
    window_text = ""
    first = True
    for line in f.readlines():
        if line.startswith(prefix):
            if not first:
                windows.append(window_text.strip())
            else:
                first = False
            window_text = line[len(prefix):]
        else:
            window_text += line

    if window_text:
        windows.append(window_text.strip())

    f.close()
    return windows

def run_common_tests(error_text="Please, reload file and try again"):
    test_file_importable()
    test_is_not_empty()
    test_is_initial_text(error_text)
    test_window_text_deleted(error_text)