refactor(settings): guard all Settings fields vs null; tidy import + test
Guard colorMode and glyphMode against null in the Settings compact constructor (alongside the existing musicLevel guard), so a hand-edited settings.json with a missing field no longer NPEs in SettingsMenu. Remove the unused MusicLevel import from SettingsMenu. Assert that the defaulted musicLevel survives a round-trip in SettingsStoreTest. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2,7 +2,6 @@ package thb.jeanluc.adventure.menu;
|
||||
|
||||
import thb.jeanluc.adventure.io.ConsoleIO;
|
||||
import thb.jeanluc.adventure.io.GameIO;
|
||||
import thb.jeanluc.adventure.io.MusicLevel;
|
||||
import thb.jeanluc.adventure.save.Settings;
|
||||
import thb.jeanluc.adventure.save.SettingsStore;
|
||||
|
||||
|
||||
@@ -7,6 +7,12 @@ import thb.jeanluc.adventure.io.MusicLevel;
|
||||
public record Settings(ConsoleIO.ColorMode colorMode, ConsoleIO.GlyphMode glyphMode, MusicLevel musicLevel) {
|
||||
|
||||
public Settings {
|
||||
if (colorMode == null) {
|
||||
colorMode = ConsoleIO.ColorMode.AUTO;
|
||||
}
|
||||
if (glyphMode == null) {
|
||||
glyphMode = ConsoleIO.GlyphMode.UNICODE;
|
||||
}
|
||||
if (musicLevel == null) {
|
||||
musicLevel = MusicLevel.MEDIUM; // backward-compat: old saves lack this field
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ class SettingsStoreTest {
|
||||
Settings loaded = store.load();
|
||||
assertThat(loaded.colorMode()).isEqualTo(ConsoleIO.ColorMode.ON);
|
||||
assertThat(loaded.glyphMode()).isEqualTo(ConsoleIO.GlyphMode.ASCII);
|
||||
assertThat(loaded.musicLevel()).isEqualTo(MusicLevel.MEDIUM);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user