recrownedgtk/RecrownedGTK.Tests/Persistence/PreferencesTest.cs

126 lines
4.3 KiB
C#

using System.IO;
using RecrownedGTK.Persistence;
using NUnit.Framework;
namespace RecrownedGTK.Tests.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();
string[] expectedLines = File.ReadAllLines("assets/preferences.xml");
string expected = null;
for (int i = 0; i < expectedLines.Length; i++) {
if (i != 1) {
expected += expectedLines[i];
}
}
string[] actualLines = File.ReadAllLines(string.Format("{0}/{1}.xml", PATH, FILENAME));
string actual = null;
for (int i = 0; i < expectedLines.Length; i++) {
if (i != 1) {
actual += actualLines[i];
}
}
Assert.AreEqual(expected, actual);
}
[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));
}
}
}
}