Skip to content

Commit 46dcbb4

Browse files
committed
Migrate the const strings to Enumeration Values
1 parent bd71b88 commit 46dcbb4

55 files changed

Lines changed: 589 additions & 468 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/UniGetUI.Core.IconStore/IconDatabase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public async Task LoadIconAndScreenshotsDatabaseAsync()
6060
try
6161
{
6262
Uri DownloadUrl = new("https://raw.githubusercontent.com/marticliment/UniGetUI/main/WebBasedData/screenshot-database-v2.json");
63-
if (Settings.Get(Settings.IconDataBaseURL))
63+
if (Settings.Get(Settings.K.IconDataBaseURL))
6464
{
65-
DownloadUrl = new Uri(Settings.GetValue(Settings.IconDataBaseURL));
65+
DownloadUrl = new Uri(Settings.GetValue(Settings.K.IconDataBaseURL));
6666
}
6767

6868
using (HttpClient client = new(CoreTools.GenericHttpClientParameters))

src/UniGetUI.Core.LanguageEngine/LanguageData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private static Person[] LoadLanguageTranslatorList()
132132
Name: (url is not null ? "@" : "") + (translator["name"] ?? "").ToString(),
133133
ProfilePicture: url is not null ? new Uri(url.ToString() + ".png") : null,
134134
GitHubUrl: url,
135-
Language: !LangShown ? LanguageData.LanguageReference[langKey.Key] : ""
135+
Language: !LangShown ? LanguageReference[langKey.Key] : ""
136136
);
137137
LangShown = true;
138138
result.Add(person);

src/UniGetUI.Core.LanguageEngine/LanguageEngine.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class LanguageEngine
2121

2222
public LanguageEngine(string ForceLanguage = "")
2323
{
24-
string LangName = Settings.GetValue(Settings.PreferredLanguage);
24+
string LangName = Settings.GetValue(Settings.K.PreferredLanguage);
2525
if (LangName is "default" or "")
2626
{
2727
LangName = CultureInfo.CurrentUICulture.ToString().Replace("-", "_");
@@ -93,7 +93,7 @@ public Dictionary<string, string> LoadLanguageFile(string LangKey)
9393

9494
string CachedLangFileToLoad = Path.Join(CoreData.UniGetUICacheDirectory_Lang, "lang_" + LangKey + ".json");
9595

96-
if (Settings.Get(Settings.DisableLangAutoUpdater))
96+
if (Settings.Get(Settings.K.DisableLangAutoUpdater))
9797
{
9898
Logger.Warn("User has updated translations disabled");
9999
}
@@ -120,7 +120,7 @@ public Dictionary<string, string> LoadLanguageFile(string LangKey)
120120
}
121121
}
122122

123-
if (!Settings.Get(Settings.DisableLangAutoUpdater))
123+
if (!Settings.Get(Settings.K.DisableLangAutoUpdater))
124124
_ = DownloadUpdatedLanguageFile(LangKey);
125125

126126
return LangDict;

src/UniGetUI.Core.Settings.Tests/SettingsTest.cs

