Skip to content

Commit 221a7e0

Browse files
Added pre-condition checks and requirements to support checking for Result and Optional types. (#77)
1 parent 96330c0 commit 221a7e0

File tree

2 files changed

+482
-0
lines changed

2 files changed

+482
-0
lines changed

OnixLabs.Core.UnitTests/PreconditionTests.cs

+314
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ namespace OnixLabs.Core.UnitTests;
2020

2121
public sealed class PreconditionTests
2222
{
23+
private static readonly Exception Exception = new("Failure");
24+
2325
[Fact(DisplayName = "Check should throw an InvalidOperationException when the condition is false")]
2426
public void CheckShouldThrowInvalidOperationExceptionWhenConditionIsFalse()
2527
{
@@ -37,6 +39,162 @@ public void CheckShouldNotThrowInvalidOperationExceptionWhenConditionIsTrue()
3739
Check(true);
3840
}
3941

42+
[Fact(DisplayName = "CheckIsFailure should throw an InvalidOperationException when the result is not a failure state")]
43+
public void CheckIsFailureShouldThrowAnInvalidOperationExceptionWhenTheResultIsNotAFailureState()
44+
{
45+
// Given
46+
Result result = Result.Success();
47+
48+
// When
49+
Exception exception = Assert.Throws<InvalidOperationException>(() => CheckIsFailure(result));
50+
51+
// Then
52+
Assert.Equal("Argument must be a Failure state.", exception.Message);
53+
}
54+
55+
[Fact(DisplayName = "CheckIsFailure should return a Failure when the result is a failure state")]
56+
public void CheckIsFailureShouldReturnFailureWhenTheResultIsAFailureState()
57+
{
58+
// Given
59+
Result result = Exception;
60+
61+
// When
62+
Result actual = CheckIsFailure(result);
63+
64+
// Then
65+
Assert.IsType<Failure>(actual);
66+
}
67+
68+
[Fact(DisplayName = "CheckIsSuccess should throw an InvalidOperationException when the result is not a success state")]
69+
public void CheckIsSuccessShouldThrowAnInvalidOperationExceptionWhenTheResultIsNotASuccessState()
70+
{
71+
// Given
72+
Result result = Exception;
73+
74+
// When
75+
Exception exception = Assert.Throws<InvalidOperationException>(() => CheckIsSuccess(result));
76+
77+
// Then
78+
Assert.Equal("Argument must be a Success state.", exception.Message);
79+
}
80+
81+
[Fact(DisplayName = "CheckIsSuccess should return a Success when the result is a success state")]
82+
public void CheckIsSuccessShouldReturnFailureWhenTheResultIsASuccessState()
83+
{
84+
// Given
85+
Result result = Result.Success();
86+
87+
// When
88+
Result actual = CheckIsSuccess(result);
89+
90+
// Then
91+
Assert.IsType<Success>(actual);
92+
}
93+
94+
[Fact(DisplayName = "CheckIsFailure<T> should throw an InvalidOperationException when the result is not a failure state")]
95+
public void CheckIsFailureTShouldThrowAnInvalidOperationExceptionWhenTheResultIsNotAFailureState()
96+
{
97+
// Given
98+
Result<int> result = 1;
99+
100+
// When
101+
Exception exception = Assert.Throws<InvalidOperationException>(() => CheckIsFailure(result));
102+
103+
// Then
104+
Assert.Equal("Argument must be a Failure state.", exception.Message);
105+
}
106+
107+
[Fact(DisplayName = "CheckIsFailure<T> should return a Failure when the result is a failure state")]
108+
public void CheckIsFailureTShouldReturnFailureWhenTheResultIsAFailureState()
109+
{
110+
// Given
111+
Result<int> result = Exception;
112+
113+
// When
114+
Result<int> actual = CheckIsFailure(result);
115+
116+
// Then
117+
Assert.IsType<Failure<int>>(actual);
118+
}
119+
120+
[Fact(DisplayName = "CheckIsSuccess<T> should throw an InvalidOperationException when the result is not a success state")]
121+
public void CheckIsSuccessTShouldThrowAnInvalidOperationExceptionWhenTheResultIsNotASuccessState()
122+
{
123+
// Given
124+
Result<int> result = Exception;
125+
126+
// When
127+
Exception exception = Assert.Throws<InvalidOperationException>(() => CheckIsSuccess(result));
128+
129+
// Then
130+
Assert.Equal("Argument must be a Success state.", exception.Message);
131+
}
132+
133+
[Fact(DisplayName = "CheckIsSuccess<T> should return a Success when the result is a success state")]
134+
public void CheckIsSuccessTShouldReturnFailureWhenTheResultIsASuccessState()
135+
{
136+
// Given
137+
Result<int> result = 1;
138+
139+
// When
140+
Result<int> actual = CheckIsSuccess(result);
141+
142+
// Then
143+
Assert.IsType<Success<int>>(actual);
144+
}
145+
146+
[Fact(DisplayName = "CheckIsNone<T> should throw an InvalidOperationException when the optional is not a None<T> value")]
147+
public void CheckIsNoneShouldThrowAnInvalidOperationExceptionWhenTheOptionalIsNotNoneValue()
148+
{
149+
// Given
150+
Optional<int> optional = 1;
151+
152+
// When
153+
Exception exception = Assert.Throws<InvalidOperationException>(() => CheckIsNone(optional));
154+
155+
// Then
156+
Assert.Equal("Argument must be a None<T> value.", exception.Message);
157+
}
158+
159+
[Fact(DisplayName = "CheckIsNone<T> should return a None<T> when the optional is a None<T> value")]
160+
public void CheckIsNoneShouldReturnNoneWhenOptionalIsNoneValue()
161+
{
162+
// Given
163+
Optional<int> optional = Optional<int>.None;
164+
165+
// When
166+
Optional<int> actual = CheckIsNone(optional);
167+
168+
// Then
169+
Assert.IsType<None<int>>(actual);
170+
}
171+
172+
[Fact(DisplayName = "CheckIsSome<T> should throw an InvalidOperationException when the optional is not a Some<T> value")]
173+
public void CheckIsSomeShouldThrowAnInvalidOperationExceptionWhenTheOptionalIsNotSomeValue()
174+
{
175+
// Given
176+
Optional<int> optional = Optional<int>.None;
177+
178+
// When
179+
Exception exception = Assert.Throws<InvalidOperationException>(() => CheckIsSome(optional));
180+
181+
// Then
182+
Assert.Equal("Argument must be a Some<T> value.", exception.Message);
183+
}
184+
185+
[Fact(DisplayName = "CheckIsSome<T> should return a Some<T> when the optional is a Some<T> value")]
186+
public void CheckIsSomeShouldReturnSomeWhenOptionalIsSomeValue()
187+
{
188+
// Given
189+
Optional<int> optional = 1;
190+
191+
// When
192+
Optional<int> actual = CheckIsSome(optional);
193+
194+
// Then
195+
Assert.IsType<Some<int>>(actual);
196+
}
197+
40198
[Fact(DisplayName = "CheckNotNull should throw an InvalidOperationException when the condition is null")]
41199
public void CheckNotNullShouldThrowInvalidOperationExceptionWhenConditionIsNull()
42200
{
@@ -165,6 +323,162 @@ public void RequireShouldNotThrowInvalidOperationExceptionWhenConditionIsTrue()
165323
Require(true);
166324
}
167325

326+
[Fact(DisplayName = "RequireIsFailure should throw an InvalidOperationException when the result is not a failure state")]
327+
public void RequireIsFailureShouldThrowAnInvalidOperationExceptionWhenTheResultIsNotAFailureState()
328+
{
329+
// Given
330+
Result result = Result.Success();
331+
332+
// When
333+
Exception exception = Assert.Throws<ArgumentException>(() => RequireIsFailure(result));
334+
335+
// Then
336+
Assert.Equal("Argument must be a Failure state.", exception.Message);
337+
}
338+
339+
[Fact(DisplayName = "RequireIsFailure should return a Failure when the result is a failure state")]
340+
public void RequireIsFailureShouldReturnFailureWhenTheResultIsAFailureState()
341+
{
342+
// Given
343+
Result result = Exception;
344+
345+
// When
346+
Result actual = RequireIsFailure(result);
347+
348+
// Then
349+
Assert.IsType<Failure>(actual);
350+
}
351+
352+
[Fact(DisplayName = "RequireIsSuccess should throw an InvalidOperationException when the result is not a success state")]
353+
public void RequireIsSuccessShouldThrowAnInvalidOperationExceptionWhenTheResultIsNotASuccessState()
354+
{
355+
// Given
356+
Result result = Exception;
357+
358+
// When
359+
Exception exception = Assert.Throws<ArgumentException>(() => RequireIsSuccess(result));
360+
361+
// Then
362+
Assert.Equal("Argument must be a Success state.", exception.Message);
363+
}
364+
365+
[Fact(DisplayName = "RequireIsSuccess should return a Success when the result is a success state")]
366+
public void RequireIsSuccessShouldReturnFailureWhenTheResultIsASuccessState()
367+
{
368+
// Given
369+
Result result = Result.Success();
370+
371+
// When
372+
Result actual = RequireIsSuccess(result);
373+
374+
// Then
375+
Assert.IsType<Success>(actual);
376+
}
377+
378+
[Fact(DisplayName = "RequireIsFailure<T> should throw an InvalidOperationException when the result is not a failure state")]
379+
public void RequireIsFailureTShouldThrowAnInvalidOperationExceptionWhenTheResultIsNotAFailureState()
380+
{
381+
// Given
382+
Result<int> result = 1;
383+
384+
// When
385+
Exception exception = Assert.Throws<ArgumentException>(() => RequireIsFailure(result));
386+
387+
// Then
388+
Assert.Equal("Argument must be a Failure state.", exception.Message);
389+
}
390+
391+
[Fact(DisplayName = "RequireIsFailure<T> should return a Failure when the result is a failure state")]
392+
public void RequireIsFailureTShouldReturnFailureWhenTheResultIsAFailureState()
393+
{
394+
// Given
395+
Result<int> result = Exception;
396+
397+
// When
398+
Result<int> actual = RequireIsFailure(result);
399+
400+
// Then
401+
Assert.IsType<Failure<int>>(actual);
402+
}
403+
404+
[Fact(DisplayName = "RequireIsSuccess<T> should throw an InvalidOperationException when the result is not a success state")]
405+
public void RequireIsSuccessTShouldThrowAnInvalidOperationExceptionWhenTheResultIsNotASuccessState()
406+
{
407+
// Given
408+
Result<int> result = Exception;
409+
410+
// When
411+
Exception exception = Assert.Throws<ArgumentException>(() => RequireIsSuccess(result));
412+
413+
// Then
414+
Assert.Equal("Argument must be a Success state.", exception.Message);
415+
}
416+
417+
[Fact(DisplayName = "RequireIsSuccess<T> should return a Success when the result is a success state")]
418+
public void RequireIsSuccessTShouldReturnFailureWhenTheResultIsASuccessState()
419+
{
420+
// Given
421+
Result<int> result = 1;
422+
423+
// When
424+
Result<int> actual = RequireIsSuccess(result);
425+
426+
// Then
427+
Assert.IsType<Success<int>>(actual);
428+
}
429+
430+
[Fact(DisplayName = "RequireIsNone<T> should throw an InvalidOperationException when the optional is not a None<T> value")]
431+
public void RequireIsNoneShouldThrowAnInvalidOperationExceptionWhenTheOptionalIsNotNoneValue()
432+
{
433+
// Given
434+
Optional<int> optional = 1;
435+
436+
// When
437+
Exception exception = Assert.Throws<ArgumentException>(() => RequireIsNone(optional));
438+
439+
// Then
440+
Assert.Equal("Argument must be a None<T> value.", exception.Message);
441+
}
442+
443+
[Fact(DisplayName = "RequireIsNone<T> should return a None<T> when the optional is a None<T> value")]
444+
public void RequireIsNoneShouldReturnNoneWhenOptionalIsNoneValue()
445+
{
446+
// Given
447+
Optional<int> optional = Optional<int>.None;
448+
449+
// When
450+
Optional<int> actual = RequireIsNone(optional);
451+
452+
// Then
453+
Assert.IsType<None<int>>(actual);
454+
}
455+
456+
[Fact(DisplayName = "RequireIsSome<T> should throw an InvalidOperationException when the optional is not a Some<T> value")]
457+
public void RequireIsSomeShouldThrowAnInvalidOperationExceptionWhenTheOptionalIsNotSomeValue()
458+
{
459+
// Given
460+
Optional<int> optional = Optional<int>.None;
461+
462+
// When
463+
Exception exception = Assert.Throws<ArgumentException>(() => RequireIsSome(optional));
464+
465+
// Then
466+
Assert.Equal("Argument must be a Some<T> value.", exception.Message);
467+
}
468+
469+
[Fact(DisplayName = "RequireIsSome<T> should return a Some<T> when the optional is a Some<T> value")]
470+
public void RequireIsSomeShouldReturnSomeWhenOptionalIsSomeValue()
471+
{
472+
// Given
473+
Optional<int> optional = 1;
474+
475+
// When
476+
Optional<int> actual = RequireIsSome(optional);
477+
478+
// Then
479+
Assert.IsType<Some<int>>(actual);
480+
}
481+
168482
[Fact(DisplayName = "RequireWithinRangeInclusive should throw an ArgumentOutOfRangeException when the value falls below the specified range")]
169483
public void RequireWithinRangeInclusiveShouldThrowArgumentOutOfRangeExceptionWhenValueFallsBelowSpecifiedRange()
170484
{

0 commit comments

Comments
 (0)