Created
March 15, 2011 21:41
-
-
Save emiaj/871549 to your computer and use it in GitHub Desktop.
Showcase different NUnit behavior for the same scenario (tested using 2.4.6 and 2.5.9.10348)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using NUnit.Framework; | |
namespace ClassLibrary1 | |
{ | |
public class PersonTester | |
{ | |
[Test] | |
public void are_equal() | |
{ | |
var first = new Person {Name = "jaime"}; | |
var second = new Person {Name = "miguel"}; | |
Assert.IsFalse(first == second); // pass | |
Assert.AreNotEqual(first, second); // fails | |
} | |
} | |
public class Person : IEnumerable | |
{ | |
private readonly IList<Person> _children; | |
public Person() | |
{ | |
_children = new List<Person>(); | |
} | |
public string Name { get; set; } | |
public void AddChild(Person child) | |
{ | |
_children.Add(child); | |
} | |
public override bool Equals(object obj) | |
{ | |
if (ReferenceEquals(null, obj)) return false; | |
if (ReferenceEquals(this, obj)) return true; | |
if (obj.GetType() != typeof(Person)) return false; | |
return Equals((Person)obj); | |
} | |
public bool Equals(Person other) | |
{ | |
if (ReferenceEquals(null, other)) return false; | |
if (ReferenceEquals(this, other)) return true; | |
return Equals(other.Name, Name); | |
} | |
public override int GetHashCode() | |
{ | |
return (Name != null ? Name.GetHashCode() : 0); | |
} | |
public IEnumerator GetEnumerator() | |
{ | |
foreach (var child in _children) | |
{ | |
yield return child; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment