Remove {unser} and add tests

This commit is contained in:
Ivan Shalganov 2013-09-15 16:05:18 +04:00
parent 7c038e0ba9
commit f9d9f098b7
7 changed files with 35 additions and 62 deletions

View File

@ -17,7 +17,7 @@ use Fenom\Template;
*/
class Fenom
{
const VERSION = '1.3';
const VERSION = '1.4';
/* Actions */
const INLINE_COMPILER = 1;
@ -246,10 +246,6 @@ class Fenom
'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::autoescapeOpen',
'close' => 'Fenom\Compiler::autoescapeClose'
),
'unset' => array(
'type' => self::INLINE_COMPILER,
'parser' => 'Fenom\Compiler::tagUnset'
)
);
@ -540,17 +536,6 @@ class Fenom
return false;
}
/**
* @param string $function
* @param Fenom\Template $template
* @return bool|string
* @deprecated
*/
public function getFunction($function, Template $template = null)
{
return $this->getTag($function, $template);
}
/**
* Returns tag info
*

View File

@ -1028,23 +1028,4 @@ class Compiler
$scope->tpl->escape = $scope["escape"];
}
/**
* Unset present variables
*
* @param Tokenizer $tokens
* @param Template $tpl
* @return string
* @throws InvalidUsageException
*/
public static function tagUnset(Tokenizer $tokens, Template $tpl)
{
$vars = array();
while ($tokens->valid()) {
$vars[] = $tpl->parseVariable($tokens);
}
if (!$vars) {
throw new InvalidUsageException("Unset must accept variable(s)");
}
return 'unset(' . implode(', ', $vars) . ')';
}
}

View File

@ -25,10 +25,6 @@ class UnexpectedTokenException extends \RuntimeException
}
if (!$tokens->curr) {
$this->message = "Unexpected end of " . ($where ? : "expression") . "$expect";
} elseif ($tokens->curr[0] === T_WHITESPACE) {
$this->message = "Unexpected whitespace$expect";
} elseif ($tokens->curr[0] === T_BAD_CHARACTER) {
$this->message = "Unexpected bad characters (below ASCII 32 except \\t, \\n and \\r) in " . ($where ? : "expression") . "$expect";
} else {
$this->message = "Unexpected token '" . $tokens->current() . "' in " . ($where ? : "expression") . "$expect";
}

View File

