Permalink
Browse files

Two small improvements

- allow define() without arguments to create an unregistered DefinitionNode
- allow run() to take a $value argument to separate definition from value processing.
  • Loading branch information...
arjan committed Oct 21, 2011
1 parent 05f5f7a commit 393ebca70bdd25220dbf792aa1c7c35747b43efa
Showing with 26 additions and 5 deletions.
  1. +4 −2 src/Structr/Structr.php
  2. +5 −3 src/Structr/Tree/Base/Node.php
  3. +17 −0 tests/Structr/Test/DefinitionTest.php
View
@@ -38,9 +38,11 @@ public static function clearAll() {
self::$_definitions = array();
}
- public static function define($name) {
+ public static function define($name = null) {
$node = new DefinitionNode();
- self::$_definitions[$name] = $node;
+ if (!empty($name)) {
+ self::$_definitions[$name] = $node;
+ }
return $node;
}
@@ -75,12 +75,14 @@ public function end() {
return $this->parent();
}
- public function run() {
+ public function run($value = null) {
if ($this->_parent !== null) {
- return $this->root()->run();
+ return $this->root()->run($value);
}
- $value = $this->getValue();
+ if ($value === null) {
+ $value = $this->getValue();
+ }
$return = $this->_walk_value($value);
@@ -57,4 +57,21 @@ public function testDefinitionClosure() {
$this->assertSame($expected, $result);
}
+
+ public function testEmptyDefinition() {
+ Structr::clearAll();
+
+ $structr = Structr::define()->isInteger()->end();
+ $this->assertInstanceOf('Structr\Tree\DefinitionNode', $structr);
+ }
+
+
+ public function testRunWithValue() {
+ Structr::clearAll();
+ $structr = Structr::define()->isInteger()->end();
+
+ $expected = 2;
+ $this->assertSame($expected, $structr->run(2));
+ }
+
}

0 comments on commit 393ebca

Please sign in to comment.