Debian Codegraph @

godot/addons/gut/gut.gd

raw
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 # View readme for usage details.
30 # ##############################################################################
31 extends Control
32
33 # -- Settings --
34 var _select_script = ''
35 var _tests_like = ''
36 var _inner_class_name = ''
37 var _should_maximize = false setget set_should_maximize, get_should_maximize
38 var _log_level = 1 setget set_log_level, get_log_level
39 var _disable_strict_datatype_checks = false setget disable_strict_datatype_checks, is_strict_datatype_checks_disabled
40 var _test_prefix = 'test_'
41 var _file_prefix = 'test_'
42 var _file_extension = '.gd'
43 var _inner_class_prefix = 'Test'
44 var _temp_directory = 'user://gut_temp_directory'
45 var _export_path = '' setget set_export_path, get_export_path
46 var _include_subdirectories = false setget set_include_subdirectories, get_include_subdirectories
47 var _double_strategy = 1 setget set_double_strategy, get_double_strategy
48 var _pre_run_script = '' setget set_pre_run_script, get_pre_run_script
49 var _post_run_script = '' setget set_post_run_script, get_post_run_script
50 var _color_output = false setget set_color_output, get_color_output
51 # -- End Settings --
52
53
54 # ###########################
55 # Other Vars
56 # ###########################
57 const LOG_LEVEL_FAIL_ONLY = 0
58 const LOG_LEVEL_TEST_AND_FAILURES = 1
59 const LOG_LEVEL_ALL_ASSERTS = 2
60 const WAITING_MESSAGE = '/# waiting #/'
61 const PAUSE_MESSAGE = '/# Pausing. Press continue button...#/'
62 const COMPLETED = 'completed'
63
64 var _utils = load('res://addons/gut/utils.gd').get_instance()
65 var _lgr = _utils.get_logger()
66 var _strutils = _utils.Strutils.new()
67 # Used to prevent multiple messages for deprecated setup/teardown messages
68 var _deprecated_tracker = _utils.ThingCounter.new()
69
70 # The instance that is created from _pre_run_script. Accessible from
71 # get_pre_run_script_instance.
72 var _pre_run_script_instance = null
73 var _post_run_script_instance = null # This is not used except in tests.
74
75
76 var _script_name = null
77 var _test_collector = _utils.TestCollector.new()
78
79 # The instanced scripts. This is populated as the scripts are run.
80 var _test_script_objects = []
81
82 var _waiting = false
83 var _done = false
84 var _is_running = false
85
86 var _current_test = null
87 var _log_text = ""
88
89 var _pause_before_teardown = false
90 # when true _pause_before_teardown will be ignored. useful
91 # when batch processing and you don't want to watch.
92 var _ignore_pause_before_teardown = false
93 var _wait_timer = Timer.new()
94
95 var _yield_between = {
96 should = false,
97 timer = Timer.new(),
98 after_x_tests = 5,
99 tests_since_last_yield = 0
100 }
101
102 var _was_yield_method_called = false
103 # used when yielding to gut instead of some other
104 # signal. Start with set_yield_time()
105 var _yield_timer = Timer.new()
106
107 var _unit_test_name = ''
108 var _new_summary = null
109
110 var _yielding_to = {
111 obj = null,
112 signal_name = ''
113 }
114
115 var _stubber = _utils.Stubber.new()
116 var _doubler = _utils.Doubler.new()
117 var _spy = _utils.Spy.new()
118 var _gui = null
119 var _orphan_counter = _utils.OrphanCounter.new()
120 var _autofree = _utils.AutoFree.new()
121
122 # This is populated by test.gd each time a paramterized test is encountered
123 # for the first time.
124 var _parameter_handler = null
125
126 # Used to cancel importing scripts if an error has occurred in the setup. This
127 # prevents tests from being run if they were exported and ensures that the
128 # error displayed is seen since importing generates a lot of text.
129 var _cancel_import = false
130
131 # Used for proper assert tracking and printing during before_all
132 var _before_all_test_obj = load('res://addons/gut/test_collector.gd').Test.new()
133 # Used for proper assert tracking and printing during after_all
134 var _after_all_test_obj = load('res://addons/gut/test_collector.gd').Test.new()
135
136 const SIGNAL_TESTS_FINISHED = 'tests_finished'
137 const SIGNAL_STOP_YIELD_BEFORE_TEARDOWN = 'stop_yield_before_teardown'
138
139 # ------------------------------------------------------------------------------
140 # ------------------------------------------------------------------------------
141 var _should_print_versions = true # used to cut down on output in tests.
142 func _init():
143 _before_all_test_obj.name = 'before_all'
144 _after_all_test_obj.name = 'after_all'
145 # When running tests for GUT itself, _utils has been setup to always return
146 # a new logger so this does not set the gut instance on the base logger
147 # when creating test instances of GUT.
148 _lgr.set_gut(self)
149
150 add_user_signal(SIGNAL_TESTS_FINISHED)
151 add_user_signal('test_finished')
152 add_user_signal(SIGNAL_STOP_YIELD_BEFORE_TEARDOWN)
153 add_user_signal('timeout')
154
155 _doubler.set_output_dir(_temp_directory)
156 _doubler.set_stubber(_stubber)
157 _doubler.set_spy(_spy)
158 _doubler.set_gut(self)
159
160 # TODO remove these, universal logger should fix this.
161 _doubler.set_logger(_lgr)
162 _spy.set_logger(_lgr)
163 _stubber.set_logger(_lgr)
164 _test_collector.set_logger(_lgr)
165
166 _gui = load('res://addons/gut/GutScene.tscn').instance()
167
168 # ------------------------------------------------------------------------------
169 # Initialize controls
170 # ------------------------------------------------------------------------------
171 func _ready():
172 if(!_utils.is_version_ok()):
173 _print_versions()
174 push_error(_utils.get_bad_version_text())
175 print('Error: ', _utils.get_bad_version_text())
176 get_tree().quit()
177 return
178
179 if(_should_print_versions):
180 _lgr.info(str('using [', OS.get_user_data_dir(), '] for temporary output.'))
181
182 set_process_input(true)
183
184 add_child(_wait_timer)
185 _wait_timer.set_wait_time(1)
186 _wait_timer.set_one_shot(true)
187
188 add_child(_yield_between.timer)
189 _wait_timer.set_one_shot(true)
190
191 add_child(_yield_timer)
192 _yield_timer.set_one_shot(true)
193 _yield_timer.connect('timeout', self, '_yielding_callback')
194
195 _setup_gui()
196
197 if(_select_script != null):
198 select_script(_select_script)
199
200 if(_tests_like != null):
201 set_unit_test_name(_tests_like)
202
203 if(_should_maximize):
204 # GUI checks for is_in_tree will not pass yet.
205 call_deferred('maximize')
206
207 # hide the panel that IS gut so that only the GUI is seen
208 self.self_modulate = Color(1,1,1,0)
209 show()
210 _print_versions()
211
212 # ------------------------------------------------------------------------------
213 # Runs right before free is called. Can't override `free`.
214 # ------------------------------------------------------------------------------
215 func _notification(what):
216 if(what == NOTIFICATION_PREDELETE):
217 for test_script in _test_script_objects:
218 if(is_instance_valid(test_script)):
219 test_script.free()
220
221 _test_script_objects = []
222
223 if(is_instance_valid(_gui)):
224 _gui.free()
225
226 func _print_versions(send_all = true):
227 if(!_should_print_versions):
228 return
229
230 var info = _utils.get_version_text()
231
232 if(send_all):
233 p(info)
234 else:
235 var printer = _lgr.get_printer('gui')
236 printer.send(info + "\n")
237
238
239 # ##############################################################################
240 #
241 # GUI Events and setup
242 #
243 # ##############################################################################
244 func _setup_gui():
245 # This is how we get the size of the control to translate to the gui when
246 # the scene is run. This is also another reason why the min_rect_size
247 # must match between both gut and the gui.
248 _gui.rect_size = self.rect_size
249 add_child(_gui)
250 _gui.set_anchor(MARGIN_RIGHT, ANCHOR_END)
251 _gui.set_anchor(MARGIN_BOTTOM, ANCHOR_END)
252 _gui.connect('run_single_script', self, '_on_run_one')
253 _gui.connect('run_script', self, '_on_new_gui_run_script')
254 _gui.connect('end_pause', self, '_on_new_gui_end_pause')
255 _gui.connect('ignore_pause', self, '_on_new_gui_ignore_pause')
256 _gui.connect('log_level_changed', self, '_on_log_level_changed')
257 var _foo = connect('tests_finished', _gui, 'end_run')
258
259 func _add_scripts_to_gui():
260 var scripts = []
261 for i in range(_test_collector.scripts.size()):
262 var s = _test_collector.scripts[i]
263 var txt = ''
264 if(s.has_inner_class()):
265 txt = str(' - ', s.inner_class_name, ' (', s.tests.size(), ')')
266 else:
267 txt = str(s.get_full_name(), ' (', s.tests.size(), ')')
268 scripts.append(txt)
269 _gui.set_scripts(scripts)
270
271 func _on_run_one(index):
272 clear_text()
273 var indexes = [index]
274 if(!_test_collector.scripts[index].has_inner_class()):
275 indexes = _get_indexes_matching_path(_test_collector.scripts[index].path)
276 _test_the_scripts(indexes)
277
278 func _on_new_gui_run_script(index):
279 var indexes = []
280 clear_text()
281 for i in range(index, _test_collector.scripts.size()):
282 indexes.append(i)
283 _test_the_scripts(indexes)
284
285 func _on_new_gui_end_pause():
286 _pause_before_teardown = false
287 emit_signal(SIGNAL_STOP_YIELD_BEFORE_TEARDOWN)
288
289 func _on_new_gui_ignore_pause(should):
290 _ignore_pause_before_teardown = should
291
292 func _on_log_level_changed(value):
293 set_log_level(value)
294
295 #####################
296 #
297 # Events
298 #
299 #####################
300
301 # ------------------------------------------------------------------------------
302 # Timeout for the built in timer. emits the timeout signal. Start timer
303 # with set_yield_time()
304 # ------------------------------------------------------------------------------
305 func _yielding_callback(from_obj=false):
306 _lgr.end_yield()
307 if(_yielding_to.obj):
308 _yielding_to.obj.call_deferred(
309 "disconnect",
310 _yielding_to.signal_name, self,
311 '_yielding_callback')
312 _yielding_to.obj = null
313 _yielding_to.signal_name = ''
314
315 if(from_obj):
316 # we must yiled for a little longer after the signal is emitted so that
317 # the signal can propagate to other objects. This was discovered trying
318 # to assert that obj/signal_name was emitted. Without this extra delay
319 # the yield returns and processing finishes before the rest of the
320 # objects can get the signal. This works b/c the timer will timeout
321 # and come back into this method but from_obj will be false.
322 _yield_timer.set_wait_time(.1)
323 _yield_timer.start()
324 else:
325 emit_signal('timeout')
326
327 # ------------------------------------------------------------------------------
328 # completed signal for GDScriptFucntionState returned from a test script that
329 # has yielded
330 # ------------------------------------------------------------------------------
331 func _on_test_script_yield_completed():
332 _waiting = false
333
334 #####################
335 #
336 # Private
337 #
338 #####################
339 func _log_test_children_warning(test_script):
340 if(!_lgr.is_type_enabled(_lgr.types.orphan)):
341 return
342
343 var kids = test_script.get_children()
344 if(kids.size() > 0):
345 var msg = ''
346 if(_log_level == 2):
347 msg = "Test script still has children when all tests finisehd.\n"
348 for i in range(kids.size()):
349 msg += str(" ", _strutils.type2str(kids[i]), "\n")
350 msg += "You can use autofree, autoqfree, add_child_autofree, or add_child_autoqfree to automatically free objects."
351 else:
352 msg = str("Test script has ", kids.size(), " unfreed children. Increase log level for more details.")
353
354
355 _lgr.warn(msg)
356
357 # ------------------------------------------------------------------------------
358 # Convert the _summary dictionary into text
359 # ------------------------------------------------------------------------------
360 func _print_summary():
361 _lgr.log("\n\n*** Run Summary ***", _lgr.fmts.yellow)
362
363 _new_summary.log_summary_text(_lgr)
364
365 var logger_text = ''
366 if(_lgr.get_errors().size() > 0):
367 logger_text += str("\n* ", _lgr.get_errors().size(), ' Errors.')
368 if(_lgr.get_warnings().size() > 0):
369 logger_text += str("\n* ", _lgr.get_warnings().size(), ' Warnings.')
370 if(_lgr.get_deprecated().size() > 0):
371 logger_text += str("\n* ", _lgr.get_deprecated().size(), ' Deprecated calls.')
372 if(logger_text != ''):
373 logger_text = "\nWarnings/Errors:" + logger_text + "\n\n"
374 _lgr.log(logger_text)
375
376 if(_new_summary.get_totals().tests > 0):
377 var fmt = _lgr.fmts.green
378 var msg = str(_new_summary.get_totals().passing) + ' passed ' + str(_new_summary.get_totals().failing) + ' failed. ' + \
379 str("Tests finished in ", _gui.elapsed_time_as_str())
380 if(_new_summary.get_totals().failing > 0):
381 fmt = _lgr.fmts.red
382 elif(_new_summary.get_totals().pending > 0):
383 fmt = _lgr.fmts.yellow
384
385 _lgr.log(msg, fmt)
386 else:
387 _lgr.log('No tests ran', _lgr.fmts.red)
388
389
390 func _validate_hook_script(path):
391 var result = {
392 valid = true,
393 instance = null
394 }
395
396 # empty path is valid but will have a null instance
397 if(path == ''):
398 return result
399
400 var f = File.new()
401 if(f.file_exists(path)):
402 var inst = load(path).new()
403 if(inst and inst is _utils.HookScript):
404 result.instance = inst
405 result.valid = true
406 else:
407 result.valid = false
408 _lgr.error('The hook script [' + path + '] does not extend res://addons/gut/hook_script.gd')
409 else:
410 result.valid = false
411 _lgr.error('The hook script [' + path + '] does not exist.')
412
413 return result
414
415
416 # ------------------------------------------------------------------------------
417 # Runs a hook script. Script must exist, and must extend
418 # res://addons/gut/hook_script.gd
419 # ------------------------------------------------------------------------------
420 func _run_hook_script(inst):
421 if(inst != null):
422 inst.gut = self
423 inst.run()
424 return inst
425
426 # ------------------------------------------------------------------------------
427 # Initialize variables for each run of a single test script.
428 # ------------------------------------------------------------------------------
429 func _init_run():
430 var valid = true
431 _test_collector.set_test_class_prefix(_inner_class_prefix)
432 _test_script_objects = []
433 _new_summary = _utils.Summary.new()
434
435 _log_text = ""
436
437 _current_test = null
438
439 _is_running = true
440
441 _yield_between.tests_since_last_yield = 0
442
443 var pre_hook_result = _validate_hook_script(_pre_run_script)
444 _pre_run_script_instance = pre_hook_result.instance
445 var post_hook_result = _validate_hook_script(_post_run_script)
446 _post_run_script_instance = post_hook_result.instance
447
448 valid = pre_hook_result.valid and post_hook_result.valid
449
450 return valid
451
452
453 # ------------------------------------------------------------------------------
454 # Print out run information and close out the run.
455 # ------------------------------------------------------------------------------
456 func _end_run():
457 _gui.end_run()
458 _print_summary()
459 p("\n")
460
461 # Do not count any of the _test_script_objects since these will be released
462 # when GUT is released.
463 _orphan_counter._counters.total += _test_script_objects.size()
464 if(_orphan_counter.get_counter('total') > 0 and _lgr.is_type_enabled('orphan')):
465 _orphan_counter.print_orphans('total', _lgr)
466 p("Note: This count does not include GUT objects that will be freed upon exit.")
467 p(" It also does not include any orphans created by global scripts")
468 p(" loaded before tests were ran.")
469 p(str("Total orphans = ", _orphan_counter.orphan_count()))
470
471 if(!_utils.is_null_or_empty(_select_script)):
472 p('Ran Scripts matching "' + _select_script + '"')
473 if(!_utils.is_null_or_empty(_unit_test_name)):
474 p('Ran Tests matching "' + _unit_test_name + '"')
475 if(!_utils.is_null_or_empty(_inner_class_name)):
476 p('Ran Inner Classes matching "' + _inner_class_name + '"')
477
478 # For some reason the text edit control isn't scrolling to the bottom after
479 # the summary is printed. As a workaround, yield for a short time and
480 # then move the cursor. I found this workaround through trial and error.
481 _yield_between.timer.set_wait_time(0.1)
482 _yield_between.timer.start()
483 yield(_yield_between.timer, 'timeout')
484 _gui.scroll_to_bottom()
485
486 _is_running = false
487 update()
488 _run_hook_script(_post_run_script_instance)
489 emit_signal(SIGNAL_TESTS_FINISHED)
490
491 _gui.set_title("Finished.")
492
493
494 # ------------------------------------------------------------------------------
495 # Checks the passed in thing to see if it is a "function state" object that gets
496 # returned when a function yields.
497 # ------------------------------------------------------------------------------
498 func _is_function_state(script_result):
499 return script_result != null and \
500 typeof(script_result) == TYPE_OBJECT and \
501 script_result is GDScriptFunctionState and \
502 script_result.is_valid()
503
504 # ------------------------------------------------------------------------------
505 # Print out the heading for a new script
506 # ------------------------------------------------------------------------------
507 func _print_script_heading(script):
508 if(_does_class_name_match(_inner_class_name, script.inner_class_name)):
509 var fmt = _lgr.fmts.underline
510 var divider = '-----------------------------------------'
511
512 var text = ''
513 if(script.inner_class_name == null):
514 text = script.path
515 else:
516 text = script.path + '.' + script.inner_class_name
517 _lgr.log("\n\n" + text, fmt)
518
519 if(!_utils.is_null_or_empty(_inner_class_name) and _does_class_name_match(_inner_class_name, script.inner_class_name)):
520 _lgr.log(str(' [',script.inner_class_name, '] matches [', _inner_class_name, ']'), fmt)
521
522 if(!_utils.is_null_or_empty(_unit_test_name)):
523 _lgr.log(' Only running tests like: "' + _unit_test_name + '"', fmt)
524
525
526 # ------------------------------------------------------------------------------
527 # Just gets more logic out of _test_the_scripts. Decides if we should yield after
528 # this test based on flags and counters.
529 # ------------------------------------------------------------------------------
530 func _should_yield_now():
531 var should = _yield_between.should and \
532 _yield_between.tests_since_last_yield == _yield_between.after_x_tests
533 if(should):
534 _yield_between.tests_since_last_yield = 0
535 else:
536 _yield_between.tests_since_last_yield += 1
537 return should
538
539 # ------------------------------------------------------------------------------
540 # Yes if the class name is null or the script's class name includes class_name
541 # ------------------------------------------------------------------------------
542 func _does_class_name_match(the_class_name, script_class_name):
543 return (the_class_name == null or the_class_name == '') or (script_class_name != null and script_class_name.findn(the_class_name) != -1)
544
545 # ------------------------------------------------------------------------------
546 # ------------------------------------------------------------------------------
547 func _setup_script(test_script):
548 test_script.gut = self
549 test_script.set_logger(_lgr)
550 add_child(test_script)
551 _test_script_objects.append(test_script)
552
553
554 # ------------------------------------------------------------------------------
555 # ------------------------------------------------------------------------------
556 func _do_yield_between(time):
557 _yield_between.timer.set_wait_time(time)
558 _yield_between.timer.start()
559 return _yield_between.timer
560
561 # ------------------------------------------------------------------------------
562 # ------------------------------------------------------------------------------
563 func _wait_for_done(result):
564 var iter_counter = 0
565 var print_after = 3
566
567 # callback method sets waiting to false.
568 result.connect(COMPLETED, self, '_on_test_script_yield_completed')
569 if(!_was_yield_method_called):
570 _lgr.log('-- Yield detected, waiting --', _lgr.fmts.yellow)
571
572 _was_yield_method_called = false
573 _waiting = true
574 _wait_timer.set_wait_time(0.4)
575
576 var dots = ''
577 while(_waiting):
578 iter_counter += 1
579 _lgr.yield_text('waiting' + dots)
580 _wait_timer.start()
581 yield(_wait_timer, 'timeout')
582 dots += '.'
583 if(dots.length() > 5):
584 dots = ''
585
586 _lgr.end_yield()
587
588 # ------------------------------------------------------------------------------
589 # returns self so it can be integrated into the yield call.
590 # ------------------------------------------------------------------------------
591 func _wait_for_continue_button():
592 p(PAUSE_MESSAGE, 0)
593 _waiting = true
594 return self
595
596 # ------------------------------------------------------------------------------
597 # ------------------------------------------------------------------------------
598 func _call_deprecated_script_method(script, method, alt):
599 if(script.has_method(method)):
600 var txt = str(script, '-', method)
601 if(!_deprecated_tracker.has(txt)):
602 # Removing the deprecated line. I think it's still too early to
603 # start bothering people with this. Left everything here though
604 # because I don't want to remember how I did this last time.
605 _lgr.deprecated(str('The method ', method, ' has been deprecated, use ', alt, ' instead.'))
606 _deprecated_tracker.add(txt)
607 script.call(method)
608
609 # ------------------------------------------------------------------------------
610 # ------------------------------------------------------------------------------
611 func _get_indexes_matching_script_name(name):
612 var indexes = [] # empty runs all
613 for i in range(_test_collector.scripts.size()):
614 if(_test_collector.scripts[i].get_filename().find(name) != -1):
615 indexes.append(i)
616 return indexes
617
618 # ------------------------------------------------------------------------------
619 # ------------------------------------------------------------------------------
620 func _get_indexes_matching_path(path):
621 var indexes = []
622 for i in range(_test_collector.scripts.size()):
623 if(_test_collector.scripts[i].path == path):
624 indexes.append(i)
625 return indexes
626
627 # ------------------------------------------------------------------------------
628 # Execute all calls of a parameterized test.
629 # ------------------------------------------------------------------------------
630 func _run_parameterized_test(test_script, test_name):
631 var script_result = _run_test(test_script, test_name)
632 if(_is_function_state(script_result)):
633 # _run_tests does _wait_for_done so just wait on it to complete
634 yield(script_result, COMPLETED)
635
636 if(_parameter_handler == null):
637 _lgr.error(str('Parameterized test ', _current_test.name, ' did not call use_parameters for the default value of the parameter.'))
638 _fail(str('Parameterized test ', _current_test.name, ' did not call use_parameters for the default value of the parameter.'))
639 else:
640 while(!_parameter_handler.is_done()):
641 script_result = _run_test(test_script, test_name)
642 if(_is_function_state(script_result)):
643 # _run_tests does _wait_for_done so just wait on it to complete
644 yield(script_result, COMPLETED)
645
646 _parameter_handler = null
647
648
649 # ------------------------------------------------------------------------------
650 # Runs a single test given a test.gd instance and the name of the test to run.
651 # ------------------------------------------------------------------------------
652 func _run_test(script_inst, test_name):
653 _lgr.log_test_name()
654 _lgr.set_indent_level(1)
655 _orphan_counter.add_counter('test')
656 var script_result = null
657
658 _call_deprecated_script_method(script_inst, 'setup', 'before_each')
659 var before_each_result = script_inst.before_each()
660 if(_is_function_state(before_each_result)):
661 yield(_wait_for_done(before_each_result), COMPLETED)
662
663 # When the script yields it will return a GDScriptFunctionState object
664 script_result = script_inst.call(test_name)
665 _new_summary.add_test(test_name)
666
667 # Cannot detect future yields since we never tell the method to resume. If
668 # there was some way to tell the method to resume we could use what comes
669 # back from that to detect additional yields. I don't think this is
670 # possible since we only know what the yield was for except when yield_for
671 # and yield_to are used.
672 if(_is_function_state(script_result)):
673 yield(_wait_for_done(script_result), COMPLETED)
674
675 # if the test called pause_before_teardown then yield until
676 # the continue button is pressed.
677 if(_pause_before_teardown and !_ignore_pause_before_teardown):
678 _gui.pause()
679 yield(_wait_for_continue_button(), SIGNAL_STOP_YIELD_BEFORE_TEARDOWN)
680
681 script_inst.clear_signal_watcher()
682
683 # call each post-each-test method until teardown is removed.
684 _call_deprecated_script_method(script_inst, 'teardown', 'after_each')
685 var after_each_result = script_inst.after_each()
686 if(_is_function_state(after_each_result)):
687 yield(_wait_for_done(after_each_result), COMPLETED)
688
689 # Free up everything in the _autofree. Yield for a bit if we
690 # have anything with a queue_free so that they have time to
691 # free and are not found by the orphan counter.
692 var aqf_count = _autofree.get_queue_free_count()
693 _autofree.free_all()
694 if(aqf_count > 0):
695 yield(_do_yield_between(0.1), 'timeout')
696
697 if(_log_level > 0):
698 _orphan_counter.print_orphans('test', _lgr)
699
700 _doubler.get_ignored_methods().clear()
701
702 # ------------------------------------------------------------------------------
703 # Calls after_all on the passed in test script and takes care of settings so all
704 # logger output appears indented and with a proper heading
705 #
706 # Calls both pre-all-tests methods until prerun_setup is removed
707 # ------------------------------------------------------------------------------
708 func _call_before_all(test_script):
709 _current_test = _before_all_test_obj
710 _current_test.has_printed_name = false
711 _lgr.inc_indent()
712
713 # Next 3 lines can be removed when prerun_setup removed.
714 _current_test.name = 'prerun_setup'
715 _call_deprecated_script_method(test_script, 'prerun_setup', 'before_all')
716 _current_test.name = 'before_all'
717
718 var result = test_script.before_all()
719 if(_is_function_state(result)):
720 yield(_wait_for_done(result), COMPLETED)
721
722 _lgr.dec_indent()
723 _current_test = null
724
725 # ------------------------------------------------------------------------------
726 # Calls after_all on the passed in test script and takes care of settings so all
727 # logger output appears indented and with a proper heading
728 #
729 # Calls both post-all-tests methods until postrun_teardown is removed.
730 # ------------------------------------------------------------------------------
731 func _call_after_all(test_script):
732 _current_test = _after_all_test_obj
733 _current_test.has_printed_name = false
734 _lgr.inc_indent()
735
736 # Next 3 lines can be removed when postrun_teardown removed.
737 _current_test.name = 'postrun_teardown'
738 _call_deprecated_script_method(test_script, 'postrun_teardown', 'after_all')
739 _current_test.name = 'after_all'
740
741 var result = test_script.after_all()
742 if(_is_function_state(result)):
743 yield(_wait_for_done(result), COMPLETED)
744
745
746 _lgr.dec_indent()
747 _current_test = null
748
749 # ------------------------------------------------------------------------------
750 # Run all tests in a script. This is the core logic for running tests.
751 # ------------------------------------------------------------------------------
752 func _test_the_scripts(indexes=[]):
753 _orphan_counter.add_counter('total')
754
755 _print_versions(false)
756 var is_valid = _init_run()
757 if(!is_valid):
758 _lgr.error('Something went wrong and the run was aborted.')
759 return
760
761 _run_hook_script(_pre_run_script_instance)
762 if(_pre_run_script_instance!= null and _pre_run_script_instance.should_abort()):
763 _lgr.error('pre-run abort')
764 emit_signal(SIGNAL_TESTS_FINISHED)
765 return
766
767 _gui.run_mode()
768
769 var indexes_to_run = []
770 if(indexes.size()==0):
771 for i in range(_test_collector.scripts.size()):
772 indexes_to_run.append(i)
773 else:
774 indexes_to_run = indexes
775
776 _gui.set_progress_script_max(indexes_to_run.size()) # New way
777 _gui.set_progress_script_value(0)
778
779 if(_doubler.get_strategy() == _utils.DOUBLE_STRATEGY.FULL):
780 _lgr.info("Using Double Strategy FULL as default strategy. Keep an eye out for weirdness, this is still experimental.")
781
782 # loop through scripts
783 for test_indexes in range(indexes_to_run.size()):
784 var the_script = _test_collector.scripts[indexes_to_run[test_indexes]]
785 _orphan_counter.add_counter('script')
786
787 if(the_script.tests.size() > 0):
788 _gui.set_title(the_script.get_full_name())
789 _lgr.set_indent_level(0)
790 _print_script_heading(the_script)
791 _new_summary.add_script(the_script.get_full_name())
792
793 var test_script = the_script.get_new()
794 var script_result = null
795 _setup_script(test_script)
796 _doubler.set_strategy(_double_strategy)
797
798 # yield between test scripts so things paint
799 if(_yield_between.should):
800 yield(_do_yield_between(0.01), 'timeout')
801
802 # !!!
803 # Hack so there isn't another indent to this monster of a method. if
804 # inner class is set and we do not have a match then empty the tests
805 # for the current test.
806 # !!!
807 if(!_does_class_name_match(_inner_class_name, the_script.inner_class_name)):
808 the_script.tests = []
809 else:
810 var before_all_result = _call_before_all(test_script)
811 if(_is_function_state(before_all_result)):
812 # _call_before_all calls _wait for done, just wait for that to finish
813 yield(before_all_result, COMPLETED)
814
815
816 _gui.set_progress_test_max(the_script.tests.size()) # New way
817
818 # Each test in the script
819 for i in range(the_script.tests.size()):
820 _stubber.clear()
821 _spy.clear()
822 _doubler.clear_output_directory()
823 _current_test = the_script.tests[i]
824 script_result = null
825
826 if((_unit_test_name != '' and _current_test.name.find(_unit_test_name) > -1) or
827 (_unit_test_name == '')):
828
829 # yield so things paint
830 if(_should_yield_now()):
831 yield(_do_yield_between(0.001), 'timeout')
832
833 if(_current_test.arg_count > 1):
834 _lgr.error(str('Parameterized test ', _current_test.name,
835 ' has too many parameters: ', _current_test.arg_count, '.'))
836 elif(_current_test.arg_count == 1):
837 script_result = _run_parameterized_test(test_script, _current_test.name)
838 else:
839 script_result = _run_test(test_script, _current_test.name)
840
841 if(_is_function_state(script_result)):
842 # _run_test calls _wait for done, just wait for that to finish
843 yield(script_result, COMPLETED)
844
845 if(_current_test.assert_count == 0 and !_current_test.pending):
846 _lgr.warn('Test did not assert')
847 _current_test.has_printed_name = false
848 _gui.set_progress_test_value(i + 1)
849 emit_signal('test_finished')
850
851
852 _current_test = null
853 _lgr.dec_indent()
854 _orphan_counter.print_orphans('script', _lgr)
855
856 if(_does_class_name_match(_inner_class_name, the_script.inner_class_name)):
857 var after_all_result = _call_after_all(test_script)
858 if(_is_function_state(after_all_result)):
859 # _call_after_all calls _wait for done, just wait for that to finish
860 yield(after_all_result, COMPLETED)
861
862
863 _log_test_children_warning(test_script)
864 # This might end up being very resource intensive if the scripts
865 # don't clean up after themselves. Might have to consolidate output
866 # into some other structure and kill the script objects with
867 # test_script.free() instead of remove child.
868 remove_child(test_script)
869
870 _lgr.set_indent_level(0)
871 if(test_script.get_assert_count() > 0):
872 var script_sum = str(test_script.get_pass_count(), '/', test_script.get_assert_count(), ' passed.')
873 _lgr.log(script_sum, _lgr.fmts.bold)
874
875 _gui.set_progress_script_value(test_indexes + 1) # new way
876 # END TEST SCRIPT LOOP
877
878 _lgr.set_indent_level(0)
879 _end_run()
880
881
882 # ------------------------------------------------------------------------------
883 # ------------------------------------------------------------------------------
884 func _pass(text=''):
885 _gui.add_passing() # increments counters
886 if(_current_test):
887 _current_test.assert_count += 1
888 _new_summary.add_pass(_current_test.name, text)
889 else:
890 if(_new_summary != null): # b/c of tests.
891 _new_summary.add_pass('script level', text)
892
893
894 # ------------------------------------------------------------------------------
895 # ------------------------------------------------------------------------------
896 func _fail(text=''):
897 _gui.add_failing() # increments counters
898 if(_current_test != null):
899 var line_text = ' at line ' + str(_extract_line_number(_current_test))
900 p(line_text, LOG_LEVEL_FAIL_ONLY)
901 # format for summary
902 line_text = "\n " + line_text
903 var call_count_text = ''
904 if(_parameter_handler != null):
905 call_count_text = str('(call #', _parameter_handler.get_call_count(), ') ')
906 _new_summary.add_fail(_current_test.name, call_count_text + text + line_text)
907 _current_test.passed = false
908 _current_test.assert_count += 1
909 else:
910 if(_new_summary != null): # b/c of tests.
911 _new_summary.add_fail('script level', text)
912
913
914 # ------------------------------------------------------------------------------
915 # Extracts the line number from curren stacktrace by matching the test case name
916 # ------------------------------------------------------------------------------
917 func _extract_line_number(current_test):
918 var line_number = -1
919 # if stack trace available than extraxt the test case line number
920 var stackTrace = get_stack()
921 if(stackTrace!=null):
922 for index in stackTrace.size():
923 var line = stackTrace[index]
924 var function = line.get("function")
925 if function == current_test.name:
926 line_number = line.get("line")
927 return line_number
928
929
930 # ------------------------------------------------------------------------------
931 # ------------------------------------------------------------------------------
932 func _pending(text=''):
933 if(_current_test):
934 _current_test.pending = true
935 _new_summary.add_pending(_current_test.name, text)
936
937
938 # ------------------------------------------------------------------------------
939 # Gets all the files in a directory and all subdirectories if get_include_subdirectories
940 # is true. The files returned are all sorted by name.
941 # ------------------------------------------------------------------------------
942 func _get_files(path, prefix, suffix):
943 var files = []
944 var directories = []
945
946 var d = Directory.new()
947 d.open(path)
948 # true parameter tells list_dir_begin not to include "." and ".." directories.
949 d.list_dir_begin(true)
950
951 # Traversing a directory is kinda odd. You have to start the process of listing
952 # the contents of a directory with list_dir_begin then use get_next until it
953 # returns an empty string. Then I guess you should end it.
954 var fs_item = d.get_next()
955 var full_path = ''
956 while(fs_item != ''):
957 full_path = path.plus_file(fs_item)
958
959 #file_exists returns fasle for directories
960 if(d.file_exists(full_path)):
961 if(fs_item.begins_with(prefix) and fs_item.ends_with(suffix)):
962 files.append(full_path)
963 elif(get_include_subdirectories() and d.dir_exists(full_path)):
964 directories.append(full_path)
965
966 fs_item = d.get_next()
967 d.list_dir_end()
968
969 for dir in range(directories.size()):
970 var dir_files = _get_files(directories[dir], prefix, suffix)
971 for i in range(dir_files.size()):
972 files.append(dir_files[i])
973
974 files.sort()
975 return files
976
977
978 #########################
979 #
980 # public
981 #
982 #########################
983
984 # ------------------------------------------------------------------------------
985 # Conditionally prints the text to the console/results variable based on the
986 # current log level and what level is passed in. Whenever currently in a test,
987 # the text will be indented under the test. It can be further indented if
988 # desired.
989 #
990 # The first time output is generated when in a test, the test name will be
991 # printed.
992 #
993 # NOT_USED_ANYMORE was indent level. This was deprecated in 7.0.0.
994 # ------------------------------------------------------------------------------
995 func p(text, level=0, NOT_USED_ANYMORE=-123):
996 if(NOT_USED_ANYMORE != -123):
997 _lgr.deprecated('gut.p no longer supports the optional 3rd parameter for indent_level parameter.')
998 var str_text = str(text)
999
1000 if(level <= _utils.nvl(_log_level, 0)):
1001 _lgr.log(str_text)
1002
1003 ################
1004 #
1005 # RUN TESTS/ADD SCRIPTS
1006 #
1007 ################
1008 func get_minimum_size():
1009 return Vector2(810, 380)
1010
1011
1012 # ------------------------------------------------------------------------------
1013 # Runs all the scripts that were added using add_script
1014 # ------------------------------------------------------------------------------
1015 func test_scripts(run_rest=false):
1016 clear_text()
1017
1018 if(_script_name != null and _script_name != ''):
1019 var indexes = _get_indexes_matching_script_name(_script_name)
1020 if(indexes == []):
1021 _lgr.error('Could not find script matching ' + _script_name)
1022 else:
1023 _test_the_scripts(indexes)
1024 else:
1025 _test_the_scripts([])
1026
1027 # alias
1028 func run_tests(run_rest=false):
1029 test_scripts(run_rest)
1030
1031
1032 # ------------------------------------------------------------------------------
1033 # Runs a single script passed in.
1034 # ------------------------------------------------------------------------------
1035 func test_script(script):
1036 _test_collector.set_test_class_prefix(_inner_class_prefix)
1037 _test_collector.clear()
1038 _test_collector.add_script(script)
1039 _test_the_scripts()
1040
1041
1042 # ------------------------------------------------------------------------------
1043 # Adds a script to be run when test_scripts called.
1044 # ------------------------------------------------------------------------------
1045 func add_script(script):
1046 if(!Engine.is_editor_hint()):
1047 _test_collector.set_test_class_prefix(_inner_class_prefix)
1048 _test_collector.add_script(script)
1049 _add_scripts_to_gui()
1050
1051
1052 # ------------------------------------------------------------------------------
1053 # Add all scripts in the specified directory that start with the prefix and end
1054 # with the suffix. Does not look in sub directories. Can be called multiple
1055 # times.
1056 # ------------------------------------------------------------------------------
1057 func add_directory(path, prefix=_file_prefix, suffix=_file_extension):
1058 # check for '' b/c the calls to addin the exported directories 1-6 will pass
1059 # '' if the field has not been populated. This will cause res:// to be
1060 # processed which will include all files if include_subdirectories is true.
1061 if(path == '' or path == null):
1062 return
1063
1064 var d = Directory.new()
1065 if(!d.dir_exists(path)):
1066 _lgr.error(str('The path [', path, '] does not exist.'))
1067 OS.exit_code = 1
1068 else:
1069 var files = _get_files(path, prefix, suffix)
1070 for i in range(files.size()):
1071 add_script(files[i])
1072
1073
1074 # ------------------------------------------------------------------------------
1075 # This will try to find a script in the list of scripts to test that contains
1076 # the specified script name. It does not have to be a full match. It will
1077 # select the first matching occurrence so that this script will run when run_tests
1078 # is called. Works the same as the select_this_one option of add_script.
1079 #
1080 # returns whether it found a match or not
1081 # ------------------------------------------------------------------------------
1082 func select_script(script_name):
1083 _script_name = script_name
1084
1085
1086 # ------------------------------------------------------------------------------
1087 # ------------------------------------------------------------------------------
1088 func export_tests(path=_export_path):
1089 if(path == null):
1090 _lgr.error('You must pass a path or set the export_path before calling export_tests')
1091 else:
1092 var result = _test_collector.export_tests(path)
1093 if(result):
1094 p(_test_collector.to_s())
1095 p("Exported to " + path)
1096
1097
1098 # ------------------------------------------------------------------------------
1099 # ------------------------------------------------------------------------------
1100 func import_tests(path=_export_path):
1101 if(!_utils.file_exists(path)):
1102 _lgr.error(str('Cannot import tests: the path [', path, '] does not exist.'))
1103 else:
1104 _test_collector.clear()
1105 var result = _test_collector.import_tests(path)
1106 if(result):
1107 p(_test_collector.to_s())
1108 p("Imported from " + path)
1109 _add_scripts_to_gui()
1110
1111
1112 # ------------------------------------------------------------------------------
1113 # ------------------------------------------------------------------------------
1114 func import_tests_if_none_found():
1115 if(!_cancel_import and _test_collector.scripts.size() == 0):
1116 import_tests()
1117
1118
1119 # ------------------------------------------------------------------------------
1120 # ------------------------------------------------------------------------------
1121 func export_if_tests_found():
1122 if(_test_collector.scripts.size() > 0):
1123 export_tests()
1124
1125 ################
1126 #
1127 # MISC
1128 #
1129 ################
1130
1131
1132 # ------------------------------------------------------------------------------
1133 # Maximize test runner window to fit the viewport.
1134 # ------------------------------------------------------------------------------
1135 func set_should_maximize(should):
1136 _should_maximize = should
1137
1138 # ------------------------------------------------------------------------------
1139 # ------------------------------------------------------------------------------
1140 func get_should_maximize():
1141 return _should_maximize
1142
1143 # ------------------------------------------------------------------------------
1144 # ------------------------------------------------------------------------------
1145 func maximize():
1146 _gui.maximize()
1147
1148 # ------------------------------------------------------------------------------
1149 # ------------------------------------------------------------------------------
1150 func disable_strict_datatype_checks(should):
1151 _disable_strict_datatype_checks = should
1152
1153 # ------------------------------------------------------------------------------
1154 # ------------------------------------------------------------------------------
1155 func is_strict_datatype_checks_disabled():
1156 return _disable_strict_datatype_checks
1157
1158 # ------------------------------------------------------------------------------
1159 # Pauses the test and waits for you to press a confirmation button. Useful when
1160 # you want to watch a test play out onscreen or inspect results.
1161 # ------------------------------------------------------------------------------
1162 func end_yielded_test():
1163 _lgr.deprecated('end_yielded_test is no longer necessary, you can remove it.')
1164
1165 # ------------------------------------------------------------------------------
1166 # Clears the text of the text box. This resets all counters.
1167 # ------------------------------------------------------------------------------
1168 func clear_text():
1169 _gui.clear_text()
1170 update()
1171
1172 # ------------------------------------------------------------------------------
1173 # Get the number of tests that were ran
1174 # ------------------------------------------------------------------------------
1175 func get_test_count():
1176 return _new_summary.get_totals().tests
1177
1178 # ------------------------------------------------------------------------------
1179 # Get the number of assertions that were made
1180 # ------------------------------------------------------------------------------
1181 func get_assert_count():
1182 var t = _new_summary.get_totals()
1183 return t.passing + t.failing
1184
1185 # ------------------------------------------------------------------------------
1186 # Get the number of assertions that passed
1187 # ------------------------------------------------------------------------------
1188 func get_pass_count():
1189 return _new_summary.get_totals().passing
1190
1191 # ------------------------------------------------------------------------------
1192 # Get the number of assertions that failed
1193 # ------------------------------------------------------------------------------
1194 func get_fail_count():
1195 return _new_summary.get_totals().failing
1196
1197 # ------------------------------------------------------------------------------
1198 # Get the number of tests flagged as pending
1199 # ------------------------------------------------------------------------------
1200 func get_pending_count():
1201 return _new_summary.get_totals().pending
1202
1203 # ------------------------------------------------------------------------------
1204 # Get the results of all tests ran as text. This string is the same as is
1205 # displayed in the text box, and similar to what is printed to the console.
1206 # ------------------------------------------------------------------------------
1207 func get_result_text():
1208 return _log_text
1209
1210 # ------------------------------------------------------------------------------
1211 # Set the log level. Use one of the various LOG_LEVEL_* constants.
1212 # ------------------------------------------------------------------------------
1213 func set_log_level(level):
1214 _log_level = max(level, 0)
1215
1216 # Level 0 settings
1217 _lgr.set_less_test_names(level == 0)
1218 # Explicitly always enabled
1219 _lgr.set_type_enabled(_lgr.types.normal, true)
1220 _lgr.set_type_enabled(_lgr.types.error, true)
1221 _lgr.set_type_enabled(_lgr.types.pending, true)
1222
1223 # Level 1 types
1224 _lgr.set_type_enabled(_lgr.types.warn, level > 0)
1225 _lgr.set_type_enabled(_lgr.types.deprecated, level > 0)
1226
1227 # Level 2 types
1228 _lgr.set_type_enabled(_lgr.types.passed, level > 1)
1229 _lgr.set_type_enabled(_lgr.types.info, level > 1)
1230 _lgr.set_type_enabled(_lgr.types.debug, level > 1)
1231
1232 if(!Engine.is_editor_hint()):
1233 _gui.set_log_level(level)
1234
1235 # ------------------------------------------------------------------------------
1236 # Get the current log level.
1237 # ------------------------------------------------------------------------------
1238 func get_log_level():
1239 return _log_level
1240
1241 # ------------------------------------------------------------------------------
1242 # Call this method to make the test pause before teardown so that you can inspect
1243 # anything that you have rendered to the screen.
1244 # ------------------------------------------------------------------------------
1245 func pause_before_teardown():
1246 _pause_before_teardown = true;
1247
1248 # ------------------------------------------------------------------------------
1249 # For batch processing purposes, you may want to ignore any calls to
1250 # pause_before_teardown that you forgot to remove.
1251 # ------------------------------------------------------------------------------
1252 func set_ignore_pause_before_teardown(should_ignore):
1253 _ignore_pause_before_teardown = should_ignore
1254 _gui.set_ignore_pause(should_ignore)
1255
1256 func get_ignore_pause_before_teardown():
1257 return _ignore_pause_before_teardown
1258
1259 # ------------------------------------------------------------------------------
1260 # Set to true so that painting of the screen will occur between tests. Allows you
1261 # to see the output as tests occur. Especially useful with long running tests that
1262 # make it appear as though it has humg.
1263 #
1264 # NOTE: not compatible with 1.0 so this is disabled by default. This will
1265 # change in future releases.
1266 # ------------------------------------------------------------------------------
1267 func set_yield_between_tests(should):
1268 _yield_between.should = should
1269
1270 func get_yield_between_tests():
1271 return _yield_between.should
1272
1273 # ------------------------------------------------------------------------------
1274 # Call _process or _fixed_process, if they exist, on obj and all it's children
1275 # and their children and so and so forth. Delta will be passed through to all
1276 # the _process or _fixed_process methods.
1277 # ------------------------------------------------------------------------------
1278 func simulate(obj, times, delta):
1279 for _i in range(times):
1280 if(obj.has_method("_process")):
1281 obj._process(delta)
1282 if(obj.has_method("_physics_process")):
1283 obj._physics_process(delta)
1284
1285 for kid in obj.get_children():
1286 simulate(kid, 1, delta)
1287
1288 # ------------------------------------------------------------------------------
1289 # Starts an internal timer with a timeout of the passed in time. A 'timeout'
1290 # signal will be sent when the timer ends. Returns itself so that it can be
1291 # used in a call to yield...cutting down on lines of code.
1292 #
1293 # Example, yield to the Gut object for 10 seconds:
1294 # yield(gut.set_yield_time(10), 'timeout')
1295 # ------------------------------------------------------------------------------
1296 func set_yield_time(time, text=''):
1297 _yield_timer.set_wait_time(time)
1298 _yield_timer.start()
1299 var msg = '-- Yielding (' + str(time) + 's)'
1300 if(text == ''):
1301 msg += ' --'
1302 else:
1303 msg += ': ' + text + ' --'
1304 _lgr.log(msg, _lgr.fmts.yellow)
1305 _was_yield_method_called = true
1306 return self
1307
1308 # ------------------------------------------------------------------------------
1309 # ------------------------------------------------------------------------------
1310 func set_yield_signal_or_time(obj, signal_name, max_wait, text=''):
1311 obj.connect(signal_name, self, '_yielding_callback', [true])
1312 _yielding_to.obj = obj
1313 _yielding_to.signal_name = signal_name
1314
1315 _yield_timer.set_wait_time(max_wait)
1316 _yield_timer.start()
1317 _was_yield_method_called = true
1318 _lgr.log(str('-- Yielding to signal "', signal_name, '" or for ', max_wait, ' seconds -- ', text), _lgr.fmts.yellow)
1319 return self
1320
1321 # ------------------------------------------------------------------------------
1322 # get the specific unit test that should be run
1323 # ------------------------------------------------------------------------------
1324 func get_unit_test_name():
1325 return _unit_test_name
1326
1327 # ------------------------------------------------------------------------------
1328 # set the specific unit test that should be run.
1329 # ------------------------------------------------------------------------------
1330 func set_unit_test_name(test_name):
1331 _unit_test_name = test_name
1332
1333 # ------------------------------------------------------------------------------
1334 # Creates an empty file at the specified path
1335 # ------------------------------------------------------------------------------
1336 func file_touch(path):
1337 var f = File.new()
1338 f.open(path, f.WRITE)
1339 f.close()
1340
1341 # ------------------------------------------------------------------------------
1342 # deletes the file at the specified path
1343 # ------------------------------------------------------------------------------
1344 func file_delete(path):
1345 var d = Directory.new()
1346 var result = d.open(path.get_base_dir())
1347 if(result == OK):
1348 d.remove(path)
1349
1350 # ------------------------------------------------------------------------------
1351 # Checks to see if the passed in file has any data in it.
1352 # ------------------------------------------------------------------------------
1353 func is_file_empty(path):
1354 var f = File.new()
1355 f.open(path, f.READ)
1356 var empty = f.get_len() == 0
1357 f.close()
1358 return empty
1359
1360 # ------------------------------------------------------------------------------
1361 # ------------------------------------------------------------------------------
1362 func get_file_as_text(path):
1363 return _utils.get_file_as_text(path)
1364
1365 # ------------------------------------------------------------------------------
1366 # deletes all files in a given directory
1367 # ------------------------------------------------------------------------------
1368 func directory_delete_files(path):
1369 var d = Directory.new()
1370 var result = d.open(path)
1371
1372 # SHORTCIRCUIT
1373 if(result != OK):
1374 return
1375
1376 # Traversing a directory is kinda odd. You have to start the process of listing
1377 # the contents of a directory with list_dir_begin then use get_next until it
1378 # returns an empty string. Then I guess you should end it.
1379 d.list_dir_begin()
1380 var thing = d.get_next() # could be a dir or a file or something else maybe?
1381 var full_path = ''
1382 while(thing != ''):
1383 full_path = path + "/" + thing
1384 #file_exists returns fasle for directories
1385 if(d.file_exists(full_path)):
1386 d.remove(full_path)
1387 thing = d.get_next()
1388 d.list_dir_end()
1389
1390 # ------------------------------------------------------------------------------
1391 # Returns the instantiated script object that is currently being run.
1392 # ------------------------------------------------------------------------------
1393 func get_current_script_object():
1394 var to_return = null
1395 if(_test_script_objects.size() > 0):
1396 to_return = _test_script_objects[-1]
1397 return to_return
1398
1399 # ------------------------------------------------------------------------------
1400 # ------------------------------------------------------------------------------
1401 func get_current_test_object():
1402 return _current_test
1403
1404 # ------------------------------------------------------------------------------
1405 # ------------------------------------------------------------------------------
1406 func get_stubber():
1407 return _stubber
1408
1409 # ------------------------------------------------------------------------------
1410 # ------------------------------------------------------------------------------
1411 func get_doubler():
1412 return _doubler
1413
1414 # ------------------------------------------------------------------------------
1415 # ------------------------------------------------------------------------------
1416 func get_spy():
1417 return _spy
1418
1419 # ------------------------------------------------------------------------------
1420 # ------------------------------------------------------------------------------
1421 func get_temp_directory():
1422 return _temp_directory
1423
1424 # ------------------------------------------------------------------------------
1425 # ------------------------------------------------------------------------------
1426 func set_temp_directory(temp_directory):
1427 _temp_directory = temp_directory
1428
1429 # ------------------------------------------------------------------------------
1430 # ------------------------------------------------------------------------------
1431 func get_inner_class_name():
1432 return _inner_class_name
1433
1434 # ------------------------------------------------------------------------------
1435 # ------------------------------------------------------------------------------
1436 func set_inner_class_name(inner_class_name):
1437 _inner_class_name = inner_class_name
1438
1439 # ------------------------------------------------------------------------------
1440 # ------------------------------------------------------------------------------
1441 func get_summary():
1442 return _new_summary
1443
1444 # ------------------------------------------------------------------------------
1445 # ------------------------------------------------------------------------------
1446 func get_double_strategy():
1447 return _double_strategy
1448
1449 # ------------------------------------------------------------------------------
1450 # ------------------------------------------------------------------------------
1451 func set_double_strategy(double_strategy):
1452 _double_strategy = double_strategy
1453 _doubler.set_strategy(double_strategy)
1454
1455 # ------------------------------------------------------------------------------
1456 # ------------------------------------------------------------------------------
1457 func get_include_subdirectories():
1458 return _include_subdirectories
1459
1460 # ------------------------------------------------------------------------------
1461 # ------------------------------------------------------------------------------
1462 func get_logger():
1463 return _lgr
1464
1465 # ------------------------------------------------------------------------------
1466 # ------------------------------------------------------------------------------
1467 func set_logger(logger):
1468 _lgr = logger
1469 _lgr.set_gut(self)
1470
1471 # ------------------------------------------------------------------------------
1472 # ------------------------------------------------------------------------------
1473 func set_include_subdirectories(include_subdirectories):
1474 _include_subdirectories = include_subdirectories
1475
1476 # ------------------------------------------------------------------------------
1477 # ------------------------------------------------------------------------------
1478 func get_test_collector():
1479 return _test_collector
1480
1481 # ------------------------------------------------------------------------------
1482 # ------------------------------------------------------------------------------
1483 func get_export_path():
1484 return _export_path
1485
1486 # ------------------------------------------------------------------------------
1487 # ------------------------------------------------------------------------------
1488 func set_export_path(export_path):
1489 _export_path = export_path
1490
1491 # ------------------------------------------------------------------------------
1492 # ------------------------------------------------------------------------------
1493 func get_version():
1494 return _utils.version
1495
1496 # ------------------------------------------------------------------------------
1497 # ------------------------------------------------------------------------------
1498 func get_pre_run_script():
1499 return _pre_run_script
1500
1501 # ------------------------------------------------------------------------------
1502 # ------------------------------------------------------------------------------
1503 func set_pre_run_script(pre_run_script):
1504 _pre_run_script = pre_run_script
1505
1506 # ------------------------------------------------------------------------------
1507 # ------------------------------------------------------------------------------
1508 func get_post_run_script():
1509 return _post_run_script
1510
1511 # ------------------------------------------------------------------------------
1512 # ------------------------------------------------------------------------------
1513 func set_post_run_script(post_run_script):
1514 _post_run_script = post_run_script
1515
1516 # ------------------------------------------------------------------------------
1517 # ------------------------------------------------------------------------------
1518 func get_pre_run_script_instance():
1519 return _pre_run_script_instance
1520
1521 # ------------------------------------------------------------------------------
1522 # ------------------------------------------------------------------------------
1523 func get_post_run_script_instance():
1524 return _post_run_script_instance
1525
1526 # ------------------------------------------------------------------------------
1527 # ------------------------------------------------------------------------------
1528 func get_color_output():
1529 return _color_output
1530
1531 # ------------------------------------------------------------------------------
1532 # ------------------------------------------------------------------------------
1533 func set_color_output(color_output):
1534 _color_output = color_output
1535 _lgr.disable_formatting(!color_output)
1536
1537 # ------------------------------------------------------------------------------
1538 # ------------------------------------------------------------------------------
1539 func get_parameter_handler():
1540 return _parameter_handler
1541
1542 # ------------------------------------------------------------------------------
1543 # ------------------------------------------------------------------------------
1544 func set_parameter_handler(parameter_handler):
1545 _parameter_handler = parameter_handler
1546 _parameter_handler.set_logger(_lgr)
1547
1548 # ------------------------------------------------------------------------------
1549 # ------------------------------------------------------------------------------
1550 func get_gui():
1551 return _gui
1552
1553 # ------------------------------------------------------------------------------
1554 # ------------------------------------------------------------------------------
1555 func get_orphan_counter():
1556 return _orphan_counter
1557
1558 # ------------------------------------------------------------------------------
1559 # ------------------------------------------------------------------------------
1560 func show_orphans(should):
1561 _lgr.set_type_enabled(_lgr.types.orphan, should)
1562
1563 # ------------------------------------------------------------------------------
1564 # ------------------------------------------------------------------------------
1565 func get_autofree():
1566 return _autofree