Debian Codegraph @

godot/addons/gut/summary.gd

raw
1 # ------------------------------------------------------------------------------
2 # Contains all the results of a single test. Allows for multiple asserts results
3 # and pending calls.
4 # ------------------------------------------------------------------------------
5 class Test:
6 var pass_texts = []
7 var fail_texts = []
8 var pending_texts = []
9
10 # NOTE: The "failed" and "pending" text must match what is outputted by
11 # the logger in order for text highlighting to occur in summary.
12 func to_s():
13 var pad = ' '
14 var to_return = ''
15 for i in range(fail_texts.size()):
16 to_return += str(pad, '[Failed]: ', fail_texts[i], "\n")
17 for i in range(pending_texts.size()):
18 to_return += str(pad, '[Pending]: ', pending_texts[i], "\n")
19 return to_return
20
21 # ------------------------------------------------------------------------------
22 # Contains all the results for a single test-script/inner class. Persists the
23 # names of the tests and results and the order in which the tests were run.
24 # ------------------------------------------------------------------------------
25 class TestScript:
26 var name = 'NOT_SET'
27 var _tests = {}
28 var _test_order = []
29
30 func _init(script_name):
31 name = script_name
32
33 func get_pass_count():
34 var count = 0
35 for key in _tests:
36 count += _tests[key].pass_texts.size()
37 return count
38
39 func get_fail_count():
40 var count = 0
41 for key in _tests:
42 count += _tests[key].fail_texts.size()
43 return count
44
45 func get_pending_count():
46 var count = 0
47 for key in _tests:
48 count += _tests[key].pending_texts.size()
49 return count
50
51 func get_test_obj(obj_name):
52 if(!_tests.has(obj_name)):
53 _tests[obj_name] = Test.new()
54 _test_order.append(obj_name)
55 return _tests[obj_name]
56
57 func add_pass(test_name, reason):
58 var t = get_test_obj(test_name)
59 t.pass_texts.append(reason)
60
61 func add_fail(test_name, reason):
62 var t = get_test_obj(test_name)
63 t.fail_texts.append(reason)
64
65 func add_pending(test_name, reason):
66 var t = get_test_obj(test_name)
67 t.pending_texts.append(reason)
68
69 # ------------------------------------------------------------------------------
70 # Summary Class
71 #
72 # This class holds the results of all the test scripts and Inner Classes that
73 # were run.
74 # -------------------------------------------d-----------------------------------
75 var _scripts = []
76
77 func add_script(name):
78 _scripts.append(TestScript.new(name))
79
80 func get_scripts():
81 return _scripts
82
83 func get_current_script():
84 return _scripts[_scripts.size() - 1]
85
86 func add_test(test_name):
87 get_current_script().get_test_obj(test_name)
88
89 func add_pass(test_name, reason = ''):
90 get_current_script().add_pass(test_name, reason)
91
92 func add_fail(test_name, reason = ''):
93 get_current_script().add_fail(test_name, reason)
94
95 func add_pending(test_name, reason = ''):
96 get_current_script().add_pending(test_name, reason)
97
98 func get_test_text(test_name):
99 return test_name + "\n" + get_current_script().get_test_obj(test_name).to_s()
100
101 # Gets the count of unique script names minus the .<Inner Class Name> at the
102 # end. Used for displaying the number of scripts without including all the
103 # Inner Classes.
104 func get_non_inner_class_script_count():
105 var unique_scripts = {}
106 for i in range(_scripts.size()):
107 var ext_loc = _scripts[i].name.find_last('.gd.')
108 if(ext_loc == -1):
109 unique_scripts[_scripts[i].name] = 1
110 else:
111 unique_scripts[_scripts[i].name.substr(0, ext_loc + 3)] = 1
112 return unique_scripts.keys().size()
113
114 func get_totals():
115 var totals = {
116 passing = 0,
117 pending = 0,
118 failing = 0,
119 tests = 0,
120 scripts = 0
121 }
122
123 for s in range(_scripts.size()):
124 totals.passing += _scripts[s].get_pass_count()
125 totals.pending += _scripts[s].get_pending_count()
126 totals.failing += _scripts[s].get_fail_count()
127 totals.tests += _scripts[s]._test_order.size()
128
129 totals.scripts = get_non_inner_class_script_count()
130
131 return totals
132
133 func log_summary_text(lgr):
134 var orig_indent = lgr.get_indent_level()
135 var found_failing_or_pending = false
136
137 for s in range(_scripts.size()):
138 lgr.set_indent_level(0)
139 if(_scripts[s].get_fail_count() > 0 or _scripts[s].get_pending_count() > 0):
140 lgr.log(_scripts[s].name, lgr.fmts.underline)
141
142
143 for t in range(_scripts[s]._test_order.size()):
144 var tname = _scripts[s]._test_order[t]
145 var test = _scripts[s].get_test_obj(tname)
146 if(test.fail_texts.size() > 0 or test.pending_texts.size() > 0):
147 found_failing_or_pending = true
148 lgr.log(str('- ', tname))
149 lgr.inc_indent()
150
151 for i in range(test.fail_texts.size()):
152 lgr.failed(test.fail_texts[i])
153 for i in range(test.pending_texts.size()):
154 lgr.pending(test.pending_texts[i])
155 lgr.dec_indent()
156
157 lgr.set_indent_level(0)
158 if(!found_failing_or_pending):
159 lgr.log('All tests passed', lgr.fmts.green)
160
161 lgr.log()
162 var _totals = get_totals()
163 lgr.log("Totals", lgr.fmts.yellow)
164 lgr.log(str('Scripts: ', get_non_inner_class_script_count()))
165 lgr.log(str('Tests: ', _totals.tests))
166 lgr.log(str('Passing asserts: ', _totals.passing))
167 lgr.log(str('Failing asserts: ',_totals.failing))
168 lgr.log(str('Pending: ', _totals.pending))
169
170 lgr.set_indent_level(orig_indent)
171