Add tests

This commit is contained in:
Ivan Shalganov
2013-09-14 11:24:23 +04:00
parent 0a9facc493
commit 2fc84c226e
7 changed files with 177 additions and 60 deletions

View File

@ -430,18 +430,18 @@ class Fenom
"float_tags" => array()
);
if (method_exists($storage, $compiler . "Open")) {
$c["open"] = $compiler . "Open";
$c["open"] = array($storage, $compiler . "Open");
} else {
throw new \LogicException("Open compiler {$compiler}Open not found");
}
if (method_exists($storage, $compiler . "Close")) {
$c["close"] = $compiler . "Close";
$c["close"] = array($storage, $compiler . "Close");
} else {
throw new \LogicException("Close compiler {$compiler}Close not found");
}
foreach ($tags as $tag) {
if (method_exists($storage, "tag" . $tag)) {
$c["tags"][$tag] = "tag" . $tag;
$c["tags"][$tag] = array($storage, "tag" . $tag);
if ($floats && in_array($tag, $floats)) {
$c['float_tags'][$tag] = 1;
}
@ -767,7 +767,6 @@ class Fenom
$fenom = $this; // used in template
$_tpl = include($this->_compile_dir . "/" . $file_name);
/* @var Fenom\Render $_tpl */
// var_dump($tpl, $_tpl->isValid()); exit;
if (!($this->_options & self::AUTO_RELOAD) || ($this->_options & self::AUTO_RELOAD) && $_tpl->isValid()) {
return $_tpl;
}

View File

@ -85,6 +85,7 @@ class Render extends \ArrayObject
$this->_fenom = $fenom;
$props += self::$_props;
$this->_name = $props["name"];
$this->_base_name = $props["base_name"];
$this->_scm = $props["scm"];
$this->_time = $props["time"];
$this->_depends = $props["depends"];

View File

@ -99,6 +99,11 @@ class Template extends Render
private $_filters = array();
/**
* @var int crc32 of the template name
*/
private $_crc = 0;
protected static $_tests = array(
'integer' => 'is_int(%s)',
'int' => 'is_int(%s)',
@ -157,7 +162,8 @@ class Template extends Render
public function load($name, $compile = true)
{
$this->_name = $name;
if ($provider = strstr($name, ":", true)) {
$this->_crc = crc32($this->_name);
if ($provider = strstr($name, ':', true)) {
$this->_scm = $provider;
$this->_base_name = substr($name, strlen($provider) + 1);
} else {
@ -300,7 +306,7 @@ class Template extends Render
*/
public function tmpVar()
{
return '$t' . ($this->i++);
return sprintf('$t%u_%d', $this->_crc, $this->i++);
}
/**
@ -622,7 +628,7 @@ class Template extends Render
$var = false;
}
if ($tokens->is('?', '!')) {
$term = $this->parseTernary($tokens, $term, $tokens->current());
$term = $this->parseTernary($tokens, $term, $var);
$var = false;
}
$exp[] = $term;
@ -873,35 +879,59 @@ class Template extends Render
*
* @param Tokenizer $tokens
* @param $var
* @param $type
* @param $is_var
* @return string
* @throws UnexpectedTokenException
*/
public function parseTernary(Tokenizer $tokens, $var, $type)
public function parseTernary(Tokenizer $tokens, $var, $is_var)
{
$empty = ($type === "?");
$empty = $tokens->is('?');
$tokens->next();
if ($tokens->is(":")) {
$tokens->next();
if ($empty) {
return '(empty(' . $var . ') ? (' . $this->parseExpr($tokens) . ') : ' . $var . ')';
if($is_var) {
return '(empty(' . $var . ') ? (' . $this->parseExpr($tokens) . ') : ' . $var . ')';
} else {
return '(' . $var . ' ?: (' . $this->parseExpr($tokens) . ')';
}
} else {
return '(isset(' . $var . ') ? ' . $var . ' : (' . $this->parseExpr($tokens) . '))';
if($is_var) {
return '(isset(' . $var . ') ? ' . $var . ' : (' . $this->parseExpr($tokens) . '))';
} else {
return '((' . $var . ' !== null) ? ' . $var . ' : (' . $this->parseExpr($tokens) . '))';
}
}
} elseif ($tokens->is(Tokenizer::MACRO_BINARY, Tokenizer::MACRO_BOOLEAN, Tokenizer::MACRO_MATH) || !$tokens->valid()) {
if ($empty) {
return '!empty(' . $var . ')';
if($is_var) {
return '!empty(' . $var . ')';
} else {
return '(' . $var . ')';
}
} else {
return 'isset(' . $var . ')';
if($is_var) {
return 'isset(' . $var . ')';
} else {
return '(' . $var . ' !== null)';
}
}
} else {
$expr1 = $this->parseExpr($tokens);
$tokens->need(':')->skip();
$expr2 = $this->parseExpr($tokens);
if ($empty) {
return '(empty(' . $var . ') ? ' . $expr2 . ' : ' . $expr1 . ')';
if($is_var) {
return '(empty(' . $var . ') ? ' . $expr2 . ' : ' . $expr1 . ')';
} else {
return '(' . $var . ' ? ' . $expr1 . ' : ' . $expr2 . ')';
}
} else {
return '(isset(' . $var . ') ? ' . $expr1 . ' : ' . $expr2 . ')';
if($is_var) {
return '(isset(' . $var . ') ? ' . $expr1 . ' : ' . $expr2 . ')';
} else {
return '((' . $var . ' !== null) ? ' . $expr1 . ' : ' . $expr2 . ')';
}
}
}
}
@ -1035,22 +1065,23 @@ class Template extends Render
*/
public function parseScalar(Tokenizer $tokens)
{
$_scalar = "";
$token = $tokens->key();
switch ($token) {
case T_CONSTANT_ENCAPSED_STRING:
case T_LNUMBER:
case T_DNUMBER:
$_scalar .= $tokens->getAndNext();
return $tokens->getAndNext();
break;
case T_ENCAPSED_AND_WHITESPACE:
case '"':
$_scalar .= $this->parseQuote($tokens);
return $this->parseQuote($tokens);
break;
case '$':
$tokens->next()->need('.')->next()->need(T_CONST)->next();
return 'constant('.$this->parseName($tokens).')';
default:
throw new UnexpectedTokenException($tokens);
}
return $_scalar;
}
/**