-
-
Notifications
You must be signed in to change notification settings - Fork 622
Expand file tree
/
Copy pathStrictNullCoalescenceTest.php
More file actions
213 lines (174 loc) · 5.24 KB
/
StrictNullCoalescenceTest.php
File metadata and controls
213 lines (174 loc) · 5.24 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
<?php
namespace Tests\Antlers\Runtime;
use Statamic\View\Cascade;
use Tests\Antlers\ParserTestCase;
class StrictNullCoalescenceTest extends ParserTestCase
{
public function test_strict_null_falls_through()
{
$template = <<<'EOT'
{{ a ??? b }}
EOT;
$this->assertSame('fallback', $this->renderString($template, [
'a' => null,
'b' => 'fallback',
]));
}
public function test_empty_string_is_preserved()
{
$template = <<<'EOT'
{{ a ??? b }}
EOT;
$this->assertSame('', $this->renderString($template, [
'a' => '',
'b' => 'fallback',
]));
}
public function test_zero_is_preserved()
{
$template = <<<'EOT'
{{ a ??? b }}
EOT;
$this->assertSame('0', $this->renderString($template, [
'a' => 0,
'b' => 'fallback',
]));
$this->assertSame('0', $this->renderString($template, [
'a' => '0',
'b' => 'fallback',
]));
}
public function test_false_is_preserved()
{
$template = <<<'EOT'
{{ a ??? b }}
EOT;
$this->assertSame('', $this->renderString($template, [
'a' => false,
'b' => 'fallback',
]));
}
public function test_undefined_variable_falls_through()
{
$template = <<<'EOT'
{{ missing ??? 'fallback' }}
EOT;
$this->assertSame('fallback', $this->renderString($template));
}
public function test_chaining_with_all_null_returns_last()
{
$template = <<<'EOT'
{{ a ??? b ??? 'final' }}
EOT;
$this->assertSame('final', $this->renderString($template, [
'a' => null,
'b' => null,
]));
}
public function test_chaining_returns_first_non_null()
{
$template = <<<'EOT'
{{ a ??? b ??? 'final' }}
EOT;
$this->assertSame('', $this->renderString($template, [
'a' => null,
'b' => '',
]));
$this->assertSame('0', $this->renderString($template, [
'a' => null,
'b' => 0,
]));
}
public function test_mixing_with_loose_null_coalescence()
{
$template = <<<'EOT'
{{ a ?? b ??? 'final' }}
EOT;
$this->assertSame('final', $this->renderString($template, [
'a' => '',
'b' => null,
]));
$this->assertSame('B', $this->renderString($template, [
'a' => null,
'b' => 'B',
]));
}
public function test_chaining_is_left_associative_with_mixed_operators()
{
// Grouping is (a ??? b) ?? c. The strict inner group preserves 0,
// but the outer loose ?? then treats 0 as null-like and falls through to c.
$template = <<<'EOT'
{{ a ??? b ?? c }}
EOT;
$this->assertSame('C', $this->renderString($template, [
'a' => 0,
'b' => 'B',
'c' => 'C',
]));
// When the strict inner group returns a truthy value, the outer ?? keeps it.
$this->assertSame('B', $this->renderString($template, [
'a' => null,
'b' => 'B',
'c' => 'C',
]));
}
public function test_modifiers_can_be_called_on_strict_group()
{
$template = <<<'EOT'
{{ (seo_title ??? title) | upper }}
EOT;
$this->assertSame('I AM THE TITLE', $this->renderString($template, [
'seo_title' => null,
'title' => 'i am the title',
], true));
$this->assertSame('I AM THE SEO TITLE', $this->renderString($template, [
'seo_title' => 'i am the seo title',
'title' => 'i am the title',
], true));
}
public function test_strict_null_coalescence_with_multi_path_parts()
{
$data = [
'config' => [
'app' => [
'name' => 'Statamic',
],
],
];
$template = <<<'EOT'
{{ settings:copyright_name ??? config:app:name }}
EOT;
$this->assertSame('Statamic', $this->renderString($template, $data));
$cascade = $this->mock(Cascade::class, function ($m) {
$m->shouldReceive('get')->with('settings')->andReturn(null);
});
$this->assertSame('Statamic', (string) $this->parser()->cascade($cascade)->render($template, $data));
}
public function test_strict_null_coalescence_short_circuits_right_side()
{
$template = <<<'EOT'
{{ hello = "Hello" }}{{ world = "World" }}{{ hello ??? (world = "Earth") }} {{ world }}
EOT;
$this->assertSame('Hello World', $this->renderString($template, [], true));
}
public function test_strict_vs_loose_divergence()
{
$loose = <<<'EOT'
{{ a ?? 'fallback' }}
EOT;
$strict = <<<'EOT'
{{ a ??? 'fallback' }}
EOT;
$falsyValues = [
['a' => 0],
['a' => false],
];
foreach ($falsyValues as $data) {
$this->assertSame('fallback', $this->renderString($loose, $data));
$this->assertNotSame('fallback', $this->renderString($strict, $data));
}
$nullData = ['a' => null];
$this->assertSame('fallback', $this->renderString($loose, $nullData));
$this->assertSame('fallback', $this->renderString($strict, $nullData));
}
}