~ubuntu-branches/debian/sid/nunit/sid

« back to all changes in this revision

Viewing changes to src/NUnitFramework/tests/EqualsFixture.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2014-09-16 13:43:36 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20140916134336-kjxz48tty6lx2ja5
Tags: 2.6.3+dfsg-1
* [c7bd1b5] Imported Upstream version 2.6.3+dfsg
* [bcb4bf8] Move nunit-console-runner to GAC-installed libnunit2.6, 
  don't treat it as a private lib. This lib is signed, and treated 
  as a GAC lib by consumers such as MonoDevelop.
* [7f08e99] Bump version to 2.6.3 as required
* [84535eb] Refreshed patches
* [8479f61] Split package up into per-assembly packages. This makes 
  ABI tracking easier in the future, as we can meaningfully have GAC 
  policy for cases where ABI isn't truly bumped, and no policy for 
  cases where it is. For example, if nunit.framework bumps ABI but 
  nunit.core does not, previously we would need to rebuild everything 
  using NUnit, but under the new split packaging, that rebuild would 
  not be needed for apps only using nunit.core.
* [17a7dc7] Add missing nunit.mocks.dll to nunit.pc

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// ****************************************************************
2
 
// This is free software licensed under the NUnit license. You
3
 
// may obtain a copy of the license as well as information regarding
4
 
// copyright ownership at http://nunit.org.
5
 
// ****************************************************************
6
 
 
7
 
using System;
8
 
using System.Text.RegularExpressions;
9
 
using System.Globalization;
10
 
using System.IO;
11
 
using System.Threading;
12
 
 
13
 
namespace NUnit.Framework.Tests
14
 
