diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.Task.Left.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.Task.Left.cs index 72671842..ac5f3c44 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.Task.Left.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.Task.Left.cs @@ -36,5 +36,63 @@ public async Task Bind_Task_Left_returns_value_if_selector_returns_value() maybe2.HasValue.Should().BeTrue(); maybe2.Value.Should().Be(T.Value2); } + + [Fact] + public async Task Bind_Task_Left_provides_context_to_selector() + { + Maybe maybe = T.Value; + var context = 5; + + var maybe2 = await maybe.AsTask().Bind( + (value, ctx) => + { + ctx.Should().Be(context); + return Maybe.From(value); + }, + context + ); + + maybe2.HasValue.Should().BeTrue(); + } + + [Fact] + public async Task Bind_Task_Left_with_context_returns_no_value_if_initial_maybe_is_null() + { + Maybe maybe = null; + + var maybe2 = await maybe.AsTask().Bind( + (value, _) => ExpectAndReturnMaybe(null, T.Value)(value), + context: 5 + ); + + maybe2.HasValue.Should().BeFalse(); + } + + [Fact] + public async Task Bind_Task_Left_with_context_returns_no_value_if_selector_returns_null() + { + Maybe maybe = T.Value; + + var maybe2 = await maybe.AsTask().Bind( + (value, _) => ExpectAndReturn(T.Value, Maybe.None)(value), + context: 5 + ); + + maybe2.HasValue.Should().BeFalse(); + } + + [Fact] + public async Task Bind_Task_Left_with_context_returns_value_if_selector_returns_value() + { + Maybe maybe = T.Value; + + var maybe2 = await maybe.AsTask().Bind( + (value, _) => ExpectAndReturnMaybe(T.Value, T.Value)(value), + 5 + ); + + maybe2.HasValue.Should().BeTrue(); + maybe2.Value.Should().Be(T.Value); + } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.Task.Right.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.Task.Right.cs index 1cddb71f..0ea3dfb5 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.Task.Right.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.Task.Right.cs @@ -36,5 +36,63 @@ public async Task Bind_Task_Right_returns_value_if_selector_returns_value() maybe2.HasValue.Should().BeTrue(); maybe2.Value.Should().Be(T.Value2); } + + [Fact] + public async Task Bind_Task_Right_provides_context_to_selector() + { + Maybe maybe = T.Value; + var context = 5; + + var maybe2 = await maybe.Bind( + (value, ctx) => + { + ctx.Should().Be(context); + return Maybe.From(value).AsTask(); + }, + context + ); + + maybe2.HasValue.Should().BeTrue(); + } + + [Fact] + public async Task Bind_Task_Right_with_context_returns_no_value_if_initial_maybe_is_null() + { + Maybe maybe = null; + + var maybe2 = await maybe.Bind( + (value, _) => ExpectAndReturnMaybe_Task(null, T.Value)(value), + context: 5 + ); + + maybe2.HasValue.Should().BeFalse(); + } + + [Fact] + public async Task Bind_Task_Right_with_context_returns_no_value_if_selector_returns_null() + { + Maybe maybe = T.Value; + + var maybe2 = await maybe.Bind( + (value, _) => ExpectAndReturn_Task(T.Value, Maybe.None)(value), + context: 5 + ); + + maybe2.HasValue.Should().BeFalse(); + } + + [Fact] + public async Task Bind_Task_Right_with_context_returns_value_if_selector_returns_value() + { + Maybe maybe = T.Value; + + var maybe2 = await maybe.Bind( + (value, _) => ExpectAndReturnMaybe_Task(T.Value, T.Value)(value), + 5 + ); + + maybe2.HasValue.Should().BeTrue(); + maybe2.Value.Should().Be(T.Value); + } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.Task.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.Task.cs index 38edaf45..217e865e 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.Task.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.Task.cs @@ -36,5 +36,63 @@ public async Task Bind_Task_returns_value_if_selector_returns_value() maybe2.HasValue.Should().BeTrue(); maybe2.Value.Should().Be(T.Value); } + + [Fact] + public async Task Bind_Task_provides_context_to_selector() + { + Maybe maybe = T.Value; + var context = 5; + + var maybe2 = await maybe.AsTask().Bind( + (value, ctx) => + { + ctx.Should().Be(context); + return Maybe.From(value).AsTask(); + }, + context + ); + + maybe2.HasValue.Should().BeTrue(); + } + + [Fact] + public async Task Bind_Task_with_context_returns_no_value_if_initial_maybe_is_null() + { + Maybe maybe = null; + + var maybe2 = await maybe.AsTask().Bind( + (value, _) => ExpectAndReturnMaybe_Task(null, T.Value)(value), + context: 5 + ); + + maybe2.HasValue.Should().BeFalse(); + } + + [Fact] + public async Task Bind_Task_with_context_returns_no_value_if_selector_returns_null() + { + Maybe maybe = T.Value; + + var maybe2 = await maybe.AsTask().Bind( + (value, _) => ExpectAndReturn_Task(T.Value, Maybe.None)(value), + context: 5 + ); + + maybe2.HasValue.Should().BeFalse(); + } + + [Fact] + public async Task Bind_Task_with_context_returns_value_if_selector_returns_value() + { + Maybe maybe = T.Value; + + var maybe2 = await maybe.AsTask().Bind( + (value, _) => ExpectAndReturnMaybe_Task(T.Value, T.Value)(value), + 5 + ); + + maybe2.HasValue.Should().BeTrue(); + maybe2.Value.Should().Be(T.Value); + } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.ValueTask.Left.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.ValueTask.Left.cs index bdd5a673..ffe4973f 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.ValueTask.Left.cs @@ -37,5 +37,63 @@ public async Task Bind_ValueTask_Left_returns_value_if_selector_returns_value() maybe2.HasValue.Should().BeTrue(); maybe2.Value.Should().Be(T.Value2); } + + [Fact] + public async Task Bind_ValueTask_Left_provides_context_to_selector() + { + Maybe maybe = T.Value; + var context = 5; + + var maybe2 = await maybe.AsValueTask().Bind( + (value, ctx) => + { + ctx.Should().Be(context); + return Maybe.From(value); + }, + context + ); + + maybe2.HasValue.Should().BeTrue(); + } + + [Fact] + public async Task Bind_ValueTask_Left_with_context_returns_no_value_if_initial_maybe_is_null() + { + Maybe maybe = null; + + var maybe2 = await maybe.AsValueTask().Bind( + (value, _) => ExpectAndReturnMaybe(null, T.Value)(value), + context: 5 + ); + + maybe2.HasValue.Should().BeFalse(); + } + + [Fact] + public async Task Bind_ValueTask_Left_with_context_returns_no_value_if_selector_returns_null() + { + Maybe maybe = T.Value; + + var maybe2 = await maybe.AsValueTask().Bind( + (value, _) => ExpectAndReturn(T.Value, Maybe.None)(value), + context: 5 + ); + + maybe2.HasValue.Should().BeFalse(); + } + + [Fact] + public async Task Bind_ValueTask_Left_with_context_returns_value_if_selector_returns_value() + { + Maybe maybe = T.Value; + + var maybe2 = await maybe.AsValueTask().Bind( + (value, _) => ExpectAndReturnMaybe(T.Value, T.Value)(value), + 5 + ); + + maybe2.HasValue.Should().BeTrue(); + maybe2.Value.Should().Be(T.Value); + } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.ValueTask.Right.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.ValueTask.Right.cs index 40b09fc0..16f27fdd 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.ValueTask.Right.cs @@ -37,5 +37,63 @@ public async Task Bind_ValueTask_Right_returns_value_if_selector_returns_value() maybe2.HasValue.Should().BeTrue(); maybe2.Value.Should().Be(T.Value2); } + + [Fact] + public async Task Bind_ValueTask_Right_provides_context_to_selector() + { + Maybe maybe = T.Value; + var context = 5; + + var maybe2 = await maybe.Bind( + (value, ctx) => + { + ctx.Should().Be(context); + return Maybe.From(value).AsTask(); + }, + context + ); + + maybe2.HasValue.Should().BeTrue(); + } + + [Fact] + public async Task Bind_ValueTask_Right_with_context_returns_no_value_if_initial_maybe_is_null() + { + Maybe maybe = null; + + var maybe2 = await maybe.Bind( + (value, _) => ExpectAndReturnMaybe_ValueTask(null, T.Value)(value), + context: 5 + ); + + maybe2.HasValue.Should().BeFalse(); + } + + [Fact] + public async Task Bind_ValueTask_Right_with_context_returns_no_value_if_selector_returns_null() + { + Maybe maybe = T.Value; + + var maybe2 = await maybe.Bind( + (value, _) => ExpectAndReturn_ValueTask(T.Value, Maybe.None)(value), + context: 5 + ); + + maybe2.HasValue.Should().BeFalse(); + } + + [Fact] + public async Task Bind_ValueTask_Right_with_context_returns_value_if_selector_returns_value() + { + Maybe maybe = T.Value; + + var maybe2 = await maybe.Bind( + (value, _) => ExpectAndReturnMaybe_ValueTask(T.Value, T.Value)(value), + 5 + ); + + maybe2.HasValue.Should().BeTrue(); + maybe2.Value.Should().Be(T.Value); + } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.ValueTask.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.ValueTask.cs index 6a886725..464f0676 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.ValueTask.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.ValueTask.cs @@ -37,5 +37,63 @@ public async Task Bind_ValueTask_returns_value_if_selector_returns_value() maybe2.HasValue.Should().BeTrue(); maybe2.Value.Should().Be(T.Value); } + + [Fact] + public async Task Bind_ValueTask_provides_context_to_selector() + { + Maybe maybe = T.Value; + var context = 5; + + var maybe2 = await maybe.AsValueTask().Bind( + (value, ctx) => + { + ctx.Should().Be(context); + return Maybe.From(value).AsValueTask(); + }, + context + ); + + maybe2.HasValue.Should().BeTrue(); + } + + [Fact] + public async Task Bind_ValueTask_with_context_returns_no_value_if_initial_maybe_is_null() + { + Maybe maybe = null; + + var maybe2 = await maybe.AsValueTask().Bind( + (value, _) => ExpectAndReturnMaybe_ValueTask(null, T.Value)(value), + context: 5 + ); + + maybe2.HasValue.Should().BeFalse(); + } + + [Fact] + public async Task Bind_ValueTask_with_context_returns_no_value_if_selector_returns_null() + { + Maybe maybe = T.Value; + + var maybe2 = await maybe.AsValueTask().Bind( + (value, _) => ExpectAndReturn_ValueTask(T.Value, Maybe.None)(value), + context: 5 + ); + + maybe2.HasValue.Should().BeFalse(); + } + + [Fact] + public async Task Bind_ValueTask_with_context_returns_value_if_selector_returns_value() + { + Maybe maybe = T.Value; + + var maybe2 = await maybe.AsValueTask().Bind( + (value, _) => ExpectAndReturnMaybe_ValueTask(T.Value, T.Value)(value), + 5 + ); + + maybe2.HasValue.Should().BeTrue(); + maybe2.Value.Should().Be(T.Value); + } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.cs index 0bc900bd..1f71fc4c 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/BindTests.cs @@ -39,7 +39,7 @@ public void Bind_returns_value_if_selector_returns_value() [Fact] public void Bind_provides_context_to_selector() { - Maybe maybe = null; + Maybe maybe = T.Value; var context = 5; var maybe2 = maybe.Bind( @@ -51,7 +51,7 @@ public void Bind_provides_context_to_selector() context ); - maybe2.HasValue.Should().BeFalse(); + maybe2.HasValue.Should().BeTrue(); } [Fact] diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Bind.Task.Left.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Bind.Task.Left.cs index e46bdbed..f39cea73 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Bind.Task.Left.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Bind.Task.Left.cs @@ -10,5 +10,11 @@ public static async Task> Bind(this Task> maybeTask, Fun var maybe = await maybeTask.DefaultAwait(); return maybe.Bind(selector); } + + public static async Task> Bind(this Task> maybeTask, Func> selector, TContext context) + { + var maybe = await maybeTask.DefaultAwait(); + return maybe.Bind(selector, context); + } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Bind.Task.Right.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Bind.Task.Right.cs index 8243eac5..83c9f289 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Bind.Task.Right.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Bind.Task.Right.cs @@ -12,5 +12,16 @@ public static Task> Bind(this Maybe maybe, Func> Bind( + this Maybe maybe, + Func>> selector, + TContext context) + { + if (maybe.HasNoValue) + return Maybe.None.AsCompletedTask(); + + return selector(maybe.GetValueOrThrow(), context); + } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Bind.Task.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Bind.Task.cs index 94523ea2..0c54dae8 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Bind.Task.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Bind.Task.cs @@ -10,5 +10,11 @@ public static async Task> Bind(this Task> maybeTask, Fun var maybe = await maybeTask.DefaultAwait(); return await maybe.Bind(selector).DefaultAwait(); } + + public static async Task> Bind(this Task> maybeTask, Func>> selector, TContext context) + { + var maybe = await maybeTask.DefaultAwait(); + return await maybe.Bind(selector, context).DefaultAwait(); + } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Bind.ValueTask.Left.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Bind.ValueTask.Left.cs index 2a2908fd..4e38386e 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Bind.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Bind.ValueTask.Left.cs @@ -11,6 +11,15 @@ public static async ValueTask> Bind(this ValueTask> mayb Maybe maybe = await maybeTask; return maybe.Bind(selector); } + + public static async ValueTask> Bind( + this ValueTask> maybeTask, + Func> selector, + TContext context) + { + Maybe maybe = await maybeTask; + return maybe.Bind(selector, context); + } } } -#endif \ No newline at end of file +#endif diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Bind.ValueTask.Right.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Bind.ValueTask.Right.cs index 84d75ead..822db190 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Bind.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Bind.ValueTask.Right.cs @@ -13,6 +13,17 @@ public static ValueTask> Bind(this Maybe maybe, Func> Bind( + this Maybe maybe, + Func>> selector, + TContext context) + { + if (maybe.HasNoValue) + return Maybe.None.AsCompletedValueTask(); + + return selector(maybe.GetValueOrThrow(), context); + } } } -#endif \ No newline at end of file +#endif diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Bind.ValueTask.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Bind.ValueTask.cs index 71a70e27..bb88885a 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Bind.ValueTask.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Bind.ValueTask.cs @@ -11,6 +11,15 @@ public static async ValueTask> Bind(this ValueTask> mayb Maybe maybe = await maybeTask; return await maybe.Bind(selector); } + + public static async ValueTask> Bind( + this ValueTask> maybeTask, + Func>> selector, + TContext context) + { + Maybe maybe = await maybeTask; + return await maybe.Bind(selector, context); + } } } -#endif \ No newline at end of file +#endif