1 | // ****************************************************************
|
---|
2 | // Copyright 2007, Charlie Poole
|
---|
3 | // This is free software licensed under the NUnit license. You may
|
---|
4 | // obtain a copy of the license at http://nunit.org/?p=license&r=2.4
|
---|
5 | // ****************************************************************
|
---|
6 |
|
---|
7 | using System;
|
---|
8 | using System.Collections;
|
---|
9 | using NUnit.Framework.Constraints;
|
---|
10 |
|
---|
11 | namespace NUnit.Framework.Tests
|
---|
12 | {
|
---|
13 | /// <summary>
|
---|
14 | /// This test fixture attempts to exercise all the syntactic
|
---|
15 | /// variations of Assert without getting into failures, errors
|
---|
16 | /// or corner cases. Thus, some of the tests may be duplicated
|
---|
17 | /// in other fixtures.
|
---|
18 | ///
|
---|
19 | /// Each test performs the same operations using the classic
|
---|
20 | /// syntax (if available) and the new syntax in both the
|
---|
21 | /// helper-based and inherited forms.
|
---|
22 | ///
|
---|
23 | /// This Fixture will eventually be duplicated in other
|
---|
24 | /// supported languages.
|
---|
25 | /// </summary>
|
---|
26 | [TestFixture]
|
---|
27 | public class AssertSyntaxTests : AssertionHelper
|
---|
28 | {
|
---|
29 | #region Simple Constraint Tests
|
---|
30 | [Test]
|
---|
31 | public void IsNull()
|
---|
32 | {
|
---|
33 | object nada = null;
|
---|
34 |
|
---|
35 | // Classic syntax
|
---|
36 | Assert.IsNull(nada);
|
---|
37 |
|
---|
38 | // Helper syntax
|
---|
39 | Assert.That(nada, Is.Null);
|
---|
40 |
|
---|
41 | // Inherited syntax
|
---|
42 | Expect(nada, Null);
|
---|
43 | }
|
---|
44 |
|
---|
45 | [Test]
|
---|
46 | public void IsNotNull()
|
---|
47 | {
|
---|
48 | // Classic syntax
|
---|
49 | Assert.IsNotNull(42);
|
---|
50 |
|
---|
51 | // Helper syntax
|
---|
52 | Assert.That(42, Is.Not.Null);
|
---|
53 |
|
---|
54 | // Inherited syntax
|
---|
55 | Expect( 42, Not.Null );
|
---|
56 | }
|
---|
57 |
|
---|
58 | [Test]
|
---|
59 | public void IsTrue()
|
---|
60 | {
|
---|
61 | // Classic syntax
|
---|
62 | Assert.IsTrue(2+2==4);
|
---|
63 |
|
---|
64 | // Helper syntax
|
---|
65 | Assert.That(2+2==4, Is.True);
|
---|
66 | Assert.That(2+2==4);
|
---|
67 |
|
---|
68 | // Inherited syntax
|
---|
69 | Expect(2+2==4, True);
|
---|
70 | Expect(2+2==4);
|
---|
71 | }
|
---|
72 |
|
---|
73 | [Test]
|
---|
74 | public void IsFalse()
|
---|
75 | {
|
---|
76 | // Classic syntax
|
---|
77 | Assert.IsFalse(2+2==5);
|
---|
78 |
|
---|
79 | // Helper syntax
|
---|
80 | Assert.That(2+2== 5, Is.False);
|
---|
81 |
|
---|
82 | // Inherited syntax
|
---|
83 | Expect(2+2==5, False);
|
---|
84 | }
|
---|
85 |
|
---|
86 | [Test]
|
---|
87 | public void IsNaN()
|
---|
88 | {
|
---|
89 | double d = double.NaN;
|
---|
90 | float f = float.NaN;
|
---|
91 |
|
---|
92 | // Classic syntax
|
---|
93 | Assert.IsNaN(d);
|
---|
94 | Assert.IsNaN(f);
|
---|
95 |
|
---|
96 | // Helper syntax
|
---|
97 | Assert.That(d, Is.NaN);
|
---|
98 | Assert.That(f, Is.NaN);
|
---|
99 |
|
---|
100 | // Inherited syntax
|
---|
101 | Expect(d, NaN);
|
---|
102 | Expect(f, NaN);
|
---|
103 | }
|
---|
104 |
|
---|
105 | [Test]
|
---|
106 | public void EmptyStringTests()
|
---|
107 | {
|
---|
108 | // Classic syntax
|
---|
109 | Assert.IsEmpty("");
|
---|
110 | Assert.IsNotEmpty("Hello!");
|
---|
111 |
|
---|
112 | // Helper syntax
|
---|
113 | Assert.That("", Is.Empty);
|
---|
114 | Assert.That("Hello!", Is.Not.Empty);
|
---|
115 |
|
---|
116 | // Inherited syntax
|
---|
117 | Expect("", Empty);
|
---|
118 | Expect("Hello!", Not.Empty);
|
---|
119 | }
|
---|
120 |
|
---|
121 | [Test]
|
---|
122 | public void EmptyCollectionTests()
|
---|
123 | {
|
---|
124 | // Classic syntax
|
---|
125 | Assert.IsEmpty(new bool[0]);
|
---|
126 | Assert.IsNotEmpty(new int[] { 1, 2, 3 });
|
---|
127 |
|
---|
128 | // Helper syntax
|
---|
129 | Assert.That(new bool[0], Is.Empty);
|
---|
130 | Assert.That(new int[] { 1, 2, 3 }, Is.Not.Empty);
|
---|
131 |
|
---|
132 | // Inherited syntax
|
---|
133 | Expect(new bool[0], Empty);
|
---|
134 | Expect(new int[] { 1, 2, 3 }, Not.Empty);
|
---|
135 | }
|
---|
136 | #endregion
|
---|
137 |
|
---|
138 | #region TypeConstraint Tests
|
---|
139 | [Test]
|
---|
140 | public void ExactTypeTests()
|
---|
141 | {
|
---|
142 | // Classic syntax workarounds
|
---|
143 | Assert.AreEqual(typeof(string), "Hello".GetType());
|
---|
144 | Assert.AreEqual("System.String", "Hello".GetType().FullName);
|
---|
145 | Assert.AreNotEqual(typeof(int), "Hello".GetType());
|
---|
146 | Assert.AreNotEqual("System.Int32", "Hello".GetType().FullName);
|
---|
147 |
|
---|
148 | // Helper syntax
|
---|
149 | Assert.That("Hello", Is.TypeOf(typeof(string)));
|
---|
150 | Assert.That("Hello", Is.Not.TypeOf(typeof(int)));
|
---|
151 |
|
---|
152 | // Inherited syntax
|
---|
153 | Expect( "Hello", TypeOf(typeof(string)));
|
---|
154 | Expect( "Hello", Not.TypeOf(typeof(int)));
|
---|
155 | }
|
---|
156 |
|
---|
157 | [Test]
|
---|
158 | public void InstanceOfTypeTests()
|
---|
159 | {
|
---|
160 | // Classic syntax
|
---|
161 | Assert.IsInstanceOf(typeof(string), "Hello");
|
---|
162 | Assert.IsNotInstanceOf(typeof(string), 5);
|
---|
163 |
|
---|
164 | // Helper syntax
|
---|
165 | Assert.That("Hello", Is.InstanceOf(typeof(string)));
|
---|
166 | Assert.That(5, Is.Not.InstanceOf(typeof(string)));
|
---|
167 |
|
---|
168 | // Inherited syntax
|
---|
169 | Expect("Hello", InstanceOf(typeof(string)));
|
---|
170 | Expect(5, Not.InstanceOf(typeof(string)));
|
---|
171 | }
|
---|
172 |
|
---|
173 | [Test]
|
---|
174 | public void AssignableFromTypeTests()
|
---|
175 | {
|
---|
176 | // Classic syntax
|
---|
177 | Assert.IsAssignableFrom(typeof(string), "Hello");
|
---|
178 | Assert.IsNotAssignableFrom(typeof(string), 5);
|
---|
179 |
|
---|
180 | // Helper syntax
|
---|
181 | Assert.That( "Hello", Is.AssignableFrom(typeof(string)));
|
---|
182 | Assert.That( 5, Is.Not.AssignableFrom(typeof(string)));
|
---|
183 |
|
---|
184 | // Inherited syntax
|
---|
185 | Expect( "Hello", AssignableFrom(typeof(string)));
|
---|
186 | Expect( 5, Not.AssignableFrom(typeof(string)));
|
---|
187 | }
|
---|
188 | #endregion
|
---|
189 |
|
---|
190 | #region StringConstraint Tests
|
---|
191 | [Test]
|
---|
192 | public void SubstringTests()
|
---|
193 | {
|
---|
194 | string phrase = "Hello World!";
|
---|
195 | string[] array = new string[] { "abc", "bad", "dba" };
|
---|
196 |
|
---|
197 | // Classic Syntax
|
---|
198 | StringAssert.Contains("World", phrase);
|
---|
199 |
|
---|
200 | // Helper syntax
|
---|
201 | Assert.That(phrase, Text.Contains("World"));
|
---|
202 | // Only available using new syntax
|
---|
203 | Assert.That(phrase, Text.DoesNotContain("goodbye"));
|
---|
204 | Assert.That(phrase, Text.Contains("WORLD").IgnoreCase);
|
---|
205 | Assert.That(phrase, Text.DoesNotContain("BYE").IgnoreCase);
|
---|
206 | Assert.That(array, Text.All.Contains( "b" ) );
|
---|
207 |
|
---|
208 | // Inherited syntax
|
---|
209 | Expect(phrase, Contains("World"));
|
---|
210 | // Only available using new syntax
|
---|
211 | Expect(phrase, Not.Contains("goodbye"));
|
---|
212 | Expect(phrase, Contains("WORLD").IgnoreCase);
|
---|
213 | Expect(phrase, Not.Contains("BYE").IgnoreCase);
|
---|
214 | Expect(array, All.Contains("b"));
|
---|
215 | }
|
---|
216 |
|
---|
217 | [Test]
|
---|
218 | public void StartsWithTests()
|
---|
219 | {
|
---|
220 | string phrase = "Hello World!";
|
---|
221 | string[] greetings = new string[] { "Hello!", "Hi!", "Hola!" };
|
---|
222 |
|
---|
223 | // Classic syntax
|
---|
224 | StringAssert.StartsWith("Hello", phrase);
|
---|
225 |
|
---|
226 | // Helper syntax
|
---|
227 | Assert.That(phrase, Text.StartsWith("Hello"));
|
---|
228 | // Only available using new syntax
|
---|
229 | Assert.That(phrase, Text.DoesNotStartWith("Hi!"));
|
---|
230 | Assert.That(phrase, Text.StartsWith("HeLLo").IgnoreCase);
|
---|
231 | Assert.That(phrase, Text.DoesNotStartWith("HI").IgnoreCase);
|
---|
232 | Assert.That(greetings, Text.All.StartsWith("h").IgnoreCase);
|
---|
233 |
|
---|
234 | // Inherited syntax
|
---|
235 | Expect(phrase, StartsWith("Hello"));
|
---|
236 | // Only available using new syntax
|
---|
237 | Expect(phrase, Not.StartsWith("Hi!"));
|
---|
238 | Expect(phrase, StartsWith("HeLLo").IgnoreCase);
|
---|
239 | Expect(phrase, Not.StartsWith("HI").IgnoreCase);
|
---|
240 | Expect(greetings, All.StartsWith("h").IgnoreCase);
|
---|
241 | }
|
---|
242 |
|
---|
243 | [Test]
|
---|
244 | public void EndsWithTests()
|
---|
245 | {
|
---|
246 | string phrase = "Hello World!";
|
---|
247 | string[] greetings = new string[] { "Hello!", "Hi!", "Hola!" };
|
---|
248 |
|
---|
249 | // Classic Syntax
|
---|
250 | StringAssert.EndsWith("!", phrase);
|
---|
251 |
|
---|
252 | // Helper syntax
|
---|
253 | Assert.That(phrase, Text.EndsWith("!"));
|
---|
254 | // Only available using new syntax
|
---|
255 | Assert.That(phrase, Text.DoesNotEndWith("?"));
|
---|
256 | Assert.That(phrase, Text.EndsWith("WORLD!").IgnoreCase);
|
---|
257 | Assert.That(greetings, Text.All.EndsWith("!"));
|
---|
258 |
|
---|
259 | // Inherited syntax
|
---|
260 | Expect(phrase, EndsWith("!"));
|
---|
261 | // Only available using new syntax
|
---|
262 | Expect(phrase, Not.EndsWith("?"));
|
---|
263 | Expect(phrase, EndsWith("WORLD!").IgnoreCase);
|
---|
264 | Expect(greetings, All.EndsWith("!") );
|
---|
265 | }
|
---|
266 |
|
---|
267 | [Test]
|
---|
268 | public void EqualIgnoringCaseTests()
|
---|
269 | {
|
---|
270 | string phrase = "Hello World!";
|
---|
271 |
|
---|
272 | // Classic syntax
|
---|
273 | StringAssert.AreEqualIgnoringCase("hello world!",phrase);
|
---|
274 |
|
---|
275 | // Helper syntax
|
---|
276 | Assert.That(phrase, Is.EqualTo("hello world!").IgnoreCase);
|
---|
277 | //Only available using new syntax
|
---|
278 | Assert.That(phrase, Is.Not.EqualTo("goodbye world!").IgnoreCase);
|
---|
279 | Assert.That(new string[] { "Hello", "World" },
|
---|
280 | Is.EqualTo(new object[] { "HELLO", "WORLD" }).IgnoreCase);
|
---|
281 | Assert.That(new string[] {"HELLO", "Hello", "hello" },
|
---|
282 | Is.All.EqualTo( "hello" ).IgnoreCase);
|
---|
283 |
|
---|
284 | // Inherited syntax
|
---|
285 | Expect(phrase, EqualTo("hello world!").IgnoreCase);
|
---|
286 | //Only available using new syntax
|
---|
287 | Expect(phrase, Not.EqualTo("goodbye world!").IgnoreCase);
|
---|
288 | Expect(new string[] { "Hello", "World" },
|
---|
289 | EqualTo(new object[] { "HELLO", "WORLD" }).IgnoreCase);
|
---|
290 | Expect(new string[] {"HELLO", "Hello", "hello" },
|
---|
291 | All.EqualTo( "hello" ).IgnoreCase);
|
---|
292 | }
|
---|
293 |
|
---|
294 | [Test]
|
---|
295 | public void RegularExpressionTests()
|
---|
296 | {
|
---|
297 | string phrase = "Now is the time for all good men to come to the aid of their country.";
|
---|
298 | string[] quotes = new string[] { "Never say never", "It's never too late", "Nevermore!" };
|
---|
299 |
|
---|
300 | // Classic syntax
|
---|
301 | StringAssert.IsMatch( "all good men", phrase );
|
---|
302 | StringAssert.IsMatch( "Now.*come", phrase );
|
---|
303 |
|
---|
304 | // Helper syntax
|
---|
305 | Assert.That( phrase, Text.Matches( "all good men" ) );
|
---|
306 | Assert.That( phrase, Text.Matches( "Now.*come" ) );
|
---|
307 | // Only available using new syntax
|
---|
308 | Assert.That(phrase, Text.DoesNotMatch("all.*men.*good"));
|
---|
309 | Assert.That(phrase, Text.Matches("ALL").IgnoreCase);
|
---|
310 | Assert.That(quotes, Text.All.Matches("never").IgnoreCase);
|
---|
311 |
|
---|
312 | // Inherited syntax
|
---|
313 | Expect( phrase, Matches( "all good men" ) );
|
---|
314 | Expect( phrase, Matches( "Now.*come" ) );
|
---|
315 | // Only available using new syntax
|
---|
316 | Expect(phrase, Not.Matches("all.*men.*good"));
|
---|
317 | Expect(phrase, Matches("ALL").IgnoreCase);
|
---|
318 | Expect(quotes, All.Matches("never").IgnoreCase);
|
---|
319 | }
|
---|
320 | #endregion
|
---|
321 |
|
---|
322 | #region Equality Tests
|
---|
323 | [Test]
|
---|
324 | public void EqualityTests()
|
---|
325 | {
|
---|
326 | int[] i3 = new int[] { 1, 2, 3 };
|
---|
327 | double[] d3 = new double[] { 1.0, 2.0, 3.0 };
|
---|
328 | int[] iunequal = new int[] { 1, 3, 2 };
|
---|
329 |
|
---|
330 | // Classic Syntax
|
---|
331 | Assert.AreEqual(4, 2 + 2);
|
---|
332 | Assert.AreEqual(i3, d3);
|
---|
333 | Assert.AreNotEqual(5, 2 + 2);
|
---|
334 | Assert.AreNotEqual(i3, iunequal);
|
---|
335 |
|
---|
336 | // Helper syntax
|
---|
337 | Assert.That(2 + 2, Is.EqualTo(4));
|
---|
338 | Assert.That(2 + 2 == 4);
|
---|
339 | Assert.That(i3, Is.EqualTo(d3));
|
---|
340 | Assert.That(2 + 2, Is.Not.EqualTo(5));
|
---|
341 | Assert.That(i3, Is.Not.EqualTo(iunequal));
|
---|
342 |
|
---|
343 | // Inherited syntax
|
---|
344 | Expect(2 + 2, EqualTo(4));
|
---|
345 | Expect(2 + 2 == 4);
|
---|
346 | Expect(i3, EqualTo(d3));
|
---|
347 | Expect(2 + 2, Not.EqualTo(5));
|
---|
348 | Expect(i3, Not.EqualTo(iunequal));
|
---|
349 | }
|
---|
350 |
|
---|
351 | [Test]
|
---|
352 | public void EqualityTestsWithTolerance()
|
---|
353 | {
|
---|
354 | // CLassic syntax
|
---|
355 | Assert.AreEqual(5.0d, 4.99d, 0.05d);
|
---|
356 | Assert.AreEqual(5.0f, 4.99f, 0.05f);
|
---|
357 |
|
---|
358 | // Helper syntax
|
---|
359 | Assert.That(4.99d, Is.EqualTo(5.0d).Within(0.05d));
|
---|
360 | Assert.That(4.0d, Is.Not.EqualTo(5.0d).Within(0.5d));
|
---|
361 | Assert.That(4.99f, Is.EqualTo(5.0f).Within(0.05f));
|
---|
362 | Assert.That(4.99m, Is.EqualTo(5.0m).Within(0.05m));
|
---|
363 | Assert.That(3999999999u, Is.EqualTo(4000000000u).Within(5u));
|
---|
364 | Assert.That(499, Is.EqualTo(500).Within(5));
|
---|
365 | Assert.That(4999999999L, Is.EqualTo(5000000000L).Within(5L));
|
---|
366 | Assert.That(5999999999ul, Is.EqualTo(6000000000ul).Within(5ul));
|
---|
367 |
|
---|
368 | // Inherited syntax
|
---|
369 | Expect(4.99d, EqualTo(5.0d).Within(0.05d));
|
---|
370 | Expect(4.0d, Not.EqualTo(5.0d).Within(0.5d));
|
---|
371 | Expect(4.99f, EqualTo(5.0f).Within(0.05f));
|
---|
372 | Expect(4.99m, EqualTo(5.0m).Within(0.05m));
|
---|
373 | Expect(499u, EqualTo(500u).Within(5u));
|
---|
374 | Expect(499, EqualTo(500).Within(5));
|
---|
375 | Expect(4999999999L, EqualTo(5000000000L).Within(5L));
|
---|
376 | Expect(5999999999ul, EqualTo(6000000000ul).Within(5ul));
|
---|
377 | }
|
---|
378 |
|
---|
379 | [Test]
|
---|
380 | public void EqualityTestsWithTolerance_MixedFloatAndDouble()
|
---|
381 | {
|
---|
382 | // Bug Fix 1743844
|
---|
383 | Assert.That(2.20492d, Is.EqualTo(2.2d).Within(0.01f),
|
---|
384 | "Double actual, Double expected, Single tolerance");
|
---|
385 | Assert.That(2.20492d, Is.EqualTo(2.2f).Within(0.01d),
|
---|
386 | "Double actual, Single expected, Double tolerance" );
|
---|
387 | Assert.That(2.20492d, Is.EqualTo(2.2f).Within(0.01f),
|
---|
388 | "Double actual, Single expected, Single tolerance" );
|
---|
389 | Assert.That(2.20492f, Is.EqualTo(2.2f).Within(0.01d),
|
---|
390 | "Single actual, Single expected, Double tolerance");
|
---|
391 | Assert.That(2.20492f, Is.EqualTo(2.2d).Within(0.01d),
|
---|
392 | "Single actual, Double expected, Double tolerance");
|
---|
393 | Assert.That(2.20492f, Is.EqualTo(2.2d).Within(0.01f),
|
---|
394 | "Single actual, Double expected, Single tolerance");
|
---|
395 | }
|
---|
396 |
|
---|
397 | [Test]
|
---|
398 | public void EqualityTestsWithTolerance_MixingTypesGenerally()
|
---|
399 | {
|
---|
400 | // Extending tolerance to all numeric types
|
---|
401 | Assert.That(202d, Is.EqualTo(200d).Within(2),
|
---|
402 | "Double actual, Double expected, int tolerance");
|
---|
403 | Assert.That( 4.87m, Is.EqualTo(5).Within(.25),
|
---|
404 | "Decimal actual, int expected, Double tolerance" );
|
---|
405 | Assert.That( 4.87m, Is.EqualTo(5ul).Within(1),
|
---|
406 | "Decimal actual, ulong expected, int tolerance" );
|
---|
407 | Assert.That( 487, Is.EqualTo(500).Within(25),
|
---|
408 | "int actual, int expected, int tolerance" );
|
---|
409 | Assert.That( 487u, Is.EqualTo(500).Within(25),
|
---|
410 | "uint actual, int expected, int tolerance" );
|
---|
411 | Assert.That( 487L, Is.EqualTo(500).Within(25),
|
---|
412 | "long actual, int expected, int tolerance" );
|
---|
413 | Assert.That( 487ul, Is.EqualTo(500).Within(25),
|
---|
414 | "ulong actual, int expected, int tolerance" );
|
---|
415 | }
|
---|
416 | #endregion
|
---|
417 |
|
---|
418 | #region Comparison Tests
|
---|
419 | [Test]
|
---|
420 | public void ComparisonTests()
|
---|
421 | {
|
---|
422 | // Classic Syntax
|
---|
423 | Assert.Greater(7, 3);
|
---|
424 | Assert.GreaterOrEqual(7, 3);
|
---|
425 | Assert.GreaterOrEqual(7, 7);
|
---|
426 |
|
---|
427 | // Helper syntax
|
---|
428 | Assert.That(7, Is.GreaterThan(3));
|
---|
429 | Assert.That(7, Is.GreaterThanOrEqualTo(3));
|
---|
430 | Assert.That(7, Is.AtLeast(3));
|
---|
431 | Assert.That(7, Is.GreaterThanOrEqualTo(7));
|
---|
432 | Assert.That(7, Is.AtLeast(7));
|
---|
433 |
|
---|
434 | // Inherited syntax
|
---|
435 | Expect(7, GreaterThan(3));
|
---|
436 | Expect(7, GreaterThanOrEqualTo(3));
|
---|
437 | Expect(7, AtLeast(3));
|
---|
438 | Expect(7, GreaterThanOrEqualTo(7));
|
---|
439 | Expect(7, AtLeast(7));
|
---|
440 |
|
---|
441 | // Classic syntax
|
---|
442 | Assert.Less(3, 7);
|
---|
443 | Assert.LessOrEqual(3, 7);
|
---|
444 | Assert.LessOrEqual(3, 3);
|
---|
445 |
|
---|
446 | // Helper syntax
|
---|
447 | Assert.That(3, Is.LessThan(7));
|
---|
448 | Assert.That(3, Is.LessThanOrEqualTo(7));
|
---|
449 | Assert.That(3, Is.AtMost(7));
|
---|
450 | Assert.That(3, Is.LessThanOrEqualTo(3));
|
---|
451 | Assert.That(3, Is.AtMost(3));
|
---|
452 |
|
---|
453 | // Inherited syntax
|
---|
454 | Expect(3, LessThan(7));
|
---|
455 | Expect(3, LessThanOrEqualTo(7));
|
---|
456 | Expect(3, AtMost(7));
|
---|
457 | Expect(3, LessThanOrEqualTo(3));
|
---|
458 | Expect(3, AtMost(3));
|
---|
459 | }
|
---|
460 | #endregion
|
---|
461 |
|
---|
462 | #region Collection Tests
|
---|
463 | [Test]
|
---|
464 | public void AllItemsTests()
|
---|
465 | {
|
---|
466 | object[] ints = new object[] { 1, 2, 3, 4 };
|
---|
467 | object[] doubles = new object[] { 0.99, 2.1, 3.0, 4.05 };
|
---|
468 | object[] strings = new object[] { "abc", "bad", "cab", "bad", "dad" };
|
---|
469 |
|
---|
470 | // Classic syntax
|
---|
471 | CollectionAssert.AllItemsAreNotNull(ints);
|
---|
472 | CollectionAssert.AllItemsAreInstancesOfType(ints, typeof(int));
|
---|
473 | CollectionAssert.AllItemsAreInstancesOfType(strings, typeof(string));
|
---|
474 | CollectionAssert.AllItemsAreUnique(ints);
|
---|
475 |
|
---|
476 | // Helper syntax
|
---|
477 | Assert.That(ints, Is.All.Not.Null);
|
---|
478 | Assert.That(ints, Has.None.Null);
|
---|
479 | Assert.That(ints, Is.All.InstanceOfType(typeof(int)));
|
---|
480 | Assert.That(ints, Has.All.InstanceOfType(typeof(int)));
|
---|
481 | Assert.That(strings, Is.All.InstanceOfType(typeof(string)));
|
---|
482 | Assert.That(strings, Has.All.InstanceOfType(typeof(string)));
|
---|
483 | Assert.That(ints, Is.Unique);
|
---|
484 | // Only available using new syntax
|
---|
485 | Assert.That(strings, Is.Not.Unique);
|
---|
486 | Assert.That(ints, Is.All.GreaterThan(0));
|
---|
487 | Assert.That(ints, Has.All.GreaterThan(0));
|
---|
488 | Assert.That(ints, Has.None.LessThanOrEqualTo(0));
|
---|
489 | Assert.That(strings, Text.All.Contains( "a" ) );
|
---|
490 | Assert.That(strings, Has.All.Contains( "a" ) );
|
---|
491 | Assert.That(strings, Has.Some.StartsWith( "ba" ) );
|
---|
492 | Assert.That( strings, Has.Some.Property( "Length" ).EqualTo( 3 ) );
|
---|
493 | Assert.That( strings, Has.Some.StartsWith( "BA" ).IgnoreCase );
|
---|
494 | Assert.That( doubles, Has.Some.EqualTo( 1.0 ).Within( .05 ) );
|
---|
495 |
|
---|
496 | // Inherited syntax
|
---|
497 | Expect(ints, All.Not.Null);
|
---|
498 | Expect(ints, None.Null);
|
---|
499 | Expect(ints, All.InstanceOfType(typeof(int)));
|
---|
500 | Expect(strings, All.InstanceOfType(typeof(string)));
|
---|
501 | Expect(ints, Unique);
|
---|
502 | // Only available using new syntax
|
---|
503 | Expect(strings, Not.Unique);
|
---|
504 | Expect(ints, All.GreaterThan(0));
|
---|
505 | Expect(ints, None.LessThanOrEqualTo(0));
|
---|
506 | Expect(strings, All.Contains( "a" ) );
|
---|
507 | Expect(strings, Some.StartsWith( "ba" ) );
|
---|
508 | Expect(strings, Some.StartsWith( "BA" ).IgnoreCase );
|
---|
509 | Expect(doubles, Some.EqualTo( 1.0 ).Within( .05 ) );
|
---|
510 | }
|
---|
511 |
|
---|
512 | [Test]
|
---|
513 | public void SomeItemTests()
|
---|
514 | {
|
---|
515 | object[] mixed = new object[] { 1, 2, "3", null, "four", 100 };
|
---|
516 | object[] strings = new object[] { "abc", "bad", "cab", "bad", "dad" };
|
---|
517 |
|
---|
518 | // Not available using the classic syntax
|
---|
519 |
|
---|
520 | // Helper syntax
|
---|
521 | Assert.That(mixed, Has.Some.Null);
|
---|
522 | Assert.That(mixed, Has.Some.InstanceOfType(typeof(int)));
|
---|
523 | Assert.That(mixed, Has.Some.InstanceOfType(typeof(string)));
|
---|
524 | Assert.That(strings, Has.Some.StartsWith( "ba" ) );
|
---|
525 | Assert.That(strings, Has.Some.Not.StartsWith( "ba" ) );
|
---|
526 |
|
---|
527 | // Inherited syntax
|
---|
528 | Expect(mixed, Some.Null);
|
---|
529 | Expect(mixed, Some.InstanceOfType(typeof(int)));
|
---|
530 | Expect(mixed, Some.InstanceOfType(typeof(string)));
|
---|
531 | Expect(strings, Some.StartsWith( "ba" ) );
|
---|
532 | Expect(strings, Some.Not.StartsWith( "ba" ) );
|
---|
533 | }
|
---|
534 |
|
---|
535 | [Test]
|
---|
536 | public void NoItemTests()
|
---|
537 | {
|
---|
538 | object[] ints = new object[] { 1, 2, 3, 4, 5 };
|
---|
539 | object[] strings = new object[] { "abc", "bad", "cab", "bad", "dad" };
|
---|
540 |
|
---|
541 | // Not available using the classic syntax
|
---|
542 |
|
---|
543 | // Helper syntax
|
---|
544 | Assert.That(ints, Has.None.Null);
|
---|
545 | Assert.That(ints, Has.None.InstanceOfType(typeof(string)));
|
---|
546 | Assert.That(ints, Has.None.GreaterThan(99));
|
---|
547 | Assert.That(strings, Has.None.StartsWith( "qu" ) );
|
---|
548 |
|
---|
549 | // Inherited syntax
|
---|
550 | Expect(ints, None.Null);
|
---|
551 | Expect(ints, None.InstanceOfType(typeof(string)));
|
---|
552 | Expect(ints, None.GreaterThan(99));
|
---|
553 | Expect(strings, None.StartsWith( "qu" ) );
|
---|
554 | }
|
---|
555 |
|
---|
556 | [Test]
|
---|
557 | public void CollectionContainsTests()
|
---|
558 | {
|
---|
559 | int[] iarray = new int[] { 1, 2, 3 };
|
---|
560 | string[] sarray = new string[] { "a", "b", "c" };
|
---|
561 |
|
---|
562 | // Classic syntax
|
---|
563 | Assert.Contains(3, iarray);
|
---|
564 | Assert.Contains("b", sarray);
|
---|
565 | CollectionAssert.Contains(iarray, 3);
|
---|
566 | CollectionAssert.Contains(sarray, "b");
|
---|
567 | CollectionAssert.DoesNotContain(sarray, "x");
|
---|
568 | // Showing that Contains uses NUnit equality
|
---|
569 | CollectionAssert.Contains( iarray, 1.0d );
|
---|
570 |
|
---|
571 | // Helper syntax
|
---|
572 | Assert.That(iarray, Has.Member(3));
|
---|
573 | Assert.That(sarray, Has.Member("b"));
|
---|
574 | Assert.That(sarray, Has.No.Member("x"));
|
---|
575 | // Showing that Contains uses NUnit equality
|
---|
576 | Assert.That(iarray, Has.Member( 1.0d ));
|
---|
577 |
|
---|
578 | // Only available using the new syntax
|
---|
579 | // Note that EqualTo and SameAs do NOT give
|
---|
580 | // identical results to Contains because
|
---|
581 | // Contains uses Object.Equals()
|
---|
582 | Assert.That(iarray, Has.Some.EqualTo(3));
|
---|
583 | Assert.That(iarray, Has.Member(3));
|
---|
584 | Assert.That(sarray, Has.Some.EqualTo("b"));
|
---|
585 | Assert.That(sarray, Has.None.EqualTo("x"));
|
---|
586 | Assert.That(iarray, Has.None.SameAs( 1.0d ));
|
---|
587 | Assert.That(iarray, Has.All.LessThan(10));
|
---|
588 | Assert.That(sarray, Has.All.Length.EqualTo(1));
|
---|
589 | Assert.That(sarray, Has.None.Property("Length").GreaterThan(3));
|
---|
590 |
|
---|
591 | // Inherited syntax
|
---|
592 | Expect(iarray, Contains(3));
|
---|
593 | Expect(sarray, Contains("b"));
|
---|
594 | Expect(sarray, Not.Contains("x"));
|
---|
595 |
|
---|
596 | // Only available using new syntax
|
---|
597 | // Note that EqualTo and SameAs do NOT give
|
---|
598 | // identical results to Contains because
|
---|
599 | // Contains uses Object.Equals()
|
---|
600 | Expect(iarray, Some.EqualTo(3));
|
---|
601 | Expect(sarray, Some.EqualTo("b"));
|
---|
602 | Expect(sarray, None.EqualTo("x"));
|
---|
603 | Expect(iarray, All.LessThan(10));
|
---|
604 | Expect(sarray, All.Length.EqualTo(1));
|
---|
605 | Expect(sarray, None.Property("Length").GreaterThan(3));
|
---|
606 | }
|
---|
607 |
|
---|
608 | [Test]
|
---|
609 | public void CollectionEquivalenceTests()
|
---|
610 | {
|
---|
611 | int[] ints1to5 = new int[] { 1, 2, 3, 4, 5 };
|
---|
612 | int[] twothrees = new int[] { 1, 2, 3, 3, 4, 5 };
|
---|
613 | int[] twofours = new int[] { 1, 2, 3, 4, 4, 5 };
|
---|
614 |
|
---|
615 | // Classic syntax
|
---|
616 | CollectionAssert.AreEquivalent(new int[] { 2, 1, 4, 3, 5 }, ints1to5);
|
---|
617 | CollectionAssert.AreNotEquivalent(new int[] { 2, 2, 4, 3, 5 }, ints1to5);
|
---|
618 | CollectionAssert.AreNotEquivalent(new int[] { 2, 4, 3, 5 }, ints1to5);
|
---|
619 | CollectionAssert.AreNotEquivalent(new int[] { 2, 2, 1, 1, 4, 3, 5 }, ints1to5);
|
---|
620 | CollectionAssert.AreNotEquivalent(twothrees, twofours);
|
---|
621 |
|
---|
622 | // Helper syntax
|
---|
623 | Assert.That(new int[] { 2, 1, 4, 3, 5 }, Is.EquivalentTo(ints1to5));
|
---|
624 | Assert.That(new int[] { 2, 2, 4, 3, 5 }, Is.Not.EquivalentTo(ints1to5));
|
---|
625 | Assert.That(new int[] { 2, 4, 3, 5 }, Is.Not.EquivalentTo(ints1to5));
|
---|
626 | Assert.That(new int[] { 2, 2, 1, 1, 4, 3, 5 }, Is.Not.EquivalentTo(ints1to5));
|
---|
627 |
|
---|
628 | // Inherited syntax
|
---|
629 | Expect(new int[] { 2, 1, 4, 3, 5 }, EquivalentTo(ints1to5));
|
---|
630 | Expect(new int[] { 2, 2, 4, 3, 5 }, Not.EquivalentTo(ints1to5));
|
---|
631 | Expect(new int[] { 2, 4, 3, 5 }, Not.EquivalentTo(ints1to5));
|
---|
632 | Expect(new int[] { 2, 2, 1, 1, 4, 3, 5 }, Not.EquivalentTo(ints1to5));
|
---|
633 | }
|
---|
634 |
|
---|
635 | [Test]
|
---|
636 | public void SubsetTests()
|
---|
637 | {
|
---|
638 | int[] ints1to5 = new int[] { 1, 2, 3, 4, 5 };
|
---|
639 |
|
---|
640 | // Classic syntax
|
---|
641 | CollectionAssert.IsSubsetOf(new int[] { 1, 3, 5 }, ints1to5);
|
---|
642 | CollectionAssert.IsSubsetOf(new int[] { 1, 2, 3, 4, 5 }, ints1to5);
|
---|
643 | CollectionAssert.IsNotSubsetOf(new int[] { 2, 4, 6 }, ints1to5);
|
---|
644 | CollectionAssert.IsNotSubsetOf(new int[] { 1, 2, 2, 2, 5 }, ints1to5);
|
---|
645 |
|
---|
646 | // Helper syntax
|
---|
647 | Assert.That(new int[] { 1, 3, 5 }, Is.SubsetOf(ints1to5));
|
---|
648 | Assert.That(new int[] { 1, 2, 3, 4, 5 }, Is.SubsetOf(ints1to5));
|
---|
649 | Assert.That(new int[] { 2, 4, 6 }, Is.Not.SubsetOf(ints1to5));
|
---|
650 |
|
---|
651 | // Inherited syntax
|
---|
652 | Expect(new int[] { 1, 3, 5 }, SubsetOf(ints1to5));
|
---|
653 | Expect(new int[] { 1, 2, 3, 4, 5 }, SubsetOf(ints1to5));
|
---|
654 | Expect(new int[] { 2, 4, 6 }, Not.SubsetOf(ints1to5));
|
---|
655 | }
|
---|
656 | #endregion
|
---|
657 |
|
---|
658 | #region Property Tests
|
---|
659 | [Test]
|
---|
660 | public void PropertyTests()
|
---|
661 | {
|
---|
662 | string[] array = { "abc", "bca", "xyz", "qrs" };
|
---|
663 | string[] array2 = { "a", "ab", "abc" };
|
---|
664 | ArrayList list = new ArrayList( array );
|
---|
665 |
|
---|
666 | // Not available using the classic syntax
|
---|
667 |
|
---|
668 | // Helper syntax
|
---|
669 | Assert.That( list, Has.Property( "Count" ) );
|
---|
670 | Assert.That( list, Has.No.Property( "Length" ) );
|
---|
671 |
|
---|
672 | Assert.That( "Hello", Has.Length.EqualTo( 5 ) );
|
---|
673 | Assert.That( "Hello", Has.Length.LessThan( 10 ) );
|
---|
674 | Assert.That( "Hello", Has.Property("Length").EqualTo(5) );
|
---|
675 | Assert.That( "Hello", Has.Property("Length").GreaterThan(3) );
|
---|
676 |
|
---|
677 | Assert.That( array, Has.Property( "Length" ).EqualTo( 4 ) );
|
---|
678 | Assert.That( array, Has.Length.EqualTo( 4 ) );
|
---|
679 | Assert.That( array, Has.Property( "Length" ).LessThan( 10 ) );
|
---|
680 |
|
---|
681 | Assert.That( array, Has.All.Property("Length").EqualTo(3) );
|
---|
682 | Assert.That( array, Has.All.Length.EqualTo( 3 ) );
|
---|
683 | Assert.That( array, Is.All.Length.EqualTo( 3 ) );
|
---|
684 | Assert.That( array, Has.All.Property("Length").EqualTo(3) );
|
---|
685 | Assert.That( array, Is.All.Property("Length").EqualTo(3) );
|
---|
686 |
|
---|
687 | Assert.That( array2, Has.Some.Property("Length").EqualTo(2) );
|
---|
688 | Assert.That( array2, Has.Some.Length.EqualTo(2) );
|
---|
689 | Assert.That( array2, Has.Some.Property("Length").GreaterThan(2) );
|
---|
690 |
|
---|
691 | Assert.That( array2, Is.Not.Property("Length").EqualTo(4) );
|
---|
692 | Assert.That( array2, Is.Not.Length.EqualTo( 4 ) );
|
---|
693 | Assert.That( array2, Has.No.Property("Length").GreaterThan(3) );
|
---|
694 |
|
---|
695 | Assert.That( List.Map( array2 ).Property("Length"), Is.EqualTo( new int[] { 1, 2, 3 } ) );
|
---|
696 | Assert.That( List.Map( array2 ).Property("Length"), Is.EquivalentTo( new int[] { 3, 2, 1 } ) );
|
---|
697 | Assert.That( List.Map( array2 ).Property("Length"), Is.SubsetOf( new int[] { 1, 2, 3, 4, 5 } ) );
|
---|
698 | Assert.That( List.Map( array2 ).Property("Length"), Is.Unique );
|
---|
699 |
|
---|
700 | Assert.That( list, Has.Count.EqualTo( 4 ) );
|
---|
701 |
|
---|
702 | // Inherited syntax
|
---|
703 | Expect( list, Property( "Count" ) );
|
---|
704 | Expect( list, Not.Property( "Nada" ) );
|
---|
705 |
|
---|
706 | Expect( "Hello", Length.EqualTo( 5 ) );
|
---|
707 | Expect( "Hello", Property("Length").EqualTo(5) );
|
---|
708 | Expect( "Hello", Property("Length").GreaterThan(0) );
|
---|
709 |
|
---|
710 | Expect( array, Property("Length").EqualTo(4) );
|
---|
711 | Expect( array, Length.EqualTo(4) );
|
---|
712 | Expect( array, Property("Length").LessThan(10));
|
---|
713 |
|
---|
714 | Expect( array, All.Length.EqualTo( 3 ) );
|
---|
715 | Expect( array, All.Property("Length").EqualTo(3));
|
---|
716 |
|
---|
717 | Expect( array2, Some.Property("Length").EqualTo(2) );
|
---|
718 | Expect( array2, Some.Length.EqualTo( 2 ) );
|
---|
719 | Expect( array2, Some.Property("Length").GreaterThan(2));
|
---|
720 |
|
---|
721 | Expect( array2, None.Property("Length").EqualTo(4) );
|
---|
722 | Expect( array2, None.Length.EqualTo( 4 ) );
|
---|
723 | Expect( array2, None.Property("Length").GreaterThan(3));
|
---|
724 |
|
---|
725 | Expect( Map( array2 ).Property("Length"), EqualTo( new int[] { 1, 2, 3 } ) );
|
---|
726 | Expect( Map( array2 ).Property("Length"), EquivalentTo( new int[] { 3, 2, 1 } ) );
|
---|
727 | Expect( Map( array2 ).Property("Length"), SubsetOf( new int[] { 1, 2, 3, 4, 5 } ) );
|
---|
728 | Expect( Map( array2 ).Property("Length"), Unique );
|
---|
729 |
|
---|
730 | Expect( list, Count.EqualTo( 4 ) );
|
---|
731 |
|
---|
732 | }
|
---|
733 | #endregion
|
---|
734 |
|
---|
735 | #region Not Tests
|
---|
736 | [Test]
|
---|
737 | public void NotTests()
|
---|
738 | {
|
---|
739 | // Not available using the classic syntax
|
---|
740 |
|
---|
741 | // Helper syntax
|
---|
742 | Assert.That(42, Is.Not.Null);
|
---|
743 | Assert.That(42, Is.Not.True);
|
---|
744 | Assert.That(42, Is.Not.False);
|
---|
745 | Assert.That(2.5, Is.Not.NaN);
|
---|
746 | Assert.That(2 + 2, Is.Not.EqualTo(3));
|
---|
747 | Assert.That(2 + 2, Is.Not.Not.EqualTo(4));
|
---|
748 | Assert.That(2 + 2, Is.Not.Not.Not.EqualTo(5));
|
---|
749 |
|
---|
750 | // Inherited syntax
|
---|
751 | Expect(42, Not.Null);
|
---|
752 | Expect(42, Not.True);
|
---|
753 | Expect(42, Not.False);
|
---|
754 | Expect(2.5, Not.NaN);
|
---|
755 | Expect(2 + 2, Not.EqualTo(3));
|
---|
756 | Expect(2 + 2, Not.Not.EqualTo(4));
|
---|
757 | Expect(2 + 2, Not.Not.Not.EqualTo(5));
|
---|
758 | }
|
---|
759 | #endregion
|
---|
760 |
|
---|
761 | #region Operator Tests
|
---|
762 | [Test]
|
---|
763 | public void NotOperator()
|
---|
764 | {
|
---|
765 | // The ! operator is only available in the new syntax
|
---|
766 | Assert.That(42, !Is.Null);
|
---|
767 | // Inherited syntax
|
---|
768 | Expect( 42, !Null );
|
---|
769 | }
|
---|
770 |
|
---|
771 | [Test]
|
---|
772 | public void AndOperator()
|
---|
773 | {
|
---|
774 | // The & operator is only available in the new syntax
|
---|
775 | Assert.That(7, Is.GreaterThan(5) & Is.LessThan(10));
|
---|
776 | // Inherited syntax
|
---|
777 | Expect( 7, GreaterThan(5) & LessThan(10));
|
---|
778 | }
|
---|
779 |
|
---|
780 | [Test]
|
---|
781 | public void OrOperator()
|
---|
782 | {
|
---|
783 | // The | operator is only available in the new syntax
|
---|
784 | Assert.That(3, Is.LessThan(5) | Is.GreaterThan(10));
|
---|
785 | Expect( 3, LessThan(5) | GreaterThan(10));
|
---|
786 | }
|
---|
787 |
|
---|
788 | [Test]
|
---|
789 | public void ComplexTests()
|
---|
790 | {
|
---|
791 | Assert.That(7, Is.Not.Null & Is.Not.LessThan(5) & Is.Not.GreaterThan(10));
|
---|
792 | Expect(7, Not.Null & Not.LessThan(5) & Not.GreaterThan(10));
|
---|
793 |
|
---|
794 | Assert.That(7, !Is.Null & !Is.LessThan(5) & !Is.GreaterThan(10));
|
---|
795 | Expect(7, !Null & !LessThan(5) & !GreaterThan(10));
|
---|
796 |
|
---|
797 | // TODO: Remove #if when mono compiler can handle null
|
---|
798 | #if MONO
|
---|
799 | Constraint x = null;
|
---|
800 | Assert.That(7, !x & !Is.LessThan(5) & !Is.GreaterThan(10));
|
---|
801 | Expect(7, !x & !LessThan(5) & !GreaterThan(10));
|
---|
802 | #else
|
---|
803 | Assert.That(7, !(Constraint)null & !Is.LessThan(5) & !Is.GreaterThan(10));
|
---|
804 | Expect(7, !(Constraint)null & !LessThan(5) & !GreaterThan(10));
|
---|
805 | #endif
|
---|
806 | }
|
---|
807 | #endregion
|
---|
808 |
|
---|
809 | #region Invalid Code Tests
|
---|
810 | // This method contains assertions that should not compile
|
---|
811 | // You can check by uncommenting it.
|
---|
812 | //public void WillNotCompile()
|
---|
813 | //{
|
---|
814 | // Assert.That(42, Is.Not);
|
---|
815 | // Assert.That(42, Is.All);
|
---|
816 | // Assert.That(42, Is.Null.Not);
|
---|
817 | // Assert.That(42, Is.Not.Null.GreaterThan(10));
|
---|
818 | // Assert.That(42, Is.GreaterThan(10).LessThan(99));
|
---|
819 |
|
---|
820 | // object[] c = new object[0];
|
---|
821 | // Assert.That(c, Is.Null.All);
|
---|
822 | // Assert.That(c, Is.Not.All);
|
---|
823 | // Assert.That(c, Is.All.Not);
|
---|
824 | //}
|
---|
825 | #endregion
|
---|
826 | }
|
---|
827 |
|
---|
828 | }
|
---|