Files
MusicPlayer/tests/fakes/autoload_snapshot.gd
T

28 lines
1.0 KiB
GDScript

class_name AutoloadSnapshot
extends RefCounted
## Saves and restores the script variables of autoloads such as Settings and Playlist,
## so a test that changes global state does not leak into the next test.
var _values: Dictionary = {}
## Records the current values of every script variable on [param autoloads].
static func take(autoloads: Array[Node]) -> AutoloadSnapshot:
var snapshot: AutoloadSnapshot = AutoloadSnapshot.new()
for autoload: Node in autoloads:
var values: Dictionary = {}
for property: Dictionary in autoload.get_property_list():
if property.usage & PROPERTY_USAGE_SCRIPT_VARIABLE:
var value: Variant = autoload.get(property.name)
values[property.name] = value.duplicate() if value is Array else value
snapshot._values[autoload] = values
return snapshot
## Puts back the values recorded by [method take].
func restore() -> void:
for autoload: Node in _values:
var values: Dictionary = _values[autoload]
for property_name: String in values:
autoload.set(property_name, values[property_name])