Skip to content

Commit 71486af

Browse files
authored
Merge pull request #24 from ArturLavrov/feature-#23
Feature #23
2 parents e138b5c + 0b0ddc4 commit 71486af

File tree

4 files changed

+203
-16
lines changed

4 files changed

+203
-16
lines changed

src/DeclarativeContracts.Tests/Postcondition/EnsureTests.cs

+41
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,29 @@ public void Ensure_ValidArgumentsAndContractViolated_ContractViolationExceptionT
4747
)
4848
);
4949
}
50+
51+
[Test]
52+
public void Ensure_ValidArgumentsAndContractViolated_ContractViolationExceptionThrowsWithCustomMessage()
53+
{
54+
var customer = new Customer()
55+
{
56+
Name = null,
57+
LastName = "Osbourne"
58+
};
59+
60+
string contractViolationCustomMessage = "custom message";
61+
62+
Assert.Throws
63+
(
64+
typeof(ContractViolationException),
65+
() => Ensure.That(
66+
customer,
67+
customer => customer.Name,
68+
(name) => name != null,
69+
contractViolationCustomMessage
70+
)
71+
);
72+
}
5073

5174
[Test]
5275
public void Ensure_ValidArguemntsStringIsNotNull_ContractViolationExceptionDoesNotThrows()
@@ -103,5 +126,23 @@ public void That_PredicateRetursFalse_ThrowsContractViolationException()
103126

104127
Assert.Throws(typeof(ContractViolationException), () => Ensure.That(customer.Name, (name) => name.Length > 10));
105128
}
129+
130+
[Test]
131+
public void That_PredicateRetursFalse_ThrowsContractViolationExceptionWithCustomMessage()
132+
{
133+
var customer = new Customer()
134+
{
135+
Name = "Ozzy",
136+
LastName = "Osbourne"
137+
};
138+
139+
string customContractViolationMessage = "custom message";
140+
141+
Assert.Throws(typeof(ContractViolationException),
142+
() => Ensure.That(
143+
customer.Name,
144+
(name) => name.Length > 10),
145+
customContractViolationMessage);
146+
}
106147
}
107148
}

src/DeclarativeContracts.Tests/Precondition/RequireTest.cs

+61-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ namespace DeclarativeContracts.Tests.Precondition
99
[TestFixture]
1010
public class RequireTest
1111
{
12+
13+
14+
1215
[Test]
1316
public void That_ValidArguemntsAndContractNotViolated_ContractViolationExceptionDoesNotThrows()
1417
{
@@ -46,6 +49,29 @@ public void That_ValidArguemntsAndContractViolated_ContractViolationExceptionThr
4649
)
4750
);
4851
}
52+
53+
54+
[Test]
55+
public void That_ValidArguemntsAndContractViolated_ContractViolationExceptionWithCustomTextThrows()
56+
{
57+
var customer = new Customer()
58+
{
59+
Name = null,
60+
LastName = "Osbourne"
61+
};
62+
63+
string contractViolationMessageText = "contract violation";
64+
65+
Assert.Throws(
66+
typeof(ContractViolationException),
67+
() => Require.That(
68+
customer,
69+
с => с.Name,
70+
(name) => name != null,
71+
contractViolationMessageText
72+
)
73+
);
74+
}
4975

5076
[Test]
5177
public void That_CustomerNameSelectorIsNullPredicateNotNull_ArgumentExceptionThrows()
@@ -137,6 +163,26 @@ public void ForAll_ListWith3ElementsOneIsLessThanZero_ThrowsContractViolationExc
137163
)
138164
);
139165
}
166+
167+
[Test]
168+
public void ForAll_ListWith3ElementsOneIsLessThanZero_ThrowsContractViolationExceptionWithCustomErrorMessage()
169+
{
170+
List<int> elementsCollection = new List<int>
171+
{
172+
-1,3,4
173+
};
174+
175+
string contractViolationMessage = "contract was violated, custom message";
176+
177+
Assert.Throws(
178+
typeof(ContractViolationException),
179+
() => Require.ForAll(
180+
elementsCollection,
181+
((element) => element > 0),
182+
contractViolationMessage
183+
)
184+
);
185+
}
140186

