Unit testing for the new preference system.

This commit is contained in:
Harrison Deng 2020-02-29 17:15:49 -05:00
parent 278031ad7d
commit 72d080c04c
2 changed files with 119 additions and 0 deletions

View File

@ -0,0 +1,110 @@
using System.IO;
using RecrownedGTK.Persistence;
using NUnit.Framework;
namespace RecrownedGTK.Test.Persistence {
[TestFixture]
public class PreferenceManagerTest {
Preferences preferences;
const string PATH = "../testdir";
const string FILENAME = "preferences";
[SetUp]
public void Setup() {
preferences = new Preferences(PATH, FILENAME);
}
[TearDown]
public void Teardown() {
string filePath = string.Format("{0}/{1}.xml", PATH, FILENAME);
if (File.Exists(filePath)) {
File.Delete(filePath);
}
}
[TestCase("volume", 8)]
[TestCase("volume", 16)]
[TestCase("dial", 2)]
[TestCase("bloom", true)]
[TestCase("bloom", false)]
[TestCase("Nope", "yep")]
public void SetPreferenceTest(string key, object value) {
preferences.SetPreference(key, value.ToString());
Assert.AreEqual(value.ToString(), preferences.GetStringValue(key));
}
[TestCase("volume")]
[TestCase("")]
public void GetPreferenceTestNotSet(string key) {
Assert.AreEqual("", preferences.GetStringValue(key));
}
[TestCase("val1", true)]
[TestCase("val-1", false)]
[TestCase("val2", true)]
public void DeletePreferenceTest(string toDelete, bool expected) {
preferences.SetPreference("val1", "1");
preferences.SetPreference("val2", 2);
preferences.SetPreference("val3", 3);
Assert.AreEqual(expected, preferences.DeletePreference(toDelete));
}
[Test]
public void SaveTestFileCreation() {
preferences.SetPreference("val1", 1);
preferences.SetPreference("val2", 2);
preferences.SetPreference("val3", 3);
preferences.Save();
FileAssert.Exists(string.Format("{0}/{1}.xml", PATH, FILENAME));
}
[Test]
public void SaveTestContent() {
preferences.SetPreference("val", 1);
preferences.Save();
FileAssert.AreEqual("assets/preferences.xml", string.Format("{0}/{1}.xml", PATH, FILENAME));
}
[Test]
public void LoadTestNoFile() {
Assert.IsFalse(preferences.Load());
}
[TestCase("volume", 8)]
[TestCase("volume", 16)]
[TestCase("dial", 2)]
[TestCase("bloom", true)]
[TestCase("bloom", false)]
[TestCase("Nope", "yep")]
[TestCase("precise", 3.42223f)]
[TestCase("anotherprecise", 3.14f)]
public void LoadTestWithFile(string key, object value) {
if (value.GetType() == typeof(string)) {
preferences.SetPreference(key, (string) value);
} else if (value.GetType() == typeof(int)) {
preferences.SetPreference(key, (int) value);
} else if (value.GetType() == typeof(float)) {
preferences.SetPreference(key, (float) value);
} else if (value.GetType() == typeof(bool)) {
preferences.SetPreference(key, (bool) value);
}
preferences.Save();
Preferences secondPreference = new Preferences(PATH, FILENAME);
secondPreference.Load();
if (value.GetType() == typeof(string)) {
Assert.AreEqual(value, secondPreference.GetStringValue(key));
} else if (value.GetType() == typeof(int)) {
Assert.AreEqual(value, secondPreference.GetIntValue(key));
} else if (value.GetType() == typeof(float)) {
Assert.AreEqual(value, secondPreference.GetFloatValue(key));
} else if (value.GetType() == typeof(bool)) {
Assert.AreEqual(value, secondPreference.GetBoolValue(key));
}
}
}
}

View File

@ -0,0 +1,9 @@
<?xml version="1.0"?>
<PreferencesInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<preferences>
<PreferenceInfo>
<key>val</key>
<value>1</value>
</PreferenceInfo>
</preferences>
</PreferencesInfo>