| 1 |
extends 'res://addons/gut/compare_result.gd' |
| 2 |
const INDENT = ' ' |
| 3 |
enum { |
| 4 |
DEEP, |
| 5 |
SHALLOW, |
| 6 |
SIMPLE |
| 7 |
} |
| 8 |
|
| 9 |
var _utils = load('res://addons/gut/utils.gd').get_instance() |
| 10 |
var _strutils = _utils.Strutils.new() |
| 11 |
var _compare = _utils.Comparator.new() |
| 12 |
var DiffTool = load('res://addons/gut/diff_tool.gd') |
| 13 |
|
| 14 |
var _value_1 = null |
| 15 |
var _value_2 = null |
| 16 |
var _total_count = 0 |
| 17 |
var _diff_type = null |
| 18 |
var _brackets = null |
| 19 |
var _valid = true |
| 20 |
var _desc_things = 'somethings' |
| 21 |
|
| 22 |
# -------- comapre_result.gd "interface" --------------------- |
| 23 |
func set_are_equal(val): |
| 24 |
_block_set('are_equal', val) |
| 25 |
|
| 26 |
func get_are_equal(): |
| 27 |
return are_equal() |
| 28 |
|
| 29 |
func set_summary(val): |
| 30 |
_block_set('summary', val) |
| 31 |
|
| 32 |
func get_summary(): |
| 33 |
return summarize() |
| 34 |
|
| 35 |
func get_different_count(): |
| 36 |
return differences.size() |
| 37 |
|
| 38 |
func get_total_count(): |
| 39 |
return _total_count |
| 40 |
|
| 41 |
func get_short_summary(): |
| 42 |
var text = str(_strutils.truncate_string(str(_value_1), 50), |
| 43 |
' ', _compare.get_compare_symbol(are_equal()), ' ', |
| 44 |
_strutils.truncate_string(str(_value_2), 50)) |
| 45 |
if(!are_equal()): |
| 46 |
text += str(' ', get_different_count(), ' of ', get_total_count(), |
| 47 |
' ', _desc_things, ' do not match.') |
| 48 |
return text |
| 49 |
|
| 50 |
func get_brackets(): |
| 51 |
return _brackets |
| 52 |
# -------- comapre_result.gd "interface" --------------------- |
| 53 |
|
| 54 |
|
| 55 |
func _invalidate(): |
| 56 |
_valid = false |
| 57 |
differences = null |
| 58 |
|
| 59 |
|
| 60 |
func _init(v1, v2, diff_type=DEEP): |
| 61 |
_value_1 = v1 |
| 62 |
_value_2 = v2 |
| 63 |
_diff_type = diff_type |
| 64 |
_compare.set_should_compare_int_to_float(false) |
| 65 |
_find_differences(_value_1, _value_2) |
| 66 |
|
| 67 |
|
| 68 |
func _find_differences(v1, v2): |
| 69 |
if(_utils.are_datatypes_same(v1, v2)): |
| 70 |
if(typeof(v1) == TYPE_ARRAY): |
| 71 |
_brackets = {'open':'[', 'close':']'} |
| 72 |
_desc_things = 'indexes' |
| 73 |
_diff_array(v1, v2) |
| 74 |
elif(typeof(v2) == TYPE_DICTIONARY): |
| 75 |
_brackets = {'open':'{', 'close':'}'} |
| 76 |
_desc_things = 'keys' |
| 77 |
_diff_dictionary(v1, v2) |
| 78 |
else: |
| 79 |
_invalidate() |
| 80 |
_utils.get_logger().error('Only Arrays and Dictionaries are supported.') |
| 81 |
else: |
| 82 |
_invalidate() |
| 83 |
_utils.get_logger().error('Only Arrays and Dictionaries are supported.') |
| 84 |
|
| 85 |
|
| 86 |
func _diff_array(a1, a2): |
| 87 |
_total_count = max(a1.size(), a2.size()) |
| 88 |
for i in range(a1.size()): |
| 89 |
var result = null |
| 90 |
if(i < a2.size()): |
| 91 |
if(_diff_type == DEEP): |
| 92 |
result = _compare.deep(a1[i], a2[i]) |
| 93 |
else: |
| 94 |
result = _compare.simple(a1[i], a2[i]) |
| 95 |
else: |
| 96 |
result = _compare.simple(a1[i], _compare.MISSING, 'index') |
| 97 |
|
| 98 |
if(!result.are_equal): |
| 99 |
differences[i] = result |
| 100 |
|
| 101 |
if(a1.size() < a2.size()): |
| 102 |
for i in range(a1.size(), a2.size()): |
| 103 |
differences[i] = _compare.simple(_compare.MISSING, a2[i], 'index') |
| 104 |
|
| 105 |
|
| 106 |
func _diff_dictionary(d1, d2): |
| 107 |
var d1_keys = d1.keys() |
| 108 |
var d2_keys = d2.keys() |
| 109 |
|
| 110 |
# Process all the keys in d1 |
| 111 |
_total_count += d1_keys.size() |
| 112 |
for key in d1_keys: |
| 113 |
if(!d2.has(key)): |
| 114 |
differences[key] = _compare.simple(d1[key], _compare.MISSING, 'key') |
| 115 |
else: |
| 116 |
d2_keys.remove(d2_keys.find(key)) |
| 117 |
|
| 118 |
var result = null |
| 119 |
if(_diff_type == DEEP): |
| 120 |
result = _compare.deep(d1[key], d2[key]) |
| 121 |
else: |
| 122 |
result = _compare.simple(d1[key], d2[key]) |
| 123 |
|
| 124 |
if(!result.are_equal): |
| 125 |
differences[key] = result |
| 126 |
|
| 127 |
# Process all the keys in d2 that didn't exist in d1 |
| 128 |
_total_count += d2_keys.size() |
| 129 |
for i in range(d2_keys.size()): |
| 130 |
differences[d2_keys[i]] = _compare.simple(_compare.MISSING, d2[d2_keys[i]], 'key') |
| 131 |
|
| 132 |
|
| 133 |
func summarize(): |
| 134 |
var summary = '' |
| 135 |
|
| 136 |
if(are_equal()): |
| 137 |
summary = get_short_summary() |
| 138 |
else: |
| 139 |
var formatter = load('res://addons/gut/diff_formatter.gd').new() |
| 140 |
formatter.set_max_to_display(max_differences) |
| 141 |
summary = formatter.make_it(self) |
| 142 |
|
| 143 |
return summary |
| 144 |
|
| 145 |
|
| 146 |
func are_equal(): |
| 147 |
if(!_valid): |
| 148 |
return null |
| 149 |
else: |
| 150 |
return differences.size() == 0 |
| 151 |
|
| 152 |
|
| 153 |
func get_diff_type(): |
| 154 |
return _diff_type |
| 155 |
|
| 156 |
|
| 157 |
func get_value_1(): |
| 158 |
return _value_1 |
| 159 |
|
| 160 |
|
| 161 |
func get_value_2(): |
| 162 |
return _value_2 |