Debian Codegraph @

godot/addons/gut/comparator.gd

raw
1 var _utils = load('res://addons/gut/utils.gd').get_instance()
2 var _strutils = _utils.Strutils.new()
3 var _max_length = 100
4 var _should_compare_int_to_float = true
5
6 const MISSING = '|__missing__gut__compare__value__|'
7 const DICTIONARY_DISCLAIMER = 'Dictionaries are compared-by-ref. See assert_eq in wiki.'
8
9 func _cannot_comapre_text(v1, v2):
10 return str('Cannot compare ', _strutils.types[typeof(v1)], ' with ',
11 _strutils.types[typeof(v2)], '.')
12
13 func _make_missing_string(text):
14 return '<missing ' + text + '>'
15
16 func _create_missing_result(v1, v2, text):
17 var to_return = null
18 var v1_str = format_value(v1)
19 var v2_str = format_value(v2)
20
21 if(typeof(v1) == TYPE_STRING and v1 == MISSING):
22 v1_str = _make_missing_string(text)
23 to_return = _utils.CompareResult.new()
24 elif(typeof(v2) == TYPE_STRING and v2 == MISSING):
25 v2_str = _make_missing_string(text)
26 to_return = _utils.CompareResult.new()
27
28 if(to_return != null):
29 to_return.summary = str(v1_str, ' != ', v2_str)
30 to_return.are_equal = false
31
32 return to_return
33
34
35 func simple(v1, v2, missing_string=''):
36 var missing_result = _create_missing_result(v1, v2, missing_string)
37 if(missing_result != null):
38 return missing_result
39
40 var result = _utils.CompareResult.new()
41 var cmp_str = null
42 var extra = ''
43
44 if(_should_compare_int_to_float and [2, 3].has(typeof(v1)) and [2, 3].has(typeof(v2))):
45 result.are_equal = v1 == v2
46
47 elif(_utils.are_datatypes_same(v1, v2)):
48 result.are_equal = v1 == v2
49 if(typeof(v1) == TYPE_DICTIONARY):
50 if(result.are_equal):
51 extra = '. Same dictionary ref. '
52 else:
53 extra = '. Different dictionary refs. '
54 extra += DICTIONARY_DISCLAIMER
55
56 if(typeof(v1) == TYPE_ARRAY):
57 var array_result = _utils.DiffTool.new(v1, v2, _utils.DIFF.SHALLOW)
58 result.summary = array_result.get_short_summary()
59 if(!array_result.are_equal()):
60 extra = ".\n" + array_result.get_short_summary()
61
62 else:
63 cmp_str = '!='
64 result.are_equal = false
65 extra = str('. ', _cannot_comapre_text(v1, v2))
66
67 cmp_str = get_compare_symbol(result.are_equal)
68 if(typeof(v1) != TYPE_ARRAY):
69 result.summary = str(format_value(v1), ' ', cmp_str, ' ', format_value(v2), extra)
70
71 return result
72
73
74 func shallow(v1, v2):
75 var result = null
76
77 if(_utils.are_datatypes_same(v1, v2)):
78 if(typeof(v1) in [TYPE_ARRAY, TYPE_DICTIONARY]):
79 result = _utils.DiffTool.new(v1, v2, _utils.DIFF.SHALLOW)
80 else:
81 result = simple(v1, v2)
82 else:
83 result = simple(v1, v2)
84
85 return result
86
87
88 func deep(v1, v2):
89 var result = null
90
91 if(_utils.are_datatypes_same(v1, v2)):
92 if(typeof(v1) in [TYPE_ARRAY, TYPE_DICTIONARY]):
93 result = _utils.DiffTool.new(v1, v2, _utils.DIFF.DEEP)
94 else:
95 result = simple(v1, v2)
96 else:
97 result = simple(v1, v2)
98
99 return result
100
101
102 func format_value(val, max_val_length=_max_length):
103 return _strutils.truncate_string(_strutils.type2str(val), max_val_length)
104
105
106 func compare(v1, v2, diff_type=_utils.DIFF.SIMPLE):
107 var result = null
108 if(diff_type == _utils.DIFF.SIMPLE):
109 result = simple(v1, v2)
110 elif(diff_type == _utils.DIFF.SHALLOW):
111 result = shallow(v1, v2)
112 elif(diff_type == _utils.DIFF.DEEP):
113 result = deep(v1, v2)
114
115 return result
116
117
118 func get_should_compare_int_to_float():
119 return _should_compare_int_to_float
120
121
122 func set_should_compare_int_to_float(should_compare_int_float):
123 _should_compare_int_to_float = should_compare_int_float
124
125
126 func get_compare_symbol(is_equal):
127 if(is_equal):
128 return '=='
129 else:
130 return '!='