-
Notifications
You must be signed in to change notification settings - Fork 3
/
ResultExtensionsTests.cs
145 lines (115 loc) · 3.36 KB
/
ResultExtensionsTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System.Collections.Generic;
using FluentAssertions;
using Xunit;
using static ResultSharp.Prelude;
namespace ResultSharp.Tests;
public class ResultExtensionsTests
{
[Fact]
public void Combine_ArrayOfResults_ReturnsResultOfIEnumerableContainingErrors()
{
IEnumerable<Result<string, int>> results = new Result<string, int>[]
{
Ok("foo"),
Err(1),
Ok("bar"),
Err(2),
Ok("baz"),
Err(3)
};
var expected = new[] { 1, 2, 3 };
var actual = results.Combine().UnwrapErr();
actual.Should().BeEquivalentTo(expected);
}
[Fact]
public void Combine_ArrayOfResults_ReturnsCombinedResultWithOkValues()
{
IEnumerable<Result<string, int>> results = new Result<string, int>[]
{
Ok("foo"),
Ok("bar"),
Ok("baz"),
};
var expected = new[] { "foo", "bar", "baz" };
var actual = results.Combine().Unwrap();
actual.Should().BeEquivalentTo(expected);
}
[Fact]
public void AndThenTry_TryChain_ReturnsFaultedResult()
{
var expected = Err(new DivideByZeroException());
var actual = Result
.Ok<int, DivideByZeroException>(10)
.AndThenTry(x => TestStubs.Divide(x, 0));
actual.Should().Be(expected);
}
[Fact]
public void Combine_MixedResultsWithoutTypeArgs_ReturnsCombinedErrorMessage()
{
IEnumerable<Result> results = new Result[]
{
Ok(),
Err("foo"),
Ok(),
Err("bar"),
Ok(),
Err("baz")
};
var expected = Err("foo\nbar\nbaz");
var actual = results.Combine("\n");
actual.Should().Be(expected);
}
[Fact]
public void Combine_OkResultsWithoutTypeArgs_ReturnsOk()
{
IEnumerable<Result> results = new Result[]
{
Ok(),
Ok(),
Ok(),
Ok(),
};
var expected = Ok();
var actual = results.Combine("\n");
actual.Should().Be(expected);
}
[Fact]
public void AndThen_ResultTWithResultTE_ReturnsErr()
{
Result<int> a = Ok(10);
Result<int, string> b = Err("foo");
Result<int> expected = Err("foo");
var actual = a.AndThen(x => b);
actual.Should().Be(expected);
}
[Fact]
public void AndThen_ResultTEWithResultT_ReturnsErr()
{
Result<int, string> a = Err("foo");
Result<char> b = Ok('1');
Result<char> expected = Err("foo");
var actual = a.AndThen(_ => b);
actual.Should().Be(expected);
}
[Fact]
public void AndThen_ResultTEWithResult_ReturnsOk()
{
Result<int, string> a = Ok(10);
Result b = Ok();
Result expected = Ok();
var actual = a.AndThen(_ => b);
actual.Should().Be(expected);
}
[Fact]
public void AndThen_ResultWithResultTE_ReturnsErr()
{
Result a = Ok();
Result<int, string> b = Err("foo");
Result<int> expected = Err("foo");
var actual = a.AndThen(() => b);
actual.Should().Be(expected);
Result<string> x = Ok("asdf");
Result<int, string> y = Ok(10);
var z = x.AndThen(_ => y);
}
}