public void ReadObjectWithCreateTest() { var dd = DataDir.Create(DataDirType.Temporary); dd.CreateIfNotExists(); string filename = dd.GetNewFileName(extension: "json"); Assert.IsFalse(dd.FileExists(filename)); var data = new TestObject { SomeInt = 12345, SomeDouble = 54.321, SomeString = "ABCD 1234567 abcd", SomeNumbers = new int[] { -10, -20, 15, -8 } }; var test1 = dd.ReadObject <TestObject>(filename, () => data); Assert.AreEqual(test1, data); test1.SomeInt = -117; test1.SomeDouble = -42.5; dd.WriteObject(filename, test1); var test2 = dd.ReadObject <TestObject>(filename, () => data); Assert.AreEqual(test1, test2); Assert.AreNotEqual(data, test2); }
public void BinarySerTest() { var dd = DataDir.Create(DataDirType.Temporary); dd.CreateIfNotExists(); var data = new TestObject { SomeInt = 0xc0ffee, SomeDouble = 12345.54321, SomeString = "the quick brown fox jumps over the lazy dog", SomeNumbers = new int[] { 1, 2, 3, 4, -10, -20, -30, -40 } }; dd.SerializationFormats.Add(".custom", new MiffTheFox.DataDirs.Serialization.BinarySerializationFormat()); string filename = DateTime.UtcNow.Ticks.ToString("x") + ".custom"; dd.WriteObject(filename, data); var data2 = dd.ReadObject <TestObject>(filename); Assert.AreEqual(data, data2); dd.DeleteFile(filename); }
public void TestOpenFile() { var contents = BinString.FromTextString("Hello, world!\n", System.Text.Encoding.ASCII); var dd = DataDir.Create(DataDirType.Temporary); string filename = DateTime.Now.Ticks.ToString("x") + ".tmp"; string fullFilename = Path.Combine(dd.Path, filename); Assert.IsFalse(File.Exists(fullFilename)); Assert.ThrowsException <FileNotFoundException>(() => dd.OpenFile(filename, FileMode.Open, FileAccess.Read)); using (var stream = dd.OpenFile(filename, FileMode.Create, FileAccess.ReadWrite)) { stream.Write(contents); stream.Close(); } Assert.IsTrue(File.Exists(fullFilename)); Assert.AreEqual(contents, (BinString)File.ReadAllBytes(fullFilename)); using (var stream = dd.OpenFile(filename, FileMode.Open, FileAccess.Read)) { var read = stream.ReadBinString(contents.Length); Assert.AreEqual(contents, read); } File.Delete(fullFilename); Assert.ThrowsException <ArgumentException>(() => dd.OpenFile(null, FileMode.Create, FileAccess.ReadWrite)); Assert.ThrowsException <ArgumentException>(() => dd.OpenFile(string.Empty, FileMode.Create, FileAccess.ReadWrite)); }
public void AutomaticNameTest() { string title = Assembly.GetExecutingAssembly().GetCustomAttributes <AssemblyTitleAttribute>().Select(attr => attr.Title).FirstOrDefault(); var tempDirectory = DataDir.Create(DataDirType.Temporary); Assert.AreEqual(title, Path.GetFileName(tempDirectory.Path)); }
public void TestDelete() { var dd = DataDir.Create(DataDirType.Temporary); string filename = "DeleteMe-" + DateTime.Now.Ticks.ToString("x") + ".txt"; string stringData = "This file should be deleted.\n"; dd.WriteAllText(filename, stringData, Encoding.ASCII); Assert.AreEqual(stringData, dd.ReadAllText(filename, Encoding.ASCII)); dd.DeleteFile(filename); Assert.ThrowsException <FileNotFoundException>(() => dd.ReadAllText(filename, Encoding.ASCII)); }
public void TestSimpleReadWrite() { string stringData = "Hello, world! This is a string.\n"; var binData = BinString.FromBytes("09F911029D74E35BD84156C5635688C0"); var dd = DataDir.Create(DataDirType.Temporary); dd.WriteAllText("test.txt", stringData, System.Text.Encoding.ASCII); Assert.AreEqual(stringData, dd.ReadAllText("test.txt", Encoding.ASCII)); dd.WriteAllBytes("test.bin", binData); Assert.AreEqual(binData, dd.ReadAllBytes("test.bin")); }
public void FailedSerTest() { var dd = DataDir.Create(DataDirType.Temporary); dd.CreateIfNotExists(); string filename = "notfound-" + DateTime.UtcNow.Ticks.ToString("x") + ".json"; Assert.ThrowsException <FileNotFoundException>(() => dd.ReadObject <TestObject>(filename)); Assert.ThrowsException <MiffTheFox.DataDirs.Serialization.UnknownSerializationFormatException>(() => dd.WriteObject("invalid.xyz", 42)); filename = "invalid-" + DateTime.UtcNow.Ticks.ToString("x") + ".json"; dd.WriteAllText(filename, "}{}!{$%{!%#()%(!@#%}!@#{$!}#%", System.Text.Encoding.Unicode); Assert.ThrowsException <MiffTheFox.DataDirs.Serialization.SerializationFailException>(() => dd.ReadObject <TestObject>(filename)); }
public void TestGetNewFileName() { const string testHint = "Test Hint"; var dd = DataDir.Create(DataDirType.Temporary); string f1 = dd.GetNewFileName(testHint, "tmp"); Assert.IsFalse(dd.FileExists(f1)); dd.WriteAllBytes(f1, BinString.Empty); Assert.IsTrue(dd.FileExists(f1)); string f2 = dd.GetNewFileName(testHint, "tmp"); Assert.AreNotEqual(f1, f2); Assert.IsFalse(dd.FileExists(f2)); }
public void TestGetPath() { var data = BinString.FromBytes("C0FFEEC0FFEEC0FFEEC0FFEE"); var dd = DataDir.Create(DataDirType.Temporary); string filename = DateTime.Now.Ticks.ToString("x") + ".tmp"; string fullPath = dd.GetFullPath(filename); Assert.IsFalse(File.Exists(fullPath)); dd.WriteAllBytes(filename, data); Assert.AreEqual(data, (BinString)File.ReadAllBytes(fullPath)); data = BinString.FromTextString("ABCDEFGHIJKLMNOPQRSTUVWXYZ", Encoding.ASCII); File.WriteAllBytes(fullPath, data); Assert.AreEqual(data, dd.ReadAllBytes(filename)); Assert.IsTrue(File.Exists(fullPath)); dd.DeleteFile(filename); Assert.IsFalse(File.Exists(fullPath)); }
public void LocationCreateTests() { var l = new List <DataDir>(); string fakeApplicationName = $"DataDir/Test {DateTime.Now:F}"; l.Add(DataDir.Create(fakeApplicationName, type: DataDirType.RoamingUser)); l.Add(DataDir.Create(fakeApplicationName, type: DataDirType.LocalUser)); l.Add(DataDir.Create(fakeApplicationName, type: DataDirType.SystemWide)); l.Add(DataDir.Create(fakeApplicationName, type: DataDirType.Temporary)); l.Add(DataDir.Create(fakeApplicationName, type: DataDirType.SavedGameData)); foreach (var d in l) { // DataDir.Create should create the directory on disk Assert.IsTrue(d.DirectoryExists); // clean up Directory.Delete(d.Path); Assert.IsFalse(d.DirectoryExists); } }
public void XmlSerTest() { var dd = DataDir.Create(DataDirType.Temporary); dd.CreateIfNotExists(); string filename = DateTime.UtcNow.Ticks.ToString("x") + ".xml"; var data = new TestObject { SomeInt = 123, SomeDouble = 45.6, SomeString = "Ξεσκεπάζω τὴν ψυχοφθόρα βδελυγμία", SomeNumbers = new int[] { 12, 11, 10 } }; dd.WriteObject(filename, data); var data2 = dd.ReadObject <TestObject>(filename); Assert.AreEqual(data, data2); dd.DeleteFile(filename); }
public void JsonSerTest() { var dd = DataDir.Create(DataDirType.Temporary); dd.CreateIfNotExists(); string filename = DateTime.UtcNow.Ticks.ToString("x") + ".json"; var data = new TestObject { SomeInt = 42, SomeDouble = 3.14, SomeString = "asdfghjkl", SomeNumbers = new int[] { 1, 2, 3, 4, 5 } }; dd.WriteObject(filename, data); var data2 = dd.ReadObject <TestObject>(filename); Assert.AreEqual(data, data2); dd.DeleteFile(filename); }