godot-xterm/addons/gut/stub_params.gd

149 lines
3.7 KiB
GDScript
Raw Normal View History

2023-01-20 23:34:39 +01:00
var _utils = load("res://addons/gut/utils.gd").get_instance()
var _lgr = _utils.get_logger()
var return_val = null
var stub_target = null
# the parameter values to match method call on.
var parameters = null
var stub_method = null
var call_super = false
# Whether this is a stub for default parameter values as they are defined in
# the script, and not an overridden default value.
var is_script_default = false
# -- Paramter Override --
# Parmater overrides are stored in here along with all the other stub info
# so that you can chain stubbing parameter overrides along with all the
# other stubbing. This adds some complexity to the logic that tries to
# find the correct stub for a call by a double. Since an instance of this
# class could be just a parameter override, or it could have been chained
# we have to have _paramter_override_only so that we know when to tell the
# difference.
var parameter_count = -1
var parameter_defaults = null
# Anything that would make this stub not just an override of paramters
# must set this flag to false. This must be private bc the actual logic
# to determine if this stub is only an override is more complicated.
var _parameter_override_only = true
# --
2023-01-20 23:34:39 +01:00
const NOT_SET = "|_1_this_is_not_set_1_|"
2023-01-20 23:34:39 +01:00
func _init(target = null, method = null, subpath = null):
stub_target = target
stub_method = method
2023-01-20 23:34:39 +01:00
if typeof(target) == TYPE_STRING:
if target.is_absolute_path():
stub_target = load(str(target))
else:
2023-01-20 23:34:39 +01:00
_lgr.warn(str(target, " is not a valid path"))
2023-01-20 23:34:39 +01:00
if stub_target is PackedScene:
2024-01-06 11:27:15 +01:00
stub_target = GutUtils.get_scene_script_object(stub_target)
# this is used internally to stub default parameters for everything that is
# doubled...or something. Look for stub_defaults_from_meta for usage. This
# behavior is not to be used by end users.
2023-01-20 23:34:39 +01:00
if typeof(method) == TYPE_DICTIONARY:
_load_defaults_from_metadata(method)
2023-01-20 23:34:39 +01:00
func _load_defaults_from_metadata(meta):
stub_method = meta.name
var values = meta.default_args.duplicate()
2023-01-20 23:34:39 +01:00
while values.size() < meta.args.size():
values.push_front(null)
param_defaults(values)
2023-01-20 23:34:39 +01:00
func to_return(val):
2023-01-20 23:34:39 +01:00
if stub_method == "_init":
_lgr.error("You cannot stub _init to do nothing. Super's _init is always called.")
else:
return_val = val
call_super = false
_parameter_override_only = false
return self
func to_do_nothing():
to_return(null)
return self
func to_call_super():
2024-01-06 11:27:15 +01:00
call_super = true
_parameter_override_only = false
return self
2023-01-20 23:34:39 +01:00
func when_passed(
p1 = NOT_SET,
p2 = NOT_SET,
p3 = NOT_SET,
p4 = NOT_SET,
p5 = NOT_SET,
p6 = NOT_SET,
p7 = NOT_SET,
p8 = NOT_SET,
p9 = NOT_SET,
p10 = NOT_SET
):
parameters = [p1, p2, p3, p4, p5, p6, p7, p8, p9, p10]
var idx = 0
2023-01-20 23:34:39 +01:00
while idx < parameters.size():
if str(parameters[idx]) == NOT_SET:
2022-11-09 21:57:46 +01:00
parameters.remove_at(idx)
else:
idx += 1
return self
func param_count(x):
parameter_count = x
return self
func param_defaults(values):
parameter_count = values.size()
parameter_defaults = values
return self
func has_param_override():
return parameter_count != -1
func is_param_override_only():
var to_return = false
2023-01-20 23:34:39 +01:00
if has_param_override():
to_return = _parameter_override_only
return to_return
func to_s():
2023-01-20 23:34:39 +01:00
var base_string = str(stub_target, ".", stub_method)
2023-01-20 23:34:39 +01:00
if has_param_override():
base_string += str(
" (param count override=", parameter_count, " defaults=", parameter_defaults
)
if is_param_override_only():
base_string += " ONLY"
2023-01-20 23:34:39 +01:00
if is_script_default:
base_string += " script default"
2023-01-20 23:34:39 +01:00
base_string += ") "
2023-01-20 23:34:39 +01:00
if call_super:
base_string += " to call SUPER"
2023-01-20 23:34:39 +01:00
if parameters != null:
base_string += str(" with params (", parameters, ") returns ", return_val)
else:
2023-01-20 23:34:39 +01:00
base_string += str(" returns ", return_val)
return base_string