Skip to content

Commit

Permalink
Fix Signature::insert()
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Oct 1, 2023
1 parent dc50af0 commit f092a0b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 5 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ XP AST ChangeLog

## ?.?.? / ????-??-??

* Fixed `Signature::insert()` - @thekid
* Refactored code base to use the class loading mechanism instead of the
*Package* class from `lang.reflect`. See xp-framework/rfc#338
(@thekid)
Expand Down
7 changes: 2 additions & 5 deletions src/main/php/lang/ast/nodes/Signature.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use lang\ast\Node;

/** @test lang.ast.unittest.nodes.SignatureTest */
class Signature extends Node {
public $kind= 'signature';
public $parameters, $returns, $byref;
Expand All @@ -21,11 +22,7 @@ public function insert($offset, Parameter $p) {
if (0 === $offset) {
array_unshift($this->parameters, $p);
} else {
$this->parameters[]= array_merge(
array_slice($this->parameters, 0, $p),
[$p],
array_slice($this->parameters, $p)
);
array_splice($this->parameters, $offset, 0, [$p]);
}
}
}
79 changes: 79 additions & 0 deletions src/test/php/lang/ast/unittest/nodes/SignatureTest.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php namespace lang\ast\unittest\nodes;

use lang\ast\nodes\{Signature, Parameter};
use lang\ast\types\IsLiteral;
use test\{Assert, Test, Values};

class SignatureTest extends NodeTest {

#[Test]
public function can_create() {
new Signature();
}

#[Test]
public function without_parameters() {
Assert::equals([], (new Signature())->parameters);
}

#[Test]
public function parameters() {
$params= [new Parameter('arg', null)];
Assert::equals($params, (new Signature($params))->parameters);
}

#[Test]
public function without_returns() {
Assert::null((new Signature([]))->returns);
}

#[Test]
public function returns_string() {
$type= new IsLiteral('string');
Assert::equals($type, (new Signature([], $type))->returns);
}

#[Test]
public function without_by_ref() {
Assert::false((new Signature([], null))->byref);
}

#[Test, Values([true, false])]
public function by_ref($value) {
Assert::equals($value, (new Signature([], null, $value))->byref);
}

#[Test]
public function add_parameter() {
$param= new Parameter('arg', null);

$signature= new Signature();
$signature->add($param);

Assert::equals([$param], $signature->parameters);
}

#[Test]
public function insert_parameter_at_beginning() {
$a= new Parameter('a', null);
$b= new Parameter('b', null);
$c= new Parameter('c', null);

$signature= new Signature([$a, $b]);
$signature->insert(0, $c);

Assert::equals([$c, $a, $b], $signature->parameters);
}

#[Test]
public function insert_parameter_at_offset() {
$a= new Parameter('a', null);
$b= new Parameter('b', null);
$c= new Parameter('c', null);

$signature= new Signature([$a, $b]);
$signature->insert(1, $c);

Assert::equals([$a, $c, $b], $signature->parameters);
}
}

0 comments on commit f092a0b

Please sign in to comment.