| 1 |
# ############################################################################## |
| 2 |
#(G)odot (U)nit (T)est class |
| 3 |
# |
| 4 |
# ############################################################################## |
| 5 |
# The MIT License (MIT) |
| 6 |
# ===================== |
| 7 |
# |
| 8 |
# Copyright (c) 2020 Tom "Butch" Wesley |
| 9 |
# |
| 10 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 |
# of this software and associated documentation files (the "Software"), to deal |
| 12 |
# in the Software without restriction, including without limitation the rights |
| 13 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 |
# copies of the Software, and to permit persons to whom the Software is |
| 15 |
# furnished to do so, subject to the following conditions: |
| 16 |
# |
| 17 |
# The above copyright notice and this permission notice shall be included in |
| 18 |
# all copies or substantial portions of the Software. |
| 19 |
# |
| 20 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 26 |
# THE SOFTWARE. |
| 27 |
# |
| 28 |
# ############################################################################## |
| 29 |
# This class wraps around the various printers and supplies formatting for the |
| 30 |
# various message types (error, warning, etc). |
| 31 |
# ############################################################################## |
| 32 |
var types = { |
| 33 |
debug = 'debug', |
| 34 |
deprecated = 'deprecated', |
| 35 |
error = 'error', |
| 36 |
failed = 'failed', |
| 37 |
info = 'info', |
| 38 |
normal = 'normal', |
| 39 |
orphan = 'orphan', |
| 40 |
passed = 'passed', |
| 41 |
pending = 'pending', |
| 42 |
warn ='warn', |
| 43 |
} |
| 44 |
|
| 45 |
var fmts = { |
| 46 |
red = 'red', |
| 47 |
yellow = 'yellow', |
| 48 |
green = 'green', |
| 49 |
|
| 50 |
bold = 'bold', |
| 51 |
underline = 'underline', |
| 52 |
|
| 53 |
none = null |
| 54 |
} |
| 55 |
|
| 56 |
var _type_data = { |
| 57 |
types.debug: {disp='DEBUG', enabled=true, fmt=fmts.none}, |
| 58 |
types.deprecated: {disp='DEPRECATED', enabled=true, fmt=fmts.none}, |
| 59 |
types.error: {disp='ERROR', enabled=true, fmt=fmts.red}, |
| 60 |
types.failed: {disp='Failed', enabled=true, fmt=fmts.red}, |
| 61 |
types.info: {disp='INFO', enabled=true, fmt=fmts.bold}, |
| 62 |
types.normal: {disp='NORMAL', enabled=true, fmt=fmts.none}, |
| 63 |
types.orphan: {disp='Orphans', enabled=true, fmt=fmts.yellow}, |
| 64 |
types.passed: {disp='Passed', enabled=true, fmt=fmts.green}, |
| 65 |
types.pending: {disp='Pending', enabled=true, fmt=fmts.yellow}, |
| 66 |
types.warn: {disp='WARNING', enabled=true, fmt=fmts.yellow}, |
| 67 |
} |
| 68 |
|
| 69 |
var _logs = { |
| 70 |
types.warn: [], |
| 71 |
types.error: [], |
| 72 |
types.info: [], |
| 73 |
types.debug: [], |
| 74 |
types.deprecated: [], |
| 75 |
} |
| 76 |
|
| 77 |
var _printers = { |
| 78 |
terminal = null, |
| 79 |
gui = null, |
| 80 |
console = null |
| 81 |
} |
| 82 |
|
| 83 |
var _gut = null |
| 84 |
var _utils = null |
| 85 |
var _indent_level = 0 |
| 86 |
var _indent_string = ' ' |
| 87 |
var _skip_test_name_for_testing = false |
| 88 |
var _less_test_names = false |
| 89 |
var _yield_calls = 0 |
| 90 |
var _last_yield_text = '' |
| 91 |
|
| 92 |
|
| 93 |
func _init(): |
| 94 |
_utils = load('res://addons/gut/utils.gd').get_instance() |
| 95 |
_printers.terminal = _utils.Printers.TerminalPrinter.new() |
| 96 |
_printers.console = _utils.Printers.ConsolePrinter.new() |
| 97 |
# There were some problems in the timing of disabling this at the right |
| 98 |
# time in gut_cmdln so it is disabled by default. This is enabled |
| 99 |
# by plugin_control.gd based on settings. |
| 100 |
_printers.console.set_disabled(true) |
| 101 |
|
| 102 |
func get_indent_text(): |
| 103 |
var pad = '' |
| 104 |
for i in range(_indent_level): |
| 105 |
pad += _indent_string |
| 106 |
|
| 107 |
return pad |
| 108 |
|
| 109 |
func _indent_text(text): |
| 110 |
var to_return = text |
| 111 |
var ending_newline = '' |
| 112 |
|
| 113 |
if(text.ends_with("\n")): |
| 114 |
ending_newline = "\n" |
| 115 |
to_return = to_return.left(to_return.length() -1) |
| 116 |
|
| 117 |
var pad = get_indent_text() |
| 118 |
to_return = to_return.replace("\n", "\n" + pad) |
| 119 |
to_return += ending_newline |
| 120 |
|
| 121 |
return pad + to_return |
| 122 |
|
| 123 |
func _should_print_to_printer(key_name): |
| 124 |
return _printers[key_name] != null and !_printers[key_name].get_disabled() |
| 125 |
|
| 126 |
func _print_test_name(): |
| 127 |
if(_gut == null): |
| 128 |
return |
| 129 |
var cur_test = _gut.get_current_test_object() |
| 130 |
if(cur_test == null): |
| 131 |
return false |
| 132 |
|
| 133 |
if(!cur_test.has_printed_name): |
| 134 |
_output('* ' + cur_test.name + "\n") |
| 135 |
cur_test.has_printed_name = true |
| 136 |
|
| 137 |
func _output(text, fmt=null): |
| 138 |
for key in _printers: |
| 139 |
if(_should_print_to_printer(key)): |
| 140 |
var info = ''#str(self, ':', key, ':', _printers[key], '| ') |
| 141 |
_printers[key].send(info + text, fmt) |
| 142 |
|
| 143 |
func _log(text, fmt=fmts.none): |
| 144 |
_print_test_name() |
| 145 |
var indented = _indent_text(text) |
| 146 |
_output(indented, fmt) |
| 147 |
|
| 148 |
# --------------- |
| 149 |
# Get Methods |
| 150 |
# --------------- |
| 151 |
func get_warnings(): |
| 152 |
return get_log_entries(types.warn) |
| 153 |
|
| 154 |
func get_errors(): |
| 155 |
return get_log_entries(types.error) |
| 156 |
|
| 157 |
func get_infos(): |
| 158 |
return get_log_entries(types.info) |
| 159 |
|
| 160 |
func get_debugs(): |
| 161 |
return get_log_entries(types.debug) |
| 162 |
|
| 163 |
func get_deprecated(): |
| 164 |
return get_log_entries(types.deprecated) |
| 165 |
|
| 166 |
func get_count(log_type=null): |
| 167 |
var count = 0 |
| 168 |
if(log_type == null): |
| 169 |
for key in _logs: |
| 170 |
count += _logs[key].size() |
| 171 |
else: |
| 172 |
count = _logs[log_type].size() |
| 173 |
return count |
| 174 |
|
| 175 |
func get_log_entries(log_type): |
| 176 |
return _logs[log_type] |
| 177 |
|
| 178 |
# --------------- |
| 179 |
# Log methods |
| 180 |
# --------------- |
| 181 |
func _output_type(type, text): |
| 182 |
var td = _type_data[type] |
| 183 |
if(!td.enabled): |
| 184 |
return |
| 185 |
|
| 186 |
_print_test_name() |
| 187 |
if(type != types.normal): |
| 188 |
if(_logs.has(type)): |
| 189 |
_logs[type].append(text) |
| 190 |
|
| 191 |
var start = str('[', td.disp, ']') |
| 192 |
if(text != null and text != ''): |
| 193 |
start += ': ' |
| 194 |
else: |
| 195 |
start += ' ' |
| 196 |
var indented_start = _indent_text(start) |
| 197 |
var indented_end = _indent_text(text) |
| 198 |
indented_end = indented_end.lstrip(_indent_string) |
| 199 |
_output(indented_start, td.fmt) |
| 200 |
_output(indented_end + "\n") |
| 201 |
|
| 202 |
func debug(text): |
| 203 |
_output_type(types.debug, text) |
| 204 |
|
| 205 |
# supply some text or the name of the deprecated method and the replacement. |
| 206 |
func deprecated(text, alt_method=null): |
| 207 |
var msg = text |
| 208 |
if(alt_method): |
| 209 |
msg = str('The method ', text, ' is deprecated, use ', alt_method , ' instead.') |
| 210 |
return _output_type(types.deprecated, msg) |
| 211 |
|
| 212 |
func error(text): |
| 213 |
_output_type(types.error, text) |
| 214 |
|
| 215 |
func failed(text): |
| 216 |
_output_type(types.failed, text) |
| 217 |
|
| 218 |
func info(text): |
| 219 |
_output_type(types.info, text) |
| 220 |
|
| 221 |
func orphan(text): |
| 222 |
_output_type(types.orphan, text) |
| 223 |
|
| 224 |
func passed(text): |
| 225 |
_output_type(types.passed, text) |
| 226 |
|
| 227 |
func pending(text): |
| 228 |
_output_type(types.pending, text) |
| 229 |
|
| 230 |
func warn(text): |
| 231 |
_output_type(types.warn, text) |
| 232 |
|
| 233 |
func log(text='', fmt=fmts.none): |
| 234 |
end_yield() |
| 235 |
if(text == ''): |
| 236 |
_output("\n") |
| 237 |
else: |
| 238 |
_log(text + "\n", fmt) |
| 239 |
return null |
| 240 |
|
| 241 |
func lograw(text, fmt=fmts.none): |
| 242 |
return _output(text, fmt) |
| 243 |
|
| 244 |
# Print the test name if we aren't skipping names of tests that pass (basically |
| 245 |
# what _less_test_names means)) |
| 246 |
func log_test_name(): |
| 247 |
# suppress output if we haven't printed the test name yet and |
| 248 |
# what to print is the test name. |
| 249 |
if(!_less_test_names): |
| 250 |
_print_test_name() |
| 251 |
|
| 252 |
# --------------- |
| 253 |
# Misc |
| 254 |
# --------------- |
| 255 |
func get_gut(): |
| 256 |
return _gut |
| 257 |
|
| 258 |
func set_gut(gut): |
| 259 |
_gut = gut |
| 260 |
if(_gut == null): |
| 261 |
_printers.gui = null |
| 262 |
else: |
| 263 |
if(_printers.gui == null): |
| 264 |
_printers.gui = _utils.Printers.GutGuiPrinter.new() |
| 265 |
_printers.gui.set_gut(gut) |
| 266 |
|
| 267 |
func get_indent_level(): |
| 268 |
return _indent_level |
| 269 |
|
| 270 |
func set_indent_level(indent_level): |
| 271 |
_indent_level = indent_level |
| 272 |
|
| 273 |
func get_indent_string(): |
| 274 |
return _indent_string |
| 275 |
|
| 276 |
func set_indent_string(indent_string): |
| 277 |
_indent_string = indent_string |
| 278 |
|
| 279 |
func clear(): |
| 280 |
for key in _logs: |
| 281 |
_logs[key].clear() |
| 282 |
|
| 283 |
func inc_indent(): |
| 284 |
_indent_level += 1 |
| 285 |
|
| 286 |
func dec_indent(): |
| 287 |
_indent_level = max(0, _indent_level -1) |
| 288 |
|
| 289 |
func is_type_enabled(type): |
| 290 |
return _type_data[type].enabled |
| 291 |
|
| 292 |
func set_type_enabled(type, is_enabled): |
| 293 |
_type_data[type].enabled = is_enabled |
| 294 |
|
| 295 |
func get_less_test_names(): |
| 296 |
return _less_test_names |
| 297 |
|
| 298 |
func set_less_test_names(less_test_names): |
| 299 |
_less_test_names = less_test_names |
| 300 |
|
| 301 |
func disable_printer(name, is_disabled): |
| 302 |
_printers[name].set_disabled(is_disabled) |
| 303 |
|
| 304 |
func is_printer_disabled(name): |
| 305 |
return _printers[name].get_disabled() |
| 306 |
|
| 307 |
func disable_formatting(is_disabled): |
| 308 |
for key in _printers: |
| 309 |
_printers[key].set_format_enabled(!is_disabled) |
| 310 |
|
| 311 |
func get_printer(printer_key): |
| 312 |
return _printers[printer_key] |
| 313 |
|
| 314 |
func _yield_text_terminal(text): |
| 315 |
var printer = _printers['terminal'] |
| 316 |
if(_yield_calls != 0): |
| 317 |
printer.clear_line() |
| 318 |
printer.back(_last_yield_text.length()) |
| 319 |
printer.send(text, fmts.yellow) |
| 320 |
|
| 321 |
func _end_yield_terminal(): |
| 322 |
var printer = _printers['terminal'] |
| 323 |
printer.clear_line() |
| 324 |
printer.back(_last_yield_text.length()) |
| 325 |
|
| 326 |
func _yield_text_gui(text): |
| 327 |
var lbl = _gut.get_gui().get_waiting_label() |
| 328 |
lbl.visible = true |
| 329 |
lbl.set_bbcode('[color=yellow]' + text + '[/color]') |
| 330 |
|
| 331 |
func _end_yield_gui(): |
| 332 |
var lbl = _gut.get_gui().get_waiting_label() |
| 333 |
lbl.visible = false |
| 334 |
lbl.set_text('') |
| 335 |
|
| 336 |
func yield_text(text): |
| 337 |
_yield_text_terminal(text) |
| 338 |
_yield_text_gui(text) |
| 339 |
_last_yield_text = text |
| 340 |
_yield_calls += 1 |
| 341 |
|
| 342 |
func end_yield(): |
| 343 |
if(_yield_calls == 0): |
| 344 |
return |
| 345 |
_end_yield_terminal() |
| 346 |
_end_yield_gui() |
| 347 |
_yield_calls = 0 |
| 348 |
_last_yield_text = '' |