{
15
 
        [TestFixture]
16
 
        public class EqualsFixture : MessageChecker
17
 
        {
18
 
                [Test]
19
 
                public void Equals()
20
 
                {
21
 
                        string nunitString = "Hello NUnit";
22
 
                        string expected = nunitString;
23
 
                        string actual = nunitString;
24
 
 
25
 
                        Assert.IsTrue(expected == actual);
26
 
                        Assert.AreEqual(expected, actual);
27
 
                }
28
 
 
29
 
                [Test]
30
 
                public void EqualsNull() 
31
 
                {
32
 
                        Assert.AreEqual(null, null);
33
 
                }
34
 
                
35
 
                [Test]
36
 
                public void Bug575936Int32Int64Comparison()
37
 
                {
38
 
                        long l64 = 0;
39
 
                        int i32 = 0;
40
 
                        Assert.AreEqual(i32, l64);
41
 
                }
42
 
                
43
 
                [Test]
44
 
                public void IntegerLongComparison()
45
 
                {
46
 
                        Assert.AreEqual(1, 1L);
47
 
                        Assert.AreEqual(1L, 1);
48
 
                }
49
 
 
50
 
                [Test]
51
 
                public void IntegerEquals()
52
 
                {
53
 
                        int val = 42;
54
 
                        Assert.AreEqual(val, 42);
55
 
                }
56
 
 
57
 
                
58
 
                [Test,ExpectedException(typeof(AssertionException))]
59
 
                public void EqualsFail()
60
 
                {
61
 
                        string junitString = "Goodbye JUnit";
62
 
                        string expected = "Hello NUnit";
63
 
 
64
 
                        expectedMessage =
65
 
                                "  Expected string length 11 but was 13. Strings differ at index 0." + Environment.NewLine +
66
 
                                "  Expected: \"Hello NUnit\"" + Environment.NewLine +
67
 
                                "  But was:  \"Goodbye JUnit\"" + Environment.NewLine +
68
 
                                "  -----------^" + Environment.NewLine;
69
 
                        Assert.AreEqual(expected, junitString);
70
 
                }
71
 
                
72
 
                [Test,ExpectedException(typeof(AssertionException))]
73
 
                public void EqualsNaNFails() 
74
 
                {
75
 
                        expectedMessage =
76
 
                                "  Expected: 1.234d +/- 0.0d" + Environment.NewLine +
77
 
                                "  But was:  NaN" + Environment.NewLine;
78
 
                        Assert.AreEqual(1.234, Double.NaN, 0.0);
79
 
                }    
80
 
 
81
 
 
82
 
                [Test]
83
 
                [ExpectedException(typeof(AssertionException))]
84
 
                public void NanEqualsFails() 
85
 
                {
86
 
                        expectedMessage =
87
 
                                "  Expected: NaN" + Environment.NewLine +
88
 
                                "  But was:  1.234d" + Environment.NewLine;
89
 
                        Assert.AreEqual(Double.NaN, 1.234, 0.0);
90
 
                }     
91
 
                
92
 
                [Test]
93
 
                public void NanEqualsNaNSucceeds() 
94
 
                {
95
 
                        Assert.AreEqual(Double.NaN, Double.NaN, 0.0);
96
 
                }     
97
 
 
98
 
                [Test]
99
 
                public void NegInfinityEqualsInfinity() 
100
 
                {
101
 
                        Assert.AreEqual(Double.NegativeInfinity, Double.NegativeInfinity, 0.0);
102
 
                }
103
 
 
104
 
                [Test]
105
 
                public void PosInfinityEqualsInfinity() 
106
 
                {
107
 
                        Assert.AreEqual(Double.PositiveInfinity, Double.PositiveInfinity, 0.0);
108
 
                }
109
 
                
110
 
                [Test,ExpectedException(typeof(AssertionException))]
111
 
                public void PosInfinityNotEquals() 
112
 
                {
113
 
                        expectedMessage =
114
 
                                "  Expected: Infinity" + Environment.NewLine +
115
 
                                "  But was:  1.23d" + Environment.NewLine;
116
 
                        Assert.AreEqual(Double.PositiveInfinity, 1.23, 0.0);
117
 
                }
118
 
 
119
 
                [Test,ExpectedException(typeof(AssertionException))]
120
 
                public void PosInfinityNotEqualsNegInfinity() 
121
 
                {
122
 
                        expectedMessage =
123
 
                                "  Expected: Infinity" + Environment.NewLine +
124
 
                                "  But was:  -Infinity" + Environment.NewLine;
125
 
                        Assert.AreEqual(Double.PositiveInfinity, Double.NegativeInfinity, 0.0);
126
 
                }
127
 
 
128
 
                [Test,ExpectedException(typeof(AssertionException))]    
129
 
                public void SinglePosInfinityNotEqualsNegInfinity() 
130
 
                {
131
 
                        expectedMessage =
132
 
                                "  Expected: Infinity" + Environment.NewLine +
133
 
                                "  But was:  -Infinity" + Environment.NewLine;
134
 
                        Assert.AreEqual(float.PositiveInfinity, float.NegativeInfinity, (float)0.0);
135
 
                }
136
 
 
137
 
                [Test,ExpectedException(typeof(AssertionException))]
138
 
                public void EqualsThrowsException()
139
 
                {
140
 
                        object o = new object();
141
 
                        Assert.Equals(o, o);
142
 
                }
143
 
 
144
 
                [Test,ExpectedException(typeof(AssertionException))]
145
 
                public void ReferenceEqualsThrowsException()
146
 
                {
147
 
                        object o = new object();
148
 
                        Assert.ReferenceEquals(o, o);
149
 
                }
150
 
                
151
 
                [Test]
152
 
                public void Float() 
153
 
                {
154
 
                        float val = (float)1.0;
155
 
                        float expected = val;
156
 
                        float actual = val;
157
 
 
158
 
                        Assert.IsTrue(expected == actual);
159
 
                        Assert.AreEqual(expected, actual, (float)0.0);
160
 
                }
161
 
 
162
 
                [Test]
163
 
                public void Byte() 
164
 
                {
165
 
                        byte val = 1;
166
 
                        byte expected = val;
167
 
                        byte actual = val;
168
 
 
169
 
                        Assert.IsTrue(expected == actual);
170
 
                        Assert.AreEqual(expected, actual);
171
 
                }
172
 
 
173
 
                [Test]
174
 
                public void String() 
175
 
                {
176
 
                        string s1 = "test";
177
 
                        string s2 = new System.Text.StringBuilder(s1).ToString();
178
 
 
179
 
                        Assert.IsTrue(s1.Equals(s2));
180
 
                        Assert.AreEqual(s1,s2);
181
 
                }
182
 
 
183
 
                [Test]
184
 
                public void Short() 
185
 
                {
186
 
                        short val = 1;
187
 
                        short expected = val;
188
 
                        short actual = val;
189
 
 
190
 
                        Assert.IsTrue(expected == actual);
191
 
                        Assert.AreEqual(expected, actual);
192
 
                }
193
 
 
194
 
                [Test]
195
 
                public void Int() 
196
 
                {
197
 
                        int val = 1;
198
 
                        int expected = val;
199
 
                        int actual = val;
200
 
 
201
 
                        Assert.IsTrue(expected == actual);
202
 
                        Assert.AreEqual(expected, actual);
203
 
                }
204
 
 
205
 
                [Test]
206
 
                public void UInt() 
207
 
                {
208
 
                        uint val = 1;
209
 
                        uint expected = val;
210
 
                        uint actual = val;
211
 
 
212
 
                        Assert.IsTrue(expected == actual);
213
 
                        Assert.AreEqual(expected, actual);
214
 
                }
215
 
 
216
 
                [Test]
217
 
                public void Decimal() 
218
 
                {
219
 
                        decimal expected = 100m;
220
 
                        decimal actual = 100.0m;
221
 
                        int integer = 100;
222
 
 
223
 
                        Assert.IsTrue( expected == actual );
224
 
                        Assert.AreEqual(expected, actual);
225
 
                        Assert.IsTrue(expected == integer);
226
 
                        Assert.AreEqual(expected, integer);
227
 
                        Assert.IsTrue(actual == integer);
228
 
                        Assert.AreEqual(actual, integer);
229
 
                }
230
 
 
231
 
 
232
 
                
233
 
                /// <summary>
234
 
                /// Checks to see that a value comparison works with all types.
235
 
                /// Current version has problems when value is the same but the
236
 
                /// types are different...C# is not like Java, and doesn't automatically
237
 
                /// perform value type conversion to simplify this type of comparison.
238
 
                /// 
239
 
                /// Related to Bug575936Int32Int64Comparison, but covers all numeric
240
 
                /// types.
241
 
                /// </summary>
242
 
                [Test]
243
 
                public void EqualsSameTypes()
244
 
                {
245
 
                        byte      b1 = 35;
246
 
                        sbyte    sb2 = 35;
247
 
                        decimal   d4 = 35;
248
 
                        double    d5 = 35;
249
 
                        float     f6 = 35;
250
 
                        int       i7 = 35;
251
 
                        uint      u8 = 35;
252
 
                        long      l9 = 35;
253
 
                        short    s10 = 35;
254
 
                        ushort  us11 = 35;
255
 
                
256
 
                        System.Byte    b12  = 35;  
257
 
                        System.SByte   sb13 = 35; 
258
 
                        System.Decimal d14  = 35; 
259
 
                        System.Double  d15  = 35; 
260
 
                        System.Single  s16  = 35; 
261
 
                        System.Int32   i17  = 35; 
262
 
                        System.UInt32  ui18 = 35; 
263
 
                        System.Int64   i19  = 35; 
264
 
                        System.UInt64  ui20 = 35; 
265
 
                        System.Int16   i21  = 35; 
266
 
                        System.UInt16  i22  = 35;
267
 
 
268
 
            Assert.AreEqual(35, b1);
269
 
            Assert.AreEqual(35, sb2);
270
 
            Assert.AreEqual(35, d4);
271
 
            Assert.AreEqual(35, d5);
272
 
            Assert.AreEqual(35, f6);
273
 
            Assert.AreEqual(35, i7);
274
 
            Assert.AreEqual(35, u8);
275
 
            Assert.AreEqual(35, l9);
276
 
            Assert.AreEqual(35, s10);
277
 
            Assert.AreEqual(35, us11);
278
 
                
279
 
                        Assert.AreEqual( 35, b12  );
280
 
                        Assert.AreEqual( 35, sb13 );
281
 
                        Assert.AreEqual( 35, d14  );
282
 
                        Assert.AreEqual( 35, d15  );
283
 
                        Assert.AreEqual( 35, s16  );
284
 
                        Assert.AreEqual( 35, i17  );
285
 
                        Assert.AreEqual( 35, ui18 );
286
 
                        Assert.AreEqual( 35, i19  );
287
 
                        Assert.AreEqual( 35, ui20 );
288
 
                        Assert.AreEqual( 35, i21  );
289
 
                        Assert.AreEqual( 35, i22  );
290
 
 
291
 
#if CLR_2_0 || CLR_4_0
292
 
            byte? b23 = 35;
293
 
            sbyte? sb24 = 35;
294
 
            decimal? d25 = 35;
295
 
            double? d26 = 35;
296
 
            float? f27 = 35;
297
 
            int? i28 = 35;
298
 
            uint? u29 = 35;
299
 
            long? l30 = 35;
300
 
            short? s31 = 35;
301
 
            ushort? us32 = 35;
302
 
 
303
 
            Assert.AreEqual(35, b23);
304
 
            Assert.AreEqual(35, sb24);
305
 
            Assert.AreEqual(35, d25);
306
 
            Assert.AreEqual(35, d26);
307
 
            Assert.AreEqual(35, f27);
308
 
            Assert.AreEqual(35, i28);
309
 
            Assert.AreEqual(35, u29);
310
 
            Assert.AreEqual(35, l30);
311
 
            Assert.AreEqual(35, s31);
312
 
            Assert.AreEqual(35, us32);
313
 
#endif
314
 
        }
315
 
 
316
 
                [Test]
317
 
                public void EnumsEqual()
318
 
                {
319
 
                        MyEnum actual = MyEnum.a;
320
 
                        Assert.AreEqual( MyEnum.a, actual );
321
 
                }
322
 
 
323
 
                [Test, ExpectedException( typeof(AssertionException) )]
324
 
                public void EnumsNotEqual()
325
 
                {
326
 
                        MyEnum actual = MyEnum.a;
327
 
                        expectedMessage =
328
 
                                "  Expected: c" + Environment.NewLine +
329
 
                                "  But was:  a" + Environment.NewLine;
330
 
                        Assert.AreEqual( MyEnum.c, actual );
331
 
                }
332
 
 
333
 
                [Test]
334
 
                public void DateTimeEqual()
335
 
                {
336
 
                        DateTime dt1 = new DateTime( 2005, 6, 1, 7, 0, 0 );
337
 
                        DateTime dt2 = new DateTime( 2005, 6, 1, 0, 0, 0 ) + TimeSpan.FromHours( 7.0 );
338
 
                        Assert.AreEqual( dt1, dt2 );
339
 
                }
340
 
 
341
 
                [Test, ExpectedException( typeof (AssertionException) )]
342
 
                public void DateTimeNotEqual()
343
 
                {
344
 
                        DateTime dt1 = new DateTime( 2005, 6, 1, 7, 0, 0 );
345
 
                        DateTime dt2 = new DateTime( 2005, 6, 1, 0, 0, 0 );
346
 
                        expectedMessage =
347
 
                                "  Expected: 2005-06-01 07:00:00.000" + Environment.NewLine +
348
 
                                "  But was:  2005-06-01 00:00:00.000" + Environment.NewLine;
349
 
                        Assert.AreEqual(dt1, dt2);
350
 
                }
351
 
 
352
 
                private enum MyEnum
353
 
                {
354
 
                        a, b, c
355
 
                }
356
 
 
357
 
                [Test]
358
 
                public void DoubleNotEqualMessageDisplaysAllDigits()
359
 
                {
360
 
                        string message = "";
361
 
 
362
 
                        try
363
 
                        {
364
 
                                double d1 = 36.1;
365
 
                                double d2 = 36.099999999999994;
366
 
                                Assert.AreEqual( d1, d2 );
367
 
                        }
368
 
                        catch(AssertionException ex)
369
 
                        {
370
 
                                message = ex.Message;
371
 
                        }
372
 
 
373
 
                        if ( message == "" )
374
 
                                Assert.Fail( "Should have thrown an AssertionException" );
375
 
 
376
 
            int i = message.IndexOf('3');
377
 
                        int j = message.IndexOf( 'd', i );
378
 
                        string expected = message.Substring( i, j - i + 1 );
379
 
                        i = message.IndexOf( '3', j );
380
 
                        j = message.IndexOf( 'd', i );
381
 
                        string actual = message.Substring( i , j - i + 1 );
382
 
                        Assert.AreNotEqual( expected, actual );
383
 
                }
384
 
 
385
 
                [Test]
386
 
                public void FloatNotEqualMessageDisplaysAllDigits()
387
 
                {
388
 
                        string message = "";
389
 
 
390
 
                        try
391
 
                        {
392
 
                                float f1 = 36.125F;
393
 
                                float f2 = 36.125004F;
394
 
                                Assert.AreEqual( f1, f2 );
395
 
                        }
396
 
                        catch(AssertionException ex)
397
 
                        {
398
 
                                message = ex.Message;
399
 
                        }
400
 
 
401
 
                        if ( message == "" )
402
 
                                Assert.Fail( "Should have thrown an AssertionException" );
403
 
 
404
 
                        int i = message.IndexOf( '3' );
405
 
                        int j = message.IndexOf( 'f', i );
406
 
                        string expected = message.Substring( i, j - i + 1 );
407
 
                        i = message.IndexOf( '3', j );
408
 
                        j = message.IndexOf( 'f', i );
409
 
                        string actual = message.Substring( i, j - i + 1 );
410
 
                        Assert.AreNotEqual( expected, actual );
411
 
                }
412
 
 
413
 
                [Test]
414
 
                public void DoubleNotEqualMessageDisplaysTolerance()
415
 
                {
416
 
            string message = "";
417
 
 
418
 
            try
419
 
            {
420
 
                double d1 = 0.15;
421
 
                double d2 = 0.12;
422
 
                double tol = 0.005;
423
 
                Assert.AreEqual(d1, d2, tol);
424
 
            }
425
 
            catch (AssertionException ex)
426
 
            {
427
 
                message = ex.Message;
428
 
            }
429
 
 
430
 
            if (message == "")
431
 
                Assert.Fail("Should have thrown an AssertionException");
432
 
 
433
 
            StringAssert.Contains("+/- 0.005", message);
434
 
        }
435
 
 
436
 
                [Test]
437
 
                public void FloatNotEqualMessageDisplaysTolerance()
438
 
                {
439
 
                        string message = "";
440
 
 
441
 
                        try
442
 
                        {
443
 
                                float f1 = 0.15F;
444
 
                                float f2 = 0.12F;
445
 
                                float tol = 0.001F;
446
 
                                Assert.AreEqual( f1, f2, tol );
447
 
                        }
448
 
                        catch( AssertionException ex )
449
 
                        {
450
 
                                message = ex.Message;
451
 
                        }
452
 
 
453
 
                        if ( message == "" )
454
 
                                Assert.Fail( "Should have thrown an AssertionException" );
455
 
 
456
 
                        StringAssert.Contains( "+/- 0.001", message );
457
 
                }
458
 
 
459
 
        [Test]
460
 
        public void DoubleNotEqualMessageDisplaysDefaultTolerance()
461
 
        {
462
 
            string message = "";
463
 
            GlobalSettings.DefaultFloatingPointTolerance = 0.005d;
464
 
 
465
 
            try
466
 
            {
467
 
                double d1 = 0.15;
468
 
                double d2 = 0.12;
469
 
                Assert.AreEqual(d1, d2);
470
 
            }
471
 
            catch (AssertionException ex)
472
 
            {
473
 
                message = ex.Message;
474
 
            }
475
 
            finally
476
 
            {
477
 
                GlobalSettings.DefaultFloatingPointTolerance = 0.0d;
478
 
            }
479
 
 
480
 
            if (message == "")
481
 
                Assert.Fail("Should have thrown an AssertionException");
482
 
 
483
 
            StringAssert.Contains("+/- 0.005", message);
484
 
        }
485
 
 
486
 
        [Test]
487
 
        public void DoubleNotEqualWithNanDoesNotDisplayDefaultTolerance()
488
 
        {
489
 
            string message = "";
490
 
            GlobalSettings.DefaultFloatingPointTolerance = 0.005d;
491
 
 
492
 
            try
493
 
            {
494
 
                double d1 = double.NaN;
495
 
                double d2 = 0.12;
496
 
                Assert.AreEqual(d1, d2);
497
 
            }
498
 
            catch (AssertionException ex)
499
 
            {
500
 
                message = ex.Message;
501
 
            }
502
 
            finally
503
 
            {
504
 
                GlobalSettings.DefaultFloatingPointTolerance = 0.0d;
505
 
            }
506
 
 
507
 
            if (message == "")
508
 
                Assert.Fail("Should have thrown an AssertionException");
509
 
 
510
 
            Assert.That(message.IndexOf("+/-") == -1);
511
 
        }
512
 
 
513
 
        [Test]
514
 
        public void DirectoryInfoEquality()
515
 
        {
516
 
            string path = Environment.CurrentDirectory;
517
 
            DirectoryInfo dir1 = new DirectoryInfo(path);
518
 
            DirectoryInfo dir2 = new DirectoryInfo(path);
519
 
 
520
 
            Assert.AreEqual(dir1, dir2);
521
 
        }
522
 
 
523
 
        [Test]
524
 
        public void DirectoryInfoEqualityIgnoresTrailingDirectorySeparator()
525
 
        {
526
 
            string path1 = Environment.CurrentDirectory;
527
 
            string path2 = path1;
528
 
            int length = path1.Length;
529
 
 
530
 
            if (path1[length - 1] == Path.DirectorySeparatorChar)
531
 
                path1 = path1.Substring(0, length - 1);
532
 
            else
533
 
                path1 += Path.DirectorySeparatorChar;
534
 
 
535
 
            DirectoryInfo dir1 = new DirectoryInfo(path1);
536
 
            DirectoryInfo dir2 = new DirectoryInfo(path2);
537
 
 
538
 
            Assert.AreEqual(dir1, dir2);
539
 
        }
540
 
 
541
 
#if CLR_2_0 || CLR_4_0
542
 
                [Test]
543
 
                public void IEquatableSuccess_OldSyntax()
544
 
                {
545
 
                        IntEquatable a = new IntEquatable(1);
546
 
 
547
 
                        Assert.AreEqual(1, a);
548
 
            Assert.AreEqual(a, 1);
549
 
                }
550
 
 
551
 
                [Test]
552
 
                public void IEquatableSuccess_ConstraintSyntax()
553
 
                {
554
 
                        IntEquatable a = new IntEquatable(1);
555
 
 
556
 
                        Assert.That(a, Is.EqualTo(1));
557
 
            Assert.That(1, Is.EqualTo(a));
558
 
                }
559
 
#endif
560
 
    }
561
 
 
562
 
#if CLR_2_0 || CLR_4_0
563
 
        public class IntEquatable : IEquatable<int>
564
 
        {
565
 
                private int i;
566
 
 
567
 
                public IntEquatable(int i)
568
 
                {
569
 
                        this.i = i;
570
 
                }
571
 
 
572
 
                public bool Equals(int other)
573
 
                {
574
 
                        return i.Equals(other);
575
 
                }
576
 
        }
577
 
#endif
 
1
// ****************************************************************
 
2
// This is free software licensed under the NUnit license. You
 
3
// may obtain a copy of the license as well as information regarding
 
4
// copyright ownership at http://nunit.org.
 
5
// ****************************************************************
 
6
 
 
7
using System;
 
8
using System.Text.RegularExpressions;
 
9
using System.Globalization;
 
10
using System.IO;
 
11
using System.Threading;
 
12
 
 
13
namespace NUnit.Framework.Tests
 
14
{
 
15
        [TestFixture]
 
16
        public class EqualsFixture : MessageChecker
 
17
        {
 
18
                [Test]
 
19
                public void Equals()
 
20
                {
 
21
                        string nunitString = "Hello NUnit";
 
22
                        string expected = nunitString;
 
23
                        string actual = nunitString;
 
24
 
 
25
                        Assert.IsTrue(expected == actual);
 
26
                        Assert.AreEqual(expected, actual);
 
27
                }
 
28
 
 
29
                [Test]
 
30
                public void EqualsNull() 
 
31
                {
 
32
                        Assert.AreEqual(null, null);
 
33
                }
 
34
                
 
35
                [Test]
 
36
                public void Bug575936Int32Int64Comparison()
 
37
                {
 
38
                        long l64 = 0;
 
39
                        int i32 = 0;
 
40
                        Assert.AreEqual(i32, l64);
 
41
                }
 
42
                
 
43
                [Test]
 
44
                public void IntegerLongComparison()
 
45
                {
 
46
                        Assert.AreEqual(1, 1L);
 
47
                        Assert.AreEqual(1L, 1);
 
48
                }
 
49
 
 
50
                [Test]
 
51
                public void IntegerEquals()
 
52
                {
 
53
                        int val = 42;
 
54
                        Assert.AreEqual(val, 42);
 
55
                }
 
56
 
 
57
                
 
58
                [Test,ExpectedException(typeof(AssertionException))]
 
59
                public void EqualsFail()
 
60
                {
 
61
                        string junitString = "Goodbye JUnit";
 
62
                        string expected = "Hello NUnit";
 
63
 
 
64
                        expectedMessage =
 
65
                                "  Expected string length 11 but was 13. Strings differ at index 0." + Environment.NewLine +
 
66
                                "  Expected: \"Hello NUnit\"" + Environment.NewLine +
 
67
                                "  But was:  \"Goodbye JUnit\"" + Environment.NewLine +
 
68
                                "  -----------^" + Environment.NewLine;
 
69
                        Assert.AreEqual(expected, junitString);
 
70
                }
 
71
                
 
72
                [Test,ExpectedException(typeof(AssertionException))]
 
73
                public void EqualsNaNFails() 
 
74
                {
 
75
                        expectedMessage =
 
76
                                "  Expected: 1.234d +/- 0.0d" + Environment.NewLine +
 
77
                                "  But was:  NaN" + Environment.NewLine;
 
78
                        Assert.AreEqual(1.234, Double.NaN, 0.0);
 
79
                }    
 
80
 
 
81
 
 
82
                [Test]
 
83
                [ExpectedException(typeof(AssertionException))]
 
84
                public void NanEqualsFails() 
 
85
                {
 
86
                        expectedMessage =
 
87
                                "  Expected: NaN" + Environment.NewLine +
 
88
                                "  But was:  1.234d" + Environment.NewLine;
 
89
                        Assert.AreEqual(Double.NaN, 1.234, 0.0);
 
90
                }     
 
91
                
 
92
                [Test]
 
93
                public void NanEqualsNaNSucceeds() 
 
94
                {
 
95
                        Assert.AreEqual(Double.NaN, Double.NaN, 0.0);
 
96
                }     
 
97
 
 
98
                [Test]
 
99
                public void NegInfinityEqualsInfinity() 
 
100
                {
 
101
                        Assert.AreEqual(Double.NegativeInfinity, Double.NegativeInfinity, 0.0);
 
102
                }
 
103
 
 
104
                [Test]
 
105
                public void PosInfinityEqualsInfinity() 
 
106
                {
 
107
                        Assert.AreEqual(Double.PositiveInfinity, Double.PositiveInfinity, 0.0);
 
108
                }
 
109
                
 
110
                [Test,ExpectedException(typeof(AssertionException))]
 
111
                public void PosInfinityNotEquals() 
 
112
                {
 
113
                        expectedMessage =
 
114
                                "  Expected: Infinity" + Environment.NewLine +
 
115
                                "  But was:  1.23d" + Environment.NewLine;
 
116
                        Assert.AreEqual(Double.PositiveInfinity, 1.23, 0.0);
 
117
                }
 
118
 
 
119
                [Test,ExpectedException(typeof(AssertionException))]
 
120
                public void PosInfinityNotEqualsNegInfinity() 
 
121
                {
 
122
                        expectedMessage =
 
123
                                "  Expected: Infinity" + Environment.NewLine +
 
124
                                "  But was:  -Infinity" + Environment.NewLine;
 
125
                        Assert.AreEqual(Double.PositiveInfinity, Double.NegativeInfinity, 0.0);
 
126
                }
 
127
 
 
128
                [Test,ExpectedException(typeof(AssertionException))]    
 
129
                public void SinglePosInfinityNotEqualsNegInfinity() 
 
130
                {
 
131
                        expectedMessage =
 
132
                                "  Expected: Infinity" + Environment.NewLine +
 
133
                                "  But was:  -Infinity" + Environment.NewLine;
 
134
                        Assert.AreEqual(float.PositiveInfinity, float.NegativeInfinity, (float)0.0);
 
135
                }
 
136
 
 
137
                [Test,ExpectedException(typeof(InvalidOperationException))]
 
138
                public void EqualsThrowsException()
 
139
                {
 
140
                        object o = new object();
 
141
                        Assert.Equals(o, o);
 
142
                }
 
143
 
 
144
                [Test,ExpectedException(typeof(InvalidOperationException))]
 
145
                public void ReferenceEqualsThrowsException()
 
146
                {
 
147
                        object o = new object();
 
148
                        Assert.ReferenceEquals(o, o);
 
149
                }
 
150
                
 
151
                [Test]
 
152
                public void Float() 
 
153
                {
 
154
                        float val = (float)1.0;
 
155
                        float expected = val;
 
156
                        float actual = val;
 
157
 
 
158
                        Assert.IsTrue(expected == actual);
 
159
                        Assert.AreEqual(expected, actual, (float)0.0);
 
160
                }
 
161
 
 
162
                [Test]
 
163
                public void Byte() 
 
164
                {
 
165
                        byte val = 1;
 
166
                        byte expected = val;
 
167
                        byte actual = val;
 
168
 
 
169
                        Assert.IsTrue(expected == actual);
 
170
                        Assert.AreEqual(expected, actual);
 
171
                }
 
172
 
 
173
                [Test]
 
174
                public void String() 
 
175
                {
 
176
                        string s1 = "test";
 
177
                        string s2 = new System.Text.StringBuilder(s1).ToString();
 
178
 
 
179
                        Assert.IsTrue(s1.Equals(s2));
 
180
                        Assert.AreEqual(s1,s2);
 
181
                }
 
182
 
 
183
                [Test]
 
184
                public void Short() 
 
185
                {
 
186
                        short val = 1;
 
187
                        short expected = val;
 
188
                        short actual = val;
 
189
 
 
190
                        Assert.IsTrue(expected == actual);
 
191
                        Assert.AreEqual(expected, actual);
 
192
                }
 
193
 
 
194
                [Test]
 
195
                public void Int() 
 
196
                {
 
197
                        int val = 1;
 
198
                        int expected = val;
 
199
                        int actual = val;
 
200
 
 
201
                        Assert.IsTrue(expected == actual);
 
202
                        Assert.AreEqual(expected, actual);
 
203
                }
 
204
 
 
205
                [Test]
 
206
                public void UInt() 
 
207
                {
 
208
                        uint val = 1;
 
209
                        uint expected = val;
 
210
                        uint actual = val;
 
211
 
 
212
                        Assert.IsTrue(expected == actual);
 
213
                        Assert.AreEqual(expected, actual);
 
214
                }
 
215
 
 
216
                [Test]
 
217
                public void Decimal() 
 
218
                {
 
219
                        decimal expected = 100m;
 
220
                        decimal actual = 100.0m;
 
221
                        int integer = 100;
 
222
 
 
223
                        Assert.IsTrue( expected == actual );
 
224
                        Assert.AreEqual(expected, actual);
 
225
                        Assert.IsTrue(expected == integer);
 
226
                        Assert.AreEqual(expected, integer);
 
227
                        Assert.IsTrue(actual == integer);
 
228
                        Assert.AreEqual(actual, integer);
 
229
                }
 
230
 
 
231
 
 
232
                
 
233
                /// <summary>
 
234
                /// Checks to see that a value comparison works with all types.
 
235
                /// Current version has problems when value is the same but the
 
236
                /// types are different...C# is not like Java, and doesn't automatically
 
237
                /// perform value type conversion to simplify this type of comparison.
 
238
                /// 
 
239
                /// Related to Bug575936Int32Int64Comparison, but covers all numeric
 
240
                /// types.
 
241
                /// </summary>
 
242
                [Test]
 
243
                public void EqualsSameTypes()
 
244
                {
 
245
                        byte      b1 = 35;
 
246
                        sbyte    sb2 = 35;
 
247
                        decimal   d4 = 35;
 
248
                        double    d5 = 35;
 
249
                        float     f6 = 35;
 
250
                        int       i7 = 35;
 
251
                        uint      u8 = 35;
 
252
                        long      l9 = 35;
 
253
                        short    s10 = 35;
 
254
                        ushort  us11 = 35;
 
255
                
 
256
                        System.Byte    b12  = 35;  
 
257
                        System.SByte   sb13 = 35; 
 
258
                        System.Decimal d14  = 35; 
 
259
                        System.Double  d15  = 35; 
 
260
                        System.Single  s16  = 35; 
 
261
                        System.Int32   i17  = 35; 
 
262
                        System.UInt32  ui18 = 35; 
 
263
                        System.Int64   i19  = 35; 
 
264
                        System.UInt64  ui20 = 35; 
 
265
                        System.Int16   i21  = 35; 
 
266
                        System.UInt16  i22  = 35;
 
267
 
 
268
            Assert.AreEqual(35, b1);
 
269
            Assert.AreEqual(35, sb2);
 
270
            Assert.AreEqual(35, d4);
 
271
            Assert.AreEqual(35, d5);
 
272
            Assert.AreEqual(35, f6);
 
273
            Assert.AreEqual(35, i7);
 
274
            Assert.AreEqual(35, u8);
 
275
            Assert.AreEqual(35, l9);
 
276
            Assert.AreEqual(35, s10);
 
277
            Assert.AreEqual(35, us11);
 
278
                
 
279
                        Assert.AreEqual( 35, b12  );
 
280
                        Assert.AreEqual( 35, sb13 );
 
281
                        Assert.AreEqual( 35, d14  );
 
282
                        Assert.AreEqual( 35, d15  );
 
283
                        Assert.AreEqual( 35, s16  );
 
284
                        Assert.AreEqual( 35, i17  );
 
285
                        Assert.AreEqual( 35, ui18 );
 
286
                        Assert.AreEqual( 35, i19  );
 
287
                        Assert.AreEqual( 35, ui20 );
 
288
                        Assert.AreEqual( 35, i21  );
 
289
                        Assert.AreEqual( 35, i22  );
 
290
 
 
291
#if CLR_2_0 || CLR_4_0
 
292
            byte? b23 = 35;
 
293
            sbyte? sb24 = 35;
 
294
            decimal? d25 = 35;
 
295
            double? d26 = 35;
 
296
            float? f27 = 35;
 
297
            int? i28 = 35;
 
298
            uint? u29 = 35;
 
299
            long? l30 = 35;
 
300
            short? s31 = 35;
 
301
            ushort? us32 = 35;
 
302
 
 
303
            Assert.AreEqual(35, b23);
 
304
            Assert.AreEqual(35, sb24);
 
305
            Assert.AreEqual(35, d25);
 
306
            Assert.AreEqual(35, d26);
 
307
            Assert.AreEqual(35, f27);
 
308
            Assert.AreEqual(35, i28);
 
309
            Assert.AreEqual(35, u29);
 
310
            Assert.AreEqual(35, l30);
 
311
            Assert.AreEqual(35, s31);
 
312
            Assert.AreEqual(35, us32);
 
313
#endif
 
314
        }
 
315
 
 
316
                [Test]
 
317
                public void EnumsEqual()
 
318
                {
 
319
                        MyEnum actual = MyEnum.a;
 
320
                        Assert.AreEqual( MyEnum.a, actual );
 
321
                }
 
322
 
 
323
                [Test, ExpectedException( typeof(AssertionException) )]
 
324
                public void EnumsNotEqual()
 
325
                {
 
326
                        MyEnum actual = MyEnum.a;
 
327
                        expectedMessage =
 
328
                                "  Expected: c" + Environment.NewLine +
 
329
                                "  But was:  a" + Environment.NewLine;
 
330
                        Assert.AreEqual( MyEnum.c, actual );
 
331
                }
 
332
 
 
333
                [Test]
 
334
                public void DateTimeEqual()
 
335
                {
 
336
                        DateTime dt1 = new DateTime( 2005, 6, 1, 7, 0, 0 );
 
337
                        DateTime dt2 = new DateTime( 2005, 6, 1, 0, 0, 0 ) + TimeSpan.FromHours( 7.0 );
 
338
                        Assert.AreEqual( dt1, dt2 );
 
339
                }
 
340
 
 
341
                [Test, ExpectedException( typeof (AssertionException) )]
 
342
                public void DateTimeNotEqual()
 
343
                {
 
344
                        DateTime dt1 = new DateTime( 2005, 6, 1, 7, 0, 0 );
 
345
                        DateTime dt2 = new DateTime( 2005, 6, 1, 0, 0, 0 );
 
346
                        expectedMessage =
 
347
                                "  Expected: 2005-06-01 07:00:00.000" + Environment.NewLine +
 
348
                                "  But was:  2005-06-01 00:00:00.000" + Environment.NewLine;
 
349
                        Assert.AreEqual(dt1, dt2);
 
350
                }
 
351
 
 
352
                private enum MyEnum
 
353
                {
 
354
                        a, b, c
 
355
                }
 
356
 
 
357
                [Test]
 
358
                public void DoubleNotEqualMessageDisplaysAllDigits()
 
359
                {
 
360
                        string message = "";
 
361
 
 
362
                        try
 
363
                        {
 
364
                                double d1 = 36.1;
 
365
                                double d2 = 36.099999999999994;
 
366
                                Assert.AreEqual( d1, d2 );
 
367
                        }
 
368
                        catch(AssertionException ex)
 
369
                        {
 
370
                                message = ex.Message;
 
371
                        }
 
372
 
 
373
                        if ( message == "" )
 
374
                                Assert.Fail( "Should have thrown an AssertionException" );
 
375
 
 
376
            int i = message.IndexOf('3');
 
377
                        int j = message.IndexOf( 'd', i );
 
378
                        string expected = message.Substring( i, j - i + 1 );
 
379
                        i = message.IndexOf( '3', j );
 
380
                        j = message.IndexOf( 'd', i );
 
381
                        string actual = message.Substring( i , j - i + 1 );
 
382
                        Assert.AreNotEqual( expected, actual );
 
383
                }
 
384
 
 
385
                [Test]
 
386
                public void FloatNotEqualMessageDisplaysAllDigits()
 
387
                {
 
388
                        string message = "";
 
389
 
 
390
                        try
 
391
                        {
 
392
                                float f1 = 36.125F;
 
393
                                float f2 = 36.125004F;
 
394
                                Assert.AreEqual( f1, f2 );
 
395
                        }
 
396
                        catch(AssertionException ex)
 
397
                        {
 
398
                                message = ex.Message;
 
399
                        }
 
400
 
 
401
                        if ( message == "" )
 
402
                                Assert.Fail( "Should have thrown an AssertionException" );
 
403
 
 
404
                        int i = message.IndexOf( '3' );
 
405
                        int j = message.IndexOf( 'f', i );
 
406
                        string expected = message.Substring( i, j - i + 1 );
 
407
                        i = message.IndexOf( '3', j );
 
408
                        j = message.IndexOf( 'f', i );
 
409
                        string actual = message.Substring( i, j - i + 1 );
 
410
                        Assert.AreNotEqual( expected, actual );
 
411
                }
 
412
 
 
413
                [Test]
 
414
                public void DoubleNotEqualMessageDisplaysTolerance()
 
415
                {
 
416
            string message = "";
 
417
 
 
418
            try
 
419
            {
 
420
                double d1 = 0.15;
 
421
                double d2 = 0.12;
 
422
                double tol = 0.005;
 
423
                Assert.AreEqual(d1, d2, tol);
 
424
            }
 
425
            catch (AssertionException ex)
 
426
            {
 
427
                message = ex.Message;
 
428
            }
 
429
 
 
430
            if (message == "")
 
431
                Assert.Fail("Should have thrown an AssertionException");
 
432
 
 
433
            StringAssert.Contains("+/- 0.005", message);
 
434
        }
 
435
 
 
436
                [Test]
 
437
                public void FloatNotEqualMessageDisplaysTolerance()
 
438
                {
 
439
                        string message = "";
 
440
 
 
441
                        try
 
442
                        {
 
443
                                float f1 = 0.15F;
 
444
                                float f2 = 0.12F;
 
445
                                float tol = 0.001F;
 
446
                                Assert.AreEqual( f1, f2, tol );
 
447
                        }
 
448
                        catch( AssertionException ex )
 
449
                        {
 
450
                                message = ex.Message;
 
451
                        }
 
452
 
 
453
                        if ( message == "" )
 
454
                                Assert.Fail( "Should have thrown an AssertionException" );
 
455
 
 
456
                        StringAssert.Contains( "+/- 0.001", message );
 
457
                }
 
458
 
 
459
        [Test]
 
460
        public void DoubleNotEqualMessageDisplaysDefaultTolerance()
 
461
        {
 
462
            string message = "";
 
463
            GlobalSettings.DefaultFloatingPointTolerance = 0.005d;
 
464
 
 
465
            try
 
466
            {
 
467
                double d1 = 0.15;
 
468
                double d2 = 0.12;
 
469
                Assert.AreEqual(d1, d2);
 
470
            }
 
471
            catch (AssertionException ex)
 
472
            {
 
473
                message = ex.Message;
 
474
            }
 
475
            finally
 
476
            {
 
477
                GlobalSettings.DefaultFloatingPointTolerance = 0.0d;
 
478
            }
 
479
 
 
480
            if (message == "")
 
481
                Assert.Fail("Should have thrown an AssertionException");
 
482
 
 
483
            StringAssert.Contains("+/- 0.005", message);
 
484
        }
 
485
 
 
486
        [Test]
 
487
        public void DoubleNotEqualWithNanDoesNotDisplayDefaultTolerance()
 
488
        {
 
489
            string message = "";
 
490
            GlobalSettings.DefaultFloatingPointTolerance = 0.005d;
 
491
 
 
492
            try
 
493
            {
 
494
                double d1 = double.NaN;
 
495
                double d2 = 0.12;
 
496
                Assert.AreEqual(d1, d2);
 
497
            }
 
498
            catch (AssertionException ex)
 
499
            {
 
500
                message = ex.Message;
 
501
            }
 
502
            finally
 
503
            {
 
504
                GlobalSettings.DefaultFloatingPointTolerance = 0.0d;
 
505
            }
 
506
 
 
507
            if (message == "")
 
508
                Assert.Fail("Should have thrown an AssertionException");
 
509
 
 
510
            Assert.That(message.IndexOf("+/-") == -1);
 
511
        }
 
512
 
 
513
        [Test]
 
514
        public void DirectoryInfoEquality()
 
515
        {
 
516
            string path = Environment.CurrentDirectory;
 
517
            DirectoryInfo dir1 = new DirectoryInfo(path);
 
518
            DirectoryInfo dir2 = new DirectoryInfo(path);
 
519
 
 
520
            Assert.AreEqual(dir1, dir2);
 
521
        }
 
522
 
 
523
        [Test]
 
524
        public void DirectoryInfoEqualityIgnoresTrailingDirectorySeparator()
 
525
        {
 
526
            string path1 = Environment.CurrentDirectory;
 
527
            string path2 = path1;
 
528
            int length = path1.Length;
 
529
 
 
530
            if (path1[length - 1] == Path.DirectorySeparatorChar)
 
531
                path1 = path1.Substring(0, length - 1);
 
532
            else
 
533
                path1 += Path.DirectorySeparatorChar;
 
534
 
 
535
            DirectoryInfo dir1 = new DirectoryInfo(path1);
 
536
            DirectoryInfo dir2 = new DirectoryInfo(path2);
 
537
 
 
538
            Assert.AreEqual(dir1, dir2);
 
539
        }
 
540
 
 
541
#if CLR_2_0 || CLR_4_0
 
542
                [Test]
 
543
                public void IEquatableSuccess_OldSyntax()
 
544
                {
 
545
                        IntEquatable a = new IntEquatable(1);
 
546
 
 
547
                        Assert.AreEqual(1, a);
 
548
            Assert.AreEqual(a, 1);
 
549
                }
 
550
 
 
551
                [Test]
 
552
                public void IEquatableSuccess_ConstraintSyntax()
 
553
                {
 
554
                        IntEquatable a = new IntEquatable(1);
 
555
 
 
556
                        Assert.That(a, Is.EqualTo(1));
 
557
            Assert.That(1, Is.EqualTo(a));
 
558
                }
 
559
#endif
 
560
    }
 
561
 
 
562
#if CLR_2_0 || CLR_4_0
 
563
        public class IntEquatable : IEquatable<int>
 
564
        {
 
565
                private int i;
 
566
 
 
567
                public IntEquatable(int i)
 
568
                {
 
569
                        this.i = i;
 
570
                }
 
571
 
 
572
                public bool Equals(int other)
 
573
                {
 
574
                        return i.Equals(other);
 
575
                }
 
576
        }
 
577
#endif
578
578
}
 
 
b'\\ No newline at end of file'