Skip to content

Commit

Permalink
Merge pull request #577 from marcinjahn/allocation-free-maybe-bind-async
Browse files Browse the repository at this point in the history
feat: add allocation-free overloads to async Maybe.Bind
  • Loading branch information
vkhorikov authored Dec 23, 2024
2 parents b0b37fe + dfb74d2 commit df38cd7
Show file tree
Hide file tree
Showing 13 changed files with 414 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> 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<T> 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<T> maybe = T.Value;

var maybe2 = await maybe.AsTask().Bind(
(value, _) => ExpectAndReturn(T.Value, Maybe<T>.None)(value),
context: 5
);

maybe2.HasValue.Should().BeFalse();
}

[Fact]
public async Task Bind_Task_Left_with_context_returns_value_if_selector_returns_value()
{
Maybe<T> maybe = T.Value;

var maybe2 = await maybe.AsTask().Bind(
(value, _) => ExpectAndReturnMaybe<T>(T.Value, T.Value)(value),
5
);

maybe2.HasValue.Should().BeTrue();
maybe2.Value.Should().Be(T.Value);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> 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<T> 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<T> maybe = T.Value;

var maybe2 = await maybe.Bind(
(value, _) => ExpectAndReturn_Task(T.Value, Maybe<T>.None)(value),
context: 5
);

maybe2.HasValue.Should().BeFalse();
}

[Fact]
public async Task Bind_Task_Right_with_context_returns_value_if_selector_returns_value()
{
Maybe<T> maybe = T.Value;

var maybe2 = await maybe.Bind(
(value, _) => ExpectAndReturnMaybe_Task<T>(T.Value, T.Value)(value),
5
);

maybe2.HasValue.Should().BeTrue();
maybe2.Value.Should().Be(T.Value);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> 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<T> 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<T> maybe = T.Value;

var maybe2 = await maybe.AsTask().Bind(
(value, _) => ExpectAndReturn_Task(T.Value, Maybe<T>.None)(value),
context: 5
);

maybe2.HasValue.Should().BeFalse();
}

[Fact]
public async Task Bind_Task_with_context_returns_value_if_selector_returns_value()
{
Maybe<T> maybe = T.Value;

var maybe2 = await maybe.AsTask().Bind(
(value, _) => ExpectAndReturnMaybe_Task<T>(T.Value, T.Value)(value),
5
);

maybe2.HasValue.Should().BeTrue();
maybe2.Value.Should().Be(T.Value);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> 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<T> 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<T> maybe = T.Value;

var maybe2 = await maybe.AsValueTask().Bind(
(value, _) => ExpectAndReturn(T.Value, Maybe<T>.None)(value),
context: 5
);

maybe2.HasValue.Should().BeFalse();
}

[Fact]
public async Task Bind_ValueTask_Left_with_context_returns_value_if_selector_returns_value()
{
Maybe<T> maybe = T.Value;

var maybe2 = await maybe.AsValueTask().Bind(
(value, _) => ExpectAndReturnMaybe<T>(T.Value, T.Value)(value),
5
);

maybe2.HasValue.Should().BeTrue();
maybe2.Value.Should().Be(T.Value);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> 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<T> 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<T> maybe = T.Value;

var maybe2 = await maybe.Bind(
(value, _) => ExpectAndReturn_ValueTask(T.Value, Maybe<T>.None)(value),
context: 5
);

maybe2.HasValue.Should().BeFalse();
}

[Fact]
public async Task Bind_ValueTask_Right_with_context_returns_value_if_selector_returns_value()
{
Maybe<T> maybe = T.Value;

var maybe2 = await maybe.Bind(
(value, _) => ExpectAndReturnMaybe_ValueTask<T>(T.Value, T.Value)(value),
5
);

maybe2.HasValue.Should().BeTrue();
maybe2.Value.Should().Be(T.Value);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> 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<T> 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<T> maybe = T.Value;

var maybe2 = await maybe.AsValueTask().Bind(
(value, _) => ExpectAndReturn_ValueTask(T.Value, Maybe<T>.None)(value),
context: 5
);

maybe2.HasValue.Should().BeFalse();
}

[Fact]
public async Task Bind_ValueTask_with_context_returns_value_if_selector_returns_value()
{
Maybe<T> maybe = T.Value;

var maybe2 = await maybe.AsValueTask().Bind(
(value, _) => ExpectAndReturnMaybe_ValueTask<T>(T.Value, T.Value)(value),
5
);

maybe2.HasValue.Should().BeTrue();
maybe2.Value.Should().Be(T.Value);
}
}
}
}
Loading

0 comments on commit df38cd7

Please sign in to comment.