SettingsScopeTests.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using static Terminal.Gui.ConfigurationManager;
  2. namespace Terminal.Gui.ConfigurationTests;
  3. public class SettingsScopeTests
  4. {
  5. [Fact]
  6. public void Update_Overrides_Defaults ()
  7. {
  8. // arrange
  9. Locations = ConfigLocations.Default;
  10. Load (true);
  11. Assert.Equal (Key.Esc, (Key)Settings ["Application.QuitKey"].PropertyValue);
  12. ThrowOnJsonErrors = true;
  13. // act
  14. var json = """
  15. {
  16. "Application.QuitKey": "Ctrl-Q"
  17. }
  18. """;
  19. Settings!.Update (json, "test", ConfigLocations.Runtime);
  20. // assert
  21. Assert.Equal (Key.Q.WithCtrl, (Key)Settings ["Application.QuitKey"].PropertyValue);
  22. // clean up
  23. Locations = ConfigLocations.All;
  24. }
  25. [Fact]
  26. public void Apply_ShouldApplyProperties ()
  27. {
  28. Locations = ConfigLocations.Default;
  29. Reset();
  30. // arrange
  31. Assert.Equal (Key.Esc, (Key)Settings ["Application.QuitKey"].PropertyValue);
  32. Assert.Equal (
  33. Key.F6,
  34. (Key)Settings ["Application.NextTabGroupKey"].PropertyValue
  35. );
  36. Assert.Equal (
  37. Key.F6.WithShift,
  38. (Key)Settings ["Application.PrevTabGroupKey"].PropertyValue
  39. );
  40. // act
  41. Settings ["Application.QuitKey"].PropertyValue = Key.Q;
  42. Settings ["Application.NextTabGroupKey"].PropertyValue = Key.F;
  43. Settings ["Application.PrevTabGroupKey"].PropertyValue = Key.B;
  44. Settings.Apply ();
  45. // assert
  46. Assert.Equal (Key.Q, Application.QuitKey);
  47. Assert.Equal (Key.F, Application.NextTabGroupKey);
  48. Assert.Equal (Key.B, Application.PrevTabGroupKey);
  49. Locations = ConfigLocations.Default;
  50. Reset ();
  51. }
  52. [Fact]
  53. [AutoInitShutdown]
  54. public void CopyUpdatedPropertiesFrom_ShouldCopyChangedPropertiesOnly ()
  55. {
  56. Settings ["Application.QuitKey"].PropertyValue = Key.End;
  57. var updatedSettings = new SettingsScope ();
  58. ///Don't set Quitkey
  59. updatedSettings ["Application.NextTabGroupKey"].PropertyValue = Key.F;
  60. updatedSettings ["Application.PrevTabGroupKey"].PropertyValue = Key.B;
  61. Settings.Update (updatedSettings);
  62. Assert.Equal (KeyCode.End, ((Key)Settings ["Application.QuitKey"].PropertyValue).KeyCode);
  63. Assert.Equal (KeyCode.F, ((Key)updatedSettings ["Application.NextTabGroupKey"].PropertyValue).KeyCode);
  64. Assert.Equal (KeyCode.B, ((Key)updatedSettings ["Application.PrevTabGroupKey"].PropertyValue).KeyCode);
  65. }
  66. [Fact]
  67. public void GetHardCodedDefaults_ShouldSetProperties ()
  68. {
  69. ConfigLocations savedLocations = Locations;
  70. Locations = ConfigLocations.Default;
  71. Reset ();
  72. Assert.Equal (5, ((Dictionary<string, ThemeScope>)Settings ["Themes"].PropertyValue).Count);
  73. GetHardCodedDefaults ();
  74. Assert.NotEmpty (Themes);
  75. Assert.Equal ("Default", Themes.Theme);
  76. Assert.True (Settings ["Application.QuitKey"].PropertyValue is Key);
  77. Assert.True (Settings ["Application.NextTabGroupKey"].PropertyValue is Key);
  78. Assert.True (Settings ["Application.PrevTabGroupKey"].PropertyValue is Key);
  79. Assert.True (Settings ["Theme"].PropertyValue is string);
  80. Assert.Equal ("Default", Settings ["Theme"].PropertyValue as string);
  81. Assert.True (Settings ["Themes"].PropertyValue is Dictionary<string, ThemeScope>);
  82. Assert.Single ((Dictionary<string, ThemeScope>)Settings ["Themes"].PropertyValue);
  83. Locations = savedLocations;
  84. }
  85. }