Browse Source

test(llk) Add test cases for the offset feature.

pull/76/head
Ivan Enderlin 1 year ago
parent
commit
e4ec8dbb75
No known key found for this signature in database
4 changed files with 78 additions and 20 deletions
  1. 5
    2
      Llk/Rule/Token.php
  2. 12
    12
      Llk/TreeNode.php
  3. 41
    6
      Test/Unit/Llk/Rule/Token.php
  4. 20
    0
      Test/Unit/Llk/TreeNode.php

+ 5
- 2
Llk/Rule/Token.php View File

@@ -245,12 +245,15 @@ class Token extends Rule
245 245
     /**
246 246
      * Set token offset.
247 247
      *
248
-     * @param int $offset
249
-     * @return void
248
+     * @param   int  $offset    Offset.
249
+     * @return  int
250 250
      */
251 251
     public function setOffset($offset)
252 252
     {
253
+        $old           = $this->_offset;
253 254
         $this->_offset = $offset;
255
+
256
+        return $old;
254 257
     }
255 258
 
256 259
     /**

+ 12
- 12
Llk/TreeNode.php View File

@@ -176,29 +176,29 @@ class TreeNode implements Visitor\Element
176 176
     }
177 177
 
178 178
     /**
179
-     * Get token offset.
179
+     * Get value value.
180 180
      *
181
-     * @return int
181
+     * @return  string
182 182
      */
183
-    public function getOffset()
183
+    public function getValueValue()
184 184
     {
185 185
         return
186
-            isset($this->_value['offset'])
187
-                ? $this->_value['offset']
188
-                : 0;
186
+            isset($this->_value['value'])
187
+                ? $this->_value['value']
188
+                : null;
189 189
     }
190 190
 
191 191
     /**
192
-     * Get value value.
192
+     * Get token offset.
193 193
      *
194
-     * @return  string
194
+     * @return  int
195 195
      */
196
-    public function getValueValue()
196
+    public function getOffset()
197 197
     {
198 198
         return
199
-            isset($this->_value['value'])
200
-                ? $this->_value['value']
201
-                : null;
199
+            isset($this->_value['offset'])
200
+                ? $this->_value['offset']
201
+                : 0;
202 202
     }
203 203
 
204 204
     /**

+ 41
- 6
Test/Unit/Llk/Rule/Token.php View File

@@ -240,12 +240,12 @@ class Token extends Test\Unit\Suite
240 240
     {
241 241
         $this
242 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),
243
+                $name        = 'foo',
244
+                $tokenName   = 'bar',
245
+                $nodeId      = 'baz',
246
+                $unification = 0,
247
+                $value       = 'qux',
248
+                $token       = new SUT($name, $tokenName, $nodeId, $unification),
249 249
                 $token->setValue($value)
250 250
             )
251 251
             ->when($result = $token->getValue())
@@ -254,6 +254,41 @@ class Token extends Test\Unit\Suite
254 254
                     ->isEqualTo($value);
255 255
     }
256 256
 
257
+    public function case_set_offset()
258
+    {
259
+        $this
260
+            ->given(
261
+                $name        = 'foo',
262
+                $tokenName   = 'bar',
263
+                $nodeId      = 'baz',
264
+                $unification = 0,
265
+                $offset      = 42,
266
+                $token       = new SUT($name, $tokenName, $nodeId, $unification)
267
+            )
268
+            ->when($result = $token->setOffset($offset))
269
+            ->then
270
+                ->integer($result)
271
+                    ->isZero();
272
+    }
273
+
274
+    public function case_get_offset()
275
+    {
276
+        $this
277
+            ->given(
278
+                $name        = 'foo',
279
+                $tokenName   = 'bar',
280
+                $nodeId      = 'baz',
281
+                $unification = 0,
282
+                $offset      = 42,
283
+                $token       = new SUT($name, $tokenName, $nodeId, $unification),
284
+                $token->setOffset($offset)
285
+            )
286
+            ->when($result = $token->getOffset())
287
+            ->then
288
+                ->integer($result)
289
+                    ->isEqualTo($offset);
290
+    }
291
+
257 292
     public function case_set_kept()
258 293
     {
259 294
         $this

+ 20
- 0
Test/Unit/Llk/TreeNode.php View File

@@ -229,6 +229,26 @@ class TreeNode extends Test\Unit\Suite
229 229
                     ->isNull();
230 230
     }
231 231
 
232
+    public function case_get_offset()
233
+    {
234
+        $this
235
+            ->given($node = new SUT('foo', ['offset' => 42]))
236
+            ->when($result = $node->getOffset())
237
+            ->then
238
+                ->integer($result)
239
+                    ->isEqualTo(42);
240
+    }
241
+
242
+    public function case_get_offset_undefined()
243
+    {
244
+        $this
245
+            ->given($node = new SUT('foo', ['bar']))
246
+            ->when($result = $node->getOffset())
247
+            ->then
248
+                ->integer($result)
249
+                    ->isZero();
250
+    }
251
+
232 252
     public function case_is_token()
233 253
     {
234 254
         $this

Loading…
Cancel
Save