forked from Crell/Serde
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DictionaryFieldTest.php
113 lines (102 loc) · 3.27 KB
/
DictionaryFieldTest.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
declare(strict_types=1);
namespace Crell\Serde;
use Crell\Serde\Attributes\DictionaryField;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
class DictionaryFieldTest extends TestCase
{
/**
* @param array<string, string> $in
*/
#[Test, DataProvider('implosionExamples')]
public function implosion(string $implodeOn, string $joinOn, array $in, string $expected): void
{
$d = new DictionaryField(
implodeOn: $implodeOn,
joinOn: $joinOn,
);
$result = $d->implode($in);
self::assertEquals($expected, $result);
}
public static function implosionExamples(): iterable
{
yield [
'implodeOn' => ',',
'joinOn' => '=',
'in' => ['narf' => 'poink'],
'expected' => 'narf=poink',
];
yield [
'implodeOn' => ',',
'joinOn' => '=',
'in' => ['narf' => 'poink', 'beep' => 'boop'],
'expected' => 'narf=poink,beep=boop',
];
yield [
'implodeOn' => ',',
'joinOn' => '=',
'in' => ['narf', 'poink'],
'expected' => '0=narf,1=poink',
];
}
/**
* @param array<string, string> $expected
*/
#[Test, DataProvider('explosionExamples')]
public function explosion(string $implodeOn, string $joinOn, string $in, array $expected): void
{
$d = new DictionaryField(
implodeOn: $implodeOn,
joinOn: $joinOn,
);
$result = $d->explode($in);
self::assertEquals($expected, $result);
}
public static function explosionExamples(): iterable
{
yield 'A single pair gets parsed' => [
'implodeOn' => ',',
'joinOn' => '=',
'in' => 'beep=boop',
'expected' => ['beep' => 'boop'],
];
yield 'Multiple pairs get parsed' => [
'implodeOn' => ',',
'joinOn' => '=',
'in' => 'beep=boop,narf=poink',
'expected' => ['beep' => 'boop', 'narf' => 'poink'],
];
yield 'Whitespace is trimmed for both keys and values' => [
'implodeOn' => ',',
'joinOn' => '=',
'in' => 'beep = boop, narf = poink',
'expected' => ['beep' => 'boop', 'narf' => 'poink'],
];
yield 'Missing joiners result in an empty string value' => [
'implodeOn' => ',',
'joinOn' => '=',
'in' => 'beep = boop, narf',
'expected' => ['beep' => 'boop', 'narf' => ''],
];
yield 'Trailing imploders are ignored' => [
'implodeOn' => ',',
'joinOn' => '=',
'in' => 'beep = boop, narf = poink,',
'expected' => ['beep' => 'boop', 'narf' => 'poink'],
];
yield 'Extra joiners are ignored' => [
'implodeOn' => ',',
'joinOn' => '=',
'in' => 'beep=boop, narf=poink=blurg,',
'expected' => ['beep' => 'boop', 'narf' => 'poink'],
];
yield 'Empty input gives empty array' => [
'implodeOn' => ',',
'joinOn' => '=',
'in' => '',
'expected' => [],
];
}
}