Lines changed: 84 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public void Dispose()
5454
[Fact]
5555
public void TestSettingsSaveToNewDirectory()
5656
{
57-
Settings.Set(Settings.FreshBoolSetting, true);
58-
Settings.SetValue(Settings.FreshValue, "test");
57+
Settings.Set(Settings.K.FreshBoolSetting, true);
58+
Settings.SetValue(Settings.K.FreshValue, "test");
5959

6060
Assert.True(File.Exists(GetNewSettingPath("FreshBoolSetting")));
6161
Assert.True(File.Exists(GetNewSettingPath("FreshValue")));
@@ -64,13 +64,13 @@ public void TestSettingsSaveToNewDirectory()
6464
[Fact]
6565
public void TestExistingSettingsMigrateToNewDirectory()
6666
{
67-
string settingName = "LegacyBoolSetting";
67+
string settingName = Settings.ResolveKey(Settings.K.Test7_Legacy);
6868
var oldPath = GetOldSettingsPath(settingName);
6969
File.WriteAllText(oldPath, "");
7070

71-
var migratedValue = Settings.Get(settingName);
71+
var migratedValue = Settings.Get(Settings.K.Test7_Legacy);
7272
var newPath = GetNewSettingPath(settingName);
73-
var valueAfterMigration = Settings.Get(settingName);
73+
var valueAfterMigration = Settings.Get(Settings.K.Test7_Legacy);
7474

7575
Assert.True(migratedValue);
7676
Assert.True(valueAfterMigration);
@@ -80,71 +80,73 @@ public void TestExistingSettingsMigrateToNewDirectory()
8080
}
8181

8282
[Theory]
83-
[InlineData("TestSetting1", true, false, false, true)]
84-
[InlineData("TestSetting2", true, false, false, false)]
85-
[InlineData("Test.Settings_with", true, false, true, true)]
86-
[InlineData("TestSettingEntry With A Space", false, true, false, false)]
87-
[InlineData("ª", false, false, false, false)]
88-
[InlineData("VeryVeryLongTestSettingEntrySoTheClassCanReallyBeStressedOut", true, false, true, true)]
89-
public void TestBooleanSettings(string SettingName, bool st1, bool st2, bool st3, bool st4)
83+
[InlineData(Settings.K.Test1, true, false, false, true)]
84+
[InlineData(Settings.K.Test2, true, false, false, false)]
85+
[InlineData(Settings.K.Test3, true, false, true, true)]
86+
[InlineData(Settings.K.Test4, false, true, false, false)]
87+
[InlineData(Settings.K.Test5, false, false, false, false)]
88+
[InlineData(Settings.K.Test6, true, false, true, true)]
89+
public void TestBooleanSettings(Settings.K key, bool st1, bool st2, bool st3, bool st4)
9090
{
91-
Settings.Set(SettingName, st1);
92-
Assert.Equal(st1, Settings.Get(SettingName));
93-
Assert.Equal(st1, File.Exists(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, SettingName)));
94-
95-
Settings.Set(SettingName, st2);
96-
Assert.Equal(st2, Settings.Get(SettingName));
97-
Assert.Equal(st2, File.Exists(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, SettingName)));
98-
99-
Settings.Set(SettingName, st3);
100-
Assert.Equal(st3, Settings.Get(SettingName));
101-
Assert.Equal(st3, File.Exists(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, SettingName)));
102-
103-
Settings.Set(SettingName, st4);
104-
Assert.Equal(st4, Settings.Get(SettingName));
105-
Assert.Equal(st4, File.Exists(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, SettingName)));
106-
107-
Settings.Set(SettingName, false); // Cleanup
108-
Assert.False(Settings.Get(SettingName));
109-
Assert.False(File.Exists(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, SettingName)));
91+
string sName = Settings.ResolveKey(key);
92+
Settings.Set(key, st1);
93+
Assert.Equal(st1, Settings.Get(key));
94+
Assert.Equal(st1, File.Exists(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, sName)));
95+
96+
Settings.Set(key, st2);
97+
Assert.Equal(st2, Settings.Get(key));
98+
Assert.Equal(st2, File.Exists(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, sName)));
99+
100+
Settings.Set(key, st3);
101+
Assert.Equal(st3, Settings.Get(key));
102+
Assert.Equal(st3, File.Exists(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, sName)));
103+
104+
Settings.Set(key, st4);
105+
Assert.Equal(st4, Settings.Get(key));
106+
Assert.Equal(st4, File.Exists(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, sName)));
107+
108+
Settings.Set(key, false); // Cleanup
109+
Assert.False(Settings.Get(key));
110+
Assert.False(File.Exists(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, sName)));
110111
}
111112