@ -304,26 +304,6 @@ class Tokenizer
return $this->next ? $this->next[1] == $token : false;
}
/**
* Return substring. This method doesn't move pointer.
* @param int $offset
* @param int $limit
* @return string
*/
public function getSubstr($offset, $limit = 0)
{
$str = '';
if (!$limit) {
$limit = $this->_max;
} else {
$limit += $offset;
}
for ($i = $offset; $i <= $limit; $i++) {
$str .= $this->tokens[$i][1] . $this->tokens[$i][2];
}
return $str;
}
/**
* Return token and move pointer
* @return mixed

View File

@ -209,6 +209,7 @@ class TemplateTest extends TestCase
array('If: {$a != 5 => 4} end', 'Fenom\Error\CompileException', "Unexpected token '=>'"),
array('If: {$a + (*6)} end', 'Fenom\Error\CompileException', "Unexpected token '*'"),
array('If: {$a + ( 6} end', 'Fenom\Error\CompileException', "Unexpected end of expression, expect ')'"),
array('If: {$a end', 'Fenom\Error\CompileException', "Unclosed tag in line"),
);
}
@ -684,6 +685,7 @@ class TemplateTest extends TestCase
array('{if null is set} block1 {else} block2 {/if}', 'block2'),
array('{if 0 is empty} block1 {else} block2 {/if}', 'block1'),
array('{if "" is empty} block1 {else} block2 {/if}', 'block1'),
array('{if [] is empty} block1 {else} block2 {/if}', 'block1'),
array('{if "data" is empty} block1 {else} block2 {/if}', 'block2'),
array('{if time() is not empty} block1 {else} block2 {/if}', 'block1'),
// is empty
@ -700,8 +702,10 @@ class TemplateTest extends TestCase
// event, odd
array('{if $one is odd} block1 {else} block2 {/if}', 'block1'),
array('{if $one is even} block1 {else} block2 {/if}', 'block2'),
array('{if ($one + 1) is even} block1 {else} block2 {/if}', 'block1'),
array('{if $two is even} block1 {else} block2 {/if}', 'block1'),
array('{if $two is odd} block1 {else} block2 {/if}', 'block2'),
array('{if ($two+1) is odd} block1 {else} block2 {/if}', 'block1'),
// template
array('{if "welcome.tpl" is template} block1 {else} block2 {/if}', 'block1'),
array('{if "welcome2.tpl" is template} block1 {else} block2 {/if}', 'block2'),
@ -754,13 +758,19 @@ class TemplateTest extends TestCase
array('{$.get.one?}', '1'),
array('{$.get.one is set}', '1'),
array('{$.get.two is empty}', '1'),
array('{$.version}', Fenom::VERSION),
array('{$.tpl?}', '1'),
array('{$.tpl.name}', 'runtime.tpl'),
array('{$.tpl.time}', '0'),
array('{$.tpl.schema}', ''),
);
}
public function _testSandbox()
{
try {
var_dump($this->fenom->setOptions(Fenom::FORCE_VERIFY)->compileCode('{if $unexist} block1 {else} block2 {/if}')->getBody());
var_dump($this->fenom->setOptions(Fenom::FORCE_VERIFY)->addFilter(function ($txt) {return $txt;})->compileCode('- <?php {$a} ?> -')->fetch(['a' => 1]));
} catch (\Exception $e) {
print_r($e->getMessage() . "\n" . $e->getTraceAsString());
}
@ -840,7 +850,10 @@ class TemplateTest extends TestCase
public function testInsert($code, $vars, $result)
{
$this->values = $vars;
$tpl = $this->assertRender($code, $result);
$this->tpl("insert.tpl", $code);
$tpl = $this->fenom->getTemplate('insert.tpl');
$this->assertSame($result, $tpl->fetch($vars));
$this->assertTrue($tpl->isValid());
}
/**

View File

@ -1,5 +1,6 @@
<?php
namespace Fenom;
use Fenom\Error\UnexpectedTokenException;
use Fenom\Tokenizer;
class TokenizerTest extends \PHPUnit_Framework_TestCase
@ -35,6 +36,7 @@ class TokenizerTest extends \PHPUnit_Framework_TestCase
$this->assertSame("sin", $tokens->getNext($tokens::MACRO_STRING));
$this->assertSame("sin", $tokens->current());
$this->assertTrue($tokens->isPrev(":"));
$this->assertSame(T_STRING, $tokens->key());
$this->assertTrue($tokens->is(T_STRING));
$this->assertTrue($tokens->is($tokens::MACRO_STRING));
@ -46,6 +48,9 @@ class TokenizerTest extends \PHPUnit_Framework_TestCase
$tokens->next();
$tokens->next();
$this->assertSame("+", $tokens->getNext($tokens::MACRO_BINARY));
$this->assertSame($code, $tokens->getSnippetAsString(-100, 100));
$this->assertSame('+', $tokens->getSnippetAsString(100, -100));
}
public function testSkip()
@ -65,4 +70,5 @@ class TokenizerTest extends \PHPUnit_Framework_TestCase
$this->assertSame(T_LNUMBER, $tokens->key());
$tokens->next();
}
}

View File

@ -32,6 +32,17 @@ class FenomTest extends \Fenom\TestCase
$fenom->clearAllCompiles();
}
public function testFactory() {
$time = $this->tpl('temp.tpl', 'Template 1 a');
$fenom = Fenom::factory($provider = new \Fenom\Provider(FENOM_RESOURCES . '/template'), FENOM_RESOURCES . '/compile', Fenom::AUTO_ESCAPE);
$this->assertInstanceOf('Fenom\Template', $tpl = $fenom->getTemplate('temp.tpl'));
$this->assertSame($provider, $tpl->getProvider());
$this->assertSame('temp.tpl', $tpl->getBaseName());
$this->assertSame('temp.tpl', $tpl->getName());
$this->assertSame($time, $tpl->getTime());
$fenom->clearAllCompiles();
}
public function testCompileFile()
{
$a = array(
@ -148,7 +159,8 @@ class FenomTest extends \Fenom\TestCase
return "|--- $text ---|";
});
$this->assertSame('+++ |--- == hello ---||--- world == ---| +++', $this->fenom->compileCode('hello {var $user} god {/var} world')->fetch(array()));
$this->assertSame('+++ |--- == hello ---||--- world == ---| +++', $this->fenom->compileCode('hello {var $user} misterio {/var} world')->fetch(array()));
$this->assertSame('+++ |--- == hello ---||--- world == ---| +++', $this->fenom->compileCode('hello {var $user} <?php misterio ?> {/var} world')->fetch(array()));
}
public function testAddInlineCompilerSmart() {