-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtest_option.py
623 lines (429 loc) · 12.1 KB
/
test_option.py
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
from collections.abc import Callable, Generator
from typing import Any
import pytest
from hypothesis import given # type: ignore
from hypothesis import strategies as st
from pydantic import BaseModel
from expression import (
Error,
Nothing,
Ok,
Option,
Result,
Some,
effect,
option,
pipe,
pipe2,
)
from expression.core.option import Nothing, Option, Some
from expression.extra.option import pipeline
from tests.utils import CustomException
def test_option_some():
xs = Some(42)
assert isinstance(xs, Option)
assert pipe(xs, option.is_some) is True
assert pipe(xs, option.is_none) is False
def test_option_some_match():
xs = Some(42)
match xs:
case Option(tag="some", some=x):
assert x == 42
case _:
assert False
def test_option_some_match_fluent():
xs = Some(42)
ys = xs.map(lambda x: x + 1)
match ys:
case Option(tag="some", some=value):
assert value == 43
case _:
assert False
def test_option_some_iterate():
xs = Some(42)
for x in option.to_list(xs):
assert x == 42
break
else:
assert False
def test_option_none():
xs = Nothing
assert isinstance(xs, Option)
assert xs.pipe(option.is_some) is False
assert xs.pipe(option.is_none) is True
def test_option_none_match():
xs = Nothing
match xs:
case Option(tag="some"):
assert False
case x if x is Nothing:
assert True
case _:
assert False
def test_option_nothing_iterate():
xs = Nothing
for _ in option.to_list(xs):
assert False
def test_option_none_equals_none():
xs = Nothing
ys = Nothing
assert xs == ys
def test_option_none_not_equals_some():
xs = Some(42)
ys = Nothing
assert xs != ys
assert ys != xs
def test_option_order_some_some_works():
xs = Some(42)
ys = Some(41)
assert xs > ys
assert ys < xs
def test_option_order_some_none_works():
xs = Some(42)
ys = Nothing
assert xs > ys
assert ys < xs
def test_option_order_none_none_works():
xs = Nothing
ys = Nothing
assert not (xs < ys)
def test_option_none_default_value():
xs = Nothing
zs = xs.default_value(42)
assert zs == 42
def test_option_some_default_value():
xs: Option[int] = Some(42)
zs = xs.default_value(0)
assert zs == 42
def test_option_none_default_with():
xs = Nothing
zs = xs.default_with(lambda: 42)
assert zs == 42
def test_option_some_default_with():
xs: Option[int] = Some(42)
zs = xs.default_with(lambda: 0)
assert zs == 42
def test_option_none_default_arg():
xs = Nothing
zs = option.default_arg(xs, 42)
assert zs == 42
def test_option_some_default_arg():
xs: Option[int] = Some(42)
zs = option.default_arg(xs, 0)
assert zs == 42
@given(
st.one_of(st.integers(), st.text(), st.floats()), # type: ignore
st.one_of(st.integers(), st.text(), st.floats()), # type: ignore
)
def test_option_some_equals_some(a: Any, b: Any):
xs = Some(a)
ys = Some(b)
assert xs == ys if a == b else xs != ys
def test_option_some_map_piped():
xs = Some(42)
mapper: Callable[[int], int] = lambda x: x + 1
ys: Option[int] = xs.pipe(option.map(mapper))
match ys:
case Option(tag="some", some=y):
assert y == 43
case _:
assert False
def test_option_none_map_piped():
xs: Option[int] = Nothing
mapper: Callable[[int], int] = lambda x: x + 1
map = option.map(mapper)
ys = xs.pipe(map)
assert ys is Nothing
def test_option_some_map_fluent():
xs = Some(42)
ys = xs.map(lambda x: x + 1)
match ys:
case Option(tag="some", some=value):
assert value == 43
case _:
assert False
def test_option_none_map():
xs = Nothing
ys = xs.map(lambda x: x + 1)
assert ys is Nothing
@given(st.integers(), st.integers()) # type: ignore
def test_option_some_map2_piped(x: int, y: int):
xs = Some(x)
ys = Some(y)
mapper: Callable[[int, int], int] = lambda x, y: x + y
zs = pipe2((xs, ys), option.map2(mapper))
match zs:
case Option(tag="some", some=value):
assert value == x + y
case _:
assert False
def test_option_starmap_fluent():
xs = Some((42, 43))
mapper: Callable[[int, int], int] = lambda x, y: x + y
ys = xs.starmap(mapper)
match ys:
case Option(tag="some", some=value):
assert value == 85
case _:
assert False
def test_option_starmap_piped():
xs = Some((42, 43))
mapper: Callable[[int, int], int] = lambda x, y: x + y
ys = pipe(xs, option.starmap(mapper))
match ys:
case Option(tag="some", some=value):
assert value == 85
case _:
assert False
def test_option_some_bind_fluent():
xs = Some(42)
ys = xs.bind(lambda x: Some(x + 1))
match ys:
case Option(tag="some", some=value):
assert value == 43
case _:
assert False
def test_option_some_bind_none_fluent():
xs = Some(42)
ys = xs.bind(lambda x: Nothing)
match ys:
case Option(tag="none"):
assert True
case _:
assert False
def test_option_none_bind_none_fluent():
xs = Nothing
ys = xs.bind(lambda x: Nothing)
match ys:
case Option(tag="some"):
assert False
case _:
assert True
def test_option_some_bind_piped():
binder: Callable[[int], Option[int]] = lambda x: Some(x + 1)
xs = Some(42)
ys = xs.pipe(
option.bind(binder),
)
match ys:
case Option(tag="some", some=value):
assert value == 43
case _:
assert False
def test_option_filter_none():
xs = Nothing
assert xs.filter(lambda x: True) == Nothing
def test_option_filter_some():
xs = Some(42)
assert xs.filter(lambda x: x > 41) == Some(42)
assert xs.filter(lambda x: x > 42) == Nothing
def test_option_none_to_list():
xs = Nothing
assert xs.to_list() == []
def test_option_some_to_list():
xs = Some(42)
assert xs.to_list() == [42]
def test_option_none_to_seq():
xs = Nothing
assert list(xs.to_seq()) == []
def test_option_some_to_seq():
xs = Some(42)
assert list(xs.to_seq()) == [42]
def test_option_none_to_str():
xs = Nothing
assert str(xs) == "Nothing"
def test_option_some_to_str():
xs = Some(42)
assert str(xs) == f"Some {xs.value}"
def test_option_none_is_none():
xs = Nothing
assert xs.is_none()
def test_option_none_is_some():
xs = Nothing
assert not xs.is_some()
def test_option_some_is_none():
xs = Some(42)
assert not xs.is_none()
def test_option_some_is_some():
xs = Some(42)
assert xs.is_some()
def test_option_of_object_none():
xs = option.of_obj(None)
assert xs.is_none()
def test_option_of_object_value():
xs = option.of_obj(42)
assert xs.is_some()
def test_option_of_result_ok():
result: Result[int, Any] = Ok(42)
xs = option.of_result(result)
assert xs.is_some()
def test_option_of_result_error():
xs: Option[int] = option.of_result(Error("oops"))
assert xs.is_none()
def test_option_to_result_ok():
xs = option.to_result(Some(42), "oops")
assert xs == Ok(42)
def test_option_to_result_error():
xs = option.to_result(Nothing, "oops")
assert xs == Error("oops")
def test_option_to_result_with_ok():
def raise_error() -> Any:
raise Exception("Should not be called")
xs = option.to_result_with(Some(42), error=raise_error)
assert xs == Ok(42)
def test_option_to_result_with_error():
xs = option.to_result_with(Nothing, error=lambda: "oops")
assert xs == Error("oops")
def test_option_to_optional_some():
xs = option.to_optional(Some(1))
assert xs == 1
def test_option_to_optional_nothing():
xs = option.to_optional(Nothing)
assert xs is None
def test_option_builder_zero():
@effect.option[int]()
def fn():
while False:
yield
xs = fn()
assert xs is Nothing
def test_option_builder_yield_value():
@effect.option[int]()
def fn():
yield 42
xs = fn()
match xs:
case Option(tag="some", some=value):
assert value == 42
case _:
assert False
def test_option_builder_yield_value_async():
@effect.option[int]()
def fn():
yield 42
xs = fn()
match xs:
case Option(tag="some", some=value):
assert value == 42
case _:
assert False
def test_option_builder_yield_some_wrapped():
@effect.option[Option[int]]()
def fn() -> Generator[Option[int], Option[int], Option[int]]:
x: Option[int] = yield Some(42)
return x
xs = fn()
match xs:
case Option(tag="some", some=value):
assert value == Some(42)
case _:
assert False
def test_option_builder_return_some():
@effect.option[int]()
def fn() -> Generator[int, int, int]:
x: int = yield 42
return x
xs = fn()
match xs:
case Option(tag="some", some=value):
assert value == 42
case _:
assert False
def test_option_builder_return_nothing_wrapped():
@effect.option[Option[int]]()
def fn():
return Nothing
yield
xs = fn()
match xs:
case Option(tag="some", some=value):
assert value is Nothing
case _:
assert False
def test_option_builder_yield_from_some():
@effect.option[int]()
def fn() -> Generator[int, int, int]:
x = yield from Some(42)
return x + 1
xs = fn()
match xs:
case Option(tag="some", some=value):
assert value == 43
case _:
assert False
def test_option_builder_yield_from_none():
@effect.option[int]()
def fn() -> Generator[int, int, int]:
x: int
x = yield from Nothing
return x
xs = fn()
assert xs is Nothing
def test_option_builder_multiple_some():
@effect.option[int]()
def fn() -> Generator[int, int, int]:
x: int = yield 42
y = yield from Some(43)
return x + y
xs = fn()
match xs:
case Option(tag="some", some=value):
assert value == 85
case _:
assert False
def test_option_builder_none_short_circuits():
@effect.option[int]()
def fn() -> Generator[int, int, int]:
x: int = yield from Nothing
y = yield from Some(43)
return x + y
xs = fn()
assert xs is Nothing
def test_option_builder_throws():
error = "do'h"
@effect.option()
def fn():
raise CustomException(error)
yield
with pytest.raises(CustomException) as ex:
fn()
assert ex.value.message == error
def test_pipeline_none():
hn = pipeline()
assert hn(42) == Some(42)
def test_pipeline_works():
fn: Callable[[int], Option[int]] = lambda x: Some(x * 10)
gn: Callable[[int], Option[int]] = lambda x: Some(x + 10)
hn = pipeline(
fn,
gn,
)
assert hn(42) == Some(430)
def test_pipeline_error():
fn: Callable[[int], Option[int]] = lambda x: Some(x * 10)
gn: Callable[[int], Option[int]] = lambda x: Nothing
hn = pipeline(
fn,
gn,
)
assert hn(42) == Nothing
class Model(BaseModel):
one: Option[int]
two: Option[str] = Nothing
three: Option[float] = Nothing
def test_parse_option_works():
obj = dict(one=10, two=None)
model = Model.model_validate(obj)
assert model.one.is_some()
assert model.one.value == 10
assert model.two == Nothing
assert model.three == Nothing
def test_serialize_option_works():
model = Model(one=Some(10))
json = model.model_dump_json()
assert json == '{"one":10,"two":null,"three":null}'
model_ = Model.model_validate_json(json)
assert model_.one.is_some()
assert model_.one.value == 10
assert model_.two == Nothing
assert model_.three == Nothing