112113
[Theory]
113-
[InlineData("TestSetting1", "RandomFirstValue", "RandomSecondValue", "", "RandomThirdValue")]
114-
[InlineData("ktjgshdfsd", "", "", "", "RandomThirdValue")]
115-
[InlineData("ª", "RandomFirstValue", " ", "\t", "RandomThirdValue")]
116-
[InlineData("TestSettingEntry With A Space", "RandomFirstValue", "", "", "")]
117-
[InlineData("VeryVeryLongTestSettingEntrySoTheClassCanReallyBeStressedOut", "\x00\x01\x02\u0003", "", "", "RandomThirdValue")]
118-
public void TestValueSettings(string SettingName, string st1, string st2, string st3, string st4)
114+
[InlineData(Settings.K.Test1, "RandomFirstValue", "RandomSecondValue", "", "RandomThirdValue")]
115+
[InlineData(Settings.K.Test2, "", "", "", "RandomThirdValue")]
116+
[InlineData(Settings.K.Test3, "RandomFirstValue", " ", "\t", "RandomThirdValue")]
117+
[InlineData(Settings.K.Test4, "RandomFirstValue", "", "", "")]
118+
[InlineData(Settings.K.Test5, "\x00\x01\x02\u0003", "", "", "RandomThirdValue")]
119+
public void TestValueSettings(Settings.K key, string st1, string st2, string st3, string st4)
119120
{
120-
Settings.SetValue(SettingName, st1);
121-
Assert.Equal(st1, Settings.GetValue(SettingName));
122-
Assert.Equal(st1 != "", File.Exists(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, SettingName)));
123-
124-
Settings.SetValue(SettingName, st2);
125-
Assert.Equal(st2, Settings.GetValue(SettingName));
126-
Assert.Equal(st2 != "", File.Exists(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, SettingName)));
127-
128-
Settings.SetValue(SettingName, st3);
129-
Assert.Equal(st3, Settings.GetValue(SettingName));
130-
Assert.Equal(st3 != "", File.Exists(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, SettingName)));
131-
132-
Settings.SetValue(SettingName, st4);
133-
Assert.Equal(st4, Settings.GetValue(SettingName));
134-
Assert.Equal(st4 != "", File.Exists(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, SettingName)));
135-
136-
Settings.Set(SettingName, false); // Cleanup
137-
Assert.False(Settings.Get(SettingName));
138-
Assert.Equal("", Settings.GetValue(SettingName));
139-
Assert.False(File.Exists(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, SettingName)));
121+
string sName = Settings.ResolveKey(key);
122+
Settings.SetValue(key, st1);
123+
Assert.Equal(st1, Settings.GetValue(key));
124+
Assert.Equal(st1 != "", File.Exists(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, sName)));
125+
126+
Settings.SetValue(key, st2);
127+
Assert.Equal(st2, Settings.GetValue(key));
128+
Assert.Equal(st2 != "", File.Exists(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, sName)));
129+
130+
Settings.SetValue(key, st3);
131+
Assert.Equal(st3, Settings.GetValue(key));
132+
Assert.Equal(st3 != "", File.Exists(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, sName)));
133+
134+
Settings.SetValue(key, st4);
135+
Assert.Equal(st4, Settings.GetValue(key));
136+
Assert.Equal(st4 != "", File.Exists(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, sName)));
137+
138+
Settings.Set(key, false); // Cleanup
139+
Assert.False(Settings.Get(key));
140+
Assert.Equal("", Settings.GetValue(key));
141+
Assert.False(File.Exists(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, sName)));
140142
}
141143

