1 // ================================================================================================
3 // IgnoreCaseDictionaryのテストクラスソース。</summary>
5 // <copyright file="IgnoreCaseDictionaryTest.cs" company="honeplusのメモ帳">
6 // Copyright (C) 2012 Honeplus. All rights reserved.</copyright>
9 // ================================================================================================
11 namespace Honememo.Models
14 using System.Collections.Generic;
15 using Microsoft.VisualStudio.TestTools.UnitTesting;
18 /// <see cref="IgnoreCaseDictionary<TValue>"/>のテストクラスです。
21 public class IgnoreCaseDictionaryTest
26 /// コンストラクタテストケース(引数なし)。
29 public void TestConstructor()
31 IgnoreCaseDictionary<string> d = new IgnoreCaseDictionary<string>();
32 Assert.AreEqual(0, d.Dictionary.Count);
36 /// コンストラクタテストケース(引数Dictionary)。
39 public void TestConstructorDictionary()
41 IDictionary<string, string> inner = new Dictionary<string, string>();
42 inner.Add("TestKey", "TestValue");
43 IgnoreCaseDictionary<string> d = new IgnoreCaseDictionary<string>(inner);
44 Assert.AreEqual(inner, d.Dictionary);
45 Assert.IsTrue(d.ContainsKey("TESTkey"));
49 /// コンストラクタテストケース(引数Dictionary、null値)。
52 [ExpectedException(typeof(ArgumentNullException))]
53 public void TestConstructorDictionaryNull()
55 new IgnoreCaseDictionary<string>(null);
60 #region 独自実装公開プロパティテストケース
63 /// Dictionaryプロパティテストケース(正常系)。
66 public void TestDictionary()
68 IgnoreCaseDictionary<string> d = new IgnoreCaseDictionary<string>();
69 IDictionary<string, string> inner = new Dictionary<string, string>();
70 inner.Add("TestKey", "TestValue");
72 Assert.AreSame(inner, d.Dictionary);
73 Assert.IsTrue(d.ContainsKey("TESTkey"));
77 /// Dictionaryプロパティテストケース(null値)。
80 [ExpectedException(typeof(ArgumentNullException))]
81 public void TestDictionaryNull()
83 IgnoreCaseDictionary<string> d = new IgnoreCaseDictionary<string>();
89 #region ラップするインスタンスを参照するプロパティテストケース
95 public void TestKeys()
97 // ラップするIDictionaryと同じ値であること
98 IDictionary<string, string> inner = new Dictionary<string, string>();
99 inner.Add("TestKey", "TestValue");
100 IgnoreCaseDictionary<string> d = new IgnoreCaseDictionary<string>(inner);
101 Assert.AreEqual(inner.Keys, d.Keys);
105 /// Valuesプロパティテストケース。
108 public void TestValues()
110 // ラップするIDictionaryと同じ値であること
111 IDictionary<string, string> inner = new Dictionary<string, string>();
112 inner.Add("TestKey", "TestValue");
113 IgnoreCaseDictionary<string> d = new IgnoreCaseDictionary<string>(inner);
114 Assert.AreEqual(inner.Values, d.Values);
118 /// Countプロパティテストケース。
121 public void TestCount()
123 // ラップするIDictionaryと同じ値であること
124 IDictionary<string, string> inner = new Dictionary<string, string>();
125 inner.Add("TestKey", "TestValue");
126 IgnoreCaseDictionary<string> d = new IgnoreCaseDictionary<string>(inner);
127 Assert.AreEqual(inner.Count, d.Count);
131 /// IsReadOnlyプロパティテストケース。
134 public void TestIsReadOnly()
136 // ラップするIDictionaryと同じ値であること
137 IDictionary<string, string> inner = new Dictionary<string, string>();
138 inner.Add("TestKey", "TestValue");
139 IgnoreCaseDictionary<string> d = new IgnoreCaseDictionary<string>(inner);
140 Assert.AreEqual(inner.IsReadOnly, d.IsReadOnly);
145 #region 独自実装インデクサーテストケース
148 /// thisインデクサーテストケース(正常系)。
151 public void TestThis()
153 IgnoreCaseDictionary<string> d = new IgnoreCaseDictionary<string>();
154 d["TestKey"] = "TestValue";
155 Assert.AreEqual("TestValue", d["TESTkey"]);
156 Assert.AreEqual("TestValue", d.Dictionary["TestKey"]);
157 d["tESTkEY"] = "TestValue2";
158 Assert.AreEqual("TestValue2", d["TESTkey"]);
159 Assert.AreEqual("TestValue2", d.Dictionary["tESTkEY"]);
160 Assert.IsFalse(d.Dictionary.ContainsKey("TestKey"));
164 /// thisインデクサーテストケース(get、null値)。
167 [ExpectedException(typeof(ArgumentNullException))]
168 public void TestThisGetNull()
170 IgnoreCaseDictionary<string> d = new IgnoreCaseDictionary<string>();
171 string dymmy = d[null];
175 /// thisインデクサーテストケース(set、null値)。
178 [ExpectedException(typeof(ArgumentNullException))]
179 public void TestThisSetNull()
181 IgnoreCaseDictionary<string> d = new IgnoreCaseDictionary<string>();
182 d[null] = "TestValue";
186 /// thisインデクサーテストケース(get、値なし)。
189 [ExpectedException(typeof(KeyNotFoundException))]
190 public void TestThisKeyNotFound()
192 IgnoreCaseDictionary<string> d = new IgnoreCaseDictionary<string>();
193 string dymmy = d["TestKey"];
198 #region 独自実装メソッドテストケース
201 /// Addメソッドテストケース(正常系)。
204 public void TestAdd()
206 // IDictionaryのAddメソッド
207 IgnoreCaseDictionary<string> d = new IgnoreCaseDictionary<string>();
208 Assert.AreEqual(0, d.Count);
209 d.Add("TestKey", "TestValue");
210 Assert.AreEqual(1, d.Count);
211 Assert.AreEqual("TestValue", d["TESTkey"]);
212 Assert.AreEqual("TestValue", d.Dictionary["TestKey"]);
213 Assert.IsFalse(d.Dictionary.ContainsKey("TESTkey"));
214 d.Add("tESTkEY2", "TestValue2");
215 Assert.AreEqual(2, d.Count);
216 Assert.AreEqual("TestValue2", d["TESTkey2"]);
217 Assert.AreEqual("TestValue2", d.Dictionary["tESTkEY2"]);
219 // ICollectionのAddメソッド
220 d.Add(new KeyValuePair<string, string>("tESTkEY3", "TestValue3"));
221 Assert.AreEqual(3, d.Count);
222 Assert.AreEqual("TestValue3", d["TESTkey3"]);
223 Assert.AreEqual("TestValue3", d.Dictionary["tESTkEY3"]);
227 /// Addメソッドテストケース(null値)。
230 [ExpectedException(typeof(ArgumentNullException))]
231 public void TestAddNull()
233 new IgnoreCaseDictionary<string>().Add(null, "TestValue");
237 /// Addメソッドテストケース(KeyValuePairのKeyがnull)。
240 [ExpectedException(typeof(ArgumentNullException))]
241 public void TestAddKeyNull()
243 new IgnoreCaseDictionary<string>().Add(new KeyValuePair<string, string>(null, "TestValue"));
247 /// Addメソッドテストケース(重複)。
250 [ExpectedException(typeof(ArgumentException))]
251 public void TestAddDuplicate()
253 // 大文字小文字が異なるキーでも重複と判断する
254 IgnoreCaseDictionary<string> d = new IgnoreCaseDictionary<string>();
255 d.Add("TestKey", "TestValue");
256 d.Add("tESTkEY", "TestValue2");
260 /// Addメソッドテストケース(KeyValuePairのKeyが重複)。
263 [ExpectedException(typeof(ArgumentException))]
264 public void TestAddKeyDuplicate()
266 // 大文字小文字が異なるキーでも重複と判断する
267 IgnoreCaseDictionary<string> d = new IgnoreCaseDictionary<string>();
268 d.Add(new KeyValuePair<string, string>("TestKey", "TestValue"));
269 d.Add(new KeyValuePair<string, string>("tESTkEY", "TestValue2"));
273 /// ContainsKeyメソッドテストケース(正常系)。
276 public void TestContainsKey()
278 IgnoreCaseDictionary<string> d = new IgnoreCaseDictionary<string>();
279 Assert.IsFalse(d.ContainsKey("TestKey"));
280 d.Add("TestKey", "TestValue");
281 Assert.IsTrue(d.ContainsKey("TestKey"));
282 Assert.IsTrue(d.ContainsKey("tESTkEy"));
283 Assert.IsFalse(d.ContainsKey("TestKey2"));
287 /// ContainsKeyメソッドテストケース(null値)。
290 [ExpectedException(typeof(ArgumentNullException))]
291 public void TestContainsKeyNull()
293 new IgnoreCaseDictionary<string>().ContainsKey(null);
297 /// Removeメソッドテストケース(正常系)。
300 public void TestRemove()
302 // IDictionaryのRemoveメソッド
303 IgnoreCaseDictionary<string> d = new IgnoreCaseDictionary<string>();
304 d.Add("TestKey", "TestValue");
305 d.Add("tESTkEY2", "TestValue2");
306 Assert.AreEqual(2, d.Count);
307 Assert.IsTrue(d.ContainsKey("TestKey"));
308 Assert.IsTrue(d.ContainsKey("tESTkEY2"));
310 Assert.IsTrue(d.Remove("tEstKey"));
311 Assert.AreEqual(1, d.Count);
312 Assert.IsFalse(d.ContainsKey("TestKey"));
313 Assert.IsTrue(d.ContainsKey("tESTkEY2"));
315 Assert.IsFalse(d.Remove("tESTkEY3"));
316 Assert.AreEqual(1, d.Count);
317 Assert.IsTrue(d.ContainsKey("tESTkEY2"));
319 // ICollectionのRemoveメソッド
320 Assert.IsFalse(d.Remove(new KeyValuePair<string, string>("tESTkEY3", "TestValue2")));
321 Assert.AreEqual(1, d.Count);
322 Assert.IsTrue(d.ContainsKey("tESTkEY2"));
324 Assert.IsTrue(d.Remove(new KeyValuePair<string, string>("TestKey2", "TestValue2")));
325 Assert.AreEqual(0, d.Count);
326 Assert.IsFalse(d.ContainsKey("tESTkEY2"));
330 /// Removeメソッドテストケース(null値)。
333 [ExpectedException(typeof(ArgumentNullException))]
334 public void TestRemoveNull()
336 new IgnoreCaseDictionary<string>().Remove(null);
340 /// TryGetValueメソッドテストケース(正常系)。
343 public void TestTryGetValue()
345 IgnoreCaseDictionary<string> d = new IgnoreCaseDictionary<string>();
348 Assert.IsFalse(d.TryGetValue("TestKey", out value));
349 Assert.IsNull(value);
351 d.Add("TestKey", "TestValue");
352 Assert.IsTrue(d.TryGetValue("TestKey", out value));
353 Assert.AreEqual("TestValue", value);
355 Assert.IsFalse(d.TryGetValue("TestKey2", out value));
356 Assert.IsNull(value);
358 Assert.IsTrue(d.TryGetValue("tESTkEy", out value));
359 Assert.AreEqual("TestValue", value);
363 /// TryGetValueメソッドテストケース(null値)。
366 [ExpectedException(typeof(ArgumentNullException))]
367 public void TestTryGetValueNull()
370 new IgnoreCaseDictionary<string>().TryGetValue(null, out dummy);
377 public void TestClear()
379 // 全データが削除されること、またラップしているオブジェクトは維持されること
380 IDictionary<string, string> inner = new Dictionary<string, string>();
381 IgnoreCaseDictionary<string> d = new IgnoreCaseDictionary<string>(inner);
382 d.Add("TestKey", "TestValue");
383 d.Add("tESTkEY2", "TestValue2");
384 Assert.AreEqual(2, d.Count);
385 Assert.AreSame(inner, d.Dictionary);
388 Assert.AreEqual(0, d.Count);
389 Assert.AreSame(inner, d.Dictionary);
393 /// Containsメソッドテストケース。
396 public void TestContains()
398 IgnoreCaseDictionary<string> d = new IgnoreCaseDictionary<string>();
399 Assert.IsFalse(d.Contains(new KeyValuePair<string, string>("TestKey", "TestValue")));
400 d.Add("TestKey", "TestValue");
401 Assert.IsTrue(d.Contains(new KeyValuePair<string, string>("TestKey", "TestValue")));
402 Assert.IsTrue(d.Contains(new KeyValuePair<string, string>("tESTkEy", "TestValue")));
403 Assert.IsFalse(d.Contains(new KeyValuePair<string, string>("TestKey2", "TestValue")));
404 Assert.IsFalse(d.Contains(new KeyValuePair<string, string>(null, "TestValue")));
408 /// CopyToメソッドテストケース(正常系)。
411 public void TestCopyTo()
413 IgnoreCaseDictionary<string> d = new IgnoreCaseDictionary<string>();
414 d.Add("TestKey", "TestValue");
415 d.Add("tESTkEY2", "TestValue2");
416 KeyValuePair<string, string>[] array = new KeyValuePair<string, string>[5];
418 Assert.IsNull(array[0].Key);
419 Assert.IsNull(array[0].Value);
420 Assert.IsNull(array[1].Key);
421 Assert.IsNull(array[1].Value);
422 Assert.IsNull(array[2].Key);
423 Assert.IsNull(array[2].Value);
424 Assert.AreEqual("TestKey", array[3].Key);
425 Assert.AreEqual("TestValue", array[3].Value);
426 Assert.AreEqual("tESTkEY2", array[4].Key);
427 Assert.AreEqual("TestValue2", array[4].Value);
431 /// CopyToメソッドテストケース(null値)。
434 [ExpectedException(typeof(ArgumentNullException))]
435 public void TestCopyToNull()
437 new IgnoreCaseDictionary<string>().CopyTo(null, 0);
441 /// CopyToメソッドテストケース(インデックスがマイナス値)。
444 [ExpectedException(typeof(ArgumentOutOfRangeException))]
445 public void TestCopyToOutOfRange()
447 new IgnoreCaseDictionary<string>().CopyTo(new KeyValuePair<string, string>[5], -1);
451 /// CopyToメソッドテストケース(領域不足)。
454 [ExpectedException(typeof(ArgumentException))]
455 public void TestCopyToOverflow()
457 IgnoreCaseDictionary<string> d = new IgnoreCaseDictionary<string>();
458 d.Add("TestKey", "TestValue");
459 d.Add("tESTkEY2", "TestValue2");
460 d.CopyTo(new KeyValuePair<string, string>[5], 4);
465 #region ラップするインスタンスを参照するメソッドテストケース
468 /// GetEnumeratorメソッドテストケース。
471 public void TestGetEnumerator()
473 // ラップするIDictionaryと同じ値であること
474 IDictionary<string, string> inner = new Dictionary<string, string>();
475 inner.Add("TestKey", "TestValue");
476 IgnoreCaseDictionary<string> d = new IgnoreCaseDictionary<string>(inner);
477 Assert.AreEqual(inner.GetEnumerator(), d.GetEnumerator());