/
EqualityComparerAssertionTest.cs
153 lines (132 loc) · 4.45 KB
/
EqualityComparerAssertionTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Collections.Generic;
using System.Reflection;
using AutoFixture.Idioms;
using TestTypeFoundation;
using Xunit;
namespace AutoFixture.IdiomsUnitTest
{
public class EqualityComparerAssertionTest
{
[Fact]
public void SutIsIdiomaticAssertion()
{
// Arrange
var dummyComposer = new Fixture();
// Act
var sut = new EqualityComparerAssertion(dummyComposer);
// Assert
Assert.IsAssignableFrom<IIdiomaticAssertion>(sut);
}
[Fact]
public void ConstructWithNullComposerThrows()
{
// Arrange
// Act & Assert
Assert.Throws<ArgumentNullException>(() =>
new EqualityComparerAssertion(null));
}
[Fact]
public void VerifyNullMethodThrows()
{
// Arrange
var dummyComposer = new Fixture();
var sut = new EqualityComparerAssertion(dummyComposer);
// Act & Assert
Assert.Throws<ArgumentNullException>(() =>
sut.Verify((MethodInfo)null));
}
[Fact]
public void VerifyNonEqualityComparerDoesNothing()
{
// Arrange
var dummyComposer = new Fixture();
var sut = new EqualityComparerAssertion(dummyComposer);
// Act & Assert
Assert.Null(Record.Exception(() =>
sut.Verify(typeof(NonEqualityComparer))));
}
[Fact]
public void VerifyWellBehavedEqualityComparerDoesNotThrow()
{
// Arrange
var dummyComposer = new Fixture();
var sut = new EqualityComparerAssertion(dummyComposer);
// Act & Assert
Assert.Null(Record.Exception(() =>
sut.Verify(typeof(WellBehavedEqualityComparer))));
}
[Fact]
public void VerifyIllBehavedNullEqualityComparerThrows()
{
// Arrange
var dummyComposer = new Fixture();
var sut = new EqualityComparerAssertion(dummyComposer);
// Act & Assert
Assert.Throws<EqualityComparerImplementationException>(() =>
sut.Verify(typeof(IllBehavedNullEqualityComparer)));
}
[Fact]
public void VerifyIllBehavedSelfEqualityComparerThrows()
{
// Arrange
var dummyComposer = new Fixture();
var sut = new EqualityComparerAssertion(dummyComposer);
// Act & Assert
Assert.Throws<EqualityComparerImplementationException>(() =>
sut.Verify(typeof(IllBehavedSelfEqualityComparer)));
}
#pragma warning disable 659
private class WellBehavedEqualityComparer : IEqualityComparer<PropertyHolder<int>>
{
public bool Equals(PropertyHolder<int> x, PropertyHolder<int> y)
{
if (ReferenceEquals(x, y))
return true;
if (ReferenceEquals(x, null))
return false;
if (ReferenceEquals(y, null))
return false;
if (x.GetType() != y.GetType())
return false;
return x.Property == y.Property;
}
public int GetHashCode(PropertyHolder<int> obj)
{
return obj.Property;
}
}
private class IllBehavedNullEqualityComparer : IEqualityComparer<PropertyHolder<int>>
{
public bool Equals(PropertyHolder<int> x, PropertyHolder<int> y)
{
if (x == null && y == null)
return false;
if (x == null ^ y == null)
return true;
return false;
}
public int GetHashCode(PropertyHolder<int> obj)
{
throw new Exception();
}
}
private class IllBehavedSelfEqualityComparer : IEqualityComparer<PropertyHolder<int>>
{
public bool Equals(PropertyHolder<int> x, PropertyHolder<int> y)
{
if (ReferenceEquals(x, y))
return false;
return true;
}
public int GetHashCode(PropertyHolder<int> obj)
{
throw new Exception();
}
}
#pragma warning restore 659
private class NonEqualityComparer
{
}
}
}