-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExamples.cs
More file actions
348 lines (293 loc) · 8.69 KB
/
Examples.cs
File metadata and controls
348 lines (293 loc) · 8.69 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
using libanvl.Exceptions;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Xunit;
namespace libanvl.opt.test;
public class Examples
{
[Fact]
public void WrapOpt()
{
var rick = new Person("Rick", "Sanchez", Org.Alpha);
var morty = new Person("Mortimer", "Smith", Org.Gamma);
var optPerson = Opt.From(rick);
Assert.True(optPerson.IsSome);
var person = optPerson.Unwrap();
if (optPerson.IsSome)
Assert.Same(person, optPerson.Unwrap());
Assert.Same(person, optPerson.SomeOr(morty));
Assert.NotNull(optPerson.SomeOrDefault());
optPerson = Opt.From<Person>(null);
Assert.True(optPerson.IsNone);
Assert.Throws<OptException>(optPerson.Unwrap);
Assert.Same(morty, optPerson.SomeOr(morty));
Assert.Null(optPerson.SomeOrDefault());
static bool AcceptOpt(Opt<Person> op) => op.IsSome;
// implicit conversion
var result = AcceptOpt(rick);
Assert.True(result);
// explicit Some factory
result = AcceptOpt(Opt.Some(rick));
Assert.True(result);
// extension function factory
result = AcceptOpt(rick);
Assert.True(result);
// This will not work:
// result = AcceptOpt(null);
// explicit None
result = AcceptOpt(Opt<Person>.None);
Assert.False(result);
}
[Fact]
public void WrapOptAndProject()
{
static Opt<DirectoryInfo> GetOptDirectoryInfo(string? path) => Opt.From(path is null ? null : new DirectoryInfo(path));
Opt<DirectoryInfo> optDirectoryInfo = GetOptDirectoryInfo(@"C:\Users");
Assert.True(optDirectoryInfo.IsSome);
optDirectoryInfo = GetOptDirectoryInfo(null);
Assert.True(optDirectoryInfo.IsNone);
}
[Fact]
public void SelectThroughOpt()
{
var rick = new Person("Rick", "Sanchez", Org.Alpha);
var morty = new Person("Mortimer", "Smith", Org.Gamma);
var optBook = Opt.From(new Book("How to drive a space car", rick, morty));
Opt<string> optEditorLastName = optBook.Select(b => b.Editor.LastName);
Assert.Equal(morty.LastName, optEditorLastName.Unwrap());
optBook = Opt<Book>.None;
optEditorLastName = optBook.Select(b => b.Editor.LastName);
Assert.True(optEditorLastName.IsNone);
}
[Fact]
public void IterateOptEnumerable()
{
var rick = new Person("Rick", "Sanchez", Org.Alpha);
var morty = new Person("Mortimer", "Smith", Org.Alpha);
var beth = new Person("Beth", "Smit", Org.Delta);
var book1 = new Book("Trans-dimensional Family Dynamics", beth, morty);
var book2 = new Book("Horse Surgeon: A Life", beth, morty);
var books = new List<Book> { book1, book2 };
var library = new Library(books, rick);
foreach (Book b in library.Books.Unwrap())
{
Assert.Same(b.Editor, morty);
}
library = new Library(Opt<IEnumerable<Book>>.None, Opt<Person>.None);
// Books will be an empty collection
foreach (Book b in library.Books)
{
Assert.Fail("Unreachable");
}
}
[Fact]
public void CastThroughOpt()
{
// implicit conversion to Opt
Opt<DeltaPerson> optJerry = new DeltaPerson("Jerry", "Smith");
Opt<Person> optPerson = optJerry.Cast<Person>();
Assert.Same(optJerry.Unwrap(), optPerson.Unwrap());
}
[Fact]
public void Operator_ImplicitConversion_ToOpt()
{
Opt<int> opt = 5;
Assert.True(opt.IsSome);
Assert.Equal(5, opt.Unwrap());
}
[Fact]
public void Operator_ImplicitConversion_FromOpt()
{
Opt<int> opt = Opt.Some(5);
int value = opt;
Assert.Equal(5, value);
}
[Fact]
public void Operator_True()
{
Opt<int> opt = 5;
if (opt)
{
Assert.True(opt.IsSome);
}
else
{
Assert.Fail("Should not be called");
}
}
[Fact]
public void Operator_False()
{
Opt<int> opt = Opt<int>.None;
if (opt)
{
Assert.Fail("Should not be called");
}
else
{
Assert.True(opt.IsNone);
}
}
[Fact]
public void Operator_Or_Opt()
{
Opt<int> opt1 = Opt<int>.None;
Opt<int> opt2 = 5;
Opt<int> result = opt1 | opt2;
Assert.True(result.IsSome);
Assert.Equal(5, result.Unwrap());
}
[Fact]
public void Operator_Or_Value()
{
Opt<int> opt = Opt<int>.None;
int result = opt | 5;
Assert.Equal(5, result);
}
[Fact]
public void Result_Ok()
{
var result = Result.Ok("Success");
Assert.True(result.IsOk);
Assert.False(result.IsErr);
Assert.Equal("Success", result.Unwrap());
}
[Fact]
public void Result_Err()
{
var result = Result.Err<string, string>("Error");
Assert.False(result.IsOk);
Assert.True(result.IsErr);
Assert.Throws<InvalidOperationException>(() => result.Unwrap());
}
[Fact]
public void Result_Match_Ok()
{
var result = Result.Ok("Success");
result.Match(
ok => Assert.Equal("Success", ok),
err => Assert.Fail("Should not be called")
);
}
[Fact]
public void Result_Match_Err()
{
var result = Result.Err<string, string>("Error");
result.Match(
ok => Assert.Fail("Should not be called"),
err => Assert.Equal("Error", err)
);
}
[Fact]
public void Result_OkOr()
{
var result = Result.Err<string, string>("Error");
var value = result.OkOr("Default");
Assert.Equal("Default", value);
}
[Fact]
public void Result_OkOrDefault()
{
var result = Result.Err<string, string>("Error");
var value = result.OkOrDefault();
Assert.Null(value);
}
[Fact]
public void Result_ImplicitConversion_Ok()
{
Result<string, Exception> result = "Success";
Assert.True(result.IsOk);
Assert.Equal("Success", result.Unwrap());
}
[Fact]
public void Result_ImplicitConversion_Err()
{
Result<int, string> result = "Error";
Assert.True(result.IsErr);
Assert.Throws<InvalidOperationException>(() => result.Unwrap());
}
[Fact]
public void Any_None()
{
Any<int> none = default;
Assert.True(none.IsNone);
}
[Fact]
public void Any_Single()
{
Any<int> single = 5;
Assert.True(single.IsSingle);
Assert.Equal(5, single.Single.Unwrap());
}
[Fact]
public void Any_Many()
{
Any<int> many = [1, 2, 3];
Assert.True(many.IsMany);
Assert.Equal(3, many.Count);
Assert.Collection(
many.Many.Unwrap(),
x => Assert.Equal(1, x),
x => Assert.Equal(2, x),
x => Assert.Equal(3, x));
}
[Fact]
public void Any_Iterate()
{
Any<int> none = default;
foreach (int i in none)
{
Assert.Fail("Unreachable");
}
Any<int> single = 5;
var sum = 0;
foreach (int i in single)
{
sum += i;
}
Any<int> many = [1, 2, 3];
sum = 0;
foreach (int i in many)
{
sum += i;
}
Assert.Equal(6, sum);
}
#if NET8_0_OR_GREATER
[Fact]
public void MapOfAny()
{
AnyMap<string, int> map = new()
{
["alpha"] = default,
["bravo"] = 2,
["charlie"] = [3, 4, 5]
};
Assert.True(map.ContainsKey("alpha"));
Assert.True(map.ContainsKey("bravo"));
Assert.True(map.ContainsKey("charlie"));
IAnyRefMap<string, int> refMap = map;
ref var value = ref refMap.GetValueRef("alpha");
Assert.True(value.IsNone);
value = ref refMap.GetValueRef("bravo");
Assert.True(value.IsSingle);
Assert.Equal(2, value.Single.Unwrap());
value = ref refMap.GetValueRef("charlie");
Assert.True(value.IsMany);
Assert.Collection(
value.Many.Unwrap(),
x => Assert.Equal(3, x),
x => Assert.Equal(4, x),
x => Assert.Equal(5, x));
refMap["alpha"].Add(1);
Assert.True(map["alpha"].IsSingle);
Assert.Equal(1, map["alpha"].Single.Unwrap());
refMap["bravo"].Add(3);
Assert.True(map["bravo"].IsMany);
Assert.Collection(
map["bravo"].Many.Unwrap(),
x => Assert.Equal(2, x),
x => Assert.Equal(3, x));
}
#endif
}