Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
Fixed AggregateOptional and AggregateOptionalNullable, fixes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Reichel committed Dec 22, 2015
1 parent 268bb9d commit a180203
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion paket.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NUGET
remote: https://nuget.org/api/v2
remote: https://www.nuget.org/api/v2
specs:
coveralls.net (0.6.0)
FAKE (4.11.3)
Expand Down
4 changes: 2 additions & 2 deletions src/NeverNull/Combinators/EnumerableExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public static Option<T> AggregateOptional<T>(this IEnumerable<T> enumerable, Fun
return accu.Match(
None: () => currentOption,
Some: previousValue => currentOption.Match(
None: () => currentOption,
None: () => previousValue,
Some: currentValue => fold(previousValue, currentValue)));
});
}
Expand Down Expand Up @@ -153,7 +153,7 @@ public static Option<T> AggregateOptionalNullable<T>(this IEnumerable<T?> enumer
return accu.Match(
None: () => currentOption,
Some: previousValue => currentOption.Match(
None: () => currentOption,
None: () => previousValue,
Some: currentValue => fold(previousValue, currentValue)));
});
}
Expand Down
17 changes: 12 additions & 5 deletions tests/NeverNull.Tests/Combinators/EnumerableExtTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Linq;
using FsCheck;
using NeverNull.Combinators;
Expand Down Expand Up @@ -56,20 +57,26 @@ public void Selecting_the_values_from_an_enumerable_of_options_of_nullables_shou
[Test]
public void Aggregating_the_values_from_an_enumerable_of_options_should_yield_None_for_an_empty_one_or_if_all_values_are_null() =>
ForAll<string[]>(xs => {
var check = xs.Aggregate(default(string), (a, c) => a == null ? c : a + c);
var check = xs.Where(x => x != null).Aggregate(default(string), (a, c) => a + c);
var o = xs.AggregateOptional((a, c) => a + c);
return o.Equals(Option.From(check));
})
.QuickCheckThrowOnFailure();

[Test]
public void Aggregating_the_values_from_an_enumerable_of_options_of_nullables_should_yield_None_for_an_empty_one_or_if_all_values_are_null() =>
ForAll<int?[]>(xs => {
var check = xs.Where(x => x.HasValue)
.Aggregate(0, (a, c) => a + c.Value);
var check = xs.Where(x => x.HasValue).Aggregate(default(int?),(a, c) => a.HasValue ? a.Value + c.Value : c.Value);
var o = xs.AggregateOptionalNullable((a, c) => a + c);
var l = xs.Any() ? string.Join(", ", xs) : "EMPTY";
Debug.WriteLine("------------");
Debug.WriteLine($"ORIG: {l}");
Debug.WriteLine($"CHECK: {check}");
Debug.WriteLine($"OPTION: {o}");
return xs.AggregateOptionalNullable((a, c) => a + c).Equals(check);
return o.Equals(Option.From(check));
})
.QuickCheckThrowOnFailure();

Expand Down

0 comments on commit a180203

Please sign in to comment.