Skip to content

Commit 303d4bf

Browse files
committed
some more tests for SpecificationBase
1 parent 7046c64 commit 303d4bf

File tree

4 files changed

+78
-4
lines changed

4 files changed

+78
-4
lines changed

Code/Epic.Prelude/Specifications/SpecificationBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public bool IsSatisfiedBy (TCandidate candidate)
130130
public ISpecification<TCandidate> And (ISpecification<TCandidate> other)
131131
{
132132
ThrowIfNull(other);
133-
if (other is Any<TCandidate> || this.Equals(other))
133+
if (other is No<TCandidate> || this.Equals(other))
134134
return other;
135135
if (other is Any<TCandidate>)
136136
return this;

Code/Epic.Prelude/VisitableBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ protected TResult AcceptMe<TResult, TVisitable>(TVisitable visitable, IVisitor<T
8585
// this would be a bug.
8686
throw new ArgumentOutOfRangeException("visitable", "You must provide the current instance.");
8787
}
88-
if(!typeof(TVisitable).Equals(this.GetType()))
88+
if(!typeof(TVisitable).IsInterface && !typeof(TVisitable).Equals(this.GetType()))
8989
{
9090
string message = string.Format("VisitableBase.AcceptMe() must be called only from leafs of the hierarchy tree.");
9191
throw new InvalidOperationException(message);

Code/UnitTests/Epic.Prelude.UnitTests/Fakes/SampleSpecification.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,19 @@ public class SampleSpecification<TCandidate> : Specifications.SpecificationBase<
3737
where TCandidate : class
3838
{
3939
public int EqualsACalled;
40+
public int IsSatisfiedByACalled;
41+
private readonly Func<TCandidate, bool> _satifactionRule;
4042
private readonly Func<ISampleSpecification<TCandidate>, ISampleSpecification<TCandidate>, bool> _equalityComparison;
4143
public SampleSpecification (Func<ISampleSpecification<TCandidate>, ISampleSpecification<TCandidate>, bool> equalityComparison)
4244
{
4345
_equalityComparison = equalityComparison;
4446
}
4547

48+
public SampleSpecification(Func<TCandidate, bool> satifactionRule)
49+
{
50+
_satifactionRule = satifactionRule;
51+
}
52+
4653
public SampleSpecification ()
4754
{
4855
}
@@ -56,7 +63,8 @@ protected override bool EqualsA (ISampleSpecification<TCandidate> otherSpecifica
5663

5764
protected override bool IsSatisfiedByA (TCandidate candidate)
5865
{
59-
throw new System.NotImplementedException ();
66+
IsSatisfiedByACalled++;
67+
return _satifactionRule(candidate);
6068
}
6169
#endregion
6270
}

Code/UnitTests/Epic.Prelude.UnitTests/Specifications/SpecificationBaseQA.cs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
//
2424
using NUnit.Framework;
2525
using System;
26+
using Rhino.Mocks;
27+
using Epic.Math;
2628

2729
namespace Epic.Specifications
2830
{
@@ -31,7 +33,7 @@ interface DummySpec<T> : ISpecification<T>, IEquatable<DummySpec<T>>
3133
{
3234
}
3335
[TestFixture()]
34-
public class SpecificationBaseQA
36+
public class SpecificationBaseQA : RhinoMocksFixtureBase
3537
{
3638
[Test]
3739
public void Initialize_aSpecificationTooAbstract_throwsTypeInitializationException()
@@ -88,6 +90,7 @@ public void Equals_toItself_isTrue()
8890
Assert.IsTrue (toTest.Equals(toTest as Object));
8991
Assert.IsTrue (toTest.Equals(toTest as Fakes.ISampleSpecification<string>));
9092
Assert.IsTrue (toTest.Equals(toTest as ISpecification<string>));
93+
Assert.AreEqual(typeof(Fakes.ISampleSpecification<string>).GetHashCode(), toTest.GetHashCode());
9194
}
9295

9396
[Test]
@@ -120,6 +123,69 @@ public void Equals_toAnotherSpecificationOfTheSameType_callEqualsA()
120123
Assert.AreEqual(3, toTest.EqualsACalled);
121124
Assert.AreEqual(3, other.EqualsACalled);
122125
}
126+
127+
[Test]
128+
public void Accept_withValidArguments_delegateVisitToTheRightVisitor()
129+
{
130+
// arrange:
131+
Fakes.ISampleSpecification<string> toTest = new Fakes.SampleSpecification<string>();
132+
object expectedResult = new object();
133+
IVisitContext context = GenerateStrictMock<IVisitContext>();
134+
IVisitor<object, Fakes.ISampleSpecification<string>> specificationVisitor = GenerateStrictMock<IVisitor<object, Fakes.ISampleSpecification<string>>>();
135+
specificationVisitor.Expect(v => v.Visit(toTest, context)).Return(expectedResult).Repeat.Once();
136+
IVisitor<object> visitor = GenerateStrictMock<IVisitor<object>>();
137+
visitor.Expect(v => v.GetVisitor(toTest)).Return(specificationVisitor).Repeat.Once ();
138+
139+
// act:
140+
object result = toTest.Accept(visitor, context);
141+
142+
// assert:
143+
Assert.AreSame(expectedResult, result);
144+
}
145+
146+
[Test]
147+
public void IsSatisfiedBy_null_isFalse()
148+
{
149+
// arrange:
150+
Fakes.SampleSpecification<string> toTest = new Fakes.SampleSpecification<string>();
151+
152+
// act:
153+
bool result = toTest.IsSatisfiedBy(null);
154+
155+
// assert:
156+
Assert.IsFalse(result);
157+
Assert.AreEqual(0, toTest.IsSatisfiedByACalled);
158+
}
159+
160+
[Test]
161+
public void IsSatifiedBy_aCandidate_callIsSatisfiedByA()
162+
{
163+
// arrange:
164+
string candidate = "test";
165+
Fakes.SampleSpecification<string> toTest1 = new Fakes.SampleSpecification<string>(s => true);
166+
Fakes.SampleSpecification<string> toTest2 = new Fakes.SampleSpecification<string>(s => false);
167+
168+
// assert:
169+
Assert.IsTrue(toTest1.IsSatisfiedBy(candidate));
170+
Assert.AreEqual(1, toTest1.IsSatisfiedByACalled);
171+
Assert.IsFalse(toTest2.IsSatisfiedBy(candidate));
172+
Assert.AreEqual(1, toTest2.IsSatisfiedByACalled);
173+
}
174+
175+
[Test]
176+
public void ApplyTo_aCandidate_callIsSatisfiedByA()
177+
{
178+
// arrange:
179+
string candidate = "test";
180+
Fakes.SampleSpecification<string> toTest1 = new Fakes.SampleSpecification<string>(s => true);
181+
Fakes.SampleSpecification<string> toTest2 = new Fakes.SampleSpecification<string>(s => false);
182+
183+
// assert:
184+
Assert.IsTrue((toTest1 as IMapping<string, bool>).ApplyTo(candidate));
185+
Assert.AreEqual(1, toTest1.IsSatisfiedByACalled);
186+
Assert.IsFalse((toTest2 as IMapping<string, bool>).ApplyTo(candidate));
187+
Assert.AreEqual(1, toTest2.IsSatisfiedByACalled);
188+
}
123189
}
124190
}
125191

0 commit comments

Comments
 (0)