Jypeli  5
The simple game programming library
JyStorageTest.cs
Siirry tämän tiedoston dokumentaatioon.
1 using System;
2 using System.Text;
3 using System.Collections.Generic;
4 using System.Linq;
5 using Microsoft.VisualStudio.TestTools.UnitTesting;
6 using Jypeli;
7 using System.Windows.Forms;
8 
9 namespace JyStorageTest
10 {
11  [TestClass]
12  public class JyStorageTest
13  {
14  private IEnumerable<FileManager> EnumerateStorages()
15  {
17  }
18 
19  [TestMethod]
20  public void TestCreateExistsRemove()
21  {
22  const string testFileName = "x0emxujnhp.jhc";
23 
24  foreach ( var storage in EnumerateStorages() )
25  {
26  storage.Delete( testFileName );
27  Assert.IsFalse( storage.Exists( testFileName ), "Test file still exists after deletion (#1) for " + storage.GetType().Name );
28 
29  StorageFile file = storage.Create( testFileName );
30  file.Close();
31  Assert.IsTrue( storage.Exists( testFileName ), "Test file does not exist after creation for " + storage.GetType().Name );
32 
33  storage.Delete( testFileName );
34  Assert.IsFalse( storage.Exists( testFileName ), "Test file still exists after deletion (#2) for " + storage.GetType().Name );
35  }
36  }
37 
38  [TestMethod]
39  public void TestMkRmDir()
40  {
41  const string testDir = "akjfhyshkl";
42 
43  foreach ( var storage in EnumerateStorages() )
44  {
45  storage.RmDir( testDir );
46  Assert.IsFalse( storage.Exists( testDir ), "Test directory still exists after deletion (#1) for " + storage.GetType().Name );
47 
48  storage.MkDir( testDir );
49  Assert.IsTrue( storage.Exists( testDir ), "Test directory does not exist after creation for " + storage.GetType().Name );
50 
51  storage.RmDir( testDir );
52  Assert.IsFalse( storage.Exists( testDir ), "Test directory still exists after deletion (#2) for " + storage.GetType().Name );
53  }
54  }
55 
56  [Save]
57  class TestClass
58  {
59  [Save] public int Attr1;
60  [Save] public char Attr2 { get; set; }
61  [Save] internal string NonPublic1;
62  [Save] internal string NonPublic2 { get; set; }
63  public short DoNotSave1;
64  public double DoNotSave2 { get; set; }
65 
66  public TestClass( int a1, string np1, string np2 )
67  {
68  Attr1 = a1;
69  NonPublic1 = np1;
70  NonPublic2 = np2;
71  }
72 
73  public string GetNonPublic1() { return NonPublic1; }
74  public string GetNonPublic2() { return NonPublic2; }
75  }
76 
77  [TestMethod]
78  public void TestSerialize()
79  {
80  const string testFileName = "wiropopipoi";
81  const short notSaved1 = 9;
82  const double notSaved2 = Math.PI;
83 
84  TestClass testObj = new TestClass( 47, "C#", "Jypeli" );
85  testObj.Attr2 = 'k';
86  testObj.DoNotSave1 = 6;
87  testObj.DoNotSave2 = 2.41;
88 
89  foreach ( var storage in EnumerateStorages() )
90  {
91  storage.Delete( testFileName );
92  Assert.IsFalse( storage.Exists( testFileName ), "Test file still exists after deletion (#1) for " + storage.GetType().Name );
93 
94  storage.Save<TestClass>( testObj, testFileName );
95  Assert.IsTrue( storage.Exists( testFileName ), "Test file does not exist after creation for " + storage.GetType().Name );
96 
97  TestClass loaded = new TestClass( -1, "vanha1", "vanha2" );
98  loaded.DoNotSave1 = notSaved1;
99  loaded.DoNotSave2 = notSaved2;
100  storage.Load<TestClass>( loaded, testFileName );
101  Assert.AreEqual( testObj.Attr1, loaded.Attr1, "Attr1 value mismatch in " + storage.GetType().Name );
102  Assert.AreEqual( testObj.Attr2, loaded.Attr2, "Attr2 value mismatch in " + storage.GetType().Name );
103  Assert.AreEqual( testObj.GetNonPublic1(), loaded.GetNonPublic1(), "NonPublic1 value mismatch in " + storage.GetType().Name );
104  Assert.AreEqual( testObj.GetNonPublic2(), loaded.GetNonPublic2(), "NonPublic2 value mismatch in " + storage.GetType().Name );
105  Assert.AreEqual( notSaved1, loaded.DoNotSave1, "DoNotSave1 value mismatch in " + storage.GetType().Name );
106  Assert.AreEqual( notSaved2, loaded.DoNotSave2, "DoNotSave2 value mismatch in " + storage.GetType().Name );
107 
108  storage.Delete( testFileName );
109  Assert.IsFalse( storage.Exists( testFileName ), "Test file still exists after deletion (#2) for " + storage.GetType().Name );
110  }
111  }
112  }
113 }
static readonly string MyDocuments
Omat tiedostot.
Usein käytettyjä polkuja Windowsissa.