Debian Codegraph @

godot/addons/gut/stubber.gd

raw
1 # {
2 # inst_id_or_path1:{
3 # method_name1: [StubParams, StubParams],
4 # method_name2: [StubParams, StubParams]
5 # },
6 # inst_id_or_path2:{
7 # method_name1: [StubParams, StubParams],
8 # method_name2: [StubParams, StubParams]
9 # }
10 # }
11 var returns = {}
12 var _utils = load('res://addons/gut/utils.gd').get_instance()
13 var _lgr = _utils.get_logger()
14 var _strutils = _utils.Strutils.new()
15
16 func _make_key_from_metadata(doubled):
17 var to_return = doubled.__gut_metadata_.path
18 if(doubled.__gut_metadata_.subpath != ''):
19 to_return += str('-', doubled.__gut_metadata_.subpath)
20 return to_return
21
22 # Creates they key for the returns hash based on the type of object passed in
23 # obj could be a string of a path to a script with an optional subpath or
24 # it could be an instance of a doubled object.
25 func _make_key_from_variant(obj, subpath=null):
26 var to_return = null
27
28 match typeof(obj):
29 TYPE_STRING:
30 # this has to match what is done in _make_key_from_metadata
31 to_return = obj
32 if(subpath != null and subpath != ''):
33 to_return += str('-', subpath)
34 TYPE_OBJECT:
35 if(_utils.is_instance(obj)):
36 to_return = _make_key_from_metadata(obj)
37 elif(_utils.is_native_class(obj)):
38 to_return = _utils.get_native_class_name(obj)
39 else:
40 to_return = obj.resource_path
41 return to_return
42
43 func _add_obj_method(obj, method, subpath=null):
44 var key = _make_key_from_variant(obj, subpath)
45 if(_utils.is_instance(obj)):
46 key = obj
47
48 if(!returns.has(key)):
49 returns[key] = {}
50 if(!returns[key].has(method)):
51 returns[key][method] = []
52
53 return key
54
55 # ##############
56 # Public
57 # ##############
58
59 # TODO: This method is only used in tests and should be refactored out. It
60 # does not support inner classes and isn't helpful.
61 func set_return(obj, method, value, parameters=null):
62 var key = _add_obj_method(obj, method)
63 var sp = _utils.StubParams.new(key, method)
64 sp.parameters = parameters
65 sp.return_val = value
66 returns[key][method].append(sp)
67
68 func add_stub(stub_params):
69 if(stub_params.stub_method == '_init'):
70 _lgr.error("You cannot stub _init. Super's _init is ALWAYS called.")
71 else:
72 var key = _add_obj_method(stub_params.stub_target, stub_params.stub_method, stub_params.target_subpath)
73 returns[key][stub_params.stub_method].append(stub_params)
74
75 # Searches returns for an entry that matches the instance or the class that
76 # passed in obj is.
77 #
78 # obj can be an instance, class, or a path.
79 func _find_stub(obj, method, parameters=null):
80 var key = _make_key_from_variant(obj)
81 var to_return = null
82
83 if(_utils.is_instance(obj)):
84 if(returns.has(obj) and returns[obj].has(method)):
85 key = obj
86 elif(obj.get('__gut_metadata_')):
87 key = _make_key_from_metadata(obj)
88
89 if(returns.has(key) and returns[key].has(method)):
90 var param_idx = -1
91 var null_idx = -1
92
93 for i in range(returns[key][method].size()):
94 if(returns[key][method][i].parameters == parameters):
95 param_idx = i
96 if(returns[key][method][i].parameters == null):
97 null_idx = i
98
99 # We have matching parameter values so return the stub value for that
100 if(param_idx != -1):
101 to_return = returns[key][method][param_idx]
102 # We found a case where the parameters were not specified so return
103 # parameters for that
104 elif(null_idx != -1):
105 to_return = returns[key][method][null_idx]
106 else:
107 _lgr.warn(str('Call to [', method, '] was not stubbed for the supplied parameters ', parameters, '. Null was returned.'))
108
109 return to_return
110
111 # Gets a stubbed return value for the object and method passed in. If the
112 # instance was stubbed it will use that, otherwise it will use the path and
113 # subpath of the object to try to find a value.
114 #
115 # It will also use the optional list of parameter values to find a value. If
116 # the object was stubbed with no parameters than any parameters will match.
117 # If it was stubbed with specific parameter values then it will try to match.
118 # If the parameters do not match BUT there was also an empty parameter list stub
119 # then it will return those.
120 # If it cannot find anything that matches then null is returned.for
121 #
122 # Parameters
123 # obj: this should be an instance of a doubled object.
124 # method: the method called
125 # parameters: optional array of parameter vales to find a return value for.
126 func get_return(obj, method, parameters=null):
127 var stub_info = _find_stub(obj, method, parameters)
128
129 if(stub_info != null):
130 return stub_info.return_val
131 else:
132 return null
133
134 func should_call_super(obj, method, parameters=null):
135 var stub_info = _find_stub(obj, method, parameters)
136 if(stub_info != null):
137 return stub_info.call_super
138 else:
139 # this log message is here because of how the generated doubled scripts
140 # are structured. With this log msg here, you will only see one
141 # "unstubbed" info instead of multiple.
142 _lgr.info('Unstubbed call to ' + method + '::' + _strutils.type2str(obj))
143 return false
144
145
146 func clear():
147 returns.clear()
148
149 func get_logger():
150 return _lgr
151
152 func set_logger(logger):
153 _lgr = logger
154
155 func to_s():
156 var text = ''
157 for thing in returns:
158 text += str(thing) + "\n"
159 for method in returns[thing]:
160 text += str("\t", method, "\n")
161 for i in range(returns[thing][method].size()):
162 text += "\t\t" + returns[thing][method][i].to_s() + "\n"
163 return text