ChocolateBird/test/unit_tests.gd

69 lines
2.0 KiB
GDScript

extends Control
var dir_user := Directory.new()
const P_TESTDATA := 'user://test_data/'
func load_snes_savefile(filename: String = 'res://test.srm'):
var save_file := File.new()
match save_file.open(filename, File.READ):
OK:
pass
var error:
print_debug('Failed to open test.srm for reading: %d' % error)
return
var save_slots = []
var save_slot_dicts = []
for i in 4:
save_slots.append(SaveLoader.get_save_slot(save_file, i))
save_slot_dicts.append(SaveLoader.get_struct(save_slots[i], 'Save_slot'))
print('Loaded test save file')
return save_slot_dicts
func generate_known_good_results():
var save_slot_dicts = load_snes_savefile()
if not save_slot_dicts:
return
match dir_user.make_dir_recursive(P_TESTDATA):
OK:
pass
var error:
print_debug('Failed to create "%s" with error code %d' % [P_TESTDATA, error])
return
var filename := P_TESTDATA + 'test.srm.json'
match Common.save_json(filename, save_slot_dicts):
OK:
pass
var error:
print_debug('Failed to save "%s" with error code %d' % [filename, error])
return
func test_save_loading() -> bool:
var save_slot_dicts = load_snes_savefile()
if not save_slot_dicts:
print_debug('Failed to load test savefile')
return false
var filename := P_TESTDATA + 'test.srm.json'
var known_good = Common.load_json(filename)
match typeof(known_good):
TYPE_ARRAY:
print_debug('Comparing known savefile results')
return Common.are_arrays_equal(save_slot_dicts, known_good)
TYPE_DICTIONARY:
print_debug('Known savefile results "%s" is a dict instead of an array. Did we change formats?' % filename)
return false
_:
print_debug('Failed to load known savefile results "%s"' % filename)
return false
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
match dir_user.open('user://'):
OK:
pass
var error:
print_debug('Failed to open user directory')
generate_known_good_results()
print(test_save_loading())
get_tree().quit()