Debian Codegraph @

godot/addons/gut/parameter_factory.gd

raw
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 # This is the home for all parameter creation helpers. These functions should
30 # all return an array of values to be used as parameters for parameterized
31 # tests.
32 # ##############################################################################
33
34 # ------------------------------------------------------------------------------
35 # Creates an array of dictionaries. It pairs up the names array with each set
36 # of values in values. If more names than values are specified then the missing
37 # values will be filled with nulls. If more values than names are specified
38 # those values will be ignored.
39 #
40 # Example:
41 # create_named_parameters(['a', 'b'], [[1, 2], ['one', 'two']]) returns
42 # [{a:1, b:2}, {a:'one', b:'two'}]
43 #
44 # This allows you to increase readability of your parameterized tests:
45 # var params = create_named_parameters(['a', 'b'], [[1, 2], ['one', 'two']])
46 # func test_foo(p = use_parameters(params)):
47 # assert_eq(p.a, p.b)
48 #
49 # Parameters:
50 # names: an array of names to be used as keys in the dictionaries
51 # values: an array of arrays of values.
52 # ------------------------------------------------------------------------------
53 static func named_parameters(names, values):
54 var named = []
55 for i in range(values.size()):
56 var entry = {}
57
58 var parray = values[i]
59 if(typeof(parray) != TYPE_ARRAY):
60 parray = [values[i]]
61
62 for j in range(names.size()):
63 if(j >= parray.size()):
64 entry[names[j]] = null
65 else:
66 entry[names[j]] = parray[j]
67 named.append(entry)
68
69 return named
70
71 # Additional Helper Ideas
72 # * File. IDK what it would look like. csv maybe.
73 # * Random values within a range?
74 # * All int values in a range or add an optioanal step.
75 # *