142144
[Theory]
143-
[InlineData("lsTestSetting1", new string[] { "UpdatedFirstValue", "RandomString1", "RandomTestValue", "AnotherRandomValue" }, new int[] { 9, 15, 21, 1001, 4567 }, new string[] { "itemA", "itemB", "itemC" })]
144-
[InlineData("lsktjgshdfsd", new string[] { "newValue1", "updatedString", "emptyString", "randomSymbols@123" }, new int[] { 42, 23, 17, 98765, 3482 }, new string[] { "itemX", "itemY", "itemZ" })]
145-
[InlineData("lsª", new string[] { "UniqueVal1", "NewVal2", "AnotherVal3", "TestVal4" }, new int[] { 123, 456, 789, 321, 654 }, new string[] { "item1", "item2", "item3" })]
146-
[InlineData("lsTestSettingEntry With A Space", new string[] { "ChangedFirstValue", "AlteredSecondVal", "TestedValue", "FinalVal" }, new int[] { 23, 98, 456, 753, 951 }, new string[] { "testA", "testB", "testC" })]
147-
[InlineData("lsVeryVeryLongTestSettingEntrySoTheClassCanReallyBeStressedOut", new string[] { "newCharacterSet\x99\x01\x02", "UpdatedRandomValue", "TestEmptyString", "FinalTestValue" }, new int[] { 0b11001100, 1234, 5678, 1000000 }, new string[] { "finalTest1", "finalTest2", "finalTest3" })]
145+
[InlineData("lsTestSetting1", new[] { "UpdatedFirstValue", "RandomString1", "RandomTestValue", "AnotherRandomValue" }, new[] { 9, 15, 21, 1001, 4567 }, new[] { "itemA", "itemB", "itemC" })]
146+
[InlineData("lsktjgshdfsd", new[] { "newValue1", "updatedString", "emptyString", "randomSymbols@123" }, new[] { 42, 23, 17, 98765, 3482 }, new[] { "itemX", "itemY", "itemZ" })]
147+
[InlineData("lsª", new[] { "UniqueVal1", "NewVal2", "AnotherVal3", "TestVal4" }, new[] { 123, 456, 789, 321, 654 }, new[] { "item1", "item2", "item3" })]
148+
[InlineData("lsTestSettingEntry With A Space", new[] { "ChangedFirstValue", "AlteredSecondVal", "TestedValue", "FinalVal" }, new[] { 23, 98, 456, 753, 951 }, new[] { "testA", "testB", "testC" })]
149+
[InlineData("lsVeryVeryLongTestSettingEntrySoTheClassCanReallyBeStressedOut", new[] { "newCharacterSet\x99\x01\x02", "UpdatedRandomValue", "TestEmptyString", "FinalTestValue" }, new[] { 0b11001100, 1234, 5678, 1000000 }, new[] { "finalTest1", "finalTest2", "finalTest3" })]
148150
public void TestListSettings(string SettingName, string[] ls1Array, int[] ls2Array, string[] ls3Array)
149151
{
150152
// Convert arrays to Lists manually
@@ -199,12 +201,12 @@ public void TestListSettings(string SettingName, string[] ls1Array, int[] ls2Arr
199201
}
200202

201203
[Theory]
202-
[InlineData("dTestSetting1", new string[] { "UpdatedFirstValue", "RandomString1", "RandomTestValue", "AnotherRandomValue" }, new int[] { 9, 15, 21, 1001, 4567 }, new string[] { "itemA", "itemB", "itemC" })]
203-
[InlineData("dktjgshdfsd", new string[] { "newValue1", "updatedString", "emptyString", "randomSymbols@123" }, new int[] { 42, 23, 17, 98765, 3482 }, new string[] { "itemX", "itemY", "itemZ" })]
204-
[InlineData("dª", new string[] { "UniqueVal1", "NewVal2", "AnotherVal3", "TestVal4" }, new int[] { 123, 456, 789, 321, 654 }, new string[] { "item1", "item2", "item3" })]
205-
[InlineData("dTestSettingEntry With A Space", new string[] { "ChangedFirstValue", "AlteredSecondVal", "TestedValue", "FinalVal" }, new int[] { 23, 98, 456, 753, 951 }, new string[] { "testA", "testB", "testC" })]
206-
[InlineData("dVeryVeryLongTestSettingEntrySoTheClassCanReallyBeStressedOut", new string[] { "newCharacterSet\x99\x01\x02", "UpdatedRandomValue", "TestEmptyString", "FinalTestValue" }, new int[] { 0b11001100, 1234, 5678, 1000000 }, new string[] { "finalTest1", "finalTest2", "finalTest3" })]
207-
public void TestDictionarySettings(string SettingName, string[] keyArray, int[] intArray, string[] strArray)
204+
[InlineData(Settings.K.Test2, new[] { "UpdatedFirstValue", "RandomString1", "RandomTestValue", "AnotherRandomValue" }, new[] { 9, 15, 21, 1001, 4567 }, new[] { "itemA", "itemB", "itemC" })]
205+
[InlineData(Settings.K.Test3, new[] { "newValue1", "updatedString", "emptyString", "randomSymbols@123" }, new[] { 42, 23, 17, 98765, 3482 }, new[] { "itemX", "itemY", "itemZ" })]
206+
[InlineData(Settings.K.Test4, new[] { "UniqueVal1", "NewVal2", "AnotherVal3", "TestVal4" }, new[] { 123, 456, 789, 321, 654 }, new[] { "item1", "item2", "item3" })]
207+
[InlineData(Settings.K.Test5, new[] { "ChangedFirstValue", "AlteredSecondVal", "TestedValue", "FinalVal" }, new[] { 23, 98, 456, 753, 951 }, new[] { "testA", "testB", "testC" })]
208+
[InlineData(Settings.K.Test6, new[] { "newCharacterSet\x99\x01\x02", "UpdatedRandomValue", "TestEmptyString", "FinalTestValue" }, new[] { 0b11001100, 1234, 5678, 1000000 }, new[] { "finalTest1", "finalTest2", "finalTest3" })]
209+
public void TestDictionarySettings(Settings.K SettingName, string[] keyArray, int[] intArray, string[] strArray)
208210
{
209211
Dictionary<string, SerializableTest?> test = [];
210212
Dictionary<string, SerializableTest?> nonEmptyDictionary = [];
@@ -222,11 +224,10 @@ public void TestDictionarySettings(string SettingName, string[] keyArray, int[]
222224
);
223225
}
224226

225-
string randStr = new Random().Next().ToString();
226-
Settings.SetDictionaryItem(randStr, "key", 12);
227-
Assert.Equal(12, Settings.GetDictionaryItem<string, int>(randStr, "key"));
227+
Settings.SetDictionaryItem(SettingName, "key", 12);
228+
Assert.Equal(12, Settings.GetDictionaryItem<string, int>(SettingName, "key"));
228229
Settings.SetDictionary(SettingName, test);
229-
Assert.Equal(JsonSerializer.Serialize(test, Settings.SerializationOptions), File.ReadAllText(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, $"{SettingName}.json")));
230+
Assert.Equal(JsonSerializer.Serialize(test, Settings.SerializationOptions), File.ReadAllText(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, $"{Settings.ResolveKey(SettingName)}.json")));
230231
Assert.Equal(test[keyArray[0]]?.sub.count, Settings.GetDictionary<string, SerializableTest?>(SettingName)?[keyArray[0]]?.sub.count);
231232
Assert.Equal(test[keyArray[1]]?.sub.count, Settings.GetDictionaryItem<string, SerializableTest?>(SettingName, keyArray[1])?.sub.count);
232233
Settings.SetDictionaryItem(SettingName, keyArray[0], test[keyArray[1]]);
@@ -258,12 +259,21 @@ public void TestDictionarySettings(string SettingName, string[] keyArray, int[]
258259

259260
Assert.Equal(
260261
JsonSerializer.Serialize(Settings.GetDictionary<string, SerializableTest>(SettingName), Settings.SerializationOptions),
261-
File.ReadAllText(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, $"{SettingName}.json"))
262+
File.ReadAllText(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, $"{Settings.ResolveKey(SettingName)}.json"))
262263
);
263264

264265
Settings.ClearDictionary(SettingName); // Cleanup
265266
Assert.Empty(Settings.GetDictionary<string, SerializableTest?>(SettingName) ?? nonEmptyDictionary);
266-
Assert.False(File.Exists(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, $"{SettingName}.json")));
267+
Assert.False(File.Exists(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, $"{Settings.ResolveKey(SettingName)}.json")));
268+
}
269+
270+
[Fact]
271+
public static void EnsureAllKeysResolve()
272+
{
273+
foreach (Settings.K key in Enum.GetValues(typeof(Settings.K)))
274+
{
275+
Assert.NotEmpty(Settings.ResolveKey(key));
276+
}
267277
}
268278
}
269279
}

0 commit comments

Comments
 (0)