diff --git a/src/main/php/com/handlebarsjs/Nodes.class.php b/src/main/php/com/handlebarsjs/Nodes.class.php index d608c15..dee8e7d 100755 --- a/src/main/php/com/handlebarsjs/Nodes.class.php +++ b/src/main/php/com/handlebarsjs/Nodes.class.php @@ -3,6 +3,7 @@ use com\github\mustache\NodeList; use lang\IllegalArgumentException; +/** @test com.handlebarsjs.unittest.NodesTest */ class Nodes extends NodeList { private $partials= []; @@ -10,8 +11,8 @@ class Nodes extends NodeList { * Declares a partial and returns the nodes associated with the name. * * @param string|com.handlebarsjs.Quoted $partial - * @param ?com.github.mustache.NodeList $nodes - * @return com.github.mustache.NodeList + * @param ?parent|self $nodes + * @return self * @throws lang.IllegalArgumentException */ public function declare($partial, $nodes= null) { @@ -23,10 +24,25 @@ public function declare($partial, $nodes= null) { throw new IllegalArgumentException('Partial names must be strings or Quoted instances, have '.typeof($partial)); } - return $this->partials[$name]= $nodes ?? new NodeList(); + if ($nodes instanceof parent) { + return $this->partials[$name]= new self($nodes->nodes); + } else { + return $this->partials[$name]= $nodes ?? new self(); + } + } + + /** + * Inherits partials from a given parent + * + * @param self $parent + * @return self + */ + public function inheriting(self $parent) { + $this->partials= $parent->partials; + return $this; } - /** @return [:com.github.mustache.NodeList] */ + /** @return [:self] */ public function partials() { return $this->partials; } /** @@ -34,7 +50,7 @@ public function partials() { return $this->partials; } * not exist. * * @param string $partial - * @return ?com.github.mustache.NodeList + * @return ?self */ public function partial($partial) { return $this->partials[$partial] ?? null; diff --git a/src/test/php/com/handlebarsjs/unittest/NodesTest.class.php b/src/test/php/com/handlebarsjs/unittest/NodesTest.class.php new file mode 100755 index 0000000..cfbf154 --- /dev/null +++ b/src/test/php/com/handlebarsjs/unittest/NodesTest.class.php @@ -0,0 +1,48 @@ +partial= new Nodes([new VariableNode('test')]); + } + + #[Test] + public function can_create() { + new Nodes(); + } + + #[Test] + public function partial_null() { + Assert::null((new Nodes())->partial('test')); + } + + #[Test] + public function partials_empty() { + Assert::equals([], (new Nodes())->partials()); + } + + #[Test] + public function declare_partial() { + $fixture= new Nodes(); + $fixture->declare('test', $this->partial); + + Assert::equals($this->partial, $fixture->partial('test')); + Assert::equals(['test' => $this->partial], $fixture->partials()); + } + + #[Test] + public function inheriting_partials() { + $parent= new Nodes(); + $parent->declare('test', $this->partial); + $child= (new Nodes())->inheriting($parent); + + Assert::equals($this->partial, $child->partial('test')); + Assert::equals(['test' => $this->partial], $child->partials()); + } +} \ No newline at end of file diff --git a/src/test/php/com/handlebarsjs/unittest/PartialNodeTest.class.php b/src/test/php/com/handlebarsjs/unittest/PartialNodeTest.class.php index 5082157..d7e6eaf 100755 --- a/src/test/php/com/handlebarsjs/unittest/PartialNodeTest.class.php +++ b/src/test/php/com/handlebarsjs/unittest/PartialNodeTest.class.php @@ -12,6 +12,13 @@ public function template() { Assert::equals($template, $partial->template()); } + #[Test] + public function options() { + $options= ['mount' => new Quoted('/')]; + $partial= new PartialNode(new Lookup('test'), $options); + Assert::equals($options, $partial->options()); + } + #[Test] public function string_representation() { Assert::equals(