OSDN Git Service

#27621 言語間リンク取得時に<includeonly>, <noinclude>を考慮するよう修正,
[wptscs/wpts.git] / HmLibTest / Utilities / ValidateTest.cs
1 // ================================================================================================
2 // <summary>
3 //      Validateのテストクラスソース。</summary>
4 //
5 // <copyright file="ValidateTest.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 System.Collections.Generic;
15     using NUnit.Framework;
16
17     /// <summary>
18     /// <see cref="Validate"/>のテストクラスです。
19     /// </summary>
20     [TestFixture]
21     class ValidateTest
22     {
23         #region NotNullメソッドテストケース
24
25         /// <summary>
26         /// <see cref="Validate.NotNull&lt;T&gt;(T, string)"/>メソッドテストケース(正常系)。
27         /// </summary>
28         [Test]
29         public void TestNotNull()
30         {
31             // パラメータ名指定無し
32             Assert.AreEqual(String.Empty, Validate.NotNull(String.Empty));
33             Assert.AreEqual("not null", Validate.NotNull("not null"));
34
35             // パラメータ名指定有り
36             Assert.AreEqual(String.Empty, Validate.NotNull(String.Empty, null));
37             Assert.AreEqual(String.Empty, Validate.NotNull(String.Empty, "test"));
38             Assert.AreEqual("not null", Validate.NotNull("not null", "test"));
39         }
40
41         /// <summary>
42         /// <see cref="Validate.NotNull&lt;T&gt;(T, string)"/>メソッドテストケース(異常系)。
43         /// </summary>
44         [Test]
45         public void TestNotNullNg()
46         {
47             // obj = nullのチェック
48             try
49             {
50                 // パラメータ名指定無し
51                 Validate.NotNull<object>(null);
52                 Assert.Fail("expected ArgumentNullException");
53             }
54             catch (ArgumentNullException ex)
55             {
56                 Assert.AreEqual("value", ex.ParamName);
57             }
58
59             // 例外パラメータ名の確認
60             try
61             {
62                 // パラメータ名指定有り
63                 Validate.NotNull<object>(null, "test");
64                 Assert.Fail("expected ArgumentNullException");
65             }
66             catch (ArgumentNullException ex)
67             {
68                 Assert.AreEqual("test", ex.ParamName);
69             }
70
71             try
72             {
73                 // パラメータ名指定有りnull
74                 Validate.NotNull<object>(null, null);
75                 Assert.Fail("expected ArgumentNullException");
76             }
77             catch (ArgumentNullException ex)
78             {
79                 Assert.IsNull(ex.ParamName);
80             }
81         }
82
83         #endregion
84
85         #region NotEmptyメソッドテストケース
86
87         /// <summary>
88         /// <see cref="Validate.NotEmpty(string, string)"/>メソッドテストケース(正常系)。
89         /// </summary>
90         [Test]
91         public void TestNotEmpty()
92         {
93             // パラメータ名指定無し
94             Assert.AreEqual("not empty", Validate.NotEmpty("not empty"));
95
96             // パラメータ名指定有り
97             Assert.AreEqual("not empty", Validate.NotEmpty("not empty", "test"));
98         }
99
100         /// <summary>
101         /// <see cref="Validate.NotEmpty(string, string)"/>メソッドテストケース(異常系)。
102         /// </summary>
103         [Test]
104         public void TestNotEmptyNg()
105         {
106             // str = nullのチェック
107             try
108             {
109                 // パラメータ名指定無し
110                 Validate.NotEmpty(null);
111                 Assert.Fail("expected ArgumentNullException");
112             }
113             catch (ArgumentNullException ex)
114             {
115                 Assert.AreEqual("value", ex.ParamName);
116             }
117
118             // 例外パラメータ名の確認
119             try
120             {
121                 // パラメータ名指定有り
122                 Validate.NotEmpty(null, "test");
123                 Assert.Fail("expected ArgumentNullException");
124             }
125             catch (ArgumentNullException ex)
126             {
127                 Assert.AreEqual("test", ex.ParamName);
128             }
129
130             try
131             {
132                 // パラメータ名指定有りnull
133                 Validate.NotEmpty(null, null);
134                 Assert.Fail("expected ArgumentNullException");
135             }
136             catch (ArgumentNullException ex)
137             {
138                 Assert.IsNull(ex.ParamName);
139             }
140
141             // 空文字列のチェック
142             try
143             {
144                 // パラメータ名指定無し
145                 Validate.NotEmpty(String.Empty);
146                 Assert.Fail("expected ArgumentException");
147             }
148             catch (ArgumentException ex)
149             {
150                 Assert.AreEqual("value", ex.ParamName);
151             }
152
153             // 例外パラメータ名の確認
154             try
155             {
156                 // パラメータ名指定有り
157                 Validate.NotEmpty(String.Empty, "test");
158                 Assert.Fail("expected ArgumentException");
159             }
160             catch (ArgumentException ex)
161             {
162                 Assert.AreEqual("test", ex.ParamName);
163             }
164         }
165
166         #endregion
167
168         #region NotBlankメソッドテストケース
169
170         /// <summary>
171         /// <see cref="Validate.NotBlank(string, string)"/>メソッドテストケース(正常系)。
172         /// </summary>
173         [Test]
174         public void TestNotBlank()
175         {
176             // パラメータ名指定無し
177             Assert.AreEqual("not blank", Validate.NotBlank("not blank"));
178
179             // パラメータ名指定有り
180             Assert.AreEqual("not blank", Validate.NotBlank("not blank", "test"));
181         }
182
183         /// <summary>
184         /// <see cref="Validate.NotBlank(string, string)"/>メソッドテストケース(異常系)。
185         /// </summary>
186         [Test]
187         public void TestNotBlankNg()
188         {
189             // str = nullのチェック
190             try
191             {
192                 // パラメータ名指定無し
193                 Validate.NotBlank(null);
194                 Assert.Fail("expected ArgumentNullException");
195             }
196             catch (ArgumentNullException ex)
197             {
198                 Assert.AreEqual("value", ex.ParamName);
199             }
200
201             // 例外パラメータ名の確認
202             try
203             {
204                 // パラメータ名指定有り
205                 Validate.NotBlank(null, "test");
206                 Assert.Fail("expected ArgumentNullException");
207             }
208             catch (ArgumentNullException ex)
209             {
210                 Assert.AreEqual("test", ex.ParamName);
211             }
212
213             try
214             {
215                 // パラメータ名指定有りnull
216                 Validate.NotBlank(null, null);
217                 Assert.Fail("expected ArgumentNullException");
218             }
219             catch (ArgumentNullException ex)
220             {
221                 Assert.IsNull(ex.ParamName);
222             }
223
224             // 空白のチェック
225             try
226             {
227                 // パラメータ名指定無し
228                 Validate.NotBlank("  ");
229                 Assert.Fail("expected ArgumentException");
230             }
231             catch (ArgumentException ex)
232             {
233                 Assert.AreEqual("value", ex.ParamName);
234             }
235
236             // 例外パラメータ名の確認
237             try
238             {
239                 // パラメータ名指定有り
240                 Validate.NotBlank("   ", "test");
241                 Assert.Fail("expected ArgumentException");
242             }
243             catch (ArgumentException ex)
244             {
245                 Assert.AreEqual("test", ex.ParamName);
246             }
247         }
248
249         #endregion
250
251         #region InRangeメソッドテストケース
252
253         /// <summary>
254         /// <see cref="Validate.InRange(string, int, string, string)"/>
255         /// メソッドテストケース(正常系)。
256         /// </summary>
257         [Test]
258         public void TestInRangeStr()
259         {
260             // ※ 例外が起きなければOK
261             // パラメータ名指定無し
262             Validate.InRange("1", 0);
263             Validate.InRange("range text", 9);
264
265             // パラメータ名指定有り
266             Validate.InRange("1", 0, "test", "testindex");
267             Validate.InRange("range text", 9, "test", "testindex");
268         }
269
270         /// <summary>
271         /// <see cref="Validate.InRange(string, int, string, string)"/>
272         /// メソッドテストケース(異常系)。
273         /// </summary>
274         [Test]
275         public void TestInRangeStrNg()
276         {
277             // str = nullのチェック
278             string str = null;
279             try
280             {
281                 // パラメータ名指定無し
282                 Validate.InRange(str, 0);
283                 Assert.Fail("expected ArgumentNullException");
284             }
285             catch (ArgumentNullException ex)
286             {
287                 Assert.AreEqual("value", ex.ParamName);
288             }
289
290             // 例外パラメータ名の確認
291             try
292             {
293                 // パラメータ名指定有り
294                 Validate.InRange(str, 0, "test", "testindex");
295                 Assert.Fail("expected ArgumentNullException");
296             }
297             catch (ArgumentNullException ex)
298             {
299                 Assert.AreEqual("test", ex.ParamName);
300             }
301
302             try
303             {
304                 // パラメータ名指定有りnull
305                 Validate.InRange(str, 0, null, null);
306                 Assert.Fail("expected ArgumentNullException");
307             }
308             catch (ArgumentNullException ex)
309             {
310                 Assert.IsNull(ex.ParamName);
311             }
312
313             // indexが範囲外のチェック
314             try
315             {
316                 // パラメータ名指定無し
317                 Validate.InRange(String.Empty, 0);
318                 Assert.Fail("expected ArgumentOutOfRangeException");
319             }
320             catch (ArgumentOutOfRangeException ex)
321             {
322                 Assert.AreEqual("index", ex.ParamName);
323             }
324
325             try
326             {
327                 // パラメータ名指定無し
328                 Validate.InRange("range text", 10);
329                 Assert.Fail("expected ArgumentOutOfRangeException");
330             }
331             catch (ArgumentOutOfRangeException ex)
332             {
333                 Assert.AreEqual("index", ex.ParamName);
334             }
335
336             try
337             {
338                 // パラメータ名指定無し
339                 Validate.InRange("range text", -1);
340                 Assert.Fail("expected ArgumentOutOfRangeException");
341             }
342             catch (ArgumentOutOfRangeException ex)
343             {
344                 Assert.AreEqual("index", ex.ParamName);
345             }
346
347             // 例外パラメータ名の確認
348             try
349             {
350                 // パラメータ名指定有り
351                 Validate.InRange(String.Empty, 0, "test", "testindex");
352                 Assert.Fail("expected ArgumentOutOfRangeException");
353             }
354             catch (ArgumentOutOfRangeException ex)
355             {
356                 Assert.AreEqual("testindex", ex.ParamName);
357             }
358
359             try
360             {
361                 // パラメータ名指定有りnull
362                 Validate.InRange(String.Empty, 0, null, null);
363                 Assert.Fail("expected ArgumentOutOfRangeException");
364             }
365             catch (ArgumentOutOfRangeException ex)
366             {
367                 Assert.IsNull(ex.ParamName);
368             }
369         }
370
371         /// <summary>
372         /// <see cref="Validate.InRange&lt;T&gt;(IList&lt;T&gt;, int, string, string)"/>
373         /// メソッドテストケース(正常系)。
374         /// </summary>
375         [Test]
376         public void TestInRangeIList()
377         {
378             // ※ 例外が起きなければOK
379             // パラメータ名指定無し
380             Validate.InRange(new object[1], 0);
381             Validate.InRange(new object[10], 9);
382
383             // パラメータ名指定有り
384             Validate.InRange(new object[1], 0, "test", "testindex");
385             Validate.InRange(new object[10], 9, "test", "testindex");
386         }
387
388         /// <summary>
389         /// <see cref="Validate.InRange&lt;T&gt;(IList&lt;T&gt;, int, string, string)"/>
390         /// メソッドテストケース(異常系)。
391         /// </summary>
392         [Test]
393         public void TestInRangeIListNg()
394         {
395             // list = nullのチェック
396             object[] list = null;
397             try
398             {
399                 // パラメータ名指定無し
400                 Validate.InRange(list, 0);
401                 Assert.Fail("expected ArgumentNullException");
402             }
403             catch (ArgumentNullException ex)
404             {
405                 Assert.AreEqual("value", ex.ParamName);
406             }
407
408             // 例外パラメータ名の確認
409             try
410             {
411                 // パラメータ名指定有り
412                 Validate.InRange(list, 0, "test", "testindex");
413                 Assert.Fail("expected ArgumentNullException");
414             }
415             catch (ArgumentNullException ex)
416             {
417                 Assert.AreEqual("test", ex.ParamName);
418             }
419
420             try
421             {
422                 // パラメータ名指定有りnull
423                 Validate.InRange(list, 0, null, null);
424                 Assert.Fail("expected ArgumentNullException");
425             }
426             catch (ArgumentNullException ex)
427             {
428                 Assert.IsNull(ex.ParamName);
429             }
430
431             // index範囲外のチェック
432             try
433             {
434                 // パラメータ名指定無し
435                 Validate.InRange(new object[0], 0);
436                 Assert.Fail("expected ArgumentOutOfRangeException");
437             }
438             catch (ArgumentOutOfRangeException ex)
439             {
440                 Assert.AreEqual("index", ex.ParamName);
441             }
442
443             try
444             {
445                 // パラメータ名指定無し
446                 Validate.InRange(new object[10], 10);
447                 Assert.Fail("expected ArgumentOutOfRangeException");
448             }
449             catch (ArgumentOutOfRangeException ex)
450             {
451                 Assert.AreEqual("index", ex.ParamName);
452             }
453
454             try
455             {
456                 // パラメータ名指定無し
457                 Validate.InRange(new object[10], -1);
458                 Assert.Fail("expected ArgumentOutOfRangeException");
459             }
460             catch (ArgumentOutOfRangeException ex)
461             {
462                 Assert.AreEqual("index", ex.ParamName);
463             }
464
465             // 例外パラメータ名の確認
466             try
467             {
468                 // パラメータ名指定有り
469                 Validate.InRange(new object[0], 0, "test", "testindex");
470                 Assert.Fail("expected ArgumentOutOfRangeException");
471             }
472             catch (ArgumentOutOfRangeException ex)
473             {
474                 Assert.AreEqual("testindex", ex.ParamName);
475             }
476
477             try
478             {
479                 // パラメータ名指定有りnull
480                 Validate.InRange(new object[0], 0, null, null);
481                 Assert.Fail("expected ArgumentOutOfRangeException");
482             }
483             catch (ArgumentOutOfRangeException ex)
484             {
485                 Assert.IsNull(ex.ParamName);
486             }
487         }
488
489         #endregion
490     }
491 }