Skip to content

Commit 62b62cf

Browse files
committed
covered Disjunction specification
1 parent 79540e7 commit 62b62cf

File tree

2 files changed

+352
-0
lines changed

2 files changed

+352
-0
lines changed

Code/UnitTests/Epic.Prelude.UnitTests/Epic.Prelude.UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<Compile Include="Specifications\NegationQA.cs" />
6969
<Compile Include="Specifications\ConjunctionQA.cs" />
7070
<Compile Include="IObject.cs" />
71+
<Compile Include="Specifications\DisjunctionQA.cs" />
7172
</ItemGroup>
7273
<ItemGroup>
7374
<Folder Include="Fakes\" />
Lines changed: 351 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,351 @@
1+
//
2+
// DisjunctionQA.cs
3+
//
4+
// Author:
5+
// Giacomo Tesio <giacomo@tesio.it>
6+
//
7+
// Copyright (c) 2010-2012 Giacomo Tesio
8+
//
9+
// This file is part of Epic.NET.
10+
//
11+
// Epic.NET is free software: you can redistribute it and/or modify
12+
// it under the terms of the GNU Affero General Public License as published by
13+
// the Free Software Foundation, either version 3 of the License, or
14+
// (at your option) any later version.
15+
//
16+
// Epic.NET is distributed in the hope that it will be useful,
17+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
// GNU Affero General Public License for more details.
20+
//
21+
// You should have received a copy of the GNU Affero General Public License
22+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
//
24+
using NUnit.Framework;
25+
using System;
26+
using Rhino.Mocks;
27+
using System.Linq;
28+
using System.Collections.Generic;
29+
30+
namespace Epic.Specifications
31+
{
32+
[TestFixture()]
33+
public class DisjunctionQA : RhinoMocksFixtureBase
34+
{
35+
[Test]
36+
public void Initialize_withoutAnyArgument_throwsArgumentNullException ()
37+
{
38+
// arrange:
39+
ISpecification<Fakes.FakeCandidate1> inner = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>>();
40+
41+
// assert:
42+
Assert.Throws<ArgumentNullException>(delegate() {
43+
new Disjunction<Fakes.FakeCandidate1>(null, inner);
44+
});
45+
Assert.Throws<ArgumentNullException>(delegate() {
46+
new Disjunction<Fakes.FakeCandidate1>(inner, null);
47+
});
48+
}
49+
50+
[Test]
51+
public void Initialize_withTwoEqualsSpecification_throwsArgumentException ()
52+
{
53+
// arrange:
54+
ISpecification<Fakes.FakeCandidate1> inner = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>>();
55+
inner.Expect(s => s.Equals(inner)).Return(true).Repeat.AtLeastOnce();
56+
57+
// assert:
58+
Assert.Throws<ArgumentException>(delegate() {
59+
new Disjunction<Fakes.FakeCandidate1>(inner, inner);
60+
});
61+
}
62+
63+
[Test]
64+
public void Initialize_withTwoDifferentSpecifications_works ()
65+
{
66+
// arrange:
67+
ISpecification<Fakes.FakeCandidate1> first = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>>();
68+
ISpecification<Fakes.FakeCandidate1> second = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>>();
69+
first.Expect(s => s.Equals(second)).Return(false).Repeat.AtLeastOnce();
70+
71+
72+
// act:
73+
var toTest = new Disjunction<Fakes.FakeCandidate1>(first, second);
74+
75+
// assert:
76+
Assert.IsNotNull(toTest);
77+
Assert.AreSame(first, toTest.ElementAt(0));
78+
Assert.AreSame(second, toTest.ElementAt(1));
79+
CollectionAssert.AreEquivalent(new Object[] { first, second }, toTest as System.Collections.IEnumerable);
80+
CollectionAssert.AreEquivalent(new ISpecification<Fakes.FakeCandidate1>[] { first, second }, toTest as IEnumerable<ISpecification<Fakes.FakeCandidate1>>);
81+
}
82+
83+
[Test]
84+
public void Initialize_withASpecificationAndADisjunction_createADisjunctionThatSequenceThemAfterFirst ()
85+
{
86+
// arrange:
87+
ISpecification<Fakes.FakeCandidate1> first = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>, IObject>();
88+
ISpecification<Fakes.FakeCandidate1> second = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>, IObject>();
89+
ISpecification<Fakes.FakeCandidate1> third = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>, IObject>();
90+
second.Expect(s => s.Equals(third)).Return(false).Repeat.Any();
91+
var other = new Disjunction<Fakes.FakeCandidate1>(second, third);
92+
first.Expect(s => s.ToString()).Return("first").Repeat.Any();
93+
second.Expect(s => s.ToString()).Return("second").Repeat.Any();
94+
third.Expect(s => s.ToString()).Return("third").Repeat.Any();
95+
first.Expect(s => s.Equals(other)).Return(false).Repeat.Any();
96+
first.Expect(s => s.Equals(second)).Return(false).Repeat.Any();
97+
first.Expect(s => s.Equals(third)).Return(false).Repeat.Any();
98+
99+
// act:
100+
var toTest = new Disjunction<Fakes.FakeCandidate1>(first, other);
101+
102+
// assert:
103+
Assert.AreEqual(3, toTest.Count());
104+
Assert.AreSame(first, toTest.ElementAt(0));
105+
Assert.AreSame(second, toTest.ElementAt(1));
106+
Assert.AreSame(third, toTest.ElementAt(2));
107+
}
108+
109+
[Test]
110+
public void Or_aKnownSpecification_returnItself ()
111+
{
112+
// arrange:
113+
ISpecification<Fakes.FakeCandidate1> first = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>>();
114+
ISpecification<Fakes.FakeCandidate1> second = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>>();
115+
first.Expect(s => s.Equals(second)).Return(false).Repeat.AtLeastOnce();
116+
second.Expect(s => s.Equals(first)).Return(false).Repeat.AtLeastOnce();
117+
second.Expect(s => s.Equals(second)).Return(true).Repeat.AtLeastOnce();
118+
var toTest = new Disjunction<Fakes.FakeCandidate1>(first, second);
119+
120+
// act:
121+
var result = toTest.Or(second);
122+
123+
// assert:
124+
Assert.AreSame(toTest, result);
125+
}
126+
127+
[Test]
128+
public void Or_anUnknownSpecification_returnANewConjuction ()
129+
{
130+
// arrange:
131+
ISpecification<Fakes.FakeCandidate1> first = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>>();
132+
ISpecification<Fakes.FakeCandidate1> second = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>>();
133+
ISpecification<Fakes.FakeCandidate1> third = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>>();
134+
first.Expect(s => s.Equals(second)).Return(false).Repeat.AtLeastOnce();
135+
third.Expect(s => s.Equals(first)).Return(false).Repeat.AtLeastOnce();
136+
third.Expect(s => s.Equals(second)).Return(false).Repeat.AtLeastOnce();
137+
var toTest = new Disjunction<Fakes.FakeCandidate1>(first, second);
138+
139+
// act:
140+
var result = toTest.Or(third) as Disjunction<Fakes.FakeCandidate1>;
141+
142+
// assert:
143+
Assert.IsNotNull(result);
144+
Assert.AreSame(first, result.ElementAt(0));
145+
Assert.AreSame(second, result.ElementAt(1));
146+
Assert.AreSame(third, result.ElementAt(2));
147+
}
148+
149+
[Test]
150+
public void Or_anotherConjunction_sequenceTheOtherAfterThis ()
151+
{
152+
// arrange:
153+
ISpecification<Fakes.FakeCandidate1> first = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>, IObject>();
154+
ISpecification<Fakes.FakeCandidate1> second = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>, IObject>();
155+
ISpecification<Fakes.FakeCandidate1> third = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>, IObject>();
156+
ISpecification<Fakes.FakeCandidate1> forth = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>, IObject>();
157+
first.Expect(s => s.ToString()).Return("first").Repeat.Any();
158+
second.Expect(s => s.ToString()).Return("second").Repeat.Any();
159+
third.Expect(s => s.ToString()).Return("third").Repeat.Any();
160+
forth.Expect(s => s.ToString()).Return("forth").Repeat.Any();
161+
first.Expect(s => s.Equals(second)).Return(false).Repeat.Any();
162+
first.Expect(s => s.Equals(third)).Return(false).Repeat.Any();
163+
first.Expect(s => s.Equals(forth)).Return(false).Repeat.Any();
164+
third.Expect(s => s.Equals(first)).Return(false).Repeat.Any();
165+
third.Expect(s => s.Equals(second)).Return(false).Repeat.Any();
166+
third.Expect(s => s.Equals(forth)).Return(false).Repeat.Any();
167+
forth.Expect(s => s.Equals(first)).Return(false).Repeat.Any();
168+
forth.Expect(s => s.Equals(second)).Return(false).Repeat.Any();
169+
var toTest = new Disjunction<Fakes.FakeCandidate1>(first, second);
170+
var other = new Disjunction<Fakes.FakeCandidate1>(third, forth);
171+
172+
173+
// act:
174+
var result = toTest.Or(other) as Disjunction<Fakes.FakeCandidate1>;
175+
176+
// assert:
177+
Assert.IsNotNull(result);
178+
Assert.AreEqual(4, result.Count());
179+
Assert.AreSame(first, result.ElementAt(0));
180+
Assert.AreSame(second, result.ElementAt(1));
181+
Assert.AreSame(third, result.ElementAt(2));
182+
Assert.AreSame(forth, result.ElementAt(3));
183+
}
184+
185+
[Test]
186+
public void Or_anotherConjunction_removeDuplicatedSpecifications ()
187+
{
188+
// arrange:
189+
ISpecification<Fakes.FakeCandidate1> first = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>, IObject>();
190+
ISpecification<Fakes.FakeCandidate1> second = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>, IObject>();
191+
ISpecification<Fakes.FakeCandidate1> third = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>, IObject>();
192+
first.Expect(s => s.ToString()).Return("first").Repeat.Any();
193+
second.Expect(s => s.ToString()).Return("second").Repeat.Any();
194+
third.Expect(s => s.ToString()).Return("third").Repeat.Any();
195+
first.Expect(s => s.Equals(second)).Return(false).Repeat.Any();
196+
first.Expect(s => s.Equals(third)).Return(false).Repeat.Any();
197+
second.Expect(s => s.Equals(second)).Return(true).Repeat.Any();
198+
second.Expect(s => s.Equals(first)).Return(false).Repeat.Any();
199+
second.Expect(s => s.Equals(third)).Return(false).Repeat.Any();
200+
third.Expect(s => s.Equals(first)).Return(false).Repeat.Any();
201+
third.Expect(s => s.Equals(second)).Return(false).Repeat.Any();
202+
var toTest = new Disjunction<Fakes.FakeCandidate1>(first, second);
203+
var other = new Disjunction<Fakes.FakeCandidate1>(third, second);
204+
205+
206+
// act:
207+
var result = toTest.Or(other) as Disjunction<Fakes.FakeCandidate1>;
208+
209+
// assert:
210+
Assert.IsNotNull(result);
211+
Assert.AreEqual(3, result.Count());
212+
Assert.AreSame(first, result.ElementAt(0));
213+
Assert.AreSame(second, result.ElementAt(1));
214+
Assert.AreSame(third, result.ElementAt(2));
215+
}
216+
217+
[Test]
218+
public void Equals_toAnotherDisjuctionWithDifferentLenght_isFalse ()
219+
{
220+
// arrange:
221+
ISpecification<Fakes.FakeCandidate1> first = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>, IObject>();
222+
ISpecification<Fakes.FakeCandidate1> second = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>, IObject>();
223+
ISpecification<Fakes.FakeCandidate1> third = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>, IObject>();
224+
first.Expect(s => s.ToString()).Return("first").Repeat.Any();
225+
second.Expect(s => s.ToString()).Return("second").Repeat.Any();
226+
third.Expect(s => s.ToString()).Return("third").Repeat.Any();
227+
first.Expect(s => s.Equals(second)).Return(false).Repeat.AtLeastOnce();
228+
third.Expect(s => s.Equals(first)).Return(false).Repeat.AtLeastOnce();
229+
third.Expect(s => s.Equals(second)).Return(false).Repeat.AtLeastOnce();
230+
var toTest = new Disjunction<Fakes.FakeCandidate1>(first, second);
231+
var other = toTest.Or(third);
232+
233+
// act:
234+
bool result = toTest.Equals(other);
235+
236+
// assert:
237+
Assert.IsFalse(result);
238+
}
239+
240+
[Test]
241+
public void Equals_toAnotherDisjuctionWithDifferentSpecifications_isFalse ()
242+
{
243+
// arrange:
244+
ISpecification<Fakes.FakeCandidate1> first = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>, IObject>();
245+
ISpecification<Fakes.FakeCandidate1> second = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>, IObject>();
246+
ISpecification<Fakes.FakeCandidate1> third = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>, IObject>();
247+
first.Expect(s => s.ToString()).Return("first").Repeat.Any();
248+
second.Expect(s => s.ToString()).Return("second").Repeat.Any();
249+
third.Expect(s => s.ToString()).Return("third").Repeat.Any();
250+
first.Expect(s => s.Equals(first)).Return(true).Repeat.Any();
251+
first.Expect(s => s.Equals(second)).Return(false).Repeat.Any();
252+
first.Expect(s => s.Equals(third)).Return(false).Repeat.Any();
253+
second.Expect(s => s.Equals(first)).Return(false).Repeat.Any();
254+
second.Expect(s => s.Equals(second)).Return(true).Repeat.Any();
255+
second.Expect(s => s.Equals(third)).Return(false).Repeat.Any();
256+
third.Expect(s => s.Equals(first)).Return(false).Repeat.Any();
257+
third.Expect(s => s.Equals(second)).Return(false).Repeat.Any();
258+
var toTest = new Disjunction<Fakes.FakeCandidate1>(first, second);
259+
var other = new Disjunction<Fakes.FakeCandidate1>(first, third);
260+
261+
// act:
262+
bool result = toTest.Equals(other);
263+
264+
// assert:
265+
Assert.IsFalse(result);
266+
}
267+
268+
[Test]
269+
public void Equals_toAnotherDisjuctionWithEqualSpecificationsInTheSameOrder_isTrue ()
270+
{
271+
// arrange:
272+
ISpecification<Fakes.FakeCandidate1> first = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>, IObject>();
273+
ISpecification<Fakes.FakeCandidate1> second = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>, IObject>();
274+
first.Expect(s => s.ToString()).Return("first").Repeat.Any();
275+
second.Expect(s => s.ToString()).Return("second").Repeat.Any();
276+
first.Expect(s => s.Equals(first)).Return(true).Repeat.Any();
277+
first.Expect(s => s.Equals(second)).Return(false).Repeat.Any();
278+
second.Expect(s => s.Equals(first)).Return(false).Repeat.Any();
279+
second.Expect(s => s.Equals(second)).Return(true).Repeat.Any();
280+
var toTest = new Disjunction<Fakes.FakeCandidate1>(first, second);
281+
var other = new Disjunction<Fakes.FakeCandidate1>(first, second);
282+
283+
// act:
284+
bool result = toTest.Equals(other);
285+
286+
// assert:
287+
Assert.IsTrue(result);
288+
}
289+
290+
[Test]
291+
public void Equals_toAnotherDisjuctionWithEqualSpecificationsInDifferentOrder_isFalse ()
292+
{
293+
// arrange:
294+
ISpecification<Fakes.FakeCandidate1> first = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>, IObject>();
295+
ISpecification<Fakes.FakeCandidate1> second = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>, IObject>();
296+
first.Expect(s => s.ToString()).Return("first").Repeat.Any();
297+
second.Expect(s => s.ToString()).Return("second").Repeat.Any();
298+
first.Expect(s => s.Equals(first)).Return(true).Repeat.Any();
299+
first.Expect(s => s.Equals(second)).Return(false).Repeat.Any();
300+
second.Expect(s => s.Equals(first)).Return(false).Repeat.Any();
301+
second.Expect(s => s.Equals(second)).Return(true).Repeat.Any();
302+
var toTest = new Disjunction<Fakes.FakeCandidate1>(first, second);
303+
var other = new Disjunction<Fakes.FakeCandidate1>(second, first);
304+
305+
// act:
306+
bool result = toTest.Equals(other);
307+
308+
// assert:
309+
Assert.IsFalse(result);
310+
}
311+
312+
[Test]
313+
public void IsSatisfiedBy_aCandidateThatDoesntSatisfyAnyInnerSpecification_isFalse ()
314+
{
315+
// arrange:
316+
ISpecification<Fakes.FakeCandidate1> first = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>, IObject>();
317+
ISpecification<Fakes.FakeCandidate1> second = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>, IObject>();
318+
Fakes.FakeCandidate1 candidate = new Epic.Fakes.FakeCandidate1();
319+
first.Expect(s => s.Equals(second)).Return(false).Repeat.Any();
320+
first.Expect(s => s.IsSatisfiedBy(candidate)).Return(false).Repeat.AtLeastOnce();
321+
second.Expect(s => s.IsSatisfiedBy(candidate)).Return(false).Repeat.AtLeastOnce();
322+
var toTest = new Disjunction<Fakes.FakeCandidate1>(first, second);
323+
324+
// act:
325+
bool result = toTest.IsSatisfiedBy(candidate);
326+
327+
// assert:
328+
Assert.IsFalse(result);
329+
}
330+
331+
[Test]
332+
public void IsSatisfiedBy_aCandidateThatSatisfiesAnyInnerSpecification_isTrue ()
333+
{
334+
// arrange:
335+
ISpecification<Fakes.FakeCandidate1> first = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>, IObject>();
336+
ISpecification<Fakes.FakeCandidate1> second = GenerateStrictMock<ISpecification<Fakes.FakeCandidate1>, IObject>();
337+
Fakes.FakeCandidate1 candidate = new Epic.Fakes.FakeCandidate1();
338+
first.Expect(s => s.Equals(second)).Return(false).Repeat.Any();
339+
first.Expect(s => s.IsSatisfiedBy(candidate)).Return(false).Repeat.AtLeastOnce();
340+
second.Expect(s => s.IsSatisfiedBy(candidate)).Return(true).Repeat.AtLeastOnce();
341+
var toTest = new Disjunction<Fakes.FakeCandidate1>(first, second);
342+
343+
// act:
344+
bool result = toTest.IsSatisfiedBy(candidate);
345+
346+
// assert:
347+
Assert.IsTrue(result);
348+
}
349+
}
350+
}
351+

0 commit comments

Comments
 (0)