1<?php
2
3/**
4 * Hoa
5 *
6 *
7 * @license
8 *
9 * New BSD License
10 *
11 * Copyright © 2007-2017, Hoa community. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are met:
15 *     * Redistributions of source code must retain the above copyright
16 *       notice, this list of conditions and the following disclaimer.
17 *     * Redistributions in binary form must reproduce the above copyright
18 *       notice, this list of conditions and the following disclaimer in the
19 *       documentation and/or other materials provided with the distribution.
20 *     * Neither the name of the Hoa nor the names of its contributors may be
21 *       used to endorse or promote products derived from this software without
22 *       specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37namespace Hoa\Compiler\Test\Unit\Llk\Rule;
38
39use Hoa\Test;
40use Mock\Hoa\Compiler\Llk\Rule as SUT;
41
42/**
43 * Class \Hoa\Compiler\Test\Unit\Llk\Rule\Rule.
44 *
45 * Test suite of a rule.
46 *
47 * @copyright  Copyright © 2007-2017 Hoa community
48 * @license    New BSD License
49 */
50class Rule extends Test\Unit\Suite
51{
52    public function case_constructor()
53    {
54        $this
55            ->given(
56                $name     = 'foo',
57                $children = ['bar']
58            )
59            ->when($result = new SUT($name, $children))
60            ->then
61                ->string($result->getName())
62                    ->isEqualTo($name)
63                ->array($result->getChildren())
64                    ->isEqualTo($children)
65                ->variable($result->getNodeId())
66                    ->isNull()
67                ->boolean($result->isTransitional())
68                    ->isTrue();
69    }
70
71    public function case_constructor_with_node_id()
72    {
73        $this
74            ->given(
75                $name     = 'foo',
76                $children = ['bar'],
77                $nodeId   = 'baz'
78            )
79            ->when($result = new SUT($name, $children, $nodeId))
80            ->then
81                ->string($result->getName())
82                    ->isEqualTo($name)
83                ->array($result->getChildren())
84                    ->isEqualTo($children)
85                ->string($result->getNodeId())
86                    ->isEqualTo($nodeId)
87                ->boolean($result->isTransitional())
88                    ->isTrue();
89    }
90
91    public function case_set_name()
92    {
93        $this
94            ->given(
95                $name     = 'foo',
96                $children = ['bar'],
97                $rule     = new SUT($name, $children)
98            )
99            ->when($result = $rule->setName('baz'))
100            ->then
101                ->string($result)
102                    ->isEqualTo($name);
103    }
104
105    public function case_get_name()
106    {
107        $this
108            ->given(
109                $name     = 'baz',
110                $children = ['bar'],
111                $rule     = new SUT('foo', $children),
112                $rule->setName($name)
113            )
114            ->when($result = $rule->getName())
115            ->then
116                ->string($result)
117                    ->isEqualTo($name);
118    }
119
120    public function case_set_children()
121    {
122        $this
123            ->given(
124                $name     = 'foo',
125                $children = ['bar'],
126                $rule     = new SUT($name, $children)
127            )
128            ->when($result = $this->invoke($rule)->setChildren(['baz']))
129            ->then
130                ->array($result)
131                    ->isEqualTo($children);
132    }
133
134    public function case_get_children()
135    {
136        $this
137            ->given(
138                $name     = 'foo',
139                $children = ['baz'],
140                $rule     = new SUT($name, ['bar']),
141                $this->invoke($rule)->setChildren($children)
142            )
143            ->when($result = $rule->getChildren())
144            ->then
145                ->array($result)
146                    ->isEqualTo($children);
147    }
148
149    public function case_set_node_id()
150    {
151        $this
152            ->given(
153                $name        = 'foo',
154                $children    = ['bar'],
155                $nodeId      = 'id',
156                $rule        = new SUT($name, $children, $nodeId)
157            )
158            ->when($result = $rule->setNodeId('baz:qux'))
159            ->then
160                ->string($result)
161                    ->isEqualTo($nodeId);
162    }
163
164    public function case_get_node_id()
165    {
166        $this
167            ->given(
168                $name     = 'foo',
169                $children = ['bar'],
170                $rule     = new SUT($name, $children),
171                $rule->setNodeId('baz')
172            )
173            ->when($result = $rule->getNodeId())
174            ->then
175                ->string($result)
176                    ->isEqualTo('baz');
177    }
178
179    public function case_get_node_id_with_options()
180    {
181        $this
182            ->given(
183                $name     = 'foo',
184                $children = ['bar'],
185                $rule     = new SUT($name, $children),
186                $rule->setNodeId('baz:qux')
187            )
188            ->when($result = $rule->getNodeId())
189            ->then
190                ->string($result)
191                    ->isEqualTo('baz');
192    }
193
194    public function case_get_node_options_empty()
195    {
196        $this
197            ->given(
198                $name     = 'foo',
199                $children = ['bar'],
200                $rule     = new SUT($name, $children),
201                $rule->setNodeId('baz')
202            )
203            ->when($result = $rule->getNodeOptions())
204            ->then
205                ->array($result)
206                    ->isEmpty();
207    }
208
209    public function case_get_node_options()
210    {
211        $this
212            ->given(
213                $name     = 'foo',
214                $children = ['bar'],
215                $rule     = new SUT($name, $children),
216                $rule->setNodeId('baz:qux')
217            )
218            ->when($result = $rule->getNodeOptions())
219            ->then
220                ->array($result)
221                    ->isEqualTo(['q', 'u', 'x']);
222    }
223
224    public function case_set_default_id()
225    {
226        $this
227            ->given(
228                $name        = 'foo',
229                $children    = ['bar'],
230                $nodeId      = 'id',
231                $rule        = new SUT($name, $children, $nodeId)
232            )
233            ->when($result = $rule->setDefaultId('baz:qux'))
234            ->then
235                ->variable($result)
236                    ->isNull();
237    }
238
239    public function case_get_default_id()
240    {
241        $this
242            ->given(
243                $name     = 'foo',
244                $children = ['bar'],
245                $rule     = new SUT($name, $children),
246                $rule->setDefaultId('baz')
247            )
248            ->when($result = $rule->getDefaultId())
249            ->then
250                ->string($result)
251                    ->isEqualTo('baz');
252    }
253
254    public function case_get_default_id_with_options()
255    {
256        $this
257            ->given(
258                $name     = 'foo',
259                $children = ['bar'],
260                $rule     = new SUT($name, $children),
261                $rule->setDefaultId('baz:qux')
262            )
263            ->when($result = $rule->getDefaultId())
264            ->then
265                ->string($result)
266                    ->isEqualTo('baz');
267    }
268
269    public function case_get_default_options_empty()
270    {
271        $this
272            ->given(
273                $name     = 'foo',
274                $children = ['bar'],
275                $rule     = new SUT($name, $children),
276                $rule->setDefaultId('baz')
277            )
278            ->when($result = $rule->getDefaultOptions())
279            ->then
280                ->array($result)
281                    ->isEmpty();
282    }
283
284    public function case_get_default_options()
285    {
286        $this
287            ->given(
288                $name     = 'foo',
289                $children = ['bar'],
290                $rule     = new SUT($name, $children),
291                $rule->setDefaultId('baz:qux')
292            )
293            ->when($result = $rule->getDefaultOptions())
294            ->then
295                ->array($result)
296                    ->isEqualTo(['q', 'u', 'x']);
297    }
298
299    public function case_set_pp_representation()
300    {
301        $this
302            ->given(
303                $name              = 'foo',
304                $children          = ['bar'],
305                $pp                = '<a> ::b:: c()?',
306                $rule              = new SUT($name, $children),
307                $oldIsTransitional = $rule->isTransitional()
308            )
309            ->when($result = $rule->setPPRepresentation($pp))
310            ->then
311                ->variable($result)
312                    ->isNull()
313                ->boolean($oldIsTransitional)
314                    ->isTrue()
315                ->boolean($rule->isTransitional())
316                    ->isFalse();
317    }
318
319    public function case_get_pp_representation()
320    {
321        $this
322            ->given(
323                $name     = 'foo',
324                $children = ['bar'],
325                $pp       = '<a> ::b:: c()?',
326                $rule     = new SUT($name, $children),
327                $rule->setPPRepresentation($pp)
328            )
329            ->when($result = $rule->getPPRepresentation())
330            ->then
331                ->string($result)
332                    ->isEqualTo($pp);
333    }
334
335    public function case_is_transitional()
336    {
337        $this
338            ->given(
339                $name              = 'foo',
340                $children          = ['bar'],
341                $pp                = '<a> ::b:: c()?',
342                $rule              = new SUT($name, $children),
343                $oldIsTransitional = $rule->isTransitional(),
344                $rule->setPPRepresentation($pp)
345            )
346            ->when($result = $rule->isTransitional())
347            ->then
348                ->boolean($oldIsTransitional)
349                    ->isTrue()
350                ->boolean($result)
351                    ->isFalse();
352    }
353}
354