OSDN Git Service

#30244 Visual Studioの自動テストはテストクラスがinternalだと実行されていなかったので修正,
[wptscs/wpts.git] / HmLibTest / Utilities / StatusManagerTest.cs
1 // ================================================================================================
2 // <summary>
3 //      StatusManagerのテストクラスソース。</summary>
4 //
5 // <copyright file="StatusManagerTest.cs" company="honeplusのメモ帳">
6 //      Copyright (C) 2012 Honeplus. All rights reserved.</copyright>
7 // <author>
8 //      Honeplus</author>
9 // ================================================================================================
10
11 namespace Honememo.Utilities
12 {
13     using System;
14     using Microsoft.VisualStudio.TestTools.UnitTesting;
15
16     /// <summary>
17     /// <see cref="StatusManager&lt;T&gt;"/>のテストクラスです。
18     /// </summary>
19     [TestClass]
20     public class StatusManagerTest
21     {
22         #region プロパティテストケース
23
24         /// <summary>
25         /// <see cref="StatusManager&lt;T&gt;.Status"/>プロパティテストケース。
26         /// </summary>
27         [TestMethod]
28         public void TestStatus()
29         {
30             var sm = new StatusManager<string>();
31
32             // 初期状態ではnull
33             Assert.IsNull(sm.Status);
34
35             // 値を設定すればその値が入る
36             sm.Status = "test";
37             Assert.AreEqual("test", sm.Status);
38
39             // 更新時はChangedイベントが呼ばれる
40             bool called = false;
41             sm.Changed += new EventHandler(delegate { called = true; });
42             sm.Status = "test2";
43             Assert.IsTrue(called);
44             Assert.AreEqual("test2", sm.Status);
45
46             // Switchで値が設定されていた場合、Statusを更新すると戻らなくなる
47             sm.Switch("switchstatus");
48             Assert.AreEqual("switchstatus", sm.Status);
49             sm.Status = "newstatus";
50             Assert.AreEqual("newstatus", sm.Status);
51             sm.Dispose();
52             Assert.AreEqual("newstatus", sm.Status);
53         }
54
55         #endregion
56
57         #region 公開メソッドテストケース
58
59         /// <summary>
60         /// <see cref="StatusManager&lt;T&gt;.Switch"/>,
61         /// <see cref="StatusManager&lt;T&gt;.Dispose"/>メソッドテストケース。
62         /// </summary>
63         [TestMethod]
64         public void TestSwitch()
65         {
66             var sm = new StatusManager<string>();
67
68             // 初期状態ではnull
69             Assert.IsNull(sm.Status);
70
71             // Switchでステータスが更新、Disposeで元の値に戻る
72             using (var sm1 = sm.Switch("switch1"))
73             {
74                 Assert.AreEqual("switch1", sm.Status);
75
76                 // 入れ子も可能
77                 using (var sm2 = sm.Switch("switch2"))
78                 {
79                     Assert.AreEqual("switch2", sm.Status);
80
81                     using (var sm3 = sm.Switch("switch3"))
82                     {
83                         Assert.AreEqual("switch3", sm.Status);
84                     }
85
86                     Assert.AreEqual("switch2", sm.Status);
87                 }
88
89                 Assert.AreEqual("switch1", sm.Status);
90
91                 // 設定時と戻り時はChangedイベントが呼ばれる
92                 int count = 0;
93                 sm.Changed += new EventHandler(delegate { ++count; });
94                 Assert.AreEqual(0, count);
95                 using (var sm2 = sm.Switch("switch4"))
96                 {
97                     Assert.AreEqual(1, count);
98                     Assert.AreEqual("switch4", sm.Status);
99                 }
100
101                 Assert.AreEqual(2, count);
102                 Assert.AreEqual("switch1", sm.Status);
103             }
104
105             Assert.IsNull(sm.Status);
106         }
107
108         /// <summary>
109         /// <see cref="StatusManager&lt;T&gt;.Clear"/>メソッドテストケース。
110         /// </summary>
111         [TestMethod]
112         public void TestClear()
113         {
114             var sm = new StatusManager<int>();
115
116             // ステータスを初期状態に戻す
117             Assert.AreEqual(0, sm.Status);
118             sm.Status = 5;
119             Assert.AreEqual(5, sm.Status);
120             sm.Clear();
121             Assert.AreEqual(0, sm.Status);
122
123             // Switchで値が設定されていた場合、Clearすると戻らなくなる
124             sm.Switch(10);
125             Assert.AreEqual(10, sm.Status);
126             sm.Switch(20);
127             Assert.AreEqual(20, sm.Status);
128             sm.Clear();
129             Assert.AreEqual(0, sm.Status);
130             sm.Dispose();
131             Assert.AreEqual(0, sm.Status);
132         }
133
134         #endregion
135     }
136 }