-
Notifications
You must be signed in to change notification settings - Fork 3
/
PreludeTests.cs
76 lines (56 loc) · 1.64 KB
/
PreludeTests.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
using System;
using Xunit;
using FluentAssertions;
using static ResultSharp.Prelude;
namespace ResultSharp.Tests;
public class PreludeTests
{
[Fact]
public void Try_CatchesException_ReturnsFaultedResult()
{
Result<int, Exception> expected = Err<Exception>(new DivideByZeroException());
Result<int, Exception> actual = Try(() => TestStubs.Divide(10, 0));
actual.Should().Be(expected);
}
[Fact]
public void Try_CatchesSpecificException_ReturnsFaultedResult()
{
var expected = Err(new DivideByZeroException());
var actual = Try<int, DivideByZeroException>(() => TestStubs.Divide(10, 0));
actual.Should().Be(expected);
}
[Fact]
public void OkIf_ConditionIsTrue_ReturnsOkResult()
{
var expected = Ok(1);
var actual = OkIf(true, 1, string.Empty);
actual.Should().Be(expected);
}
[Fact]
public void OkIf_ConditionIsFalse_ReturnsFaultedResult()
{
var expected = Err(-1);
var actual = OkIf(false, string.Empty, -1);
actual.Should().Be(expected);
}
[Fact]
public void OkIf_ConditionIsTrue_ReturnsOkValueFromDelegate()
{
var expected = Ok("ok");
var actual = OkIf<string, int>(
true,
() => "ok",
() => throw new Exception());
actual.Should().Be(expected);
}
[Fact]
public void OkIf_ConditionIsFalse_ReturnsErrFromDelegate()
{
var expected = Err(-1);
var actual = OkIf<int, int>(
false,
() => throw new Exception(),
() => -1);
actual.Should().Be(expected);
}
}