| 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 |
# Version - see gut.gd |
| 32 |
# ############################################################################## |
| 33 |
# Class that all test scripts must extend. |
| 34 |
# |
| 35 |
# This provides all the asserts and other testing features. Test scripts are |
| 36 |
# run by the Gut class in gut.gd |
| 37 |
# ############################################################################## |
| 38 |
extends Node |
| 39 |
|
| 40 |
# ------------------------------------------------------------------------------ |
| 41 |
# Helper class to hold info for objects to double. This extracts info and has |
| 42 |
# some convenience methods. This is key in being able to make the "smart double" |
| 43 |
# method which makes doubling much easier for the user. |
| 44 |
# ------------------------------------------------------------------------------ |
| 45 |
class DoubleInfo: |
| 46 |
var path |
| 47 |
var subpath |
| 48 |
var strategy |
| 49 |
var make_partial |
| 50 |
var extension |
| 51 |
var _utils = load('res://addons/gut/utils.gd').get_instance() |
| 52 |
var _is_native = false |
| 53 |
var is_valid = false |
| 54 |
|
| 55 |
# Flexible init method. p2 can be subpath or stategy unless p3 is |
| 56 |
# specified, then p2 must be subpath and p3 is strategy. |
| 57 |
# |
| 58 |
# Examples: |
| 59 |
# (object_to_double) |
| 60 |
# (object_to_double, subpath) |
| 61 |
# (object_to_double, strategy) |
| 62 |
# (object_to_double, subpath, strategy) |
| 63 |
func _init(thing, p2=null, p3=null): |
| 64 |
strategy = p2 |
| 65 |
|
| 66 |
# short-circuit and ensure that is_valid |
| 67 |
# is not set to true. |
| 68 |
if(_utils.is_instance(thing)): |
| 69 |
return |
| 70 |
|
| 71 |
if(typeof(p2) == TYPE_STRING): |
| 72 |
strategy = p3 |
| 73 |
subpath = p2 |
| 74 |
|
| 75 |
if(typeof(thing) == TYPE_OBJECT): |
| 76 |
if(_utils.is_native_class(thing)): |
| 77 |
path = thing |
| 78 |
_is_native = true |
| 79 |
extension = 'native_class_not_used' |
| 80 |
else: |
| 81 |
path = thing.resource_path |
| 82 |
else: |
| 83 |
path = thing |
| 84 |
|
| 85 |
if(!_is_native): |
| 86 |
extension = path.get_extension() |
| 87 |
|
| 88 |
is_valid = true |
| 89 |
|
| 90 |
func is_scene(): |
| 91 |
return extension == 'tscn' |
| 92 |
|
| 93 |
func is_script(): |
| 94 |
return extension == 'gd' |
| 95 |
|
| 96 |
func is_native(): |
| 97 |
return _is_native |
| 98 |
|
| 99 |
# ------------------------------------------------------------------------------ |
| 100 |
# Begin test.gd |
| 101 |
# ------------------------------------------------------------------------------ |
| 102 |
var _utils = load('res://addons/gut/utils.gd').get_instance() |
| 103 |
var _compare = _utils.Comparator.new() |
| 104 |
|
| 105 |
# constant for signal when calling yield_for |
| 106 |
const YIELD = 'timeout' |
| 107 |
|
| 108 |
# Need a reference to the instance that is running the tests. This |
| 109 |
# is set by the gut class when it runs the tests. This gets you |
| 110 |
# access to the asserts in the tests you write. |
| 111 |
var gut = null |
| 112 |
|
| 113 |
var _disable_strict_datatype_checks = false |
| 114 |
# Holds all the text for a test's fail/pass. This is used for testing purposes |
| 115 |
# to see the text of a failed sub-test in test_test.gd |
| 116 |
var _fail_pass_text = [] |
| 117 |
|
| 118 |
const EDITOR_PROPERTY = PROPERTY_USAGE_SCRIPT_VARIABLE | PROPERTY_USAGE_DEFAULT |
| 119 |
const VARIABLE_PROPERTY = PROPERTY_USAGE_SCRIPT_VARIABLE |
| 120 |
|
| 121 |
# Used with assert_setget |
| 122 |
enum { |
| 123 |
DEFAULT_SETTER_GETTER, |
| 124 |
SETTER_ONLY, |
| 125 |
GETTER_ONLY |
| 126 |
} |
| 127 |
|
| 128 |
# Summary counts for the test. |
| 129 |
var _summary = { |
| 130 |
asserts = 0, |
| 131 |
passed = 0, |
| 132 |
failed = 0, |
| 133 |
tests = 0, |
| 134 |
pending = 0 |
| 135 |
} |
| 136 |
|
| 137 |
# This is used to watch signals so we can make assertions about them. |
| 138 |
var _signal_watcher = load('res://addons/gut/signal_watcher.gd').new() |
| 139 |
|
| 140 |
# Convenience copy of _utils.DOUBLE_STRATEGY |
| 141 |
var DOUBLE_STRATEGY = null |
| 142 |
var _lgr = _utils.get_logger() |
| 143 |
var _strutils = _utils.Strutils.new() |
| 144 |
|
| 145 |
# syntax sugar |
| 146 |
var ParameterFactory = _utils.ParameterFactory |
| 147 |
var CompareResult = _utils.CompareResult |
| 148 |
|
| 149 |
func _init(): |
| 150 |
DOUBLE_STRATEGY = _utils.DOUBLE_STRATEGY # yes, this is right |
| 151 |
|
| 152 |
func _str(thing): |
| 153 |
return _strutils.type2str(thing) |
| 154 |
|
| 155 |
# ------------------------------------------------------------------------------ |
| 156 |
# Fail an assertion. Causes test and script to fail as well. |
| 157 |
# ------------------------------------------------------------------------------ |
| 158 |
func _fail(text): |
| 159 |
_summary.asserts += 1 |
| 160 |
_summary.failed += 1 |
| 161 |
_fail_pass_text.append('failed: ' + text) |
| 162 |
if(gut): |
| 163 |
_lgr.failed(text) |
| 164 |
gut._fail(text) |
| 165 |
|
| 166 |
# ------------------------------------------------------------------------------ |
| 167 |
# Pass an assertion. |
| 168 |
# ------------------------------------------------------------------------------ |
| 169 |
func _pass(text): |
| 170 |
_summary.asserts += 1 |
| 171 |
_summary.passed += 1 |
| 172 |
_fail_pass_text.append('passed: ' + text) |
| 173 |
if(gut): |
| 174 |
_lgr.passed(text) |
| 175 |
gut._pass(text) |
| 176 |
|
| 177 |
# ------------------------------------------------------------------------------ |
| 178 |
# Checks if the datatypes passed in match. If they do not then this will cause |
| 179 |
# a fail to occur. If they match then TRUE is returned, FALSE if not. This is |
| 180 |
# used in all the assertions that compare values. |
| 181 |
# ------------------------------------------------------------------------------ |
| 182 |
func _do_datatypes_match__fail_if_not(got, expected, text): |
| 183 |
var did_pass = true |
| 184 |
|
| 185 |
if(!_disable_strict_datatype_checks): |
| 186 |
var got_type = typeof(got) |
| 187 |
var expect_type = typeof(expected) |
| 188 |
if(got_type != expect_type and got != null and expected != null): |
| 189 |
# If we have a mismatch between float and int (types 2 and 3) then |
| 190 |
# print out a warning but do not fail. |
| 191 |
if([2, 3].has(got_type) and [2, 3].has(expect_type)): |
| 192 |
_lgr.warn(str('Warn: Float/Int comparison. Got ', _strutils.types[got_type], |
| 193 |
' but expected ', _strutils.types[expect_type])) |
| 194 |
else: |
| 195 |
_fail('Cannot compare ' + _strutils.types[got_type] + '[' + _str(got) + '] to ' + \ |
| 196 |
_strutils.types[expect_type] + '[' + _str(expected) + ']. ' + text) |
| 197 |
did_pass = false |
| 198 |
|
| 199 |
return did_pass |
| 200 |
|
| 201 |
# ------------------------------------------------------------------------------ |
| 202 |
# Create a string that lists all the methods that were called on an spied |
| 203 |
# instance. |
| 204 |
# ------------------------------------------------------------------------------ |
| 205 |
func _get_desc_of_calls_to_instance(inst): |
| 206 |
var BULLET = ' * ' |
| 207 |
var calls = gut.get_spy().get_call_list_as_string(inst) |
| 208 |
# indent all the calls |
| 209 |
calls = BULLET + calls.replace("\n", "\n" + BULLET) |
| 210 |
# remove trailing newline and bullet |
| 211 |
calls = calls.substr(0, calls.length() - BULLET.length() - 1) |
| 212 |
return "Calls made on " + str(inst) + "\n" + calls |
| 213 |
|
| 214 |
# ------------------------------------------------------------------------------ |
| 215 |
# Signal assertion helper. Do not call directly, use _can_make_signal_assertions |
| 216 |
# ------------------------------------------------------------------------------ |
| 217 |
func _fail_if_does_not_have_signal(object, signal_name): |
| 218 |
var did_fail = false |
| 219 |
if(!_signal_watcher.does_object_have_signal(object, signal_name)): |
| 220 |
_fail(str('Object ', object, ' does not have the signal [', signal_name, ']')) |
| 221 |
did_fail = true |
| 222 |
return did_fail |
| 223 |
|
| 224 |
# ------------------------------------------------------------------------------ |
| 225 |
# Signal assertion helper. Do not call directly, use _can_make_signal_assertions |
| 226 |
# ------------------------------------------------------------------------------ |
| 227 |
func _fail_if_not_watching(object): |
| 228 |
var did_fail = false |
| 229 |
if(!_signal_watcher.is_watching_object(object)): |
| 230 |
_fail(str('Cannot make signal assertions because the object ', object, \ |
| 231 |
' is not being watched. Call watch_signals(some_object) to be able to make assertions about signals.')) |
| 232 |
did_fail = true |
| 233 |
return did_fail |
| 234 |
|
| 235 |
# ------------------------------------------------------------------------------ |
| 236 |
# Returns text that contains original text and a list of all the signals that |
| 237 |
# were emitted for the passed in object. |
| 238 |
# ------------------------------------------------------------------------------ |
| 239 |
func _get_fail_msg_including_emitted_signals(text, object): |
| 240 |
return str(text," (Signals emitted: ", _signal_watcher.get_signals_emitted(object), ")") |
| 241 |
|
| 242 |
# ------------------------------------------------------------------------------ |
| 243 |
# This validates that parameters is an array and generates a specific error |
| 244 |
# and a failure with a specific message |
| 245 |
# ------------------------------------------------------------------------------ |
| 246 |
func _fail_if_parameters_not_array(parameters): |
| 247 |
var invalid = parameters != null and typeof(parameters) != TYPE_ARRAY |
| 248 |
if(invalid): |
| 249 |
_lgr.error('The "parameters" parameter must be an array of expected parameter values.') |
| 250 |
_fail('Cannot compare paramter values because an array was not passed.') |
| 251 |
return invalid |
| 252 |
|
| 253 |
|
| 254 |
func _create_obj_from_type(type): |
| 255 |
var obj = null |
| 256 |
if type.is_class("PackedScene"): |
| 257 |
obj = type.instance() |
| 258 |
add_child(obj) |
| 259 |
else: |
| 260 |
obj = type.new() |
| 261 |
return obj |
| 262 |
|
| 263 |
|
| 264 |
func _get_type_from_obj(obj): |
| 265 |
var type = null |
| 266 |
if obj.has_method(get_filename()): |
| 267 |
type = load(obj.get_filename()) |
| 268 |
else: |
| 269 |
type = obj.get_script() |
| 270 |
return type |
| 271 |
|
| 272 |
# ####################### |
| 273 |
# Virtual Methods |
| 274 |
# ####################### |
| 275 |
|
| 276 |
# alias for prerun_setup |
| 277 |
func before_all(): |
| 278 |
pass |
| 279 |
|
| 280 |
# alias for setup |
| 281 |
func before_each(): |
| 282 |
pass |
| 283 |
|
| 284 |
# alias for postrun_teardown |
| 285 |
func after_all(): |
| 286 |
pass |
| 287 |
|
| 288 |
# alias for teardown |
| 289 |
func after_each(): |
| 290 |
pass |
| 291 |
|
| 292 |
# ####################### |
| 293 |
# Public |
| 294 |
# ####################### |
| 295 |
|
| 296 |
func get_logger(): |
| 297 |
return _lgr |
| 298 |
|
| 299 |
func set_logger(logger): |
| 300 |
_lgr = logger |
| 301 |
|
| 302 |
|
| 303 |
# ####################### |
| 304 |
# Asserts |
| 305 |
# ####################### |
| 306 |
|
| 307 |
# ------------------------------------------------------------------------------ |
| 308 |
# Asserts that the expected value equals the value got. |
| 309 |
# ------------------------------------------------------------------------------ |
| 310 |
func assert_eq(got, expected, text=""): |
| 311 |
|
| 312 |
if(_do_datatypes_match__fail_if_not(got, expected, text)): |
| 313 |
var disp = "[" + _str(got) + "] expected to equal [" + _str(expected) + "]: " + text |
| 314 |
var result = null |
| 315 |
|
| 316 |
if(typeof(got) == TYPE_ARRAY): |
| 317 |
result = _compare.shallow(got, expected) |
| 318 |
else: |
| 319 |
result = _compare.simple(got, expected) |
| 320 |
|
| 321 |
if(typeof(got) in [TYPE_ARRAY, TYPE_DICTIONARY]): |
| 322 |
disp = str(result.summary, ' ', text) |
| 323 |
|
| 324 |
if(result.are_equal): |
| 325 |
_pass(disp) |
| 326 |
else: |
| 327 |
_fail(disp) |
| 328 |
|
| 329 |
|
| 330 |
# ------------------------------------------------------------------------------ |
| 331 |
# Asserts that the value got does not equal the "not expected" value. |
| 332 |
# ------------------------------------------------------------------------------ |
| 333 |
func assert_ne(got, not_expected, text=""): |
| 334 |
if(_do_datatypes_match__fail_if_not(got, not_expected, text)): |
| 335 |
var disp = "[" + _str(got) + "] expected to not equal [" + _str(not_expected) + "]: " + text |
| 336 |
var result = null |
| 337 |
|
| 338 |
if(typeof(got) == TYPE_ARRAY): |
| 339 |
result = _compare.shallow(got, not_expected) |
| 340 |
else: |
| 341 |
result = _compare.simple(got, not_expected) |
| 342 |
|
| 343 |
if(typeof(got) in [TYPE_ARRAY, TYPE_DICTIONARY]): |
| 344 |
disp = str(result.summary, ' ', text) |
| 345 |
|
| 346 |
if(result.are_equal): |
| 347 |
_fail(disp) |
| 348 |
else: |
| 349 |
_pass(disp) |
| 350 |
|
| 351 |
|
| 352 |
# ------------------------------------------------------------------------------ |
| 353 |
# Asserts that the expected value almost equals the value got. |
| 354 |
# ------------------------------------------------------------------------------ |
| 355 |
func assert_almost_eq(got, expected, error_interval, text=''): |
| 356 |
var disp = "[" + _str(got) + "] expected to equal [" + _str(expected) + "] +/- [" + str(error_interval) + "]: " + text |
| 357 |
if(_do_datatypes_match__fail_if_not(got, expected, text) and _do_datatypes_match__fail_if_not(got, error_interval, text)): |
| 358 |
if(got < (expected - error_interval) or got > (expected + error_interval)): |
| 359 |
_fail(disp) |
| 360 |
else: |
| 361 |
_pass(disp) |
| 362 |
|
| 363 |
# ------------------------------------------------------------------------------ |
| 364 |
# Asserts that the expected value does not almost equal the value got. |
| 365 |
# ------------------------------------------------------------------------------ |
| 366 |
func assert_almost_ne(got, not_expected, error_interval, text=''): |
| 367 |
var disp = "[" + _str(got) + "] expected to not equal [" + _str(not_expected) + "] +/- [" + str(error_interval) + "]: " + text |
| 368 |
if(_do_datatypes_match__fail_if_not(got, not_expected, text) and _do_datatypes_match__fail_if_not(got, error_interval, text)): |
| 369 |
if(got < (not_expected - error_interval) or got > (not_expected + error_interval)): |
| 370 |
_pass(disp) |
| 371 |
else: |
| 372 |
_fail(disp) |
| 373 |
|
| 374 |
# ------------------------------------------------------------------------------ |
| 375 |
# Asserts got is greater than expected |
| 376 |
# ------------------------------------------------------------------------------ |
| 377 |
func assert_gt(got, expected, text=""): |
| 378 |
var disp = "[" + _str(got) + "] expected to be > than [" + _str(expected) + "]: " + text |
| 379 |
if(_do_datatypes_match__fail_if_not(got, expected, text)): |
| 380 |
if(got > expected): |
| 381 |
_pass(disp) |
| 382 |
else: |
| 383 |
_fail(disp) |
| 384 |
|
| 385 |
# ------------------------------------------------------------------------------ |
| 386 |
# Asserts got is less than expected |
| 387 |
# ------------------------------------------------------------------------------ |
| 388 |
func assert_lt(got, expected, text=""): |
| 389 |
var disp = "[" + _str(got) + "] expected to be < than [" + _str(expected) + "]: " + text |
| 390 |
if(_do_datatypes_match__fail_if_not(got, expected, text)): |
| 391 |
if(got < expected): |
| 392 |
_pass(disp) |
| 393 |
else: |
| 394 |
_fail(disp) |
| 395 |
|
| 396 |
# ------------------------------------------------------------------------------ |
| 397 |
# asserts that got is true |
| 398 |
# ------------------------------------------------------------------------------ |
| 399 |
func assert_true(got, text=""): |
| 400 |
if(typeof(got) == TYPE_BOOL): |
| 401 |
if(got): |
| 402 |
_pass(text) |
| 403 |
else: |
| 404 |
_fail(text) |
| 405 |
else: |
| 406 |
var msg = str("Cannot convert ", _strutils.type2str(got), " to boolean") |
| 407 |
_fail(msg) |
| 408 |
|
| 409 |
# ------------------------------------------------------------------------------ |
| 410 |
# Asserts that got is false |
| 411 |
# ------------------------------------------------------------------------------ |
| 412 |
func assert_false(got, text=""): |
| 413 |
if(typeof(got) == TYPE_BOOL): |
| 414 |
if(got): |
| 415 |
_fail(text) |
| 416 |
else: |
| 417 |
_pass(text) |
| 418 |
else: |
| 419 |
var msg = str("Cannot convert ", _strutils.type2str(got), " to boolean") |
| 420 |
_fail(msg) |
| 421 |
|
| 422 |
# ------------------------------------------------------------------------------ |
| 423 |
# Asserts value is between (inclusive) the two expected values. |
| 424 |
# ------------------------------------------------------------------------------ |
| 425 |
func assert_between(got, expect_low, expect_high, text=""): |
| 426 |
var disp = "[" + _str(got) + "] expected to be between [" + _str(expect_low) + "] and [" + str(expect_high) + "]: " + text |
| 427 |
|
| 428 |
if(_do_datatypes_match__fail_if_not(got, expect_low, text) and _do_datatypes_match__fail_if_not(got, expect_high, text)): |
| 429 |
if(expect_low > expect_high): |
| 430 |
disp = "INVALID range. [" + str(expect_low) + "] is not less than [" + str(expect_high) + "]" |
| 431 |
_fail(disp) |
| 432 |
else: |
| 433 |
if(got < expect_low or got > expect_high): |
| 434 |
_fail(disp) |
| 435 |
else: |
| 436 |
_pass(disp) |
| 437 |
|
| 438 |
# ------------------------------------------------------------------------------ |
| 439 |
# Asserts value is not between (exclusive) the two expected values. |
| 440 |
# ------------------------------------------------------------------------------ |
| 441 |
func assert_not_between(got, expect_low, expect_high, text=""): |
| 442 |
var disp = "[" + _str(got) + "] expected not to be between [" + _str(expect_low) + "] and [" + str(expect_high) + "]: " + text |
| 443 |
|
| 444 |
if(_do_datatypes_match__fail_if_not(got, expect_low, text) and _do_datatypes_match__fail_if_not(got, expect_high, text)): |
| 445 |
if(expect_low > expect_high): |
| 446 |
disp = "INVALID range. [" + str(expect_low) + "] is not less than [" + str(expect_high) + "]" |
| 447 |
_fail(disp) |
| 448 |
else: |
| 449 |
if(got > expect_low and got < expect_high): |
| 450 |
_fail(disp) |
| 451 |
else: |
| 452 |
_pass(disp) |
| 453 |
|
| 454 |
# ------------------------------------------------------------------------------ |
| 455 |
# Uses the 'has' method of the object passed in to determine if it contains |
| 456 |
# the passed in element. |
| 457 |
# ------------------------------------------------------------------------------ |
| 458 |
func assert_has(obj, element, text=""): |
| 459 |
var disp = str('Expected [', _str(obj), '] to contain value: [', _str(element), ']: ', text) |
| 460 |
if(obj.has(element)): |
| 461 |
_pass(disp) |
| 462 |
else: |
| 463 |
_fail(disp) |
| 464 |
|
| 465 |
# ------------------------------------------------------------------------------ |
| 466 |
# ------------------------------------------------------------------------------ |
| 467 |
func assert_does_not_have(obj, element, text=""): |
| 468 |
var disp = str('Expected [', _str(obj), '] to NOT contain value: [', _str(element), ']: ', text) |
| 469 |
if(obj.has(element)): |
| 470 |
_fail(disp) |
| 471 |
else: |
| 472 |
_pass(disp) |
| 473 |
|
| 474 |
# ------------------------------------------------------------------------------ |
| 475 |
# Asserts that a file exists |
| 476 |
# ------------------------------------------------------------------------------ |
| 477 |
func assert_file_exists(file_path): |
| 478 |
var disp = 'expected [' + file_path + '] to exist.' |
| 479 |
var f = File.new() |
| 480 |
if(f.file_exists(file_path)): |
| 481 |
_pass(disp) |
| 482 |
else: |
| 483 |
_fail(disp) |
| 484 |
|
| 485 |
# ------------------------------------------------------------------------------ |
| 486 |
# Asserts that a file should not exist |
| 487 |
# ------------------------------------------------------------------------------ |
| 488 |
func assert_file_does_not_exist(file_path): |
| 489 |
var disp = 'expected [' + file_path + '] to NOT exist' |
| 490 |
var f = File.new() |
| 491 |
if(!f.file_exists(file_path)): |
| 492 |
_pass(disp) |
| 493 |
else: |
| 494 |
_fail(disp) |
| 495 |
|
| 496 |
# ------------------------------------------------------------------------------ |
| 497 |
# Asserts the specified file is empty |
| 498 |
# ------------------------------------------------------------------------------ |
| 499 |
func assert_file_empty(file_path): |
| 500 |
var disp = 'expected [' + file_path + '] to be empty' |
| 501 |
var f = File.new() |
| 502 |
if(f.file_exists(file_path) and gut.is_file_empty(file_path)): |
| 503 |
_pass(disp) |
| 504 |
else: |
| 505 |
_fail(disp) |
| 506 |
|
| 507 |
# ------------------------------------------------------------------------------ |
| 508 |
# Asserts the specified file is not empty |
| 509 |
# ------------------------------------------------------------------------------ |
| 510 |
func assert_file_not_empty(file_path): |
| 511 |
var disp = 'expected [' + file_path + '] to contain data' |
| 512 |
if(!gut.is_file_empty(file_path)): |
| 513 |
_pass(disp) |
| 514 |
else: |
| 515 |
_fail(disp) |
| 516 |
|
| 517 |
# ------------------------------------------------------------------------------ |
| 518 |
# Asserts the object has the specified method |
| 519 |
# ------------------------------------------------------------------------------ |
| 520 |
func assert_has_method(obj, method): |
| 521 |
assert_true(obj.has_method(method), _str(obj) + ' should have method: ' + method) |
| 522 |
|
| 523 |
# Old deprecated method name |
| 524 |
func assert_get_set_methods(obj, property, default, set_to): |
| 525 |
_lgr.deprecated('assert_get_set_methods', 'assert_accessors') |
| 526 |
assert_accessors(obj, property, default, set_to) |
| 527 |
|
| 528 |
# ------------------------------------------------------------------------------ |
| 529 |
# Verifies the object has get and set methods for the property passed in. The |
| 530 |
# property isn't tied to anything, just a name to be appended to the end of |
| 531 |
# get_ and set_. Asserts the get_ and set_ methods exist, if not, it stops there. |
| 532 |
# If they exist then it asserts get_ returns the expected default then calls |
| 533 |
# set_ and asserts get_ has the value it was set to. |
| 534 |
# ------------------------------------------------------------------------------ |
| 535 |
func assert_accessors(obj, property, default, set_to): |
| 536 |
var fail_count = _summary.failed |
| 537 |
var get = 'get_' + property |
| 538 |
var set = 'set_' + property |
| 539 |
assert_has_method(obj, get) |
| 540 |
assert_has_method(obj, set) |
| 541 |
# SHORT CIRCUIT |
| 542 |
if(_summary.failed > fail_count): |
| 543 |
return |
| 544 |
assert_eq(obj.call(get), default, 'It should have the expected default value.') |
| 545 |
obj.call(set, set_to) |
| 546 |
assert_eq(obj.call(get), set_to, 'The set value should have been returned.') |
| 547 |
|
| 548 |
|
| 549 |
# --------------------------------------------------------------------------- |
| 550 |
# Property search helper. Used to retrieve Dictionary of specified property |
| 551 |
# from passed object. Returns null if not found. |
| 552 |
# If provided, property_usage constrains the type of property returned by |
| 553 |
# passing either: |
| 554 |
# EDITOR_PROPERTY for properties defined as: export(int) var some_value |
| 555 |
# VARIABLE_PROPERTY for properties defined as: var another_value |
| 556 |
# --------------------------------------------------------------------------- |
| 557 |
func _find_object_property(obj, property_name, property_usage=null): |
| 558 |
var result = null |
| 559 |
var found = false |
| 560 |
var properties = obj.get_property_list() |
| 561 |
|
| 562 |
while !found and !properties.empty(): |
| 563 |
var property = properties.pop_back() |
| 564 |
if property['name'] == property_name: |
| 565 |
if property_usage == null or property['usage'] == property_usage: |
| 566 |
result = property |
| 567 |
found = true |
| 568 |
return result |
| 569 |
|
| 570 |
# ------------------------------------------------------------------------------ |
| 571 |
# Asserts a class exports a variable. |
| 572 |
# ------------------------------------------------------------------------------ |
| 573 |
func assert_exports(obj, property_name, type): |
| 574 |
var disp = 'expected %s to have editor property [%s]' % [_str(obj), property_name] |
| 575 |
var property = _find_object_property(obj, property_name, EDITOR_PROPERTY) |
| 576 |
if property != null: |
| 577 |
disp += ' of type [%s]. Got type [%s].' % [_strutils.types[type], _strutils.types[property['type']]] |
| 578 |
if property['type'] == type: |
| 579 |
_pass(disp) |
| 580 |
else: |
| 581 |
_fail(disp) |
| 582 |
else: |
| 583 |
_fail(disp) |
| 584 |
|
| 585 |
# ------------------------------------------------------------------------------ |
| 586 |
# Signal assertion helper. |
| 587 |
# |
| 588 |
# Verifies that the object and signal are valid for making signal assertions. |
| 589 |
# This will fail with specific messages that indicate why they are not valid. |
| 590 |
# This returns true/false to indicate if the object and signal are valid. |
| 591 |
# ------------------------------------------------------------------------------ |
| 592 |
func _can_make_signal_assertions(object, signal_name): |
| 593 |
return !(_fail_if_not_watching(object) or _fail_if_does_not_have_signal(object, signal_name)) |
| 594 |
|
| 595 |
# ------------------------------------------------------------------------------ |
| 596 |
# Check if an object is connected to a signal on another object. Returns True |
| 597 |
# if it is and false otherwise |
| 598 |
# ------------------------------------------------------------------------------ |
| 599 |
func _is_connected(signaler_obj, connect_to_obj, signal_name, method_name=""): |
| 600 |
if(method_name != ""): |
| 601 |
return signaler_obj.is_connected(signal_name, connect_to_obj, method_name) |
| 602 |
else: |
| 603 |
var connections = signaler_obj.get_signal_connection_list(signal_name) |
| 604 |
for conn in connections: |
| 605 |
if((conn.source == signaler_obj) and (conn.target == connect_to_obj)): |
| 606 |
return true |
| 607 |
return false |
| 608 |
# ------------------------------------------------------------------------------ |
| 609 |
# Watch the signals for an object. This must be called before you can make |
| 610 |
# any assertions about the signals themselves. |
| 611 |
# ------------------------------------------------------------------------------ |
| 612 |
func watch_signals(object): |
| 613 |
_signal_watcher.watch_signals(object) |
| 614 |
|
| 615 |
# ------------------------------------------------------------------------------ |
| 616 |
# Asserts that an object is connected to a signal on another object |
| 617 |
# |
| 618 |
# This will fail with specific messages if the target object is not connected |
| 619 |
# to the specified signal on the source object. |
| 620 |
# ------------------------------------------------------------------------------ |
| 621 |
func assert_connected(signaler_obj, connect_to_obj, signal_name, method_name=""): |
| 622 |
pass |
| 623 |
var method_disp = '' |
| 624 |
if (method_name != ""): |
| 625 |
method_disp = str(' using method: [', method_name, '] ') |
| 626 |
var disp = str('Expected object ', _str(signaler_obj),\ |
| 627 |
' to be connected to signal: [', signal_name, '] on ',\ |
| 628 |
_str(connect_to_obj), method_disp) |
| 629 |
if(_is_connected(signaler_obj, connect_to_obj, signal_name, method_name)): |
| 630 |
_pass(disp) |
| 631 |
else: |
| 632 |
_fail(disp) |
| 633 |
|
| 634 |
# ------------------------------------------------------------------------------ |
| 635 |
# Asserts that an object is not connected to a signal on another object |
| 636 |
# |
| 637 |
# This will fail with specific messages if the target object is connected |
| 638 |
# to the specified signal on the source object. |
| 639 |
# ------------------------------------------------------------------------------ |
| 640 |
func assert_not_connected(signaler_obj, connect_to_obj, signal_name, method_name=""): |
| 641 |
var method_disp = '' |
| 642 |
if (method_name != ""): |
| 643 |
method_disp = str(' using method: [', method_name, '] ') |
| 644 |
var disp = str('Expected object ', _str(signaler_obj),\ |
| 645 |
' to not be connected to signal: [', signal_name, '] on ',\ |
| 646 |
_str(connect_to_obj), method_disp) |
| 647 |
if(_is_connected(signaler_obj, connect_to_obj, signal_name, method_name)): |
| 648 |
_fail(disp) |
| 649 |
else: |
| 650 |
_pass(disp) |
| 651 |
|
| 652 |
# ------------------------------------------------------------------------------ |
| 653 |
# Asserts that a signal has been emitted at least once. |
| 654 |
# |
| 655 |
# This will fail with specific messages if the object is not being watched or |
| 656 |
# the object does not have the specified signal |
| 657 |
# ------------------------------------------------------------------------------ |
| 658 |
func assert_signal_emitted(object, signal_name, text=""): |
| 659 |
var disp = str('Expected object ', _str(object), ' to have emitted signal [', signal_name, ']: ', text) |
| 660 |
if(_can_make_signal_assertions(object, signal_name)): |
| 661 |
if(_signal_watcher.did_emit(object, signal_name)): |
| 662 |
_pass(disp) |
| 663 |
else: |
| 664 |
_fail(_get_fail_msg_including_emitted_signals(disp, object)) |
| 665 |
|
| 666 |
# ------------------------------------------------------------------------------ |
| 667 |
# Asserts that a signal has not been emitted. |
| 668 |
# |
| 669 |
# This will fail with specific messages if the object is not being watched or |
| 670 |
# the object does not have the specified signal |
| 671 |
# ------------------------------------------------------------------------------ |
| 672 |
func assert_signal_not_emitted(object, signal_name, text=""): |
| 673 |
var disp = str('Expected object ', _str(object), ' to NOT emit signal [', signal_name, ']: ', text) |
| 674 |
if(_can_make_signal_assertions(object, signal_name)): |
| 675 |
if(_signal_watcher.did_emit(object, signal_name)): |
| 676 |
_fail(disp) |
| 677 |
else: |
| 678 |
_pass(disp) |
| 679 |
|
| 680 |
# ------------------------------------------------------------------------------ |
| 681 |
# Asserts that a signal was fired with the specified parameters. The expected |
| 682 |
# parameters should be passed in as an array. An optional index can be passed |
| 683 |
# when a signal has fired more than once. The default is to retrieve the most |
| 684 |
# recent emission of the signal. |
| 685 |
# |
| 686 |
# This will fail with specific messages if the object is not being watched or |
| 687 |
# the object does not have the specified signal |
| 688 |
# ------------------------------------------------------------------------------ |
| 689 |
func assert_signal_emitted_with_parameters(object, signal_name, parameters, index=-1): |
| 690 |
var disp = str('Expected object ', _str(object), ' to emit signal [', signal_name, '] with parameters ', parameters, ', got ') |
| 691 |
if(_can_make_signal_assertions(object, signal_name)): |
| 692 |
if(_signal_watcher.did_emit(object, signal_name)): |
| 693 |
var parms_got = _signal_watcher.get_signal_parameters(object, signal_name, index) |
| 694 |
var diff_result = _compare.deep(parameters, parms_got) |
| 695 |
if(diff_result.are_equal()): |
| 696 |
_pass(str(disp, parms_got)) |
| 697 |
else: |
| 698 |
_fail(str('Expected object ', _str(object), ' to emit signal [', signal_name, '] with parameters ', diff_result.summarize())) |
| 699 |
else: |
| 700 |
var text = str('Object ', object, ' did not emit signal [', signal_name, ']') |
| 701 |
_fail(_get_fail_msg_including_emitted_signals(text, object)) |
| 702 |
|
| 703 |
# ------------------------------------------------------------------------------ |
| 704 |
# Assert that a signal has been emitted a specific number of times. |
| 705 |
# |
| 706 |
# This will fail with specific messages if the object is not being watched or |
| 707 |
# the object does not have the specified signal |
| 708 |
# ------------------------------------------------------------------------------ |
| 709 |
func assert_signal_emit_count(object, signal_name, times, text=""): |
| 710 |
|
| 711 |
if(_can_make_signal_assertions(object, signal_name)): |
| 712 |
var count = _signal_watcher.get_emit_count(object, signal_name) |
| 713 |
var disp = str('Expected the signal [', signal_name, '] emit count of [', count, '] to equal [', times, ']: ', text) |
| 714 |
if(count== times): |
| 715 |
_pass(disp) |
| 716 |
else: |
| 717 |
_fail(_get_fail_msg_including_emitted_signals(disp, object)) |
| 718 |
|
| 719 |
# ------------------------------------------------------------------------------ |
| 720 |
# Assert that the passed in object has the specified signal |
| 721 |
# ------------------------------------------------------------------------------ |
| 722 |
func assert_has_signal(object, signal_name, text=""): |
| 723 |
var disp = str('Expected object ', _str(object), ' to have signal [', signal_name, ']: ', text) |
| 724 |
if(_signal_watcher.does_object_have_signal(object, signal_name)): |
| 725 |
_pass(disp) |
| 726 |
else: |
| 727 |
_fail(disp) |
| 728 |
|
| 729 |
# ------------------------------------------------------------------------------ |
| 730 |
# Returns the number of times a signal was emitted. -1 returned if the object |
| 731 |
# is not being watched. |
| 732 |
# ------------------------------------------------------------------------------ |
| 733 |
func get_signal_emit_count(object, signal_name): |
| 734 |
return _signal_watcher.get_emit_count(object, signal_name) |
| 735 |
|
| 736 |
# ------------------------------------------------------------------------------ |
| 737 |
# Get the parmaters of a fired signal. If the signal was not fired null is |
| 738 |
# returned. You can specify an optional index (use get_signal_emit_count to |
| 739 |
# determine the number of times it was emitted). The default index is the |
| 740 |
# latest time the signal was fired (size() -1 insetead of 0). The parameters |
| 741 |
# returned are in an array. |
| 742 |
# ------------------------------------------------------------------------------ |
| 743 |
func get_signal_parameters(object, signal_name, index=-1): |
| 744 |
return _signal_watcher.get_signal_parameters(object, signal_name, index) |
| 745 |
|
| 746 |
# ------------------------------------------------------------------------------ |
| 747 |
# Get the parameters for a method call to a doubled object. By default it will |
| 748 |
# return the most recent call. You can optionally specify an index. |
| 749 |
# |
| 750 |
# Returns: |
| 751 |
# * an array of parameter values if a call the method was found |
| 752 |
# * null when a call to the method was not found or the index specified was |
| 753 |
# invalid. |
| 754 |
# ------------------------------------------------------------------------------ |
| 755 |
func get_call_parameters(object, method_name, index=-1): |
| 756 |
var to_return = null |
| 757 |
if(_utils.is_double(object)): |
| 758 |
to_return = gut.get_spy().get_call_parameters(object, method_name, index) |
| 759 |
else: |
| 760 |
_lgr.error('You must pass a doulbed object to get_call_parameters.') |
| 761 |
|
| 762 |
return to_return |
| 763 |
|
| 764 |
# ------------------------------------------------------------------------------ |
| 765 |
# Assert that object is an instance of a_class |
| 766 |
# ------------------------------------------------------------------------------ |
| 767 |
func assert_extends(object, a_class, text=''): |
| 768 |
_lgr.deprecated('assert_extends', 'assert_is') |
| 769 |
assert_is(object, a_class, text) |
| 770 |
|
| 771 |
# Alias for assert_extends |
| 772 |
func assert_is(object, a_class, text=''): |
| 773 |
var disp = ''#var disp = str('Expected [', _str(object), '] to be type of [', a_class, ']: ', text) |
| 774 |
var NATIVE_CLASS = 'GDScriptNativeClass' |
| 775 |
var GDSCRIPT_CLASS = 'GDScript' |
| 776 |
var bad_param_2 = 'Parameter 2 must be a Class (like Node2D or Label). You passed ' |
| 777 |
|
| 778 |
if(typeof(object) != TYPE_OBJECT): |
| 779 |
_fail(str('Parameter 1 must be an instance of an object. You passed: ', _str(object))) |
| 780 |
elif(typeof(a_class) != TYPE_OBJECT): |
| 781 |
_fail(str(bad_param_2, _str(a_class))) |
| 782 |
else: |
| 783 |
var a = _str(a_class) |
| 784 |
disp = str('Expected [', _str(object), '] to extend [', _str(a_class), ']: ', text) |
| 785 |
if(a_class.get_class() != NATIVE_CLASS and a_class.get_class() != GDSCRIPT_CLASS): |
| 786 |
_fail(str(bad_param_2, _str(a_class))) |
| 787 |
else: |
| 788 |
if(object is a_class): |
| 789 |
_pass(disp) |
| 790 |
else: |
| 791 |
_fail(disp) |
| 792 |
|
| 793 |
func _get_typeof_string(the_type): |
| 794 |
var to_return = "" |
| 795 |
if(_strutils.types.has(the_type)): |
| 796 |
to_return += str(the_type, '(', _strutils.types[the_type], ')') |
| 797 |
else: |
| 798 |
to_return += str(the_type) |
| 799 |
return to_return |
| 800 |
|
| 801 |
# ------------------------------------------------------------------------------ |
| 802 |
# ------------------------------------------------------------------------------ |
| 803 |
func assert_typeof(object, type, text=''): |
| 804 |
var disp = str('Expected [typeof(', object, ') = ') |
| 805 |
disp += _get_typeof_string(typeof(object)) |
| 806 |
disp += '] to equal [' |
| 807 |
disp += _get_typeof_string(type) + ']' |
| 808 |
disp += '. ' + text |
| 809 |
if(typeof(object) == type): |
| 810 |
_pass(disp) |
| 811 |
else: |
| 812 |
_fail(disp) |
| 813 |
|
| 814 |
# ------------------------------------------------------------------------------ |
| 815 |
# ------------------------------------------------------------------------------ |
| 816 |
func assert_not_typeof(object, type, text=''): |
| 817 |
var disp = str('Expected [typeof(', object, ') = ') |
| 818 |
disp += _get_typeof_string(typeof(object)) |
| 819 |
disp += '] to not equal [' |
| 820 |
disp += _get_typeof_string(type) + ']' |
| 821 |
disp += '. ' + text |
| 822 |
if(typeof(object) != type): |
| 823 |
_pass(disp) |
| 824 |
else: |
| 825 |
_fail(disp) |
| 826 |
|
| 827 |
# ------------------------------------------------------------------------------ |
| 828 |
# Assert that text contains given search string. |
| 829 |
# The match_case flag determines case sensitivity. |
| 830 |
# ------------------------------------------------------------------------------ |
| 831 |
func assert_string_contains(text, search, match_case=true): |
| 832 |
var empty_search = 'Expected text and search strings to be non-empty. You passed \'%s\' and \'%s\'.' |
| 833 |
var disp = 'Expected \'%s\' to contain \'%s\', match_case=%s' % [text, search, match_case] |
| 834 |
if(text == '' or search == ''): |
| 835 |
_fail(empty_search % [text, search]) |
| 836 |
elif(match_case): |
| 837 |
if(text.find(search) == -1): |
| 838 |
_fail(disp) |
| 839 |
else: |
| 840 |
_pass(disp) |
| 841 |
else: |
| 842 |
if(text.to_lower().find(search.to_lower()) == -1): |
| 843 |
_fail(disp) |
| 844 |
else: |
| 845 |
_pass(disp) |
| 846 |
|
| 847 |
# ------------------------------------------------------------------------------ |
| 848 |
# Assert that text starts with given search string. |
| 849 |
# match_case flag determines case sensitivity. |
| 850 |
# ------------------------------------------------------------------------------ |
| 851 |
func assert_string_starts_with(text, search, match_case=true): |
| 852 |
var empty_search = 'Expected text and search strings to be non-empty. You passed \'%s\' and \'%s\'.' |
| 853 |
var disp = 'Expected \'%s\' to start with \'%s\', match_case=%s' % [text, search, match_case] |
| 854 |
if(text == '' or search == ''): |
| 855 |
_fail(empty_search % [text, search]) |
| 856 |
elif(match_case): |
| 857 |
if(text.find(search) == 0): |
| 858 |
_pass(disp) |
| 859 |
else: |
| 860 |
_fail(disp) |
| 861 |
else: |
| 862 |
if(text.to_lower().find(search.to_lower()) == 0): |
| 863 |
_pass(disp) |
| 864 |
else: |
| 865 |
_fail(disp) |
| 866 |
|
| 867 |
# ------------------------------------------------------------------------------ |
| 868 |
# Assert that text ends with given search string. |
| 869 |
# match_case flag determines case sensitivity. |
| 870 |
# ------------------------------------------------------------------------------ |
| 871 |
func assert_string_ends_with(text, search, match_case=true): |
| 872 |
var empty_search = 'Expected text and search strings to be non-empty. You passed \'%s\' and \'%s\'.' |
| 873 |
var disp = 'Expected \'%s\' to end with \'%s\', match_case=%s' % [text, search, match_case] |
| 874 |
var required_index = len(text) - len(search) |
| 875 |
if(text == '' or search == ''): |
| 876 |
_fail(empty_search % [text, search]) |
| 877 |
elif(match_case): |
| 878 |
if(text.find(search) == required_index): |
| 879 |
_pass(disp) |
| 880 |
else: |
| 881 |
_fail(disp) |
| 882 |
else: |
| 883 |
if(text.to_lower().find(search.to_lower()) == required_index): |
| 884 |
_pass(disp) |
| 885 |
else: |
| 886 |
_fail(disp) |
| 887 |
|
| 888 |
# ------------------------------------------------------------------------------ |
| 889 |
# Assert that a method was called on an instance of a doubled class. If |
| 890 |
# parameters are supplied then the params passed in when called must match. |
| 891 |
# TODO make 3rd parameter "param_or_text" and add fourth parameter of "text" and |
| 892 |
# then work some magic so this can have a "text" parameter without being |
| 893 |
# annoying. |
| 894 |
# ------------------------------------------------------------------------------ |
| 895 |
func assert_called(inst, method_name, parameters=null): |
| 896 |
var disp = str('Expected [',method_name,'] to have been called on ',_str(inst)) |
| 897 |
|
| 898 |
if(_fail_if_parameters_not_array(parameters)): |
| 899 |
return |
| 900 |
|
| 901 |
if(!_utils.is_double(inst)): |
| 902 |
_fail('You must pass a doubled instance to assert_called. Check the wiki for info on using double.') |
| 903 |
else: |
| 904 |
if(gut.get_spy().was_called(inst, method_name, parameters)): |
| 905 |
_pass(disp) |
| 906 |
else: |
| 907 |
if(parameters != null): |
| 908 |
disp += str(' with parameters ', parameters) |
| 909 |
_fail(str(disp, "\n", _get_desc_of_calls_to_instance(inst))) |
| 910 |
|
| 911 |
# ------------------------------------------------------------------------------ |
| 912 |
# Assert that a method was not called on an instance of a doubled class. If |
| 913 |
# parameters are specified then this will only fail if it finds a call that was |
| 914 |
# sent matching parameters. |
| 915 |
# ------------------------------------------------------------------------------ |
| 916 |
func assert_not_called(inst, method_name, parameters=null): |
| 917 |
var disp = str('Expected [', method_name, '] to NOT have been called on ', _str(inst)) |
| 918 |
|
| 919 |
if(_fail_if_parameters_not_array(parameters)): |
| 920 |
return |
| 921 |
|
| 922 |
if(!_utils.is_double(inst)): |
| 923 |
_fail('You must pass a doubled instance to assert_not_called. Check the wiki for info on using double.') |
| 924 |
else: |
| 925 |
if(gut.get_spy().was_called(inst, method_name, parameters)): |
| 926 |
if(parameters != null): |
| 927 |
disp += str(' with parameters ', parameters) |
| 928 |
_fail(str(disp, "\n", _get_desc_of_calls_to_instance(inst))) |
| 929 |
else: |
| 930 |
_pass(disp) |
| 931 |
|
| 932 |
# ------------------------------------------------------------------------------ |
| 933 |
# Assert that a method on an instance of a doubled class was called a number |
| 934 |
# of times. If parameters are specified then only calls with matching |
| 935 |
# parameter values will be counted. |
| 936 |
# ------------------------------------------------------------------------------ |
| 937 |
func assert_call_count(inst, method_name, expected_count, parameters=null): |
| 938 |
var count = gut.get_spy().call_count(inst, method_name, parameters) |
| 939 |
|
| 940 |
if(_fail_if_parameters_not_array(parameters)): |
| 941 |
return |
| 942 |
|
| 943 |
var param_text = '' |
| 944 |
if(parameters): |
| 945 |
param_text = ' with parameters ' + str(parameters) |
| 946 |
var disp = 'Expected [%s] on %s to be called [%s] times%s. It was called [%s] times.' |
| 947 |
disp = disp % [method_name, _str(inst), expected_count, param_text, count] |
| 948 |
|
| 949 |
if(!_utils.is_double(inst)): |
| 950 |
_fail('You must pass a doubled instance to assert_call_count. Check the wiki for info on using double.') |
| 951 |
else: |
| 952 |
if(count == expected_count): |
| 953 |
_pass(disp) |
| 954 |
else: |
| 955 |
_fail(str(disp, "\n", _get_desc_of_calls_to_instance(inst))) |
| 956 |
|
| 957 |
# ------------------------------------------------------------------------------ |
| 958 |
# Asserts the passed in value is null |
| 959 |
# ------------------------------------------------------------------------------ |
| 960 |
func assert_null(got, text=''): |
| 961 |
var disp = str('Expected [', _str(got), '] to be NULL: ', text) |
| 962 |
if(got == null): |
| 963 |
_pass(disp) |
| 964 |
else: |
| 965 |
_fail(disp) |
| 966 |
|
| 967 |
# ------------------------------------------------------------------------------ |
| 968 |
# Asserts the passed in value is null |
| 969 |
# ------------------------------------------------------------------------------ |
| 970 |
func assert_not_null(got, text=''): |
| 971 |
var disp = str('Expected [', _str(got), '] to be anything but NULL: ', text) |
| 972 |
if(got == null): |
| 973 |
_fail(disp) |
| 974 |
else: |
| 975 |
_pass(disp) |
| 976 |
|
| 977 |
# ----------------------------------------------------------------------------- |
| 978 |
# Asserts object has been freed from memory |
| 979 |
# We pass in a title (since if it is freed, we lost all identity data) |
| 980 |
# ----------------------------------------------------------------------------- |
| 981 |
func assert_freed(obj, title): |
| 982 |
var disp = title |
| 983 |
if(is_instance_valid(obj)): |
| 984 |
disp = _strutils.type2str(obj) + title |
| 985 |
assert_true(not is_instance_valid(obj), "Expected [%s] to be freed" % disp) |
| 986 |
|
| 987 |
# ------------------------------------------------------------------------------ |
| 988 |
# Asserts Object has not been freed from memory |
| 989 |
# ----------------------------------------------------------------------------- |
| 990 |
func assert_not_freed(obj, title): |
| 991 |
var disp = title |
| 992 |
if(is_instance_valid(obj)): |
| 993 |
disp = _strutils.type2str(obj) + title |
| 994 |
assert_true(is_instance_valid(obj), "Expected [%s] to not be freed" % disp) |
| 995 |
|
| 996 |
# ------------------------------------------------------------------------------ |
| 997 |
# Asserts that the current test has not introduced any new orphans. This only |
| 998 |
# applies to the test code that preceedes a call to this method so it should be |
| 999 |
# the last thing your test does. |
| 1000 |
# ------------------------------------------------------------------------------ |
| 1001 |
func assert_no_new_orphans(text=''): |
| 1002 |
var count = gut.get_orphan_counter().get_counter('test') |
| 1003 |
var msg = '' |
| 1004 |
if(text != ''): |
| 1005 |
msg = ': ' + text |
| 1006 |
# Note that get_counter will return -1 if the counter does not exist. This |
| 1007 |
# can happen with a misplaced assert_no_new_orphans. Checking for > 0 |
| 1008 |
# ensures this will not cause some weird failure. |
| 1009 |
if(count > 0): |
| 1010 |
_fail(str('Expected no orphans, but found ', count, msg)) |
| 1011 |
else: |
| 1012 |
_pass('No new orphans found.' + msg) |
| 1013 |
|
| 1014 |
# ------------------------------------------------------------------------------ |
| 1015 |
# Returns a dictionary that contains |
| 1016 |
# - an is_valid flag whether validation was successful or not and |
| 1017 |
# - a message that gives some information about the validation errors. |
| 1018 |
# ------------------------------------------------------------------------------ |
| 1019 |
func _validate_assert_setget_called_input(type, name_property |
| 1020 |
, name_setter, name_getter): |
| 1021 |
var obj = null |
| 1022 |
var result = {"is_valid": true, "msg": ""} |
| 1023 |
|
| 1024 |
if null == type or typeof(type) != TYPE_OBJECT or not type.is_class("Resource"): |
| 1025 |
result.is_valid = false |
| 1026 |
result.msg = str("The type parameter should be a ressource, input is ", _str(type)) |
| 1027 |
return result |
| 1028 |
|
| 1029 |
if null == double(type): |
| 1030 |
result.is_valid = false |
| 1031 |
result.msg = str("Attempt to double the type parameter failed. The type parameter should be a ressource that can be doubled.") |
| 1032 |
return result |
| 1033 |
|
| 1034 |
obj = _create_obj_from_type(type) |
| 1035 |
var property = _find_object_property(obj, str(name_property)) |
| 1036 |
|
| 1037 |
if null == property: |
| 1038 |
result.is_valid = false |
| 1039 |
result.msg += str("The property %s does not exist." % _str(name_property)) |
| 1040 |
if name_setter == "" and name_getter == "": |
| 1041 |
result.is_valid = false |
| 1042 |
result.msg += str("Either setter or getter method must be specified.") |
| 1043 |
if name_setter != "" and not obj.has_method(str(name_setter)): |
| 1044 |
result.is_valid = false |
| 1045 |
result.msg += str("Setter method %s does not exist. " % _str(name_setter)) |
| 1046 |
if name_getter != "" and not obj.has_method(str(name_getter)): |
| 1047 |
result.is_valid = false |
| 1048 |
result.msg += str("Getter method %s does not exist. " %_str(name_getter)) |
| 1049 |
|
| 1050 |
obj.free() |
| 1051 |
return result |
| 1052 |
|
| 1053 |
# ------------------------------------------------------------------------------ |
| 1054 |
# Asserts the given setter and getter methods are called when the given property |
| 1055 |
# is accessed. |
| 1056 |
# ------------------------------------------------------------------------------ |
| 1057 |
func _assert_setget_called(type, name_property, setter = "", getter = ""): |
| 1058 |
var name_setter = _utils.nvl(setter, "") |
| 1059 |
var name_getter = _utils.nvl(getter, "") |
| 1060 |
|
| 1061 |
var validation = _validate_assert_setget_called_input(type, name_property, str(name_setter), str(name_getter)) |
| 1062 |
if not validation.is_valid: |
| 1063 |
_fail(validation.msg) |
| 1064 |
return |
| 1065 |
|
| 1066 |
var message = "" |
| 1067 |
var amount_calls_setter = 0 |
| 1068 |
var amount_calls_getter = 0 |
| 1069 |
var expected_calls_setter = 0 |
| 1070 |
var expected_calls_getter = 0 |
| 1071 |
var obj = _create_obj_from_type(double(type)) |
| 1072 |
|
| 1073 |
if name_setter != '': |
| 1074 |
expected_calls_setter = 1 |
| 1075 |
stub(obj, name_setter).to_do_nothing() |
| 1076 |
obj.set(name_property, null) |
| 1077 |
amount_calls_setter = gut.get_spy().call_count(obj, str(name_setter)) |
| 1078 |
|
| 1079 |
if name_getter != '': |
| 1080 |
expected_calls_getter = 1 |
| 1081 |
stub(obj, name_getter).to_do_nothing() |
| 1082 |
var new_property = obj.get(name_property) |
| 1083 |
amount_calls_getter = gut.get_spy().call_count(obj, str(name_getter)) |
| 1084 |
|
| 1085 |
obj.free() |
| 1086 |
|
| 1087 |
# assert |
| 1088 |
|
| 1089 |
if amount_calls_setter == expected_calls_setter and amount_calls_getter == expected_calls_getter: |
| 1090 |
_pass(str("setget for %s is correctly configured." % _str(name_property))) |
| 1091 |
else: |
| 1092 |
if amount_calls_setter < expected_calls_setter: |
| 1093 |
message += " The setter was not called." |
| 1094 |
elif amount_calls_setter > expected_calls_setter: |
| 1095 |
message += " The setter was called but should not have been." |
| 1096 |
if amount_calls_getter < expected_calls_getter: |
| 1097 |
message += " The getter was not called." |
| 1098 |
elif amount_calls_getter > expected_calls_getter: |
| 1099 |
message += " The getter was called but should not have been." |
| 1100 |
_fail(str(message)) |
| 1101 |
|
| 1102 |
# ------------------------------------------------------------------------------ |
| 1103 |
# Wrapper: invokes assert_setget_called but provides a slightly more convenient |
| 1104 |
# signature |
| 1105 |
# ------------------------------------------------------------------------------ |
| 1106 |
func assert_setget( |
| 1107 |
instance, name_property, |
| 1108 |
const_or_setter = DEFAULT_SETTER_GETTER, getter="__not_set__"): |
| 1109 |
|
| 1110 |
var getter_name = null |
| 1111 |
if(getter != "__not_set__"): |
| 1112 |
getter_name = getter |
| 1113 |
|
| 1114 |
var setter_name = null |
| 1115 |
if(typeof(const_or_setter) == TYPE_INT): |
| 1116 |
if(const_or_setter in [SETTER_ONLY, DEFAULT_SETTER_GETTER]): |
| 1117 |
setter_name = str("set_", name_property) |
| 1118 |
|
| 1119 |
if(const_or_setter in [GETTER_ONLY, DEFAULT_SETTER_GETTER]): |
| 1120 |
getter_name = str("get_", name_property) |
| 1121 |
else: |
| 1122 |
setter_name = const_or_setter |
| 1123 |
|
| 1124 |
var resource = null |
| 1125 |
if instance.is_class("Resource"): |
| 1126 |
resource = instance |
| 1127 |
else: |
| 1128 |
resource = _get_type_from_obj(instance) |
| 1129 |
|
| 1130 |
_assert_setget_called(resource, str(name_property), setter_name, getter_name) |
| 1131 |
|
| 1132 |
|
| 1133 |
# ------------------------------------------------------------------------------ |
| 1134 |
# Wrapper: asserts if the property exists, the accessor methods exist and the |
| 1135 |
# setget keyword is set for accessor methods |
| 1136 |
# ------------------------------------------------------------------------------ |
| 1137 |
func assert_property(instance, name_property, default_value, new_value) -> void: |
| 1138 |
var free_me = [] |
| 1139 |
var resource = null |
| 1140 |
var obj = null |
| 1141 |
if instance.is_class("Resource"): |
| 1142 |
resource = instance |
| 1143 |
obj = _create_obj_from_type(resource) |
| 1144 |
free_me.append(obj) |
| 1145 |
else: |
| 1146 |
resource = _get_type_from_obj(instance) |
| 1147 |
obj = instance |
| 1148 |
|
| 1149 |
var name_setter = "set_" + str(name_property) |
| 1150 |
var name_getter = "get_" + str(name_property) |
| 1151 |
|
| 1152 |
var pre_fail_count = get_fail_count() |
| 1153 |
assert_accessors(obj, str(name_property), default_value, new_value) |
| 1154 |
_assert_setget_called(resource, str(name_property), name_setter, name_getter) |
| 1155 |
|
| 1156 |
for entry in free_me: |
| 1157 |
entry.free() |
| 1158 |
|
| 1159 |
# assert |
| 1160 |
if get_fail_count() == pre_fail_count: |
| 1161 |
_pass(str("The property is set up as expected.")) |
| 1162 |
else: |
| 1163 |
_fail(str("The property is not set up as expected. Examine subtests to see what failed.")) |
| 1164 |
|
| 1165 |
|
| 1166 |
# ------------------------------------------------------------------------------ |
| 1167 |
# Mark the current test as pending. |
| 1168 |
# ------------------------------------------------------------------------------ |
| 1169 |
func pending(text=""): |
| 1170 |
_summary.pending += 1 |
| 1171 |
if(gut): |
| 1172 |
_lgr.pending(text) |
| 1173 |
gut._pending(text) |
| 1174 |
|
| 1175 |
# ------------------------------------------------------------------------------ |
| 1176 |
# Returns the number of times a signal was emitted. -1 returned if the object |
| 1177 |
# is not being watched. |
| 1178 |
# ------------------------------------------------------------------------------ |
| 1179 |
|
| 1180 |
# ------------------------------------------------------------------------------ |
| 1181 |
# Yield for the time sent in. The optional message will be printed when |
| 1182 |
# Gut detects the yield. When the time expires the YIELD signal will be |
| 1183 |
# emitted. |
| 1184 |
# ------------------------------------------------------------------------------ |
| 1185 |
func yield_for(time, msg=''): |
| 1186 |
return gut.set_yield_time(time, msg) |
| 1187 |
|
| 1188 |
# ------------------------------------------------------------------------------ |
| 1189 |
# Yield to a signal or a maximum amount of time, whichever comes first. When |
| 1190 |
# the conditions are met the YIELD signal will be emitted. |
| 1191 |
# ------------------------------------------------------------------------------ |
| 1192 |
func yield_to(obj, signal_name, max_wait, msg=''): |
| 1193 |
watch_signals(obj) |
| 1194 |
gut.set_yield_signal_or_time(obj, signal_name, max_wait, msg) |
| 1195 |
|
| 1196 |
return gut |
| 1197 |
|
| 1198 |
# ------------------------------------------------------------------------------ |
| 1199 |
# Ends a test that had a yield in it. You only need to use this if you do |
| 1200 |
# not make assertions after a yield. |
| 1201 |
# ------------------------------------------------------------------------------ |
| 1202 |
func end_test(): |
| 1203 |
_lgr.deprecated('end_test is no longer necessary, you can remove it.') |
| 1204 |
#gut.end_yielded_test() |
| 1205 |
|
| 1206 |
func get_summary(): |
| 1207 |
return _summary |
| 1208 |
|
| 1209 |
func get_fail_count(): |
| 1210 |
return _summary.failed |
| 1211 |
|
| 1212 |
func get_pass_count(): |
| 1213 |
return _summary.passed |
| 1214 |
|
| 1215 |
func get_pending_count(): |
| 1216 |
return _summary.pending |
| 1217 |
|
| 1218 |
func get_assert_count(): |
| 1219 |
return _summary.asserts |
| 1220 |
|
| 1221 |
func clear_signal_watcher(): |
| 1222 |
_signal_watcher.clear() |
| 1223 |
|
| 1224 |
func get_double_strategy(): |
| 1225 |
return gut.get_doubler().get_strategy() |
| 1226 |
|
| 1227 |
func set_double_strategy(double_strategy): |
| 1228 |
gut.get_doubler().set_strategy(double_strategy) |
| 1229 |
|
| 1230 |
func pause_before_teardown(): |
| 1231 |
gut.pause_before_teardown() |
| 1232 |
|
| 1233 |
# ------------------------------------------------------------------------------ |
| 1234 |
# Convert the _summary dictionary into text |
| 1235 |
# ------------------------------------------------------------------------------ |
| 1236 |
func get_summary_text(): |
| 1237 |
var to_return = get_script().get_path() + "\n" |
| 1238 |
to_return += str(' ', _summary.passed, ' of ', _summary.asserts, ' passed.') |
| 1239 |
if(_summary.pending > 0): |
| 1240 |
to_return += str("\n ", _summary.pending, ' pending') |
| 1241 |
if(_summary.failed > 0): |
| 1242 |
to_return += str("\n ", _summary.failed, ' failed.') |
| 1243 |
return to_return |
| 1244 |
|
| 1245 |
# ------------------------------------------------------------------------------ |
| 1246 |
# Double a script, inner class, or scene using a path or a loaded script/scene. |
| 1247 |
# |
| 1248 |
# |
| 1249 |
# ------------------------------------------------------------------------------ |
| 1250 |
|
| 1251 |
# ------------------------------------------------------------------------------ |
| 1252 |
# ------------------------------------------------------------------------------ |
| 1253 |
func _smart_double(double_info): |
| 1254 |
var override_strat = _utils.nvl(double_info.strategy, gut.get_doubler().get_strategy()) |
| 1255 |
var to_return = null |
| 1256 |
|
| 1257 |
if(double_info.is_scene()): |
| 1258 |
if(double_info.make_partial): |
| 1259 |
to_return = gut.get_doubler().partial_double_scene(double_info.path, override_strat) |
| 1260 |
else: |
| 1261 |
to_return = gut.get_doubler().double_scene(double_info.path, override_strat) |
| 1262 |
|
| 1263 |
elif(double_info.is_native()): |
| 1264 |
if(double_info.make_partial): |
| 1265 |
to_return = gut.get_doubler().partial_double_gdnative(double_info.path) |
| 1266 |
else: |
| 1267 |
to_return = gut.get_doubler().double_gdnative(double_info.path) |
| 1268 |
|
| 1269 |
elif(double_info.is_script()): |
| 1270 |
if(double_info.subpath == null): |
| 1271 |
if(double_info.make_partial): |
| 1272 |
to_return = gut.get_doubler().partial_double(double_info.path, override_strat) |
| 1273 |
else: |
| 1274 |
to_return = gut.get_doubler().double(double_info.path, override_strat) |
| 1275 |
else: |
| 1276 |
if(double_info.make_partial): |
| 1277 |
to_return = gut.get_doubler().partial_double_inner(double_info.path, double_info.subpath, override_strat) |
| 1278 |
else: |
| 1279 |
to_return = gut.get_doubler().double_inner(double_info.path, double_info.subpath, override_strat) |
| 1280 |
return to_return |
| 1281 |
|
| 1282 |
# ------------------------------------------------------------------------------ |
| 1283 |
# ------------------------------------------------------------------------------ |
| 1284 |
func double(thing, p2=null, p3=null): |
| 1285 |
var double_info = DoubleInfo.new(thing, p2, p3) |
| 1286 |
if(!double_info.is_valid): |
| 1287 |
_lgr.error('double requires a class or path, you passed an instance: ' + _str(thing)) |
| 1288 |
return null |
| 1289 |
|
| 1290 |
double_info.make_partial = false |
| 1291 |
|
| 1292 |
return _smart_double(double_info) |
| 1293 |
|
| 1294 |
# ------------------------------------------------------------------------------ |
| 1295 |
# ------------------------------------------------------------------------------ |
| 1296 |
func partial_double(thing, p2=null, p3=null): |
| 1297 |
var double_info = DoubleInfo.new(thing, p2, p3) |
| 1298 |
if(!double_info.is_valid): |
| 1299 |
_lgr.error('partial_double requires a class or path, you passed an instance: ' + _str(thing)) |
| 1300 |
return null |
| 1301 |
|
| 1302 |
double_info.make_partial = true |
| 1303 |
|
| 1304 |
return _smart_double(double_info) |
| 1305 |
|
| 1306 |
|
| 1307 |
# ------------------------------------------------------------------------------ |
| 1308 |
# Specifically double a scene |
| 1309 |
# ------------------------------------------------------------------------------ |
| 1310 |
func double_scene(path, strategy=null): |
| 1311 |
var override_strat = _utils.nvl(strategy, gut.get_doubler().get_strategy()) |
| 1312 |
return gut.get_doubler().double_scene(path, override_strat) |
| 1313 |
|
| 1314 |
# ------------------------------------------------------------------------------ |
| 1315 |
# Specifically double a script |
| 1316 |
# ------------------------------------------------------------------------------ |
| 1317 |
func double_script(path, strategy=null): |
| 1318 |
var override_strat = _utils.nvl(strategy, gut.get_doubler().get_strategy()) |
| 1319 |
return gut.get_doubler().double(path, override_strat) |
| 1320 |
|
| 1321 |
# ------------------------------------------------------------------------------ |
| 1322 |
# Specifically double an Inner class in a a script |
| 1323 |
# ------------------------------------------------------------------------------ |
| 1324 |
func double_inner(path, subpath, strategy=null): |
| 1325 |
var override_strat = _utils.nvl(strategy, gut.get_doubler().get_strategy()) |
| 1326 |
return gut.get_doubler().double_inner(path, subpath, override_strat) |
| 1327 |
|
| 1328 |
# ------------------------------------------------------------------------------ |
| 1329 |
# Add a method that the doubler will ignore. You can pass this the path to a |
| 1330 |
# script or scene or a loaded script or scene. When running tests, these |
| 1331 |
# ignores are cleared after every test. |
| 1332 |
# ------------------------------------------------------------------------------ |
| 1333 |
func ignore_method_when_doubling(thing, method_name): |
| 1334 |
var double_info = DoubleInfo.new(thing) |
| 1335 |
var path = double_info.path |
| 1336 |
|
| 1337 |
if(double_info.is_scene()): |
| 1338 |
var inst = thing.instance() |
| 1339 |
if(inst.get_script()): |
| 1340 |
path = inst.get_script().get_path() |
| 1341 |
|
| 1342 |
gut.get_doubler().add_ignored_method(path, method_name) |
| 1343 |
|
| 1344 |
# ------------------------------------------------------------------------------ |
| 1345 |
# Stub something. |
| 1346 |
# |
| 1347 |
# Parameters |
| 1348 |
# 1: the thing to stub, a file path or a instance or a class |
| 1349 |
# 2: either an inner class subpath or the method name |
| 1350 |
# 3: the method name if an inner class subpath was specified |
| 1351 |
# NOTE: right now we cannot stub inner classes at the path level so this should |
| 1352 |
# only be called with two parameters. I did the work though so I'm going |
| 1353 |
# to leave it but not update the wiki. |
| 1354 |
# ------------------------------------------------------------------------------ |
| 1355 |
func stub(thing, p2, p3=null): |
| 1356 |
if(_utils.is_instance(thing) and !_utils.is_double(thing)): |
| 1357 |
_lgr.error(str('You cannot use stub on ', _str(thing), ' because it is not a double.')) |
| 1358 |
return _utils.StubParams.new() |
| 1359 |
|
| 1360 |
var method_name = p2 |
| 1361 |
var subpath = null |
| 1362 |
if(p3 != null): |
| 1363 |
subpath = p2 |
| 1364 |
method_name = p3 |
| 1365 |
|
| 1366 |
var sp = _utils.StubParams.new(thing, method_name, subpath) |
| 1367 |
gut.get_stubber().add_stub(sp) |
| 1368 |
return sp |
| 1369 |
|
| 1370 |
# ------------------------------------------------------------------------------ |
| 1371 |
# convenience wrapper. |
| 1372 |
# ------------------------------------------------------------------------------ |
| 1373 |
func simulate(obj, times, delta): |
| 1374 |
gut.simulate(obj, times, delta) |
| 1375 |
|
| 1376 |
# ------------------------------------------------------------------------------ |
| 1377 |
# Replace the node at base_node.get_node(path) with with_this. All references |
| 1378 |
# to the node via $ and get_node(...) will now return with_this. with_this will |
| 1379 |
# get all the groups that the node that was replaced had. |
| 1380 |
# |
| 1381 |
# The node that was replaced is queued to be freed. |
| 1382 |
# |
| 1383 |
# TODO see replace_by method, this could simplify the logic here. |
| 1384 |
# ------------------------------------------------------------------------------ |
| 1385 |
func replace_node(base_node, path_or_node, with_this): |
| 1386 |
var path = path_or_node |
| 1387 |
|
| 1388 |
if(typeof(path_or_node) != TYPE_STRING): |
| 1389 |
# This will cause an engine error if it fails. It always returns a |
| 1390 |
# NodePath, even if it fails. Checking the name count is the only way |
| 1391 |
# I found to check if it found something or not (after it worked I |
| 1392 |
# didn't look any farther). |
| 1393 |
path = base_node.get_path_to(path_or_node) |
| 1394 |
if(path.get_name_count() == 0): |
| 1395 |
_lgr.error('You passed an object that base_node does not have. Cannot replace node.') |
| 1396 |
return |
| 1397 |
|
| 1398 |
if(!base_node.has_node(path)): |
| 1399 |
_lgr.error(str('Could not find node at path [', path, ']')) |
| 1400 |
return |
| 1401 |
|
| 1402 |
var to_replace = base_node.get_node(path) |
| 1403 |
var parent = to_replace.get_parent() |
| 1404 |
var replace_name = to_replace.get_name() |
| 1405 |
|
| 1406 |
parent.remove_child(to_replace) |
| 1407 |
parent.add_child(with_this) |
| 1408 |
with_this.set_name(replace_name) |
| 1409 |
with_this.set_owner(parent) |
| 1410 |
|
| 1411 |
var groups = to_replace.get_groups() |
| 1412 |
for i in range(groups.size()): |
| 1413 |
with_this.add_to_group(groups[i]) |
| 1414 |
|
| 1415 |
to_replace.queue_free() |
| 1416 |
|
| 1417 |
|
| 1418 |
# ------------------------------------------------------------------------------ |
| 1419 |
# This method does a somewhat complicated dance with Gut. It assumes that Gut |
| 1420 |
# will clear its parameter handler after it finishes calling a parameterized test |
| 1421 |
# enough times. |
| 1422 |
# ------------------------------------------------------------------------------ |
| 1423 |
func use_parameters(params): |
| 1424 |
var ph = gut.get_parameter_handler() |
| 1425 |
if(ph == null): |
| 1426 |
ph = _utils.ParameterHandler.new(params) |
| 1427 |
gut.set_parameter_handler(ph) |
| 1428 |
|
| 1429 |
var output = str('(call #', ph.get_call_count() + 1, ') with paramters: ', ph.get_current_parameters()) |
| 1430 |
_lgr.log(output) |
| 1431 |
_lgr.inc_indent() |
| 1432 |
return ph.next_parameters() |
| 1433 |
|
| 1434 |
# ------------------------------------------------------------------------------ |
| 1435 |
# Marks whatever is passed in to be freed after the test finishes. It also |
| 1436 |
# returns what is passed in so you can save a line of code. |
| 1437 |
# var thing = autofree(Thing.new()) |
| 1438 |
# ------------------------------------------------------------------------------ |
| 1439 |
func autofree(thing): |
| 1440 |
gut.get_autofree().add_free(thing) |
| 1441 |
return thing |
| 1442 |
|
| 1443 |
# ------------------------------------------------------------------------------ |
| 1444 |
# Works the same as autofree except queue_free will be called on the object |
| 1445 |
# instead. This also imparts a brief pause after the test finishes so that |
| 1446 |
# the queued object has time to free. |
| 1447 |
# ------------------------------------------------------------------------------ |
| 1448 |
func autoqfree(thing): |
| 1449 |
gut.get_autofree().add_queue_free(thing) |
| 1450 |
return thing |
| 1451 |
|
| 1452 |
# ------------------------------------------------------------------------------ |
| 1453 |
# The same as autofree but it also adds the object as a child of the test. |
| 1454 |
# ------------------------------------------------------------------------------ |
| 1455 |
func add_child_autofree(node, legible_unique_name = false): |
| 1456 |
gut.get_autofree().add_free(node) |
| 1457 |
# Explicitly calling super here b/c add_child MIGHT change and I don't want |
| 1458 |
# a bug sneaking its way in here. |
| 1459 |
.add_child(node, legible_unique_name) |
| 1460 |
return node |
| 1461 |
|
| 1462 |
# ------------------------------------------------------------------------------ |
| 1463 |
# The same as autoqfree but it also adds the object as a child of the test. |
| 1464 |
# ------------------------------------------------------------------------------ |
| 1465 |
func add_child_autoqfree(node, legible_unique_name=false): |
| 1466 |
gut.get_autofree().add_queue_free(node) |
| 1467 |
# Explicitly calling super here b/c add_child MIGHT change and I don't want |
| 1468 |
# a bug sneaking its way in here. |
| 1469 |
.add_child(node, legible_unique_name) |
| 1470 |
return node |
| 1471 |
|
| 1472 |
# ------------------------------------------------------------------------------ |
| 1473 |
# Returns true if the test is passing as of the time of this call. False if not. |
| 1474 |
# ------------------------------------------------------------------------------ |
| 1475 |
func is_passing(): |
| 1476 |
if(gut.get_current_test_object() != null and |
| 1477 |
!['before_all', 'after_all'].has(gut.get_current_test_object().name)): |
| 1478 |
return gut.get_current_test_object().passed and \ |
| 1479 |
gut.get_current_test_object().assert_count > 0 |
| 1480 |
else: |
| 1481 |
_lgr.error('No current test object found. is_passing must be called inside a test.') |
| 1482 |
return null |
| 1483 |
|
| 1484 |
# ------------------------------------------------------------------------------ |
| 1485 |
# Returns true if the test is failing as of the time of this call. False if not. |
| 1486 |
# ------------------------------------------------------------------------------ |
| 1487 |
func is_failing(): |
| 1488 |
if(gut.get_current_test_object() != null and |
| 1489 |
!['before_all', 'after_all'].has(gut.get_current_test_object().name)): |
| 1490 |
return !gut.get_current_test_object().passed |
| 1491 |
else: |
| 1492 |
_lgr.error('No current test object found. is_failing must be called inside a test.') |
| 1493 |
return null |
| 1494 |
|
| 1495 |
# ------------------------------------------------------------------------------ |
| 1496 |
# Marks the test as passing. Does not override any failing asserts or calls to |
| 1497 |
# fail_test. Same as a passing assert. |
| 1498 |
# ------------------------------------------------------------------------------ |
| 1499 |
func pass_test(text): |
| 1500 |
_pass(text) |
| 1501 |
|
| 1502 |
# ------------------------------------------------------------------------------ |
| 1503 |
# Marks the test as failing. Same as a failing assert. |
| 1504 |
# ------------------------------------------------------------------------------ |
| 1505 |
func fail_test(text): |
| 1506 |
_fail(text) |
| 1507 |
|
| 1508 |
# ------------------------------------------------------------------------------ |
| 1509 |
# Peforms a deep compare on both values, a CompareResult instnace is returned. |
| 1510 |
# The optional max_differences paramter sets the max_differences to be displayed. |
| 1511 |
# ------------------------------------------------------------------------------ |
| 1512 |
func compare_deep(v1, v2, max_differences=null): |
| 1513 |
var result = _compare.deep(v1, v2) |
| 1514 |
if(max_differences != null): |
| 1515 |
result.max_differences = max_differences |
| 1516 |
return result |
| 1517 |
|
| 1518 |
# ------------------------------------------------------------------------------ |
| 1519 |
# Peforms a shallow compare on both values, a CompareResult instnace is returned. |
| 1520 |
# The optional max_differences paramter sets the max_differences to be displayed. |
| 1521 |
# ------------------------------------------------------------------------------ |
| 1522 |
func compare_shallow(v1, v2, max_differences=null): |
| 1523 |
var result = _compare.shallow(v1, v2) |
| 1524 |
if(max_differences != null): |
| 1525 |
result.max_differences = max_differences |
| 1526 |
return result |
| 1527 |
|
| 1528 |
# ------------------------------------------------------------------------------ |
| 1529 |
# Performs a deep compare and asserts the values are equal |
| 1530 |
# ------------------------------------------------------------------------------ |
| 1531 |
func assert_eq_deep(v1, v2): |
| 1532 |
var result = compare_deep(v1, v2) |
| 1533 |
if(result.are_equal): |
| 1534 |
_pass(result.get_short_summary()) |
| 1535 |
else: |
| 1536 |
_fail(result.summary) |
| 1537 |
|
| 1538 |
# ------------------------------------------------------------------------------ |
| 1539 |
# Performs a deep compare and asserts the values are not equal |
| 1540 |
# ------------------------------------------------------------------------------ |
| 1541 |
func assert_ne_deep(v1, v2): |
| 1542 |
var result = compare_deep(v1, v2) |
| 1543 |
if(!result.are_equal): |
| 1544 |
_pass(result.get_short_summary()) |
| 1545 |
else: |
| 1546 |
_fail(result.get_short_summary()) |
| 1547 |
|
| 1548 |
# ------------------------------------------------------------------------------ |
| 1549 |
# Performs a shallow compare and asserts the values are equal |
| 1550 |
# ------------------------------------------------------------------------------ |
| 1551 |
func assert_eq_shallow(v1, v2): |
| 1552 |
var result = compare_shallow(v1, v2) |
| 1553 |
if(result.are_equal): |
| 1554 |
_pass(result.get_short_summary()) |
| 1555 |
else: |
| 1556 |
_fail(result.summary) |
| 1557 |
|
| 1558 |
# ------------------------------------------------------------------------------ |
| 1559 |
# Performs a shallow compare and asserts the values are not equal |
| 1560 |
# ------------------------------------------------------------------------------ |
| 1561 |
func assert_ne_shallow(v1, v2): |
| 1562 |
var result = compare_shallow(v1, v2) |
| 1563 |
if(!result.are_equal): |
| 1564 |
_pass(result.get_short_summary()) |
| 1565 |
else: |
| 1566 |
_fail(result.get_short_summary()) |