Debian Codegraph @

godot/addons/gut/plugin_control.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 # This is the control that is added via the editor. It exposes GUT settings
30 # through the editor and delays the creation of the GUT instance until
31 # Engine.get_main_loop() works as expected.
32 # ##############################################################################
33 tool
34 extends Control
35
36 # ------------------------------------------------------------------------------
37 # GUT Settings
38 # ------------------------------------------------------------------------------
39 export(String, 'AnonymousPro', 'CourierPrime', 'LobsterTwo', 'Default') var _font_name = 'AnonymousPro'
40 export(int) var _font_size = 20
41 export(Color) var _font_color = Color(.8, .8, .8, 1)
42 export(Color) var _background_color = Color(.15, .15, .15, 1)
43 # Enable/Disable coloring of output.
44 export(bool) var _color_output = true
45 # The full/partial name of a script to select upon startup
46 export(String) var _select_script = ''
47 # The full/partial name of a test. All tests that contain the string will be
48 # run
49 export(String) var _tests_like = ''
50 # The full/partial name of an Inner Class to be run. All Inner Classes that
51 # contain the string will be run.
52 export(String) var _inner_class_name = ''
53 # Start running tests when the scene finishes loading
54 export var _run_on_load = false
55 # Maximize the GUT control on startup
56 export var _should_maximize = false
57 # Print output to the consol as well
58 export var _should_print_to_console = true
59 # Display orphan counts at the end of tests/scripts.
60 export var _show_orphans = true
61 # The log level.
62 export(int, 'Fail/Errors', 'Errors/Warnings/Test Names', 'Everything') var _log_level = 1
63 # When enabled GUT will yield between tests to give the GUI time to paint.
64 # Disabling this can make the program appear to hang and can have some
65 # unwanted consequences with the timing of freeing objects
66 export var _yield_between_tests = true
67 # When GUT compares values it first checks the types to prevent runtime errors.
68 # This behavior can be disabled if desired. This flag was added early in
69 # development to prevent any breaking changes and will likely be removed in
70 # the future.
71 export var _disable_strict_datatype_checks = false
72 # The prefix used to find test methods.
73 export var _test_prefix = 'test_'
74 # The prefix used to find test scripts.
75 export var _file_prefix = 'test_'
76 # The file extension for test scripts (I don't think you can change this and
77 # everythign work).
78 export var _file_extension = '.gd'
79 # The prefix used to find Inner Test Classes.
80 export var _inner_class_prefix = 'Test'
81 # The directory GUT will use to write any temporary files. This isn't used
82 # much anymore since there was a change to the double creation implementation.
83 # This will be removed in a later release.
84 export(String) var _temp_directory = 'user://gut_temp_directory'
85 # The path and filename for exported test information.
86 export(String) var _export_path = ''
87 # When enabled, any directory added will also include its subdirectories when
88 # GUT looks for test scripts.
89 export var _include_subdirectories = false
90 # Allow user to add test directories via editor. This is done with strings
91 # instead of an array because the interface for editing arrays is really
92 # cumbersome and complicates testing because arrays set through the editor
93 # apply to ALL instances. This also allows the user to use the built in
94 # dialog to pick a directory.
95 export(String, DIR) var _directory1 = ''
96 export(String, DIR) var _directory2 = ''
97 export(String, DIR) var _directory3 = ''
98 export(String, DIR) var _directory4 = ''
99 export(String, DIR) var _directory5 = ''
100 export(String, DIR) var _directory6 = ''
101 # Must match the types in _utils for double strategy
102 export(int, 'FULL', 'PARTIAL') var _double_strategy = 1
103 # Path and filename to the script to run before all tests are run.
104 export(String, FILE) var _pre_run_script = ''
105 # Path and filename to the script to run after all tests are run.
106 export(String, FILE) var _post_run_script = ''
107 # ------------------------------------------------------------------------------
108
109
110 # ------------------------------------------------------------------------------
111 # Signals
112 # ------------------------------------------------------------------------------
113 # Emitted when all the tests have finished running.
114 signal tests_finished
115 # Emitted when GUT is ready to be interacted with, and before any tests are run.
116 signal gut_ready
117
118
119 # ------------------------------------------------------------------------------
120 # Private stuff.
121 # ------------------------------------------------------------------------------
122 var _gut = null
123 var _lgr = null
124 var _cancel_import = false
125 var _placeholder = null
126
127 func _init():
128 # This min size has to be what the min size of the GutScene's min size is
129 # but it has to be set here and not inferred i think.
130 rect_min_size = Vector2(740, 250)
131
132 func _ready():
133 # Must call this deferred so that there is enough time for
134 # Engine.get_main_loop() is populated and the psuedo singleton utils.gd
135 # can be setup correctly.
136 if(Engine.editor_hint):
137 _placeholder = load('res://addons/gut/GutScene.tscn').instance()
138 call_deferred('add_child', _placeholder)
139 _placeholder.rect_size = rect_size
140 else:
141 call_deferred('_setup_gut')
142
143 connect('resized', self, '_on_resized')
144
145 func _on_resized():
146 if(_placeholder != null):
147 _placeholder.rect_size = rect_size
148
149
150 # Templates can be missing if tests are exported and the export config for the
151 # project does not include '*.txt' files. This check and related flags make
152 # sure GUT does not blow up and that the error is not lost in all the import
153 # output that is generated as well as ensuring that no tests are run.
154 #
155 # Assumption: This is only a concern when running from the scene since you
156 # cannot run GUT from the command line in an exported game.
157 func _check_for_templates():
158 var f = File.new()
159 if(!f.file_exists('res://addons/gut/double_templates/function_template.txt')):
160 _lgr.error('Templates are missing. Make sure you are exporting "*.txt" or "addons/gut/double_templates/*.txt".')
161 _run_on_load = false
162 _cancel_import = true
163 return false
164 return true
165
166 func _setup_gut():
167 var _utils = load('res://addons/gut/utils.gd').get_instance()
168
169 _lgr = _utils.get_logger()
170 _gut = load('res://addons/gut/gut.gd').new()
171 _gut.connect('tests_finished', self, '_on_tests_finished')
172
173 if(!_check_for_templates()):
174 return
175
176 _gut._select_script = _select_script
177 _gut._tests_like = _tests_like
178 _gut._inner_class_name = _inner_class_name
179
180 _gut._test_prefix = _test_prefix
181 _gut._file_prefix = _file_prefix
182 _gut._file_extension = _file_extension
183 _gut._inner_class_prefix = _inner_class_prefix
184 _gut._temp_directory = _temp_directory
185
186 _gut.set_should_maximize(_should_maximize)
187 _gut.set_yield_between_tests(_yield_between_tests)
188 _gut.disable_strict_datatype_checks(_disable_strict_datatype_checks)
189 _gut.set_export_path(_export_path)
190 _gut.set_include_subdirectories(_include_subdirectories)
191 _gut.set_double_strategy(_double_strategy)
192 _gut.set_pre_run_script(_pre_run_script)
193 _gut.set_post_run_script(_post_run_script)
194 _gut.set_color_output(_color_output)
195 _gut.show_orphans(_show_orphans)
196
197 get_parent().add_child(_gut)
198
199 if(!_utils.is_version_ok()):
200 return
201
202 _gut.set_log_level(_log_level)
203
204 _gut.add_directory(_directory1)
205 _gut.add_directory(_directory2)
206 _gut.add_directory(_directory3)
207 _gut.add_directory(_directory4)
208 _gut.add_directory(_directory5)
209 _gut.add_directory(_directory6)
210
211 _gut.get_logger().disable_printer('console', !_should_print_to_console)
212 # When file logging enabled then the log will contain terminal escape
213 # strings. So when running the scene this is disabled. Also if enabled
214 # this may cause duplicate entries into the logs.
215 _gut.get_logger().disable_printer('terminal', true)
216
217 _gut.get_gui().set_font_size(_font_size)
218 _gut.get_gui().set_font(_font_name)
219 _gut.get_gui().set_default_font_color(_font_color)
220 _gut.get_gui().set_background_color(_background_color)
221 _gut.get_gui().rect_size = rect_size
222 emit_signal('gut_ready')
223
224 if(_run_on_load):
225 # Run the test scripts. If one has been selected then only run that one
226 # otherwise all tests will be run.
227 var run_rest_of_scripts = _select_script == null
228 _gut.test_scripts(run_rest_of_scripts)
229
230 func _is_ready_to_go(action):
231 if(_gut == null):
232 push_error(str('GUT is not ready for ', action, ' yet. Perform actions on GUT in/after the gut_ready signal.'))
233 return _gut != null
234
235 func _on_tests_finished():
236 emit_signal('tests_finished')
237
238 func get_gut():
239 return _gut
240
241 func export_if_tests_found():
242 if(_is_ready_to_go('export_if_tests_found')):
243 _gut.export_if_tests_found()
244
245 func import_tests_if_none_found():
246 if(_is_ready_to_go('import_tests_if_none_found') and !_cancel_import):
247 _gut.import_tests_if_none_found()