Skip to content

Commit cab70ce

Browse files
committed
extended test suite and fixed pattern inner collection mutability
1 parent b07ab64 commit cab70ce

14 files changed

Lines changed: 1152 additions & 26 deletions
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using Xunit;
2+
3+
namespace Funk.Tests
4+
{
5+
public class CurryTests : Test
6+
{
7+
[Fact]
8+
public void Curry_2_Param_Func()
9+
{
10+
UnitTest(
11+
_ => func((int a, int b) => a + b),
12+
f => f.Curry(),
13+
curried => Assert.Equal(5, curried(2)(3))
14+
);
15+
}
16+
17+
[Fact]
18+
public void Curry_3_Param_Func()
19+
{
20+
UnitTest(
21+
_ => func((int a, int b, int c) => a + b + c),
22+
f => f.Curry(),
23+
curried => Assert.Equal(6, curried(1)(2)(3))
24+
);
25+
}
26+
27+
[Fact]
28+
public void Curry_4_Param_Func()
29+
{
30+
UnitTest(
31+
_ => func((int a, int b, int c, int d) => a + b + c + d),
32+
f => f.Curry(),
33+
curried => Assert.Equal(10, curried(1)(2)(3)(4))
34+
);
35+
}
36+
37+
[Fact]
38+
public void Curry_5_Param_Func()
39+
{
40+
UnitTest(
41+
_ => func((int a, int b, int c, int d, int e) => a + b + c + d + e),
42+
f => f.Curry(),
43+
curried => Assert.Equal(15, curried(1)(2)(3)(4)(5))
44+
);
45+
}
46+
47+
[Fact]
48+
public void Curry_2_Param_Func_Produces_Same_Result()
49+
{
50+
UnitTest(
51+
_ => func((int a, int b) => a * b),
52+
f => (Original: f, Curried: f.Curry()),
53+
r => Assert.Equal(r.Original(3, 7), r.Curried(3)(7))
54+
);
55+
}
56+
57+
[Fact]
58+
public void Curry_2_Param_Partial_Application()
59+
{
60+
UnitTest(
61+
_ => func((string prefix, string name) => $"{prefix} {name}").Curry(),
62+
curried => curried("Hello"),
63+
greet => Assert.Equal("Hello World", greet("World"))
64+
);
65+
}
66+
67+
[Fact]
68+
public void Curry_3_Param_Partial_Application()
69+
{
70+
UnitTest(
71+
_ => func((int a, int b, int c) => a * b + c).Curry(),
72+
curried => curried(2)(3),
73+
partial => Assert.Equal(10, partial(4))
74+
);
75+
}
76+
77+
[Fact]
78+
public void Curry_4_Param_Partial_Application()
79+
{
80+
UnitTest(
81+
_ => func((int a, int b, int c, int d) => a + b + c + d).Curry(),
82+
curried => curried(10)(20),
83+
partial => Assert.Equal(60, partial(10)(20))
84+
);
85+
}
86+
87+
[Fact]
88+
public void Curry_5_Param_Partial_Application()
89+
{
90+
UnitTest(
91+
_ => func((string a, string b, string c, string d, string e) => $"{a}-{b}-{c}-{d}-{e}").Curry(),
92+
curried => curried("a")("b")("c"),
93+
partial => Assert.Equal("a-b-c-d-e", partial("d")("e"))
94+
);
95+
}
96+
}
97+
}
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Xunit;
4+
5+
namespace Funk.Tests
6+
{
7+
public class ExcExtTests : Test
8+
{
9+
[Fact]
10+
public void AsSuccess_On_Successful_Exc_Returns_NonEmpty_Maybe()
11+
{
12+
UnitTest(
13+
_ => success<string, Exception>("Funk"),
14+
e => e.AsSuccess(),
15+
m =>
16+
{
17+
Assert.True(m.NotEmpty);
18+
Assert.Equal("Funk", m.UnsafeGet());
19+
}
20+
);
21+
}
22+
23+
[Fact]
24+
public void AsSuccess_On_Failed_Exc_Returns_Empty_Maybe()
25+
{
26+
UnitTest(
27+
_ => failure<string, Exception>(new Exception("error")),
28+
e => e.AsSuccess(),
29+
m => Assert.True(m.IsEmpty)
30+
);
31+
}
32+
33+
[Fact]
34+
public void AsSuccess_On_Empty_Exc_Returns_Empty_Maybe()
35+
{
36+
UnitTest(
37+
_ => Exc.Empty<string, Exception>(),
38+
e => e.AsSuccess(),
39+
m => Assert.True(m.IsEmpty)
40+
);
41+
}
42+
43+
[Fact]
44+
public void Flatten_Nested_Exc()
45+
{
46+
UnitTest(
47+
_ => success<Exc<string, Exception>, Exception>(success<string, Exception>("Funk")),
48+
e => e.Flatten(),
49+
r =>
50+
{
51+
Assert.True(r.IsSuccess);
52+
Assert.Equal("Funk", r.UnsafeGetSuccess());
53+
}
54+
);
55+
}
56+
57+
[Fact]
58+
public void MapFailure_Transforms_Exception_Type()
59+
{
60+
UnitTest(
61+
_ => failure<string, ArgumentException>(new ArgumentException("arg error")),
62+
e => e.MapFailure(f => new InvalidOperationException(f.Root.UnsafeGet().Message)),
63+
r =>
64+
{
65+
Assert.True(r.IsFailure);
66+
Assert.IsType<EnumerableException<InvalidOperationException>>(r.Failure.UnsafeGet());
67+
Assert.Equal("arg error", r.RootFailure.UnsafeGet().Message);
68+
}
69+
);
70+
}
71+
72+
[Fact]
73+
public async Task MapFailureAsync_Transforms_Exception_Type()
74+
{
75+
await UnitTestAsync(
76+
_ => result(failure<string, ArgumentException>(new ArgumentException("async error"))),
77+
e => e.MapFailureAsync(f => result(new InvalidOperationException(f.Root.UnsafeGet().Message))),
78+
r =>
79+
{
80+
Assert.True(r.IsFailure);
81+
Assert.IsType<EnumerableException<InvalidOperationException>>(r.Failure.UnsafeGet());
82+
Assert.Equal("async error", r.RootFailure.UnsafeGet().Message);
83+
}
84+
);
85+
}
86+
87+
[Fact]
88+
public void OnFailure_Recovers_From_Failure()
89+
{
90+
UnitTest(
91+
_ => failure<string, Exception>(new Exception("failed")),
92+
e => e.OnFailure(_ => "recovered"),
93+
r =>
94+
{
95+
Assert.True(r.IsSuccess);
96+
Assert.Equal("recovered", r.UnsafeGetSuccess());
97+
}
98+
);
99+
}
100+
101+
[Fact]
102+
public void OnFlatFailure_Recovers_From_Failure_With_Exc()
103+
{
104+
UnitTest(
105+
_ => failure<string, Exception>(new Exception("failed")),
106+
e => e.OnFlatFailure(_ => success<string, Exception>("recovered")),
107+
r =>
108+
{
109+
Assert.True(r.IsSuccess);
110+
Assert.Equal("recovered", r.UnsafeGetSuccess());
111+
}
112+
);
113+
}
114+
115+
[Fact]
116+
public async Task OnFailureAsync_Recovers_From_Failure()
117+
{
118+
await UnitTestAsync(
119+
_ => result(failure<string, Exception>(new Exception("failed"))),
120+
e => e.OnFailureAsync(_ => result("recovered")),
121+
r =>
122+
{
123+
Assert.True(r.IsSuccess);
124+
Assert.Equal("recovered", r.UnsafeGetSuccess());
125+
}
126+
);
127+
}
128+
129+
[Fact]
130+
public void OnEmpty_Recovers_From_Empty()
131+
{
132+
UnitTest(
133+
_ => Exc.Empty<string, Exception>(),
134+
e => e.OnEmpty(_ => "recovered"),
135+
r =>
136+
{
137+
Assert.True(r.IsSuccess);
138+
Assert.Equal("recovered", r.UnsafeGetSuccess());
139+
}
140+
);
141+
}
142+
143+
[Fact]
144+
public void OnFlatEmpty_Recovers_From_Empty_With_Exc()
145+
{
146+
UnitTest(
147+
_ => Exc.Empty<string, Exception>(),
148+
e => e.OnFlatEmpty(_ => success<string, Exception>("recovered")),
149+
r =>
150+
{
151+
Assert.True(r.IsSuccess);
152+
Assert.Equal("recovered", r.UnsafeGetSuccess());
153+
}
154+
);
155+
}
156+
157+
[Fact]
158+
public async Task OnEmptyAsync_Recovers_From_Empty()
159+
{
160+
await UnitTestAsync(
161+
_ => result(Exc.Empty<string, Exception>()),
162+
e => e.OnEmptyAsync(_ => result("recovered")),
163+
r =>
164+
{
165+
Assert.True(r.IsSuccess);
166+
Assert.Equal("recovered", r.UnsafeGetSuccess());
167+
}
168+
);
169+
}
170+
171+
[Fact]
172+
public void Merge_Two_Successful_Excs()
173+
{
174+
UnitTest(
175+
_ => success<int, Exception>(1),
176+
e => e.Merge(success<int, Exception>(2)),
177+
r =>
178+
{
179+
Assert.True(r.IsSuccess);
180+
Assert.Equal(2, r.Success.UnsafeGet().Count);
181+
Assert.Equal(1, r.Success.UnsafeGet()[0]);
182+
Assert.Equal(2, r.Success.UnsafeGet()[1]);
183+
}
184+
);
185+
}
186+
187+
[Fact]
188+
public void Merge_Successful_With_Failed_Exc()
189+
{
190+
UnitTest(
191+
_ => success<string, Exception>("ok"),
192+
e => e.Merge(failure<string, Exception>(new Exception("fail"))),
193+
r =>
194+
{
195+
Assert.True(r.IsFailure);
196+
Assert.Equal(1, r.NestedFailures.UnsafeGet().Count);
197+
}
198+
);
199+
}
200+
201+
[Fact]
202+
public void MergeRange_Multiple_Excs()
203+
{
204+
UnitTest(
205+
_ => success<string, Exception>("a"),
206+
e => e.MergeRange([success<string, Exception>("b"), success<string, Exception>("c")]),
207+
r =>
208+
{
209+
Assert.True(r.IsSuccess);
210+
Assert.Equal(3, r.Success.UnsafeGet().Count);
211+
}
212+
);
213+
}
214+
215+
[Fact]
216+
public async Task MapAsync_On_TaskExc_Chains()
217+
{
218+
await UnitTestAsync(
219+
_ => result(success<string, Exception>("Funk")),
220+
e => e.MapAsync(s => result(s.Length)),
221+
r =>
222+
{
223+
Assert.True(r.IsSuccess);
224+
Assert.Equal(4, r.UnsafeGetSuccess());
225+
}
226+
);
227+
}
228+
229+
[Fact]
230+
public async Task FlatMapAsync_On_TaskExc_Chains()
231+
{
232+
await UnitTestAsync(
233+
_ => result(success<string, Exception>("Funk")),
234+
e => e.FlatMapAsync(s => result(success<int, Exception>(s.Length))),
235+
r =>
236+
{
237+
Assert.True(r.IsSuccess);
238+
Assert.Equal(4, r.UnsafeGetSuccess());
239+
}
240+
);
241+
}
242+
}
243+
}

0 commit comments

Comments
 (0)