mirror of
https://github.com/smarty-php/smarty.git
synced 2025-07-30 16:07:13 +02:00
Fix modifier default implementation and improve unit tests
This commit is contained in:
@ -28,7 +28,7 @@ function smarty_modifiercompiler_default($params, Smarty_Internal_TemplateCompil
|
||||
foreach ($params as $param) {
|
||||
|
||||
if ($compiler->syntaxMatchesVariable($output)) {
|
||||
$output = '(empty(' . $output . ') ? ' . $param . ' : ' . $output . ')';
|
||||
$output = '(!isset(' . $output . ') || ' . $output . ' === \'\' ? ' . $param . ' : ' . $output . ')';
|
||||
} else {
|
||||
$output = '(($tmp = ' . $output . ')===null||$tmp===\'\' ? ' . $param . ' : $tmp)';
|
||||
}
|
||||
|
@ -63,20 +63,31 @@ class PluginModifierDefaultTest extends PHPUnit_Smarty
|
||||
$this->assertEquals('B', $this->smarty->fetch($tpl));
|
||||
}
|
||||
|
||||
public function testNull()
|
||||
/**
|
||||
* @dataProvider dataFalsyValues
|
||||
*/
|
||||
public function testFalsyValues($falsyvalue, $expected)
|
||||
{
|
||||
$tpl = $this->smarty->createTemplate('string:{$s|default:"B"}');
|
||||
$this->smarty->assign('s', null);
|
||||
$this->assertEquals('B', $this->smarty->fetch($tpl));
|
||||
$this->smarty->assign('s', $falsyvalue);
|
||||
$this->assertEquals($expected, $this->smarty->fetch($tpl));
|
||||
}
|
||||
|
||||
public function testFalse()
|
||||
{
|
||||
$tpl = $this->smarty->createTemplate('string:{$s|default:"B"}');
|
||||
$this->smarty->assign('s', false);
|
||||
$this->assertEquals('B', $this->smarty->fetch($tpl));
|
||||
/**
|
||||
* Data for ::testFalsyValues
|
||||
*/
|
||||
public function dataFalsyValues() {
|
||||
return array(
|
||||
array(false, false),
|
||||
array(0, 0),
|
||||
array('0', '0'),
|
||||
array(array(), 'Array'),
|
||||
array(null, 'B'),
|
||||
array('', 'B'),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public function testFunctionCall()
|
||||
{
|
||||
$tpl = $this->smarty->createTemplate('string:{strlen("a")|default:"B"}');
|
||||
@ -137,7 +148,4 @@ class PluginModifierDefaultTest extends PHPUnit_Smarty
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user