ChocolateBird/test/unit_tests.gd

116 lines
3.7 KiB
GDScript3
Raw Permalink Normal View History

extends Control
var dir_user := Directory.new()
const P_TESTDATA := 'user://test_data/'
const FILENAME_TEST_SAVE := 'res://test.srm'
2023-08-03 21:02:41 +09:30
# Shared state between tests
var save_slot_buffers = []
var save_slot_dicts = []
func test(label, input):
if input:
print('SUCCESS: ' + label)
else:
print('FAILURE: ' + label)
func load_snes_savefile(filename: String = FILENAME_TEST_SAVE):
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
for i in 4:
2023-08-03 21:02:41 +09:30
self.save_slot_buffers.append(SaveLoader.get_save_slot(save_file, i))
self.save_slot_dicts.append(SaveLoader.deserialize_save_slot(self.save_slot_buffers[i]))
print('Loaded test save file')
func generate_known_good_results():
2023-08-03 21:02:41 +09:30
load_snes_savefile()
if not self.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'
2023-08-03 21:02:41 +09:30
match Common.save_json(filename, self.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:
2023-08-03 21:02:41 +09:30
load_snes_savefile()
if not self.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')
2023-08-03 21:02:41 +09:30
return Common.are_arrays_equal(self.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
func test_save_slot_serialization() -> bool:
2023-08-03 21:02:41 +09:30
if not self.save_slot_dicts:
print_debug('test savefile not loaded')
return false
for i in 4:
var bytes = SaveLoader.serialize_save_slot(self.save_slot_dicts[i]).data_array
2023-08-03 21:02:41 +09:30
if bytes != self.save_slot_buffers[i].data_array:
print_debug('Slot %d failed to serialize correctly, rescanning' % i)
for j in 0x700:
var b1: int = bytes[j]
var b2: int = self.save_slot_buffers[i].data_array[j]
if b1 != b2:
print_debug('Mismatch occurs at byte $%04X (%d): $%02X (%d) vs $%02X (%d)' % [j, j, b1, b1, b2, b2])
2023-08-03 21:02:41 +09:30
return false
return true
func test_snes_save_serialization() -> bool:
if not self.save_slot_dicts:
print_debug('test savefile not loaded')
return false
var bytes := SaveLoader.make_snes_save_file(self.save_slot_dicts)
var file := File.new()
match file.open(FILENAME_TEST_SAVE, File.READ):
OK:
var orig_bytes := file.get_buffer(file.get_len())
if orig_bytes != bytes:
print_debug('SNES Save File failed to serialize correctly, rescanning')
for j in 0x2000:
var b1: int = bytes[j]
var b2: int = orig_bytes[j]
if b1 != b2:
print_debug('Mismatch occurs at byte $%04X (%d): $%02X (%d) vs $%02X (%d)' % [j, j, b1, b1, b2, b2])
return false
var error:
print_debug('Failed to open test.srm for reading: %d' % error)
return false
return true
2023-08-03 21:02:41 +09:30
# 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')
2023-08-03 17:30:41 +09:30
# generate_known_good_results() # Uncomment this to get your sample on first run
2023-08-03 21:02:41 +09:30
test('SNES save file loaded to array of dictionaries', test_save_loading())
test('SNES save file slots serialized from dictionaries', test_save_slot_serialization())
test('SNES save file serialized to original source bytes', test_snes_save_serialization())
get_tree().quit()