| 1 |
# ------------------------------------------------------------------------------ |
| 2 |
# Used to keep track of info about each test ran. |
| 3 |
# ------------------------------------------------------------------------------ |
| 4 |
class Test: |
| 5 |
# indicator if it passed or not. defaults to true since it takes only |
| 6 |
# one failure to make it not pass. _fail in gut will set this. |
| 7 |
var passed = true |
| 8 |
# the name of the function |
| 9 |
var name = "" |
| 10 |
# flag to know if the name has been printed yet. |
| 11 |
var has_printed_name = false |
| 12 |
# the number of arguments the method has |
| 13 |
var arg_count = 0 |
| 14 |
# The number of asserts in the test |
| 15 |
var assert_count = 0 |
| 16 |
# if the test has been marked pending at anypont during |
| 17 |
# execution. |
| 18 |
var pending = false |
| 19 |
|
| 20 |
|
| 21 |
# ------------------------------------------------------------------------------ |
| 22 |
# This holds all the meta information for a test script. It contains the |
| 23 |
# name of the inner class and an array of Test "structs". |
| 24 |
# |
| 25 |
# This class also facilitates all the exporting and importing of tests. |
| 26 |
# ------------------------------------------------------------------------------ |
| 27 |
class TestScript: |
| 28 |
var inner_class_name = null |
| 29 |
var tests = [] |
| 30 |
var path = null |
| 31 |
var _utils = null |
| 32 |
var _lgr = null |
| 33 |
|
| 34 |
func _init(utils=null, logger=null): |
| 35 |
_utils = utils |
| 36 |
_lgr = logger |
| 37 |
|
| 38 |
func to_s(): |
| 39 |
var to_return = path |
| 40 |
if(inner_class_name != null): |
| 41 |
to_return += str('.', inner_class_name) |
| 42 |
to_return += "\n" |
| 43 |
for i in range(tests.size()): |
| 44 |
to_return += str(' ', tests[i].name, "\n") |
| 45 |
return to_return |
| 46 |
|
| 47 |
func get_new(): |
| 48 |
return load_script().new() |
| 49 |
|
| 50 |
func load_script(): |
| 51 |
#print('loading: ', get_full_name()) |
| 52 |
var to_return = load(path) |
| 53 |
if(inner_class_name != null): |
| 54 |
# If we wanted to do inner classes in inner classses |
| 55 |
# then this would have to become some kind of loop or recursive |
| 56 |
# call to go all the way down the chain or this class would |
| 57 |
# have to change to hold onto the loaded class instead of |
| 58 |
# just path information. |
| 59 |
to_return = to_return.get(inner_class_name) |
| 60 |
return to_return |
| 61 |
|
| 62 |
func get_filename_and_inner(): |
| 63 |
var to_return = get_filename() |
| 64 |
if(inner_class_name != null): |
| 65 |
to_return += '.' + inner_class_name |
| 66 |
return to_return |
| 67 |
|
| 68 |
func get_full_name(): |
| 69 |
var to_return = path |
| 70 |
if(inner_class_name != null): |
| 71 |
to_return += '.' + inner_class_name |
| 72 |
return to_return |
| 73 |
|
| 74 |
func get_filename(): |
| 75 |
return path.get_file() |
| 76 |
|
| 77 |
func has_inner_class(): |
| 78 |
return inner_class_name != null |
| 79 |
|
| 80 |
# Note: although this no longer needs to export the inner_class names since |
| 81 |
# they are pulled from metadata now, it is easier to leave that in |
| 82 |
# so we don't have to cut the export down to unique script names. |
| 83 |
func export_to(config_file, section): |
| 84 |
config_file.set_value(section, 'path', path) |
| 85 |
config_file.set_value(section, 'inner_class', inner_class_name) |
| 86 |
var names = [] |
| 87 |
for i in range(tests.size()): |
| 88 |
names.append(tests[i].name) |
| 89 |
config_file.set_value(section, 'tests', names) |
| 90 |
|
| 91 |
func _remap_path(source_path): |
| 92 |
var to_return = source_path |
| 93 |
if(!_utils.file_exists(source_path)): |
| 94 |
_lgr.debug('Checking for remap for: ' + source_path) |
| 95 |
var remap_path = source_path.get_basename() + '.gd.remap' |
| 96 |
if(_utils.file_exists(remap_path)): |
| 97 |
var cf = ConfigFile.new() |
| 98 |
cf.load(remap_path) |
| 99 |
to_return = cf.get_value('remap', 'path') |
| 100 |
else: |
| 101 |
_lgr.warn('Could not find remap file ' + remap_path) |
| 102 |
return to_return |
| 103 |
|
| 104 |
func import_from(config_file, section): |
| 105 |
path = config_file.get_value(section, 'path') |
| 106 |
path = _remap_path(path) |
| 107 |
# Null is an acceptable value, but you can't pass null as a default to |
| 108 |
# get_value since it thinks you didn't send a default...then it spits |
| 109 |
# out red text. This works around that. |
| 110 |
var inner_name = config_file.get_value(section, 'inner_class', 'Placeholder') |
| 111 |
if(inner_name != 'Placeholder'): |
| 112 |
inner_class_name = inner_name |
| 113 |
else: # just being explicit |
| 114 |
inner_class_name = null |
| 115 |
|
| 116 |
func get_test_named(name): |
| 117 |
return _utils.search_array(tests, 'name', name) |
| 118 |
|
| 119 |
# ------------------------------------------------------------------------------ |
| 120 |
# start test_collector, I don't think I like the name. |
| 121 |
# ------------------------------------------------------------------------------ |
| 122 |
var scripts = [] |
| 123 |
var _test_prefix = 'test_' |
| 124 |
var _test_class_prefix = 'Test' |
| 125 |
|
| 126 |
var _utils = load('res://addons/gut/utils.gd').get_instance() |
| 127 |
var _lgr = _utils.get_logger() |
| 128 |
|
| 129 |
func _does_inherit_from_test(thing): |
| 130 |
var base_script = thing.get_base_script() |
| 131 |
var to_return = false |
| 132 |
if(base_script != null): |
| 133 |
var base_path = base_script.get_path() |
| 134 |
if(base_path == 'res://addons/gut/test.gd'): |
| 135 |
to_return = true |
| 136 |
else: |
| 137 |
to_return = _does_inherit_from_test(base_script) |
| 138 |
return to_return |
| 139 |
|
| 140 |
func _populate_tests(test_script): |
| 141 |
var methods = test_script.load_script().get_script_method_list() |
| 142 |
for i in range(methods.size()): |
| 143 |
var name = methods[i]['name'] |
| 144 |
if(name.begins_with(_test_prefix)): |
| 145 |
var t = Test.new() |
| 146 |
t.name = name |
| 147 |
t.arg_count = methods[i]['args'].size() |
| 148 |
test_script.tests.append(t) |
| 149 |
|
| 150 |
func _get_inner_test_class_names(loaded): |
| 151 |
var inner_classes = [] |
| 152 |
var const_map = loaded.get_script_constant_map() |
| 153 |
for key in const_map: |
| 154 |
var thing = const_map[key] |
| 155 |
if(typeof(thing) == TYPE_OBJECT): |
| 156 |
if(key.begins_with(_test_class_prefix)): |
| 157 |
if(_does_inherit_from_test(thing)): |
| 158 |
inner_classes.append(key) |
| 159 |
else: |
| 160 |
_lgr.warn(str('Ignoring Inner Class ', key, |
| 161 |
' because it does not extend res://addons/gut/test.gd')) |
| 162 |
|
| 163 |
# This could go deeper and find inner classes within inner classes |
| 164 |
# but requires more experimentation. Right now I'm keeping it at |
| 165 |
# one level since that is what the previous version did and there |
| 166 |
# has been no demand for deeper nesting. |
| 167 |
# _populate_inner_test_classes(thing) |
| 168 |
return inner_classes |
| 169 |
|
| 170 |
func _parse_script(test_script): |
| 171 |
var inner_classes = [] |
| 172 |
var scripts_found = [] |
| 173 |
|
| 174 |
var loaded = load(test_script.path) |
| 175 |
if(_does_inherit_from_test(loaded)): |
| 176 |
_populate_tests(test_script) |
| 177 |
scripts_found.append(test_script.path) |
| 178 |
inner_classes = _get_inner_test_class_names(loaded) |
| 179 |
|
| 180 |
for i in range(inner_classes.size()): |
| 181 |
var loaded_inner = loaded.get(inner_classes[i]) |
| 182 |
if(_does_inherit_from_test(loaded_inner)): |
| 183 |
var ts = TestScript.new(_utils, _lgr) |
| 184 |
ts.path = test_script.path |
| 185 |
ts.inner_class_name = inner_classes[i] |
| 186 |
_populate_tests(ts) |
| 187 |
scripts.append(ts) |
| 188 |
scripts_found.append(test_script.path + '[' + inner_classes[i] +']') |
| 189 |
|
| 190 |
return scripts_found |
| 191 |
|
| 192 |
# ----------------- |
| 193 |
# Public |
| 194 |
# ----------------- |
| 195 |
func add_script(path): |
| 196 |
# SHORTCIRCUIT |
| 197 |
if(has_script(path)): |
| 198 |
return [] |
| 199 |
|
| 200 |
var f = File.new() |
| 201 |
# SHORTCIRCUIT |
| 202 |
if(!f.file_exists(path)): |
| 203 |
_lgr.error('Could not find script: ' + path) |
| 204 |
return |
| 205 |
|
| 206 |
var ts = TestScript.new(_utils, _lgr) |
| 207 |
ts.path = path |
| 208 |
scripts.append(ts) |
| 209 |
return _parse_script(ts) |
| 210 |
|
| 211 |
func clear(): |
| 212 |
scripts.clear() |
| 213 |
|
| 214 |
func has_script(path): |
| 215 |
var found = false |
| 216 |
var idx = 0 |
| 217 |
while(idx < scripts.size() and !found): |
| 218 |
if(scripts[idx].get_full_name() == path): |
| 219 |
found = true |
| 220 |
else: |
| 221 |
idx += 1 |
| 222 |
return found |
| 223 |
|
| 224 |
func export_tests(path): |
| 225 |
var success = true |
| 226 |
var f = ConfigFile.new() |
| 227 |
for i in range(scripts.size()): |
| 228 |
scripts[i].export_to(f, str('TestScript-', i)) |
| 229 |
var result = f.save(path) |
| 230 |
if(result != OK): |
| 231 |
_lgr.error(str('Could not save exported tests to [', path, ']. Error code: ', result)) |
| 232 |
success = false |
| 233 |
return success |
| 234 |
|
| 235 |
func import_tests(path): |
| 236 |
var success = false |
| 237 |
var f = ConfigFile.new() |
| 238 |
var result = f.load(path) |
| 239 |
if(result != OK): |
| 240 |
_lgr.error(str('Could not load exported tests from [', path, ']. Error code: ', result)) |
| 241 |
else: |
| 242 |
var sections = f.get_sections() |
| 243 |
for key in sections: |
| 244 |
var ts = TestScript.new(_utils, _lgr) |
| 245 |
ts.import_from(f, key) |
| 246 |
_populate_tests(ts) |
| 247 |
scripts.append(ts) |
| 248 |
success = true |
| 249 |
return success |
| 250 |
|
| 251 |
func get_script_named(name): |
| 252 |
return _utils.search_array(scripts, 'get_filename_and_inner', name) |
| 253 |
|
| 254 |
func get_test_named(script_name, test_name): |
| 255 |
var s = get_script_named(script_name) |
| 256 |
if(s != null): |
| 257 |
return s.get_test_named(test_name) |
| 258 |
else: |
| 259 |
return null |
| 260 |
|
| 261 |
func to_s(): |
| 262 |
var to_return = '' |
| 263 |
for i in range(scripts.size()): |
| 264 |
to_return += scripts[i].to_s() + "\n" |
| 265 |
return to_return |
| 266 |
|
| 267 |
# --------------------- |
| 268 |
# Accessors |
| 269 |
# --------------------- |
| 270 |
func get_logger(): |
| 271 |
return _lgr |
| 272 |
|
| 273 |
func set_logger(logger): |
| 274 |
_lgr = logger |
| 275 |
|
| 276 |
func get_test_prefix(): |
| 277 |
return _test_prefix |
| 278 |
|
| 279 |
func set_test_prefix(test_prefix): |
| 280 |
_test_prefix = test_prefix |
| 281 |
|
| 282 |
func get_test_class_prefix(): |
| 283 |
return _test_class_prefix |
| 284 |
|
| 285 |
func set_test_class_prefix(test_class_prefix): |
| 286 |
_test_class_prefix = test_class_prefix |