-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaybeExamples.cs
More file actions
143 lines (115 loc) · 3.47 KB
/
MaybeExamples.cs
File metadata and controls
143 lines (115 loc) · 3.47 KB
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
namespace Example.Tests;
using Trellis;
using Xunit;
public class MaybeExamples
{
[Fact]
public void Explicit_construction()
{
// Arrange
Maybe<string> apple1 = Maybe.From("apple");
var apple2 = Maybe.From("apple");
// Assert
apple1.Should().BeOfType<Maybe<string>>();
apple2.Should().BeOfType<Maybe<string>>();
apple1.Value.Should().Be("apple");
apple2.Value.Should().Be("apple");
}
[Fact]
public void Construction_None_X2F_No_Value()
{
// Arrange
Maybe<string> fruit1 = Maybe<string>.None;
Maybe<string> fruit2 = default; // reference type
Maybe<int> fruit3 = default; // value type
// Assert
fruit1.Should().BeOfType<Maybe<string>>();
fruit1.HasNoValue.Should().BeTrue();
fruit2.Should().BeOfType<Maybe<string>>();
fruit2.HasNoValue.Should().BeTrue();
fruit3.Should().BeOfType<Maybe<int>>();
fruit3.HasNoValue.Should().BeTrue();
}
[Fact]
public void Implicit_Conversion()
{
// Arrange
Maybe<string> apple = "apple"; // implicit conversion
// Or as a method return value
static Maybe<string> GetFruit(string fruit)
{
if (string.IsNullOrWhiteSpace(fruit))
return Maybe<string>.None;
return fruit; // implicit conversion
}
// Act
var fruit = GetFruit("apple");
// Assert
apple.Should().BeOfType<Maybe<string>>();
apple.Value.Should().Be("apple");
fruit.Should().BeOfType<Maybe<string>>();
fruit.Value.Should().Be("apple");
}
[Fact]
public void Equality()
{
// Arrange
Maybe<string> apple = "apple";
Maybe<string> orange = "orange";
string alsoOrange = "orange";
Maybe<string> noFruit = Maybe<string>.None;
// Act
// Assert
apple.Should().NotBe(orange);
orange.Should().Be(alsoOrange);
noFruit.Should().NotBe(orange);
}
[Fact]
public void Convert_to_string()
{
// Arrange
Maybe<string> apple = "apple";
Maybe<string> noFruit = Maybe<string>.None;
// Act
// Assert
apple.ToString().Should().Be("apple");
noFruit.ToString().Should().Be(string.Empty);
}
[Fact]
public void GetValueOrThrow()
{
// Arrange
Maybe<string> apple = "apple";
Maybe<string> noFruit = Maybe<string>.None;
// Act
var action1 = () => apple.GetValueOrThrow();
var action2 = () => noFruit.GetValueOrThrow();
// Assert
action1.Should().NotThrow<InvalidOperationException>();
action2.Should().Throw<InvalidOperationException>();
}
[Fact]
public void HasValue_and_HasNoValue()
{
// Arrange
Maybe<string> apple = "apple";
Maybe<string> noFruit = Maybe<string>.None;
// Act
// Assert
apple.HasValue.Should().BeTrue();
noFruit.HasNoValue.Should().BeTrue();
}
[Fact]
public void GetValueOrDefault()
{
// Arrange
Maybe<string> apple = "apple";
Maybe<string> unknownFruit = Maybe<string>.None;
// Act
string appleValue = apple.GetValueOrDefault("banana");
string unknownFruitValue = unknownFruit.GetValueOrDefault("banana");
// Assert
apple.Should().Be("apple");
unknownFruitValue.Should().Be("banana");
}
}