The quiz card's settings are saved to quiz_settings.cfg in OS.get_config_dir() on every change and loaded at start. Debug builds use their own file, and MUSICPLAYER_CONFIG_DIR moves the file elsewhere so tests never touch it. Count plays in statistics and Prefer less played soundtracks are not saved and move from the quiz card to the Settings popup.
98 lines
3.0 KiB
GDScript
98 lines
3.0 KiB
GDScript
extends GdUnitTestSuite
|
|
## Saving the quiz settings to a file in the config folder and loading them back.
|
|
|
|
const SAVED_VALUES: Dictionary = {
|
|
&"auto_repeat_song": false, &"stop_after_current": false, &"hide_next_track": false,
|
|
&"hide_beginning": true, &"hide_for_seconds": 7.0, &"hide_length": true,
|
|
&"hide_ticks": true, &"winning_score": 13, &"inspiration_list_speed": 4,
|
|
}
|
|
|
|
var _snapshot: AutoloadSnapshot
|
|
var _config_dir_before: String
|
|
var _config_dir: String
|
|
|
|
|
|
func before_test() -> void:
|
|
_snapshot = AutoloadSnapshot.take([Settings])
|
|
_config_dir_before = OS.get_environment(Settings.CONFIG_DIR_ENV)
|
|
_config_dir = create_temp_dir("quiz_settings_file")
|
|
OS.set_environment(Settings.CONFIG_DIR_ENV, _config_dir)
|
|
DirAccess.remove_absolute(Settings.quiz_settings_path())
|
|
|
|
|
|
func after_test() -> void:
|
|
OS.set_environment(Settings.CONFIG_DIR_ENV, _config_dir_before)
|
|
_snapshot.restore()
|
|
|
|
|
|
func _set_settings(values: Dictionary) -> void:
|
|
for setting: StringName in values:
|
|
Settings.set(setting, values[setting])
|
|
|
|
|
|
func test_saved_settings_load_back() -> void:
|
|
_set_settings(SAVED_VALUES)
|
|
Settings.save_quiz_settings()
|
|
_snapshot.restore()
|
|
|
|
Settings.load_quiz_settings()
|
|
|
|
for setting: StringName in SAVED_VALUES:
|
|
assert_that(Settings.get(setting)).override_failure_message(setting) \
|
|
.is_equal(SAVED_VALUES[setting])
|
|
|
|
|
|
func test_every_quiz_setting_is_saved() -> void:
|
|
assert_array(Settings.QUIZ_SETTINGS).contains_exactly_in_any_order(SAVED_VALUES.keys())
|
|
|
|
|
|
func test_missing_file_keeps_the_current_settings() -> void:
|
|
Settings.winning_score = 9
|
|
|
|
Settings.load_quiz_settings()
|
|
|
|
assert_int(Settings.winning_score).is_equal(9)
|
|
|
|
|
|
func test_value_of_the_wrong_type_is_ignored() -> void:
|
|
var file: ConfigFile = ConfigFile.new()
|
|
file.set_value("quiz", "winning_score", "lots")
|
|
file.set_value("quiz", "hide_ticks", true)
|
|
DirAccess.make_dir_recursive_absolute(Settings.quiz_settings_path().get_base_dir())
|
|
file.save(Settings.quiz_settings_path())
|
|
Settings.winning_score = 9
|
|
Settings.hide_ticks = false
|
|
|
|
Settings.load_quiz_settings()
|
|
|
|
assert_int(Settings.winning_score).is_equal(9)
|
|
assert_bool(Settings.hide_ticks).is_true()
|
|
|
|
|
|
func test_statistics_settings_are_not_saved() -> void:
|
|
Settings.save_quiz_settings()
|
|
|
|
var file: ConfigFile = ConfigFile.new()
|
|
file.load(Settings.quiz_settings_path())
|
|
assert_bool(file.has_section_key("quiz", "add_to_stats")).is_false()
|
|
assert_bool(file.has_section_key("quiz", "use_low_played_mode")).is_false()
|
|
|
|
|
|
func test_file_is_in_the_folder_from_the_environment() -> void:
|
|
assert_str(Settings.quiz_settings_path().get_base_dir()).is_equal(_config_dir)
|
|
|
|
|
|
func test_file_is_in_the_config_folder_without_the_environment() -> void:
|
|
OS.unset_environment(Settings.CONFIG_DIR_ENV)
|
|
|
|
assert_str(Settings.quiz_settings_path().get_base_dir()) \
|
|
.is_equal(OS.get_config_dir().path_join("MusicPlayer"))
|
|
|
|
|
|
func test_debug_builds_use_a_file_of_their_own() -> void:
|
|
Settings.is_debug = true
|
|
var debug_path: String = Settings.quiz_settings_path()
|
|
Settings.is_debug = false
|
|
|
|
assert_str(debug_path).is_not_equal(Settings.quiz_settings_path())
|