-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathMaybeMonoidTest.php
99 lines (84 loc) · 3.27 KB
/
MaybeMonoidTest.php
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
<?php
declare(strict_types=1);
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Widmogrod\Functional as f;
use Widmogrod\Monad\Maybe\Just;
use Widmogrod\Monad\Maybe\Maybe;
use Widmogrod\Primitive\Stringg;
use function Widmogrod\Functional\map;
use function Widmogrod\Monad\Maybe\just;
use function Widmogrod\Monad\Maybe\maybeNull;
use const Widmogrod\Monad\Maybe\maybeNull;
class MaybeMonoidTest extends TestCase
{
#[DataProvider('provideData')]
public function test_it_should_concat_only_strings_and_skip_nulls(array $data, array $expected, string $asString)
{
$fullName = f\fromIterable($data)
->map(maybeNull)
->map(map(Stringg::of))
->reduce(f\concatM, just(Stringg::mempty()));
$this->assertInstanceOf(Just::class, $fullName);
$this->assertEquals($fullName, just(Stringg::of($asString)));
}
#[DataProvider('provideData')]
public function test_it_should_concat_only_just_values_naive_string_implementation(array $data, array $expected, string $asString)
{
// $makeMaybeMonoid :: string -> Maybe Stringg
$makeMaybeMonoid = function ($val): Maybe {
return maybeNull($val)->map(Stringg::of);
};
// $names :: array Maybe Stringg
$names = array_values(array_map($makeMaybeMonoid, $data));
// $firstName :: Maybe Stringg
// $middleName :: Maybe Stringg
// $lastName :: Maybe Stringg
list($firstName, $middleName, $lastName) = $names;
// $fullName :: Maybe Stringg
$fullName = $firstName->concat($middleName)->concat($lastName);
$this->assertInstanceOf(Just::class, $fullName);
$this->assertEquals($fullName, just(Stringg::of($asString)));
}
#[DataProvider('provideData')]
public function test_it_should_concat_only_just_values_list_naive_implementation2(array $data, array $expected)
{
// $makeMaybeMonoid :: string -> Maybe Listt string
$makeMaybeMonoid = function ($val): Maybe {
return maybeNull($val)->map(f\fromValue);
};
// $names :: array Maybe Listt string
$names = array_values(array_map($makeMaybeMonoid, $data));
// $firstName :: Maybe Listt string
// $middleName :: Maybe Listt string
// $lastName :: Maybe Listt string
list($firstName, $middleName, $lastName) = $names;
// $fullName :: Maybe Listt string
$fullName = $firstName->concat($middleName)->concat($lastName);
$this->assertInstanceOf(Just::class, $fullName);
$this->assertEquals($fullName, just(f\fromIterable($expected)));
}
public static function provideData()
{
return [
'array with null values' => [
[
'firstName' => 'First',
'middleName' => null,
'lastName' => 'Last'
],
['First', 'Last'],
'FirstLast',
],
'array with strings' => [
[
'firstName' => 'First',
'middleName' => 'Middle',
'lastName' => 'Last'
],
['First', 'Middle', 'Last'],
'FirstMiddleLast',
]
];
}
}