Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add async Select and Consume #39

Merged
merged 4 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 152 additions & 1 deletion Maybe.Test/MaybeExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using FluentAssertions;
using System;
using System.Threading.Tasks;
using FluentAssertions;
using Xunit;

namespace ZBRA.Maybe.Test
Expand Down Expand Up @@ -106,6 +107,15 @@ public void Select_WithNullableProperty_ReturnsSelectedProperty(Maybe<NullableIn
result.Should().Be(expected);
}

[Theory]
[MemberData(nameof(Select_WithNullablePropertyTestCases))]
public async Task SelectAsync_WithNullableProperty_ReturnsSelectedProperty(Maybe<NullableIntObj> subject, Maybe<int> expected)
{
var result = await subject.SelectAsync(async o => await Task.FromResult(o.Count));

result.Should().Be(expected);
}

public static TheoryData<Maybe<NullableIntObj>, Maybe<int>> Select_WithNullablePropertyTestCases()
{
return new TheoryData<Maybe<NullableIntObj>, Maybe<int>>
Expand Down Expand Up @@ -141,6 +151,15 @@ public void SelectMany_WithMaybeProperty_ReturnsSelectedProperty(Maybe<MaybeIntO
result.Should().Be(expected);
}

[Theory]
[MemberData(nameof(SelectMany_WithMaybePropertyTestCases))]
public async Task SelectManyAsync_WithMaybeProperty_ReturnsSelectedProperty(Maybe<MaybeIntObj> subject, Maybe<int> expected)
{
var result = await subject.SelectManyAsync(async o => await Task.FromResult(o.Count));

result.Should().Be(expected);
}

public static TheoryData<Maybe<MaybeIntObj>, Maybe<int>> SelectMany_WithMaybePropertyTestCases()
{
return new TheoryData<Maybe<MaybeIntObj>, Maybe<int>>
Expand All @@ -158,5 +177,137 @@ public void SelectMany_NullArgument_ShouldThrow()

subject.Should().ThrowExactly<ArgumentNullException>();
}

[Fact]
public void Consume_WithValue_ShouldExecuteAction()
{
string result = null;
var expected = 1;

expected.ToMaybe()
.Consume(i => result = i.ToString());

result.Should().Be(expected.ToString());
}

[Fact]
public async Task ConsumeAsync_WithValue_ShouldExecuteAction()
{
string result = null;
const int expected = 1;

await expected.ToMaybe()
.ConsumeAsync(async i =>
{
result = i.ToString();
await Task.CompletedTask;
});

result.Should().Be(expected.ToString());
}

[Fact]
public void Consume_WithNoValue_ShouldNotExecuteAction()
{
var result = "a";

Maybe<string>.Nothing
.Consume(i => result = "b");

result.Should().Be("a");
}

[Fact]
public async Task ConsumeAsync_WithNoValue_ShouldNotExecuteAction()
{
var result = "a";

await Maybe<string>.Nothing
.ConsumeAsync(async _ =>
{
result = "b";
await Task.CompletedTask;
});

result.Should().Be("a");
}

[Fact]
public void Consume_NullArgument_ShouldThrow()
{
Action subject = () => 1.ToMaybe().Consume((Action<int>)null);

subject.Should().ThrowExactly<ArgumentNullException>();
}

[Theory]
[InlineData(null, null, null)]
[InlineData(null, 2, null)]
[InlineData(2, null, null)]
[InlineData(1, 2, "3")]
public void Zip_WithTransformer_ShouldZipValues(int? value, double? otherValue, string expected)
{
static string transformer(int v, double o) => (v + o).ToString();

var result = value.ToMaybe()
.Zip(otherValue.ToMaybe(), transformer);

result.Should().Be(expected.ToMaybe());
}

[Theory]
[InlineData(null, null, null)]
[InlineData(null, 2, null)]
[InlineData(2, null, null)]
[InlineData(1, 2, "3")]
public void Zip_WithMaybeTransformer_ShouldZipValues(int? value, double? otherValue, string expected)
{
static Maybe<string> transformer(int v, double o) => (v + o).ToString().ToMaybe();

var result = value.ToMaybe()
.Zip(otherValue.ToMaybe(), transformer);

result.Should().Be(expected.ToMaybe());
}

[Fact]
public void Zip_NullArgument_ShouldThrow()
{
Action subject = () => 1.ToMaybe().Zip(Maybe<int>.Nothing, (Func<int, int, int>)null);

subject.Should().ThrowExactly<ArgumentNullException>();
}

[Fact]
public void Zip_MaybeNullArgument_ShouldThrow()
{
Action subject = () => 1.ToMaybe().Zip(Maybe<int>.Nothing, (Func<int, int, Maybe<int>>)null);

subject.Should().ThrowExactly<ArgumentNullException>();
}

[Theory]
[InlineData(null, null, null)]
[InlineData(null, 2, null)]
[InlineData(2, null, null)]
[InlineData(1, 2, "3")]
public void ZipConsume_WhenBothValuesExist_ShouldExecuteAction(int? value, double? otherValue, string expected)
{
string result = null;
void action(int v, double o) => result = (v + o).ToString();

value.ToMaybe()
.ZipAndConsume(otherValue.ToMaybe(), action);

result.Should().Be(expected);
}

[Fact]
public void ZipAndConsume_NullArgument_ShouldThrow()
{
Action subject = () => 1.ToMaybe().ZipAndConsume(Maybe<int>.Nothing, null);

subject.Should().ThrowExactly<ArgumentNullException>();
}
}
}
141 changes: 39 additions & 102 deletions Maybe.Test/MaybeTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FluentAssertions;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using FluentAssertions;
using Xunit;

