Skip to content

Commit 88e9d8e

Browse files
Added several new monad features to existing Optional<T> and Result<T> monads, and added completely new non-generic Result monad. (#51)
1 parent 5cf25f8 commit 88e9d8e

9 files changed

+806
-71
lines changed

OnixLabs.Core.UnitTests/OptionalTests.cs

+34-28
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ public void OptionalSomeShouldProduceExpectedResult()
104104
Assert.Equal("abc", text);
105105
}
106106

107-
[Fact(DisplayName = "Optional implicit operator should produce the expected result.")]
108-
public void OptionalImplicitOperatorShouldProduceExpectedResult()
107+
[Fact(DisplayName = "Optional implicit operator should produce the expected some result.")]
108+
public void OptionalImplicitOperatorShouldProduceExpectedSomeResult()
109109
{
110110
// Given / When
111111
Optional<int> number = 123;
@@ -121,6 +121,18 @@ public void OptionalImplicitOperatorShouldProduceExpectedResult()
121121
Assert.Equal("abc", text);
122122
}
123123

124+
[Fact(DisplayName = "Optional implicit operator should produce the expected none result.")]
125+
public void OptionalImplicitOperatorShouldProduceExpectedNoneResult()
126+
{
127+
// Given / When
128+
const string? value = default;
129+
Optional<string> optional = value;
130+
131+
// Then
132+
Assert.False(optional.HasValue);
133+
Assert.IsType<None<string>>(optional);
134+
}
135+
124136
[Fact(DisplayName = "Optional Some explicit operator should produce the expected result.")]
125137
public void OptionalSomeExplicitOperatorShouldProduceExpectedResult()
126138
{
@@ -210,10 +222,10 @@ public void OptionalSomeGetHashCodeShouldProduceExpectedResult()
210222
{
211223
// Given
212224
const int expected = 123;
213-
Optional<int> value = 123;
225+
Optional<int> optional = 123;
214226

215227
// When
216-
int actual = value.GetHashCode();
228+
int actual = optional.GetHashCode();
217229

218230
// Then
219231
Assert.Equal(expected, actual);
@@ -224,10 +236,10 @@ public void OptionalNoneGetHashCodeShouldProduceExpectedResult()
224236
{
225237
// Given
226238
const int expected = default;
227-
Optional<int> value = Optional<int>.None;
239+
Optional<int> optional = Optional<int>.None;
228240

229241
// When
230-
int actual = value.GetHashCode();
242+
int actual = optional.GetHashCode();
231243

232244
// Then
233245
Assert.Equal(expected, actual);
@@ -334,13 +346,10 @@ public void OptionalSomeMatchShouldExecuteSomeAction()
334346
{
335347
// Given
336348
bool someCalled = false;
337-
Optional<int> number = 123;
349+
Optional<int> optional = 123;
338350

339351
// When
340-
number.Match(
341-
some: _ => { someCalled = true; },
342-
none: () => { }
343-
);
352+
optional.Match(some: _ => { someCalled = true; });
344353

345354
// Then
346355
Assert.True(someCalled);
@@ -351,13 +360,10 @@ public void OptionalNoneMatchShouldExecuteNoneAction()
351360
{
352361
// Given
353362
bool noneCalled = false;
354-
Optional<int> number = Optional<int>.None;
363+
Optional<int> optional = Optional<int>.None;
355364

356365
// When
357-
number.Match(
358-
some: _ => { },
359-
none: () => { noneCalled = true; }
360-
);
366+
optional.Match(none: () => { noneCalled = true; });
361367

362368
// Then
363369
Assert.True(noneCalled);
@@ -368,10 +374,10 @@ public void OptionalSomeMatchShouldProduceExpectedResult()
368374
{
369375
// Given
370376
const int expected = 9;
371-
Optional<int> number = 3;
377+
Optional<int> optional = 3;
372378

373379
// When
374-
int actual = number.Match(
380+
int actual = optional.Match(
375381
some: value => value * value,
376382
none: () => 0
377383
);
@@ -385,10 +391,10 @@ public void OptionalNoneMatchShouldProduceExpectedResult()
385391
{
386392
// Given
387393
const int expected = 0;
388-
Optional<int> number = Optional<int>.None;
394+
Optional<int> optional = Optional<int>.None;
389395

390396
// When
391-
int actual = number.Match(
397+
int actual = optional.Match(
392398
some: value => value * value,
393399
none: () => 0
394400
);
@@ -402,10 +408,10 @@ public void OptionalSomeSelectShouldProduceExpectedResult()
402408
{
403409
// Given
404410
const int expected = 9;
405-
Optional<int> number = 3;
411+
Optional<int> optional = 3;
406412

407413
// When
408-
Optional<int> actual = number.Select(value => value * value);
414+
Optional<int> actual = optional.Select(value => value * value);
409415

410416
// Then
411417
Assert.Equal(expected, actual);
@@ -415,10 +421,10 @@ public void OptionalSomeSelectShouldProduceExpectedResult()
415421
public void OptionalNoneSelectShouldProduceExpectedResult()
416422
{
417423
// Given
418-
Optional<int> number = Optional<int>.None;
424+
Optional<int> optional = Optional<int>.None;
419425

420426
// When
421-
Optional<int> actual = number.Select(value => value * value);
427+
Optional<int> actual = optional.Select(value => value * value);
422428

423429
// Then
424430
Assert.Equal(Optional<int>.None, actual);
@@ -429,10 +435,10 @@ public void OptionalSomeSelectManyShouldProduceExpectedResult()
429435
{
430436
// Given
431437
const int expected = 9;
432-
Optional<int> number = 3;
438+
Optional<int> optional = 3;
433439

434440
// When
435-
Optional<int> actual = number.SelectMany<int>(value => value * value);
441+
Optional<int> actual = optional.SelectMany<int>(value => value * value);
436442

437443
// Then
438444
Assert.Equal(expected, actual);
@@ -442,10 +448,10 @@ public void OptionalSomeSelectManyShouldProduceExpectedResult()
442448
public void OptionalNoneSelectManyShouldProduceExpectedResult()
443449
{
444450
// Given
445-
Optional<int> number = Optional<int>.None;
451+
Optional<int> optional = Optional<int>.None;
446452

447453
// When
448-
Optional<int> actual = number.SelectMany<int>(value => value * value);
454+
Optional<int> actual = optional.SelectMany<int>(value => value * value);
449455

450456
// Then
451457
Assert.Equal(Optional<int>.None, actual);

0 commit comments

Comments
 (0)