Skip to content

Commit

Permalink
explaining test.
Browse files Browse the repository at this point in the history
clearing scope.
  • Loading branch information
SlejmUr committed Jan 9, 2025
1 parent 0e0efa4 commit 1b5e20e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
35 changes: 25 additions & 10 deletions Fuyu.Common/Config/ConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,16 @@ public static void FreeInstance(string platform)
Unloaded?.Invoke(platform);
}

private string GetConfigPath(string configName)
{
return $"./Fuyu/Configs/{_platform}/{configName}.json";
}

#region Regular config

public T GetConfig<T>(string configName) where T : AbstractConfig, new()
{
var file = $"./Fuyu/Configs/{_platform}/{configName}.json";
var file = GetConfigPath(configName);
if (VFS.Exists(file))
{
return Json.Parse<T>(VFS.ReadTextFile(file));
Expand All @@ -49,48 +55,56 @@ public static void FreeInstance(string platform)

public bool IsConfigExists(string configName)
{
var file = $"./Fuyu/Configs/{_platform}/{configName}.json";
var file = GetConfigPath(configName);
return VFS.Exists(file);
}

public void SaveConfig<T>(string configName, T Config) where T : AbstractConfig
{
var file = $"./Fuyu/Configs/{_platform}/{configName}.json";
var file = GetConfigPath(configName);
var json = Json.Stringify(Config);
VFS.WriteTextFile(file, json);
}

public void DeleteConfig(string configName)
{
var file = $"./Fuyu/Configs/{_platform}/{configName}.json";
var file = GetConfigPath(configName);
File.Delete(file);
}

#endregion

#region Lazy config
public void GetConfigLazy<T>(string configName, ref T value, bool reload = false) where T : AbstractConfig, new()

public void GetConfigLazy<T>(string configName, ref T value, bool reload = false)
where T : AbstractConfig, new()
{
value = new();
if (lazyConfigs.Value.ContainsKey(configName))
{
// TODO: Check if can parse to that type.
// Check if can parse to that type.
var x = lazyConfigs.Value[configName];
if (typeof(T).IsAssignableFrom(x.GetType()))
{
value = (T)lazyConfigs.Value[configName];
// if we dont want to reload from the file, we just return
if (!reload)
{
return;
}
}
}
// we loading here if we dont already have it.
var file = $"./Fuyu/Configs/{_platform}/{configName}.json";
var file = GetConfigPath(configName);
if (VFS.Exists(file))
{
value = Json.Parse<T>(VFS.ReadTextFile(file));
// storing the files.
}
// storing the config name and the value.
if (!lazyConfigs.Value.ContainsKey(configName))
{
lazyConfigs.Value.Add(configName, value);
}
lazyConfigs.Value[configName] = value;
}

Expand All @@ -102,7 +116,7 @@ public bool IsConfigExistsLazy(string configName)
public void SaveConfigLazy(string configName)
{
var Config = lazyConfigs.Value[configName];
var file = $"./Fuyu/Configs/{_platform}/{configName}.json";
var file = GetConfigPath(configName);
var json = Json.Stringify(Config);
VFS.WriteTextFile(file, json);
}
Expand All @@ -114,8 +128,9 @@ public void DeleteConfigLazy(string configName, bool deleteFile = true)
{
return;
}
var file = $"./Fuyu/Configs/{_platform}/{configName}.json";
var file = GetConfigPath(configName);
File.Delete(file);
}

#endregion
}
39 changes: 35 additions & 4 deletions Tests/Fuyu.Tests.Common/Units/ConfigTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,65 @@ public class ConfigTest
[TestMethod]
public void TestConfigService()
{
// Before you scream at me why I use GetHashCode let me explain.
// even if the two object share the same, and ergo the GetHashCode that usually checked between 2 type is not used by the UnitTesting tool.
// but atleast it works.
// GetHashCode returns a hash of the value. so we don't compare references
// Other check if value which is checked after hashcode

// creating new instance
var configservice = ConfigService.GetInstance("test");
Assert.IsNotNull(configservice);

// creating empty and other empty TestConfig.
var empty_config = new TestConfig();
var test_config = configservice.GetConfig<TestConfig>("testing");

// we are checking if both are the same
Assert.AreEqual(test_config.GetHashCode(), empty_config.GetHashCode());
Assert.AreEqual(test_config.Text, empty_config.Text);

// we changing the value and saving it
test_config.Text = "changedValue";
configservice.SaveConfig("testing", test_config);
// we make sure it is not Equal!
Assert.AreNotEqual(test_config.Text, empty_config.Text);

// we reload from the config change
test_config = configservice.GetConfig<TestConfig>("testing");

// we make sure the test_config is not empty!
Assert.AreNotEqual(test_config.GetHashCode(), empty_config.GetHashCode());
Assert.AreNotEqual(test_config.Text, empty_config.Text);

// we make sure the Text is our prev set value.
Assert.AreEqual(test_config.Text, "changedValue");

// freeing and re-getting for test.
ConfigService.FreeInstance("test");
configservice = ConfigService.GetInstance("test");
Assert.IsNotNull(configservice);

// check if the config from disk not empty
test_config = configservice.GetConfig<TestConfig>("testing");
Assert.AreNotEqual(test_config.GetHashCode(), empty_config.GetHashCode());
Assert.AreNotEqual(test_config.Text, empty_config.Text);

// deleting the config
configservice.DeleteConfig("testing");

// getting the deleted config.
test_config = configservice.GetConfig<TestConfig>("testing");

// testing if its actually empty.
Assert.AreEqual(test_config.GetHashCode(), empty_config.GetHashCode());
Assert.AreEqual(test_config.Text, empty_config.Text);

// lastly we free (ensuring nothing will use it.)
ConfigService.FreeInstance("test");
}

[TestMethod]
public void TestConfigServiceLazy()
{
// testing with Lazy Dictionary.

var configservice = ConfigService.GetInstance("lazy");
Assert.IsNotNull(configservice);

Expand Down Expand Up @@ -103,7 +132,9 @@ public class TestConfig : AbstractConfig
public override int GetHashCode()
{
if (!string.IsNullOrEmpty(Text))
{
return Text.GetHashCode();
}
return 0;
}
}
Expand Down

0 comments on commit 1b5e20e

Please sign in to comment.