141187
[Test]
142188
public void That_ContractViolatedPassCustomException_CustomExceptionWasThrows()
@@ -151,8 +197,7 @@ public void That_ContractViolatedPassCustomException_CustomExceptionWasThrows()
151197
typeof(ArgumentException),
152198
() => Require.That(customer.Name, n => n.Length > 5, new ArgumentException()));
153199
}
154-
155-
200+
156201
[Test]
157202
public void That_TrueForAllElements_ReturnTrue()
158203
{
@@ -166,6 +211,20 @@ public void That_FalseForOneElementFromCollection_ContractViolationExceptionWasT
166211
var collection = new List<string>(){ "o", "World"};
167212
Assert.Throws(typeof(ContractViolationException), () => Require.TrueForAll(collection, (item) => item.Length > 2));
168213
}
214+
215+
[Test]
216+
public void That_FalseForOneElementFromCollection_ContractViolationExceptionWasThrownWithCustomErrorMessage()
217+
{
218+
var collection = new List<string>(){ "o", "World"};
219+
220+
string contractViolationMessage = "contract was violated, custom message";
221+
222+
Assert.Throws(
223+
typeof(ContractViolationException),
224+
() => Require.TrueForAll(collection, (item) => item.Length > 2),
225+
contractViolationMessage
226+
);
227+
}
169228

170229
[Test]
171230
public void That_PredicateForApplyIsNull_ArgumentExceptionWasThrown()

src/DeclarativeContracts/Postcondition/Ensure.cs

+38
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,27 @@ public static void That<TEntity, TMember>(TEntity entity, Func<TEntity, TMember>
2828
throw new ContractViolationException("Contact postcondition was violated. Expected that predicate returns true.");
2929
}
3030
}
31+
32+
/// <summary>
33+
/// Method that verify element satisfied specified predicate
34+
/// </summary>
35+
/// <param name="entity"></param>
36+
/// <param name="memberSelector"></param>
37+
/// <param name="predicate"></param>
38+
/// <param name="contractViolationMsg">Custom text that will be throw as an exception text</param>
39+
/// <typeparam name="TEntity"></typeparam>
40+
/// <typeparam name="TMember"></typeparam>
41+
/// <returns></returns>
42+
public static void That<TEntity, TMember>(TEntity entity, Func<TEntity, TMember> memberSelector, Predicate<TMember> predicate, string contractViolationMsg)
43+
{
44+
ArgumentChecker.CheckArgumentsNull(memberSelector, predicate);
45+
46+
var member = memberSelector.Invoke(entity);
47+
if (!predicate(member))
48+
{
49+
throw new ContractViolationException(contractViolationMsg);
50+
}
51+
}
3152

3253
/// <summary>
3354
/// Verify that verify element satisfied specified predicate
@@ -45,6 +66,23 @@ public static void That<TElement>(TElement element, Predicate<TElement> predicat
4566
}
4667
}
4768

69+
/// <summary>
70+
/// Verify that verify element satisfied specified predicate
71+
/// </summary>
72+
/// <param name="element">Element that needs to be checked</param>
73+
/// <param name="predicate">Predicate that checks selected member</param>
74+
/// <param name="contractViolationMessage">Custom text that will be throw as an exception text</param>
75+
/// <typeparam name="TElement">Element type to check</typeparam>
76+
public static void That<TElement>(TElement element, Predicate<TElement> predicate, string contractViolationMessage)
77+
{
78+
ArgumentChecker.CheckArgumentsNull(predicate, predicate);
79+
80+
if (!predicate(element))
81+
{
82+
throw new ContractViolationException(contractViolationMessage);
83+
}
84+
}
85+
4886
/// <summary>
4987
///Verify that element satisfied specified predicate, and throw passed exception.
5088
/// </summary>

