sunder/addons/gut/double_tools.gd

83 lines
2.4 KiB
GDScript3
Raw Permalink Normal View History

2026-02-17 23:36:57 -05:00
var thepath = ''
var subpath = ''
2026-02-19 03:14:22 -05:00
var singleton_name = null
2026-02-17 23:36:57 -05:00
var is_partial = null
2026-02-19 03:14:22 -05:00
var double_ref : WeakRef = null
var stubber_ref : WeakRef = null
var spy_ref : WeakRef = null
var gut_ref : WeakRef = null
var singleton_ref : WeakRef = null
var __gutdbl_values = {}
2026-02-17 23:36:57 -05:00
const NO_DEFAULT_VALUE = '!__gut__no__default__value__!'
2026-02-19 03:14:22 -05:00
func _init(double = null):
if(double != null):
var values = double.__gutdbl_values
__gutdbl_values = double.__gutdbl_values
double_ref = weakref(double)
2026-02-17 23:36:57 -05:00
thepath = values.thepath
subpath = values.subpath
2026-02-19 03:14:22 -05:00
stubber_ref = weakref_from_id(values.stubber)
spy_ref = weakref_from_id(values.spy)
gut_ref = weakref_from_id(values.gut)
singleton_ref = weakref_from_id(values.singleton)
singleton_name = values.singleton_name
2026-02-17 23:36:57 -05:00
is_partial = values.is_partial
2026-02-19 03:14:22 -05:00
if(gut_ref.get_ref() != null):
gut_ref.get_ref().get_autofree().add_free(double_ref.get_ref())
2026-02-17 23:36:57 -05:00
func _get_stubbed_method_to_call(method_name, called_with):
2026-02-19 03:14:22 -05:00
var method = stubber_ref.get_ref().get_call_this(double_ref.get_ref(), method_name, called_with)
2026-02-17 23:36:57 -05:00
if(method != null):
method = method.bindv(called_with)
return method
return method
2026-02-19 03:14:22 -05:00
func weakref_from_id(inst_id):
2026-02-17 23:36:57 -05:00
if(inst_id == -1):
2026-02-19 03:14:22 -05:00
return weakref(null)
2026-02-17 23:36:57 -05:00
else:
2026-02-19 03:14:22 -05:00
return weakref(instance_from_id(inst_id))
2026-02-17 23:36:57 -05:00
func is_stubbed_to_call_super(method_name, called_with):
2026-02-19 03:14:22 -05:00
if(stubber_ref.get_ref() != null):
return stubber_ref.get_ref().should_call_super(double_ref.get_ref(), method_name, called_with)
2026-02-17 23:36:57 -05:00
else:
return false
func handle_other_stubs(method_name, called_with):
2026-02-19 03:14:22 -05:00
if(stubber_ref.get_ref() == null):
2026-02-17 23:36:57 -05:00
return
var method = _get_stubbed_method_to_call(method_name, called_with)
if(method != null):
return await method.call()
else:
2026-02-19 03:14:22 -05:00
return stubber_ref.get_ref().get_return(double_ref.get_ref(), method_name, called_with)
2026-02-17 23:36:57 -05:00
func spy_on(method_name, called_with):
2026-02-19 03:14:22 -05:00
if(spy_ref.get_ref() != null):
spy_ref.get_ref().add_call(double_ref.get_ref(), method_name, called_with)
2026-02-17 23:36:57 -05:00
2026-02-19 03:14:22 -05:00
func default_val(method_name, p_index):
if(stubber_ref.get_ref() == null):
2026-02-17 23:36:57 -05:00
return null
2026-02-19 03:14:22 -05:00
else:
var result = stubber_ref.get_ref().get_default_value(double_ref.get_ref(), method_name, p_index)
return result
2026-02-17 23:36:57 -05:00
2026-02-19 03:14:22 -05:00
func get_singleton():
var to_return = singleton_ref.get_ref()
if(to_return == null):
push_error("Trying to get a singleton reference on a non-singleton double: ",
__gutdbl_values.singleton_name, "/", __gutdbl_values.singleton)
return to_return