Skip to content

Commit

Permalink
add more syntax check for #206
Browse files Browse the repository at this point in the history
  • Loading branch information
Zordius Chen committed Feb 4, 2016
1 parent a810fc7 commit 6ba3556
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public static function arrayString($list) {
*
* @return array<integer|boolean|array> analyzed result
*
* @expect array(0, false, array('foo')) when input array('flags' => array('spvar' => 0)), array(0, 'foo')
* @expect array(1, false, array('foo')) when input array('flags' => array('spvar' => 0)), array(1, 'foo')
*/
public static function analyze($context, $var) {
$levels = 0;
Expand Down
8 changes: 8 additions & 0 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ protected static function getLiteral($name, $asis, $quote = false) {
*
* @expect array('this') when input 'this', array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 0)), 0
* @expect array() when input 'this', array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 1)), 0
* @expect array(1) when input '..', array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 1, 'parent' => 1), 'usedFeature' => array('parent' => 0)), 0
* @expect array(1) when input '../', array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 1, 'parent' => 1), 'usedFeature' => array('parent' => 0)), 0
* @expect array(1) when input '../.', array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 1, 'parent' => 1), 'usedFeature' => array('parent' => 0)), 0
* @expect array(1) when input '../this', array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 1, 'parent' => 1), 'usedFeature' => array('parent' => 0)), 0
Expand Down Expand Up @@ -161,6 +162,13 @@ protected static function getExpression($v, &$context, $pos) {
preg_match_all('/([^\\.\\/]+)/', $v, $matchedall);
}

if ($v !== '.') {
$vv = implode('.', $matchedall[1]);
if (strlen($v) !== strlen($vv)) {
$context['error'][] = "Unexpected charactor in '$v' ! (should it be '$vv' ?)";
}
}

foreach ($matchedall[1] as $m) {
if ($context['flags']['advar'] && substr($m, 0, 1) === '[') {
$ret[] = substr($m, 1, -1);
Expand Down
12 changes: 12 additions & 0 deletions tests/ExpressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ public function testOn_arrayString() {
array('a', 'b', 'c')
))));
}
/**
* @covers LightnCandy\Expression::analyze
*/
public function testOn_analyze() {
$method = new \ReflectionMethod('LightnCandy\Expression', 'analyze');
$this->assertEquals(array(0, false, array('foo')), $method->invokeArgs(null, array_by_ref(array(
array('flags' => array('spvar' => 0)), array(0, 'foo')
))));
$this->assertEquals(array(1, false, array('foo')), $method->invokeArgs(null, array_by_ref(array(
array('flags' => array('spvar' => 0)), array(1, 'foo')
))));
}
/**
* @covers LightnCandy\Expression::toString
*/
Expand Down
6 changes: 6 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public function testOn_getExpression() {
$this->assertEquals(array(), $method->invokeArgs(null, array_by_ref(array(
'this', array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 1)), 0
))));
$this->assertEquals(array(1), $method->invokeArgs(null, array_by_ref(array(
'..', array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 1, 'parent' => 1), 'usedFeature' => array('parent' => 0)), 0
))));
$this->assertEquals(array('..foo'), $method->invokeArgs(null, array_by_ref(array(
'..foo', array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 1, 'parent' => 1), 'usedFeature' => array('parent' => 0)), 0
))));
$this->assertEquals(array(1), $method->invokeArgs(null, array_by_ref(array(
'../', array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 1, 'parent' => 1), 'usedFeature' => array('parent' => 0)), 0
))));
Expand Down
40 changes: 32 additions & 8 deletions tests/errorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,26 @@ public function errorProvider()
Array(
'template' => '{{wi[n]ner.test3}}',
'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
'expected' => 'Wrong variable naming as \'wi[n]ner.test3\' in {{wi[n]ner.test3}} !',
'expected' => Array(
'Wrong variable naming as \'wi[n]ner.test3\' in {{wi[n]ner.test3}} !',
"Unexpected charactor in 'wi[n]ner.test3' ! (should it be 'wi.[n].ner.test3' ?)",
),
),
Array(
'template' => '{{winner].[test4]}}',
'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
'expected' => 'Wrong variable naming as \'winner].[test4]\' in {{winner].[test4]}} !',
'expected' => Array(
'Wrong variable naming as \'winner].[test4]\' in {{winner].[test4]}} !',
"Unexpected charactor in 'winner].[test4]' ! (should it be 'winner.[test4]' ?)",
),
),
Array(
'template' => '{{winner[.test5]}}',
'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
'expected' => 'Wrong variable naming as \'winner[.test5]\' in {{winner[.test5]}} !',
'expected' => Array(
'Wrong variable naming as \'winner[.test5]\' in {{winner[.test5]}} !',
"Unexpected charactor in 'winner[.test5]' ! (should it be 'winner.[.test5]' ?)",
),
),
Array(
'template' => '{{winner.[.test6]}}',
Expand All @@ -215,7 +224,10 @@ public function errorProvider()
Array(
'template' => '{{test9]}}',
'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
'expected' => 'Wrong variable naming as \'test9]\' in {{test9]}} !',
'expected' => Array(
'Wrong variable naming as \'test9]\' in {{test9]}} !',
"Unexpected charactor in 'test9]' ! (should it be 'test9' ?)",
),
),
Array(
'template' => '{{testA[}}',
Expand All @@ -236,7 +248,10 @@ public function errorProvider()
Array(
'template' => '{{]testC}}',
'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
'expected' => 'Wrong variable naming as \']testC\' in {{]testC}} !',
'expected' => Array(
'Wrong variable naming as \']testC\' in {{]testC}} !',
"Unexpected charactor in ']testC' ! (should it be 'testC' ?)",
)
),
Array(
'template' => '{{[testD]}}',
Expand Down Expand Up @@ -288,17 +303,26 @@ public function errorProvider()
Array(
'template' => '{{te.t[est].endL}}',
'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
'expected' => 'Wrong variable naming as \'te.t[est].endL\' in {{te.t[est].endL}} !',
'expected' => Array(
'Wrong variable naming as \'te.t[est].endL\' in {{te.t[est].endL}} !',
"Unexpected charactor in 'te.t[est].endL' ! (should it be 'te.t.[est].endL' ?)",
),
),
Array(
'template' => '{{te.t[est]o.endM}}',
'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
'expected' => 'Wrong variable naming as \'te.t[est]o.endM\' in {{te.t[est]o.endM}} !',
'expected' => Array(
'Wrong variable naming as \'te.t[est]o.endM\' in {{te.t[est]o.endM}} !',
"Unexpected charactor in 'te.t[est]o.endM' ! (should it be 'te.t.[est].o.endM' ?)"
),
),
Array(
'template' => '{{te.[est]o.endN}}',
'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
'expected' => 'Wrong variable naming as \'te.[est]o.endN\' in {{te.[est]o.endN}} !',
'expected' => Array(
'Wrong variable naming as \'te.[est]o.endN\' in {{te.[est]o.endN}} !',
"Unexpected charactor in 'te.[est]o.endN' ! (should it be 'te.[est].o.endN' ?)",
),
),
Array(
'template' => '{{te.[e.st].endO}}',
Expand Down

0 comments on commit 6ba3556

Please sign in to comment.