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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
# ##############################################################################
#(G)odot (U)nit (T)est class
#
# ##############################################################################
# The MIT License (MIT)
# =====================
#
# Copyright (c) 2020 Tom "Butch" Wesley
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ##############################################################################
# This is the control that is added via the editor. It exposes GUT settings
# through the editor and delays the creation of the GUT instance until
# Engine.get_main_loop() works as expected.
# ##############################################################################
tool
extends Control
# ------------------------------------------------------------------------------
# GUT Settings
# ------------------------------------------------------------------------------
export(String, 'AnonymousPro', 'CourierPrime', 'LobsterTwo', 'Default') var _font_name = 'AnonymousPro'
export(int) var _font_size = 20
export(Color) var _font_color = Color(.8, .8, .8, 1)
export(Color) var _background_color = Color(.15, .15, .15, 1)
# Enable/Disable coloring of output.
export(bool) var _color_output = true
# The full/partial name of a script to select upon startup
export(String) var _select_script = ''
# The full/partial name of a test. All tests that contain the string will be
# run
export(String) var _tests_like = ''
# The full/partial name of an Inner Class to be run. All Inner Classes that
# contain the string will be run.
export(String) var _inner_class_name = ''
# Start running tests when the scene finishes loading
export var _run_on_load = false
# Maximize the GUT control on startup
export var _should_maximize = false
# Print output to the consol as well
export var _should_print_to_console = true
# Display orphan counts at the end of tests/scripts.
export var _show_orphans = true
# The log level.
export(int, 'Fail/Errors', 'Errors/Warnings/Test Names', 'Everything') var _log_level = 1
# When enabled GUT will yield between tests to give the GUI time to paint.
# Disabling this can make the program appear to hang and can have some
# unwanted consequences with the timing of freeing objects
export var _yield_between_tests = true
# When GUT compares values it first checks the types to prevent runtime errors.
# This behavior can be disabled if desired. This flag was added early in
# development to prevent any breaking changes and will likely be removed in
# the future.
export var _disable_strict_datatype_checks = false
# The prefix used to find test methods.
export var _test_prefix = 'test_'
# The prefix used to find test scripts.
export var _file_prefix = 'test_'
# The file extension for test scripts (I don't think you can change this and
# everythign work).
export var _file_extension = '.gd'
# The prefix used to find Inner Test Classes.
export var _inner_class_prefix = 'Test'
# The directory GUT will use to write any temporary files. This isn't used
# much anymore since there was a change to the double creation implementation.
# This will be removed in a later release.
export(String) var _temp_directory = 'user://gut_temp_directory'
# The path and filename for exported test information.
export(String) var _export_path = ''
# When enabled, any directory added will also include its subdirectories when
# GUT looks for test scripts.
export var _include_subdirectories = false
# Allow user to add test directories via editor. This is done with strings
# instead of an array because the interface for editing arrays is really
# cumbersome and complicates testing because arrays set through the editor
# apply to ALL instances. This also allows the user to use the built in
# dialog to pick a directory.
export(String, DIR) var _directory1 = ''
export(String, DIR) var _directory2 = ''
export(String, DIR) var _directory3 = ''
export(String, DIR) var _directory4 = ''
export(String, DIR) var _directory5 = ''
export(String, DIR) var _directory6 = ''
# Must match the types in _utils for double strategy
export(int, 'FULL', 'PARTIAL') var _double_strategy = 1
# Path and filename to the script to run before all tests are run.
export(String, FILE) var _pre_run_script = ''
# Path and filename to the script to run after all tests are run.
export(String, FILE) var _post_run_script = ''
# Path to the file that gut will export results to in the junit xml format
export(String, FILE) var _junit_xml_file = ''
# Flag to include a timestamp in the filename of _junit_xml_file
export(bool) var _junit_xml_timestamp = false
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Signals
# ------------------------------------------------------------------------------
# Emitted when all the tests have finished running.
signal tests_finished
# Emitted when GUT is ready to be interacted with, and before any tests are run.
signal gut_ready
# ------------------------------------------------------------------------------
# Private stuff.
# ------------------------------------------------------------------------------
var _gut = null
var _lgr = null
var _cancel_import = false
var _placeholder = null
func _init():
# This min size has to be what the min size of the GutScene's min size is
# but it has to be set here and not inferred i think.
rect_min_size = Vector2(740, 250)
func _ready():
# Must call this deferred so that there is enough time for
# Engine.get_main_loop() is populated and the psuedo singleton utils.gd
# can be setup correctly.
if(Engine.editor_hint):
_placeholder = load('res://addons/gut/GutScene.tscn').instance()
call_deferred('add_child', _placeholder)
_placeholder.rect_size = rect_size
else:
call_deferred('_setup_gut')
connect('resized', self, '_on_resized')
func _on_resized():
if(_placeholder != null):
_placeholder.rect_size = rect_size
# Templates can be missing if tests are exported and the export config for the
# project does not include '*.txt' files. This check and related flags make
# sure GUT does not blow up and that the error is not lost in all the import
# output that is generated as well as ensuring that no tests are run.
#
# Assumption: This is only a concern when running from the scene since you
# cannot run GUT from the command line in an exported game.
func _check_for_templates():
var f = File.new()
if(!f.file_exists('res://addons/gut/double_templates/function_template.txt')):
_lgr.error('Templates are missing. Make sure you are exporting "*.txt" or "addons/gut/double_templates/*.txt".')
_run_on_load = false
_cancel_import = true
return false
return true
func _setup_gut():
var _utils = load('res://addons/gut/utils.gd').get_instance()
_lgr = _utils.get_logger()
_gut = load('res://addons/gut/gut.gd').new()
_gut.connect('tests_finished', self, '_on_tests_finished')
if(!_check_for_templates()):
return
_gut._select_script = _select_script
_gut._tests_like = _tests_like
_gut._inner_class_name = _inner_class_name
_gut._test_prefix = _test_prefix
_gut._file_prefix = _file_prefix
_gut._file_extension = _file_extension
_gut._inner_class_prefix = _inner_class_prefix
_gut._temp_directory = _temp_directory
_gut.set_should_maximize(_should_maximize)
_gut.set_yield_between_tests(_yield_between_tests)
_gut.disable_strict_datatype_checks(_disable_strict_datatype_checks)
_gut.set_export_path(_export_path)
_gut.set_include_subdirectories(_include_subdirectories)
_gut.set_double_strategy(_double_strategy)
_gut.set_pre_run_script(_pre_run_script)
_gut.set_post_run_script(_post_run_script)
_gut.set_color_output(_color_output)
_gut.show_orphans(_show_orphans)
_gut.set_junit_xml_file(_junit_xml_file)
_gut.set_junit_xml_timestamp(_junit_xml_timestamp)
get_parent().add_child(_gut)
if(!_utils.is_version_ok()):
return
_gut.set_log_level(_log_level)
_gut.add_directory(_directory1)
_gut.add_directory(_directory2)
_gut.add_directory(_directory3)
_gut.add_directory(_directory4)
_gut.add_directory(_directory5)
_gut.add_directory(_directory6)
_gut.get_logger().disable_printer('console', !_should_print_to_console)
# When file logging enabled then the log will contain terminal escape
# strings. So when running the scene this is disabled. Also if enabled
# this may cause duplicate entries into the logs.
_gut.get_logger().disable_printer('terminal', true)
_gut.get_gui().set_font_size(_font_size)
_gut.get_gui().set_font(_font_name)
_gut.get_gui().set_default_font_color(_font_color)
_gut.get_gui().set_background_color(_background_color)
_gut.get_gui().rect_size = rect_size
emit_signal('gut_ready')
if(_run_on_load):
# Run the test scripts. If one has been selected then only run that one
# otherwise all tests will be run.
var run_rest_of_scripts = _select_script == null
_gut.test_scripts(run_rest_of_scripts)
func _is_ready_to_go(action):
if(_gut == null):
push_error(str('GUT is not ready for ', action, ' yet. Perform actions on GUT in/after the gut_ready signal.'))
return _gut != null
func _on_tests_finished():
emit_signal('tests_finished')
func get_gut():
return _gut
func export_if_tests_found():
if(_is_ready_to_go('export_if_tests_found')):
_gut.export_if_tests_found()
func import_tests_if_none_found():
if(_is_ready_to_go('import_tests_if_none_found') and !_cancel_import):
_gut.import_tests_if_none_found()
|