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\Compiler as LUT;
40use Hoa\Compiler\Llk\Rule\Token as SUT;
41use Hoa\Test;
42
43/**
44 * Class \Hoa\Compiler\Test\Unit\Llk\Rule\Token.
45 *
46 * Test suite of a token.
47 *
48 * @copyright  Copyright © 2007-2017 Hoa community
49 * @license    New BSD License
50 */
51class Token extends Test\Unit\Suite
52{
53    public function case_is_a_rule()
54    {
55        $this
56            ->when($result = new SUT('name', 'tokenName', 'nodeId', 0))
57            ->then
58                ->object($result)
59                    ->isInstanceOf(LUT\Llk\Rule::class);
60    }
61
62    public function case_constructor()
63    {
64        $this
65            ->given(
66                $name        = 'foo',
67                $tokenName   = 'bar',
68                $nodeId      = 'baz',
69                $unification = 0
70            )
71            ->when($result = new SUT($name, $tokenName, $nodeId, $unification))
72            ->then
73                ->string($result->getName())
74                    ->isEqualTo($name)
75                ->string($result->getTokenName())
76                    ->isEqualTo($tokenName)
77                ->string($result->getNodeId())
78                    ->isEqualTo($nodeId)
79                ->integer($result->getUnificationIndex())
80                    ->isEqualTo($unification)
81                ->boolean($result->isKept())
82                    ->isFalse();
83    }
84
85    public function case_constructor_with_kept_flag()
86    {
87        $this
88            ->given(
89                $name        = 'foo',
90                $tokenName   = 'bar',
91                $nodeId      = 'baz',
92                $unification = 0,
93                $kept        = true
94            )
95            ->when($result = new SUT($name, $tokenName, $nodeId, $unification, $kept))
96            ->then
97                ->string($result->getName())
98                    ->isEqualTo($name)
99                ->string($result->getTokenName())
100                    ->isEqualTo($tokenName)
101                ->string($result->getNodeId())
102                    ->isEqualTo($nodeId)
103                ->integer($result->getUnificationIndex())
104                    ->isEqualTo($unification)
105                ->boolean($result->isKept())
106                    ->isTrue();
107    }
108
109    public function case_get_token_name()
110    {
111        $this
112            ->given(
113                $name        = 'foo',
114                $tokenName   = 'bar',
115                $nodeId      = 'baz',
116                $unification = 0,
117                $token       = new SUT($name, $tokenName, $nodeId, $unification)
118            )
119            ->when($result = $token->getTokenName())
120            ->then
121                ->string($result)
122                    ->isEqualTo($tokenName);
123    }
124
125    public function case_set_namespace()
126    {
127        $this
128            ->given(
129                $name        = 'foo',
130                $tokenName   = 'bar',
131                $nodeId      = 'baz',
132                $unification = 0,
133                $namespace   = 'qux',
134                $token       = new SUT($name, $tokenName, $nodeId, $unification)
135            )
136            ->when($result = $token->setNamespace($namespace))
137            ->then
138                ->variable($result)
139                    ->isNull();
140    }
141
142    public function case_get_namespace()
143    {
144        $this
145            ->given(
146                $name        = 'foo',
147                $tokenName   = 'bar',
148                $nodeId      = 'baz',
149                $unification = 0,
150                $namespace   = 'qux',
151                $token       = new SUT($name, $tokenName, $nodeId, $unification),
152                $token->setNamespace($namespace)
153            )
154            ->when($result = $token->getNamespace())
155            ->then
156                ->string($result)
157                    ->isEqualTo($namespace);
158    }
159
160    public function case_set_representation()
161    {
162        $this
163            ->given(
164                $name           = 'foo',
165                $tokenName      = 'bar',
166                $nodeId         = 'baz',
167                $unification    = 0,
168                $representation = 'qux',
169                $token          = new SUT($name, $tokenName, $nodeId, $unification)
170            )
171            ->when($result = $token->setRepresentation($representation))
172            ->then
173                ->variable($result)
174                    ->isNull();
175    }
176
177    public function case_get_representation()
178    {
179        $this
180            ->given(
181                $name           = 'foo',
182                $tokenName      = 'bar',
183                $nodeId         = 'baz',
184                $unification    = 0,
185                $representation = 'qux',
186                $token          = new SUT($name, $tokenName, $nodeId, $unification),
187                $token->setRepresentation($representation)
188            )
189            ->when($result = $token->getRepresentation())
190            ->then
191                ->string($result)
192                    ->isEqualTo($representation);
193    }
194
195    public function case_get_ast()
196    {
197        $this
198            ->given(
199                $name           = 'foo',
200                $tokenName      = 'bar',
201                $nodeId         = 'baz',
202                $unification    = 0,
203                $representation = 'qux',
204                $token          = new SUT($name, $tokenName, $nodeId, $unification),
205                $token->setRepresentation($representation)
206            )
207            ->when($result = $token->getAST())
208            ->then
209                ->object($result)
210                    ->isInstanceOf(LUT\Llk\TreeNode::class)
211                ->let($dumper = new LUT\Visitor\Dump())
212                ->string($dumper->visit($result))
213                    ->isEqualTo(
214                        '>  #expression' . "\n" .
215                        '>  >  #concatenation' . "\n" .
216                        '>  >  >  token(literal, q)' . "\n" .
217                        '>  >  >  token(literal, u)' . "\n" .
218                        '>  >  >  token(literal, x)' . "\n"
219                    );
220    }
221
222    public function case_set_value()
223    {
224        $this
225            ->given(
226                $name        = 'foo',
227                $tokenName   = 'bar',
228                $nodeId      = 'baz',
229                $unification = 0,
230                $value       = 'qux',
231                $token       = new SUT($name, $tokenName, $nodeId, $unification)
232            )
233            ->when($result = $token->setValue($value))
234            ->then
235                ->variable($result)
236                    ->isNull();
237    }
238
239    public function case_get_value()
240    {
241        $this
242            ->given(
243                $name           = 'foo',
244                $tokenName      = 'bar',
245                $nodeId         = 'baz',
246                $unification    = 0,
247                $value = 'qux',
248                $token          = new SUT($name, $tokenName, $nodeId, $unification),
249                $token->setValue($value)
250            )
251            ->when($result = $token->getValue())
252            ->then
253                ->string($result)
254                    ->isEqualTo($value);
255    }
256
257    public function case_set_kept()
258    {
259        $this
260            ->given(
261                $name        = 'foo',
262                $tokenName   = 'bar',
263                $nodeId      = 'baz',
264                $unification = 0,
265                $kept        = true,
266                $token       = new SUT($name, $tokenName, $nodeId, $unification)
267            )
268            ->when($result = $token->setKept($kept))
269            ->then
270                ->boolean($result)
271                    ->isFalse();
272    }
273
274    public function case_is_kept()
275    {
276        $this
277            ->given(
278                $name        = 'foo',
279                $tokenName   = 'bar',
280                $nodeId      = 'baz',
281                $unification = 0,
282                $kept        = true,
283                $token       = new SUT($name, $tokenName, $nodeId, $unification),
284                $token->setKept($kept)
285            )
286            ->when($result = $token->isKept())
287            ->then
288                ->boolean($result)
289                    ->isTrue();
290    }
291
292    public function case_get_unification_index()
293    {
294        $this
295            ->given(
296                $name        = 'foo',
297                $tokenName   = 'bar',
298                $nodeId      = 'baz',
299                $unification = 42,
300                $token       = new SUT($name, $tokenName, $nodeId, $unification)
301            )
302            ->when($result = $token->getUnificationIndex())
303            ->then
304                ->integer($result)
305                    ->isEqualTo($unification);
306    }
307}
308