namespace ZBRA.Maybe.Test
Expand Down Expand Up @@ -29,6 +30,17 @@ public void Or_Func_ShouldReturnValueOrDefaultValue(int? value, int defaultValue
result.Should().Be(expected);
}

[Theory]
[InlineData(1, 2, 1)]
[InlineData(null, 2, 2)]
public async Task OrAsync_Func_ShouldReturnValueOrDefaultValue(int? value, int defaultValue, int expected)
{
var result = await value.ToMaybe()
.OrAsync(async () => await Task.FromResult(defaultValue));

result.Should().Be(expected);
}

[Fact]
public void Or_Func_NullArgument_ShouldThrow()
{
Expand Down Expand Up @@ -74,107 +86,6 @@ public void OrThrow_NullArgument_ShouldThrow()
subject.Should().ThrowExactly<ArgumentNullException>();
}

[Fact]
public void Consume_WithValue_ShouldExecuteAction()
{
string result = null;
var expected = 1;

expected.ToMaybe()
.Consume(i => result = i.ToString());

result.Should().Be(expected.ToString());
}

[Fact]
public void Consume_WithNoValue_ShouldNotExecuteAction()
{
var result = "a";

Maybe<string>.Nothing
.Consume(i => result = "b");

result.Should().Be("a");
}

[Fact]
public void Consume_NullArgument_ShouldThrow()
{
Action subject = () => 1.ToMaybe().Consume(null);

subject.Should().ThrowExactly<ArgumentNullException>();
}

[Theory]
[InlineData(null, null, null)]
[InlineData(null, 2, null)]
[InlineData(2, null, null)]
[InlineData(1, 2, "3")]
public void Zip_WithTransformer_ShouldZipValues(int? value, double? otherValue, string expected)
{
static string transformer(int v, double o) => (v + o).ToString();

var result = value.ToMaybe()
.Zip(otherValue.ToMaybe(), transformer);

result.Should().Be(expected.ToMaybe());
}

[Theory]
[InlineData(null, null, null)]
[InlineData(null, 2, null)]
[InlineData(2, null, null)]
[InlineData(1, 2, "3")]
public void Zip_WithMaybeTransformer_ShouldZipValues(int? value, double? otherValue, string expected)
{
static Maybe<string> transformer(int v, double o) => (v + o).ToString().ToMaybe();

var result = value.ToMaybe()
.Zip(otherValue.ToMaybe(), transformer);

result.Should().Be(expected.ToMaybe());
}

[Fact]
public void Zip_NullArgument_ShouldThrow()
{
Action subject = () => 1.ToMaybe().Zip(Maybe<int>.Nothing, (Func<int, int, int>)null);

subject.Should().ThrowExactly<ArgumentNullException>();
}

[Fact]
public void Zip_MaybeNullArgument_ShouldThrow()
{
Action subject = () => 1.ToMaybe().Zip(Maybe<int>.Nothing, (Func<int, int, Maybe<int>>)null);

subject.Should().ThrowExactly<ArgumentNullException>();
}

[Theory]
[InlineData(null, null, null)]
[InlineData(null, 2, null)]
[InlineData(2, null, null)]
[InlineData(1, 2, "3")]
public void ZipConsume_WhenBothValuesExist_ShouldExecuteAction(int? value, double? otherValue, string expected)
{
string result = null;
void action(int v, double o) => result = (v + o).ToString();

value.ToMaybe()
.ZipAndConsume(otherValue.ToMaybe(), action);

result.Should().Be(expected);
}

[Fact]
public void ZipAndConsume_NullArgument_ShouldThrow()
{
Action subject = () => 1.ToMaybe().ZipAndConsume(Maybe<int>.Nothing, null);

subject.Should().ThrowExactly<ArgumentNullException>();
}

[Fact]
public void Equals_WhenObjectReferencesAreEqual()
{
Expand Down Expand Up @@ -294,6 +205,16 @@ public void OrMaybe_WithAlternative_ShouldReturnSubjectOrAlternative(Maybe<Strin
result.Should().Be(expected.ToMaybe());
}

[Theory]
[MemberData(nameof(OrMaybe_WithAlternative_ShouldReturnSubjectOrAlternativeTestCases))]
public async Task OrMaybeAsync_WithAlternative_ShouldReturnSubjectOrAlternative(Maybe<StringObj> subject, string alternative, string expected)
{
var result = await subject
.Select(s => s.Name)
.OrMaybeAsync(async () => await Task.FromResult(alternative));
result.Should().Be(expected.ToMaybe());
}

public static TheoryData<Maybe<StringObj>, string, string> OrMaybe_WithAlternative_ShouldReturnSubjectOrAlternativeTestCases()
{
return new TheoryData<Maybe<StringObj>, string, string>
Expand All @@ -313,6 +234,14 @@ public void Or_NullArgument_ShouldThrow()
subject.Should().ThrowExactly<ArgumentNullException>();
}

[Fact]
public void OrAsync_NullArgument_ShouldThrow()
{
Func<Task> subject = async () => await 1.ToMaybe().OrAsync(null);

subject.Should().ThrowExactly<ArgumentNullException>();
}

[Fact]
public void OrMaybe_MaybeNullArgument_ShouldThrow()
{
Expand All @@ -329,6 +258,14 @@ public void OrMaybe_Func_MaybeNullArgument_ShouldThrow()
subject.Should().ThrowExactly<ArgumentNullException>();
}

[Fact]
public void OrMaybeAsync_Func_MaybeNullArgument_ShouldThrow()
{
Func<Task> subject = async () => await 1.ToMaybe().OrMaybeAsync(null);

subject.Should().ThrowExactly<ArgumentNullException>();
}

[Fact]
public void ImplicitConversion_FromNullObject_ShouldReturnMaybeWithoutValue()
{
Expand Down
Loading