• R/O
  • SSH
  • HTTPS

Commit

Frequently used words (click to add to your profile)

javaandroidc++linuxc#objective-ccocoa誰得qtrubybathyscaphegamephpguicwindows翻訳pythonomegattwitterframeworkbtronarduinovb.net計画中(planning stage)directxpreviewertestゲームエンジンdom

FreeTrainの進化系を目指す


Commit MetaInfo

Revision69 (tree)
Zeit2016-12-18 17:31:36
Autorc477

Log Message

UnitTestプロジェクトの追加

Ändern Zusammenfassung

Diff

--- trunk/TestCoreProject/Properties/AssemblyInfo.cs (nonexistent)
+++ trunk/TestCoreProject/Properties/AssemblyInfo.cs (revision 69)
@@ -0,0 +1,36 @@
1+using System.Reflection;
2+using System.Runtime.CompilerServices;
3+using System.Runtime.InteropServices;
4+
5+// General Information about an assembly is controlled through the following
6+// set of attributes. Change these attribute values to modify the information
7+// associated with an assembly.
8+[assembly: AssemblyTitle("TestCoreProject")]
9+[assembly: AssemblyDescription("")]
10+[assembly: AssemblyConfiguration("")]
11+[assembly: AssemblyCompany("")]
12+[assembly: AssemblyProduct("TestCoreProject")]
13+[assembly: AssemblyCopyright("Copyright © 2016")]
14+[assembly: AssemblyTrademark("")]
15+[assembly: AssemblyCulture("")]
16+
17+// Setting ComVisible to false makes the types in this assembly not visible
18+// to COM components. If you need to access a type in this assembly from
19+// COM, set the ComVisible attribute to true on that type.
20+[assembly: ComVisible(false)]
21+
22+// The following GUID is for the ID of the typelib if this project is exposed to COM
23+[assembly: Guid("26224857-53c2-443e-8c3f-c02228bf6823")]
24+
25+// Version information for an assembly consists of the following four values:
26+//
27+// Major Version
28+// Minor Version
29+// Build Number
30+// Revision
31+//
32+// You can specify all the values or you can default the Build and Revision Numbers
33+// by using the '*' as shown below:
34+// [assembly: AssemblyVersion("1.0.*")]
35+[assembly: AssemblyVersion("1.0.0.0")]
36+[assembly: AssemblyFileVersion("1.0.0.0")]
--- trunk/TestCoreProject/TestExample.cs (nonexistent)
+++ trunk/TestCoreProject/TestExample.cs (revision 69)
@@ -0,0 +1,39 @@
1+using System;
2+using Microsoft.VisualStudio.TestTools.UnitTesting;
3+using static System.Diagnostics.Trace;
4+using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert;
5+using System.Collections.Generic;
6+
7+namespace nft.test
8+{
9+ [TestClass]
10+ public class TestExample
11+ {
12+
13+ [TestMethod]
14+ public void TestMethod1()
15+ {
16+ Console.WriteLine("テスト中のコンソール出力は Test Explorer の Output をクリック");
17+ // Doubleは第三引数で許容誤差を指定できる
18+ AreEqual(0.1, 0.11, 0.01, "誤算0.01以内に収まりません");
19+
20+ var expected = new List<int> { 1, 1, 2, 3, 4, 5 };
21+ // コレクション専用のAssertクラスあり
22+ CollectionAssert.AreEquivalent(expected, new List<int> { 1, 5, 4, 3, 2, 1 });
23+ }
24+
25+ [AssemblyInitialize]
26+ public static void AssemblyInit(TestContext c) => WriteLine("Assembly単位の初期化処理");
27+ [ClassInitialize]
28+ public static void ClassInit(TestContext c) => WriteLine("TestClass単位の初期化処理 ", c.TestName);
29+ [TestInitialize]
30+ public void TestInitialize() => WriteLine("Testメソッド単位の初期化処理");
31+ [TestCleanup]
32+ public void TestCleanup() => WriteLine("Testメソッド単位の解放処理");
33+ [ClassCleanup]
34+ public static void ClassCleanup() => WriteLine("TestClass単位の解放処理");
35+ [AssemblyCleanup]
36+ public static void AssemblyCleanup() => WriteLine("Assemblyの解放処理");
37+
38+ }
39+}
--- trunk/TestCoreProject/core/RepositoryBaseTest.cs (nonexistent)
+++ trunk/TestCoreProject/core/RepositoryBaseTest.cs (revision 69)
@@ -0,0 +1,131 @@
1+using System;
2+using Microsoft.VisualStudio.TestTools.UnitTesting;
3+using nft.core.data;
4+using nft.framework;
5+using static System.Diagnostics.Trace;
6+using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert;
7+using System.Collections.Generic;
8+using System.Linq;
9+
10+namespace TestCoreProject.core
11+{
12+
13+ [TestClass]
14+ public class RepositoryBaseTest
15+ {
16+ private TestRepository repository;
17+
18+ [TestMethod]
19+ public void RepositoryBase()
20+ {
21+ this.repository = new TestRepository();
22+ WriteLine("TestRepository created.");
23+
24+ AreEqual(repository.Count, 0, "最初はCount=0");
25+
26+ string id0 = "999";
27+ TestData data0 = new TestData(id0, $"#{id0}");
28+
29+ repository.Add(data0);
30+ AreEqual(repository.Count, 1, "追加したのでCount=1");
31+ AreEqual(repository[id0], data0, "ID=999で登録したオブジェクトが取得できること");
32+ AreEqual(repository["000"], null, "存在しないID=000でnullが取得できること");
33+ AreEqual(repository.RemoveByID("000"), null, "存在しないID=000で削除してもエラーにならずnullが返却されること");
34+
35+ List<IHasNameAndID> list = repository.List.ToList<IHasNameAndID>();
36+ CollectionAssert.AreEqual(list, new List<IHasNameAndID> { data0 });
37+
38+ TestData removed = repository.RemoveByID(id0);
39+ AreEqual(repository.Count, 0, "削除したのでCount=0");
40+ AreEqual(removed, data0, "ID=999で登録したオブジェクトが削除されていること");
41+
42+ WriteLine("複数要素追加のテスト");
43+ Dictionary<string, IHasNameAndID> savedData = new Dictionary<string, IHasNameAndID>();
44+ TestData data;
45+ for (int i = 1; i < 10; i++)
46+ { // 1~9まで追加していき、数とリストの中身が正しいことを確認
47+ string id = $"{i}";
48+ data = new TestData(id, $"#{id}");
49+ savedData.Add(id, data);
50+ repository.Add(data);
51+ AreEqual(repository.Count, savedData.Count);
52+ CollectionAssert.AreEqual(repository.List.ToList<IHasNameAndID>(), savedData.Values);
53+ }
54+ for (int i = 1; i < 10; i++)
55+ { // 一部のデータを削除しつつ、idとの紐付けが正しいことを確認
56+ string id = $"{i}";
57+ switch (i % 3)
58+ { // 3パターンを試す
59+ case 0: // 何もせず比較
60+ AreEqual(repository[id], savedData[id]);
61+ break;
62+ case 1: // 比較して削除
63+ AreEqual(repository[id], savedData[id]);
64+ data = repository.RemoveByID(id);
65+ AreEqual(data, savedData[id]);
66+ AreEqual(repository[id], null);
67+ break;
68+ case 2: // 削除後に比較
69+ data = repository.RemoveByID(id);
70+ AreEqual(data, savedData[id]);
71+ AreEqual(repository[id], null);
72+ break;
73+ }
74+ }
75+ this.repository = null;
76+ }
77+
78+ [TestMethod]
79+ public void IModelRepositoryExtension()
80+ {
81+ this.repository = new TestRepository();
82+ WriteLine("IModelRepository拡張メソッドのテスト");
83+ Dictionary<string, TestData> savedData = new Dictionary<string, TestData>();
84+ TestData data;
85+ for (int i = 1; i < 10; i++)
86+ { // 1~9まで追加していき、数とリストの中身が正しいことを確認
87+ string id = $"{i}";
88+ data = new TestData(id, $"#{id}");
89+ savedData.Add(id, data);
90+ repository.Add(data);
91+ CollectionAssert.AreEqual(repository.List.ToList<IHasNameAndID>(), savedData.Values);
92+ }
93+ TestData _default = new TestData("default", "default");
94+ // 拡張メソッド findByID, hasID, removeByID
95+ for (int i = 1; i < 10; i++)
96+ { // 一部のデータを削除しつつ、idとの紐付けが正しいことを確認
97+ string id = $"{i}";
98+ switch (i % 3)
99+ {
100+ case 0: // 何もせず比較
101+ AreEqual(repository.findByID(id, _default), savedData[id]);
102+ break;
103+ default: // 比較して削除
104+ AreEqual(repository.findByID(id, _default), savedData[id]);
105+ repository.Remove(savedData[id]);
106+ AreEqual(repository.findByID(id, _default), _default);
107+ break;
108+ }
109+ }
110+ this.repository = null;
111+ }
112+ }
113+
114+ class TestData : IHasNameAndID
115+ {
116+ private readonly string id;
117+ private readonly string name;
118+ public string ID { get { return id; } }
119+ public string Name { get { return name; } }
120+ public TestData(string id, string name)
121+ {
122+ this.id = id;
123+ this.name = name;
124+ }
125+ }
126+
127+ class TestRepository : RepositoryBase<TestData>
128+ {
129+
130+ }
131+}
--- trunk/core/core/data/IModelRepository.cs (revision 68)
+++ trunk/core/core/data/IModelRepository.cs (revision 69)
@@ -74,9 +74,9 @@
7474 return repository[model.ID] != null;
7575 }
7676
77- public static T findByID<T>(this IRepository<T> repository, T model, T _default = null) where T : class, IHasNameAndID
77+ public static T findByID<T>(this IRepository<T> repository, string id, T _default = null) where T : class, IHasNameAndID
7878 {
79- return repository[model.ID] ?? _default;
79+ return repository[id] ?? _default;
8080 }
8181 }
8282 }