2023-01-07 20:26:17 +01:00
|
|
|
var _utils = load('res://addons/gut/utils.gd').get_instance()
|
2022-06-02 04:13:43 +02:00
|
|
|
var _strutils = _utils.Strutils.new()
|
|
|
|
var _max_length = 100
|
|
|
|
var _should_compare_int_to_float = true
|
|
|
|
|
2023-01-07 20:26:17 +01:00
|
|
|
const MISSING = '|__missing__gut__compare__value__|'
|
|
|
|
const DICTIONARY_DISCLAIMER = 'Dictionaries are compared-by-ref. See assert_eq in wiki.'
|
2022-06-02 04:13:43 +02:00
|
|
|
|
2023-01-07 20:26:17 +01:00
|
|
|
func _cannot_compare_text(v1, v2):
|
|
|
|
return str('Cannot compare ', _strutils.types[typeof(v1)], ' with ',
|
|
|
|
_strutils.types[typeof(v2)], '.')
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
func _make_missing_string(text):
|
2023-01-07 20:26:17 +01:00
|
|
|
return '<missing ' + text + '>'
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
func _create_missing_result(v1, v2, text):
|
|
|
|
var to_return = null
|
|
|
|
var v1_str = format_value(v1)
|
|
|
|
var v2_str = format_value(v2)
|
|
|
|
|
2023-01-07 20:26:17 +01:00
|
|
|
if(typeof(v1) == TYPE_STRING and v1 == MISSING):
|
2022-06-02 04:13:43 +02:00
|
|
|
v1_str = _make_missing_string(text)
|
|
|
|
to_return = _utils.CompareResult.new()
|
2023-01-07 20:26:17 +01:00
|
|
|
elif(typeof(v2) == TYPE_STRING and v2 == MISSING):
|
2022-06-02 04:13:43 +02:00
|
|
|
v2_str = _make_missing_string(text)
|
|
|
|
to_return = _utils.CompareResult.new()
|
|
|
|
|
2023-01-07 20:26:17 +01:00
|
|
|
if(to_return != null):
|
|
|
|
to_return.summary = str(v1_str, ' != ', v2_str)
|
2022-06-02 04:13:43 +02:00
|
|
|
to_return.are_equal = false
|
|
|
|
|
|
|
|
return to_return
|
|
|
|
|
|
|
|
|
2023-01-07 20:26:17 +01:00
|
|
|
func simple(v1, v2, missing_string=''):
|
2022-06-02 04:13:43 +02:00
|
|
|
var missing_result = _create_missing_result(v1, v2, missing_string)
|
2023-01-07 20:26:17 +01:00
|
|
|
if(missing_result != null):
|
2022-06-02 04:13:43 +02:00
|
|
|
return missing_result
|
|
|
|
|
|
|
|
var result = _utils.CompareResult.new()
|
|
|
|
var cmp_str = null
|
2023-01-07 20:26:17 +01:00
|
|
|
var extra = ''
|
2022-06-02 04:13:43 +02:00
|
|
|
|
2023-01-07 20:26:17 +01:00
|
|
|
var tv1 = typeof(v1)
|
|
|
|
var tv2 = typeof(v2)
|
2022-06-02 04:13:43 +02:00
|
|
|
|
2023-01-07 20:26:17 +01:00
|
|
|
# print(tv1, '::', tv2, ' ', _strutils.types[tv1], '::', _strutils.types[tv2])
|
|
|
|
if(_should_compare_int_to_float and [TYPE_INT, TYPE_FLOAT].has(tv1) and [TYPE_INT, TYPE_FLOAT].has(tv2)):
|
2022-06-02 04:13:43 +02:00
|
|
|
result.are_equal = v1 == v2
|
2023-01-07 20:26:17 +01:00
|
|
|
elif([TYPE_STRING, TYPE_STRING_NAME].has(tv1) and [TYPE_STRING, TYPE_STRING_NAME].has(tv2)):
|
|
|
|
result.are_equal = v1 == v2
|
|
|
|
elif(_utils.are_datatypes_same(v1, v2)):
|
|
|
|
result.are_equal = v1 == v2
|
|
|
|
if(typeof(v1) == TYPE_DICTIONARY):
|
|
|
|
if(result.are_equal):
|
|
|
|
extra = '. Same dictionary ref. '
|
2022-06-02 04:13:43 +02:00
|
|
|
else:
|
2023-01-07 20:26:17 +01:00
|
|
|
extra = '. Different dictionary refs. '
|
2022-06-02 04:13:43 +02:00
|
|
|
extra += DICTIONARY_DISCLAIMER
|
|
|
|
|
2023-01-07 20:26:17 +01:00
|
|
|
if(typeof(v1) == TYPE_ARRAY):
|
2022-06-02 04:13:43 +02:00
|
|
|
var array_result = _utils.DiffTool.new(v1, v2, _utils.DIFF.SHALLOW)
|
|
|
|
result.summary = array_result.get_short_summary()
|
2023-01-07 20:26:17 +01:00
|
|
|
if(!array_result.are_equal):
|
2022-06-02 04:13:43 +02:00
|
|
|
extra = ".\n" + array_result.get_short_summary()
|
|
|
|
|
|
|
|
else:
|
2023-01-07 20:26:17 +01:00
|
|
|
cmp_str = '!='
|
2022-06-02 04:13:43 +02:00
|
|
|
result.are_equal = false
|
2023-01-07 20:26:17 +01:00
|
|
|
extra = str('. ', _cannot_compare_text(v1, v2))
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
cmp_str = get_compare_symbol(result.are_equal)
|
2023-01-07 20:26:17 +01:00
|
|
|
result.summary = str(format_value(v1), ' ', cmp_str, ' ', format_value(v2), extra)
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
func shallow(v1, v2):
|
2023-01-07 20:26:17 +01:00
|
|
|
var result = null
|
2022-06-02 04:13:43 +02:00
|
|
|
|
2023-01-07 20:26:17 +01:00
|
|
|
if(_utils.are_datatypes_same(v1, v2)):
|
|
|
|
if(typeof(v1) in [TYPE_ARRAY, TYPE_DICTIONARY]):
|
2022-06-02 04:13:43 +02:00
|
|
|
result = _utils.DiffTool.new(v1, v2, _utils.DIFF.SHALLOW)
|
|
|
|
else:
|
|
|
|
result = simple(v1, v2)
|
|
|
|
else:
|
|
|
|
result = simple(v1, v2)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
func deep(v1, v2):
|
2023-01-07 20:26:17 +01:00
|
|
|
var result = null
|
2022-06-02 04:13:43 +02:00
|
|
|
|
2023-01-07 20:26:17 +01:00
|
|
|
if(_utils.are_datatypes_same(v1, v2)):
|
|
|
|
if(typeof(v1) in [TYPE_ARRAY, TYPE_DICTIONARY]):
|
2022-06-02 04:13:43 +02:00
|
|
|
result = _utils.DiffTool.new(v1, v2, _utils.DIFF.DEEP)
|
|
|
|
else:
|
|
|
|
result = simple(v1, v2)
|
|
|
|
else:
|
|
|
|
result = simple(v1, v2)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2023-01-07 20:26:17 +01:00
|
|
|
func format_value(val, max_val_length=_max_length):
|
2022-06-02 04:13:43 +02:00
|
|
|
return _strutils.truncate_string(_strutils.type2str(val), max_val_length)
|
|
|
|
|
|
|
|
|
2023-01-07 20:26:17 +01:00
|
|
|
func compare(v1, v2, diff_type=_utils.DIFF.SIMPLE):
|
2022-06-02 04:13:43 +02:00
|
|
|
var result = null
|
2023-01-07 20:26:17 +01:00
|
|
|
if(diff_type == _utils.DIFF.SIMPLE):
|
2022-06-02 04:13:43 +02:00
|
|
|
result = simple(v1, v2)
|
2023-01-07 20:26:17 +01:00
|
|
|
elif(diff_type == _utils.DIFF.SHALLOW):
|
2022-06-02 04:13:43 +02:00
|
|
|
result = shallow(v1, v2)
|
2023-01-07 20:26:17 +01:00
|
|
|
elif(diff_type == _utils.DIFF.DEEP):
|
2022-06-02 04:13:43 +02:00
|
|
|
result = deep(v1, v2)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
func get_should_compare_int_to_float():
|
|
|
|
return _should_compare_int_to_float
|
|
|
|
|
|
|
|
|
|
|
|
func set_should_compare_int_to_float(should_compare_int_float):
|
|
|
|
_should_compare_int_to_float = should_compare_int_float
|
|
|
|
|
|
|
|
|
|
|
|
func get_compare_symbol(is_equal):
|
2023-01-07 20:26:17 +01:00
|
|
|
if(is_equal):
|
|
|
|
return '=='
|
2022-06-02 04:13:43 +02:00
|
|
|
else:
|
2023-01-07 20:26:17 +01:00
|
|
|
return '!='
|