src/DeclarativeContracts/Precondition/Require/ForAll.cs

+63-14
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,23 @@ public static void ForAll<TElement>(IEnumerable<TElement> elementsSequence, Pred
2323
{
2424
ArgumentChecker.CheckArgumentsNull(predicate);
2525

26-
try
27-
{
28-
foreach (var element in elementsSequence)
29-
{
30-
if(!predicate(element))
31-
{
32-
throw new ContractViolationException($"Contact was violated.Predicate for element {element} return false.");
33-
}
34-
}
35-
}
36-
catch(Exception ex)
37-
{
38-
throw new ContractViolationException("Contact was violated because an unhandled exception occured.", ex);
39-
}
26+
InternalForAll(elementsSequence, predicate);
27+
}
28+
29+
/// <summary>
30+
/// Methods that checks whether all members of input sequence satisfy the predicate
31+
/// </summary>
32+
/// <param name="elementsSequence">Input sequence of elements</param>
33+
/// <param name="predicate">Predicate that tests every element of sequence</param>
34+
/// <param name="contractViolationMessage">Custom text that will be throw as an exception text</param>
35+
/// <typeparam name="TElement">Element type</typeparam>
36+
/// <exception cref="ContractViolationException"></exception>
37+
/// <throws>Throws ContractViolationException if element does not satisfy predicate</throws>
38+
public static void ForAll<TElement>(IEnumerable<TElement> elementsSequence, Predicate<TElement> predicate, string contractViolationMessage)
39+
{
40+
ArgumentChecker.CheckArgumentsNull(predicate);
41+
42+
InternalForAll(elementsSequence, predicate, contractViolationMessage);
4043
}
4144

4245
/// <summary>
@@ -56,5 +59,51 @@ public static void TrueForAll<TElement>(IEnumerable<TElement> elementsSequence,
5659
throw new ContractViolationException();
5760
}
5861
}
62+
63+
/// <summary>
64+
/// Methods that checks whether predicate is true for all member of input sequence
65+
/// </summary>
66+
/// <param name="elementsSequence">Input sequence of elements</param>
67+
/// <param name="predicate">Predicate that tests every element of sequence</param>
68+
/// <param name="contractViolationMessage">Custom text that will be throw as an exception text</param>
69+
/// <typeparam name="TElement">Element type</typeparam>
70+
/// <exception cref="ContractViolationException"></exception>
71+
/// <throws>Throws ContractViolationException if element does not satisfy predicate</throws>
72+
public static void TrueForAll<TElement>(IEnumerable<TElement> elementsSequence, Predicate<TElement> predicate, string contractViolationMessage)
73+
{
74+
ArgumentChecker.CheckArgumentsNull(predicate);
75+
76+
if (elementsSequence.Any(element => !predicate(element)))
77+
{
78+
throw new ContractViolationException(contractViolationMessage);
79+
}
80+
}
81+
82+
private static void InternalForAll<TElement>(IEnumerable<TElement> elementsSequence, Predicate<TElement> predicate, string contractViolationMessage = null)
83+
{
84+
try
85+
{
86+
foreach (var element in elementsSequence)
87+
{
88+
if(!predicate(element))
89+
{
90+
throw new ContractViolationException(contractViolationMessage ?? $"Contact was violated.Predicate for element {element} return false.");
91+
}
92+
}
93+
}
94+
catch(Exception ex)
95+
{
96+
throw new ContractViolationException(contractViolationMessage ?? "Contact was violated because an unhandled exception occured.", ex);
97+
}
98+
}
99+
100+
public static void InternalTrueForAll<TElement>(IEnumerable<TElement> elementsSequence,
101+
Predicate<TElement> predicate, string contractViolationMessage)
102+
{
103+
if (elementsSequence.Any(element => !predicate(element)))
104+
{
105+
throw new ContractViolationException(contractViolationMessage ?? "default message");
106+
}
107+
}
59108
}
60109
}

0 commit comments

Comments
 (0)