Merge remote-tracking branch 'refs/remotes/origin/develop'

This commit is contained in:
bzick 2013-07-22 18:44:51 +04:00
commit a5f83a59b8
5 changed files with 85 additions and 13 deletions

View File

@ -742,12 +742,12 @@ class Fenom {
* @throws \RuntimeException if key from custom assoc doesn't exists into possible values * @throws \RuntimeException if key from custom assoc doesn't exists into possible values
*/ */
private static function _makeMask(array $values, array $options, $mask = 0) { private static function _makeMask(array $values, array $options, $mask = 0) {
foreach($values as $value) { foreach ($values as $key=>$value) {
if(isset($options[$value])) { if (isset($options[$key])) {
if($options[$value]) { if ($options[$key]) {
$mask |= $options[$value]; $mask |= $options[$key];
} else { } else {
$mask &= ~$options[$value]; $mask &= ~$options[$key];
} }
} else { } else {
throw new \RuntimeException("Undefined parameter $value"); throw new \RuntimeException("Undefined parameter $value");

View File

@ -615,7 +615,7 @@ class Compiler {
} elseif(isset($params[ $param->getPosition() ])) { } elseif(isset($params[ $param->getPosition() ])) {
$args[] = $params[ $param->getPosition() ]; $args[] = $params[ $param->getPosition() ];
} elseif($param->isOptional()) { } elseif($param->isOptional()) {
$args[] = $param->getDefaultValue(); $args[] = var_export($param->getDefaultValue(), true);
} }
} }
return "$function(".implode(", ", $args).')'; return "$function(".implode(", ", $args).')';

View File

@ -62,7 +62,6 @@ class Template extends Render {
* Escape outputs value * Escape outputs value
* @var bool * @var bool
*/ */
// public $escape = false;
public $escape = false; public $escape = false;
public $_extends; public $_extends;
public $_extended = false; public $_extended = false;
@ -109,7 +108,7 @@ class Template extends Render {
'array' => 'is_array(%s)', 'array' => 'is_array(%s)',
'iterable' => '\Fenom\Modifier::isIterable(%s)', 'iterable' => '\Fenom\Modifier::isIterable(%s)',
'const' => 'defined(%s)', 'const' => 'defined(%s)',
'template' => '$this->getStorage()->templateExists(%s)', 'template' => '$tpl->getStorage()->templateExists(%s)',
'empty' => 'empty(%s)', 'empty' => 'empty(%s)',
'set' => 'isset(%s)', 'set' => 'isset(%s)',
'_empty' => '!%s', // for none variable '_empty' => '!%s', // for none variable
@ -648,12 +647,12 @@ class Template extends Render {
throw new TokenizeException("Unexpected token ".$tokens->getNext().", isset() and empty() accept only variables"); throw new TokenizeException("Unexpected token ".$tokens->getNext().", isset() and empty() accept only variables");
} }
$term = true; $term = true;
} elseif(!$term && $tokens->is(Tokenizer::MACRO_UNARY)) { // like unary operator, see Tokenizer::MACRO_UNARY } elseif(!$term && $tokens->is(Tokenizer::MACRO_UNARY)) {
if(!$tokens->isNext(T_VARIABLE, T_DNUMBER, T_LNUMBER, T_STRING, T_ISSET, T_EMPTY)) { if(!$tokens->isNext(T_VARIABLE, T_DNUMBER, T_LNUMBER, T_STRING, T_ISSET, T_EMPTY, '(')) {
break; break;
} }
$_exp[] = $tokens->getAndNext(); $_exp[] = $tokens->getAndNext();
$term = false; $term = 0;
} elseif($tokens->is(Tokenizer::MACRO_BINARY)) { // like binary operator, see Tokenizer::MACRO_BINARY } elseif($tokens->is(Tokenizer::MACRO_BINARY)) { // like binary operator, see Tokenizer::MACRO_BINARY
if(!$term) { if(!$term) {
throw new UnexpectedTokenException($tokens); throw new UnexpectedTokenException($tokens);
@ -671,6 +670,8 @@ class Template extends Render {
} }
$_exp[] = " ".$tokens->getAndNext()." "; $_exp[] = " ".$tokens->getAndNext()." ";
$term = 0; $term = 0;
} elseif($tokens->is('[')) {
$_exp[] = $this->parseArray($tokens);
} else { } else {
break; break;
} }
@ -1284,7 +1285,7 @@ class Template extends Render {
} }
if($tokens->is("=")) { if($tokens->is("=")) {
$tokens->next(); $tokens->next();
$params[ $key ] = $this->parseExp($tokens); $params[ $key ] = $this->parseExp($tokens);
} else { } else {
$params[ $key ] = 'true'; $params[ $key ] = 'true';
} }

View File

@ -0,0 +1,70 @@
<?php
namespace Fenom;
class FunctionsTest extends TestCase {
const FUNCTION_ARGUMENT_CONSTANT = 1;
public static function functionSum($of = array()) {
return array_sum($of);
}
public static function functionPow($a, $n = 2) {
return pow($a, $n);
}
public static function functionInc($a, $i = self::FUNCTION_ARGUMENT_CONSTANT) {
return $a + $i;
}
public function setUp() {
parent::setUp();
$this->fenom->addFunctionSmart('sum', __CLASS__ . '::functionSum');
$this->fenom->addFunctionSmart('pow', __CLASS__ . '::functionPow');
$this->fenom->addFunctionSmart('inc', __CLASS__ . '::functionInc');
$this->tpl('function_params_scalar.tpl', '{pow a=2 n=3}');
$this->tpl('function_params_dynamic.tpl', '{pow a=$a n=$n}');
$this->tpl('function_default_param_scalar.tpl', '{pow a=2}');
$this->tpl('function_default_param_empty_array.tpl', '{sum}');
$this->tpl('function_default_param_const.tpl', '{inc a=1}');
$this->tpl('function_array_param.tpl', '{sum of=[1, 2, 3, 4, 5]}');
$this->tpl('function_array_param_pos.tpl', '{sum [1, 2, 3, 4, 5]}');
}
public function testFunctionWithParams() {
$output = $this->fenom->fetch('function_params_scalar.tpl');
$this->assertEquals('8', $output);
}
public function testFunctionWithDynamicParams() {
$output = $this->fenom->fetch('function_params_dynamic.tpl', array('a' => 3, 'n' => 4));
$this->assertEquals('81', $output);
}
public function testFunctionWithDefaultParamScalar() {
$output = $this->fenom->fetch('function_default_param_scalar.tpl');
$this->assertEquals('4', $output);
}
public function testFunctionWithDefaultParamArray() {
$output = $this->fenom->fetch('function_default_param_empty_array.tpl');
$this->assertEquals('0', $output);
}
public function testFunctionWithDefaultParamConst() {
$output = $this->fenom->fetch('function_default_param_const.tpl');
$this->assertEquals('2', $output);
}
public function testFunctionWithArrayNamedParam() {
$output = $this->fenom->fetch('function_array_param.tpl');
$this->assertEquals('15', $output);
}
public function testFunctionWithArrayPositionalParam() {
$output = $this->fenom->fetch('function_array_param_pos.tpl');
$this->assertEquals('15', $output);
}
}

View File

@ -157,8 +157,9 @@ class TemplateTest extends TestCase {
array('Exp: {$y-$x} result', $b, 'Exp: 18 result'), array('Exp: {$y-$x} result', $b, 'Exp: 18 result'),
array('Exp: {$y*$x} result', $b, 'Exp: 243 result'), array('Exp: {$y*$x} result', $b, 'Exp: 243 result'),
array('Exp: {$y^$x} result', $b, 'Exp: 18 result'), array('Exp: {$y^$x} result', $b, 'Exp: 18 result'),
array('Exp: {-$x} result', $b, 'Exp: -9 result'), array('Exp: {-($x)} result', $b, 'Exp: -9 result'),
array('Exp: {!$x} result', $b, 'Exp: result'), array('Exp: {!$x} result', $b, 'Exp: result'),
array('Exp: {!($x)} result', $b, 'Exp: result'),
array('Exp: {!5} result', $b, 'Exp: result'), array('Exp: {!5} result', $b, 'Exp: result'),
array('Exp: {-1} result', $b, 'Exp: -1 result'), array('Exp: {-1} result', $b, 'Exp: -1 result'),
array('Exp: {$z = 5} {$z} result', $b, 'Exp: 5 5 result'), array('Exp: {$z = 5} {$z} result', $b, 'Exp: 5 5 result'),