Skip to content

Commit

Permalink
Add test cases to cover various error cases
Browse files Browse the repository at this point in the history
- syntax error
- extra tokens (both ignore and error)
- invalid token
- undefined constant

More exceptions handling test cases
  • Loading branch information
dregad committed Apr 30, 2016
1 parent db05b48 commit a55cb58
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions tests/Mantis/ConfigParserTest.php
Expand Up @@ -91,6 +91,52 @@ public function testArrays() {
}
}

public function testExtraTokensError() {
$this->setExpectedExceptionRegExp('Exception', '/^Extra tokens found/');

$t_parser = new ConfigParser( '1; 2' );
$t_parser->parse( ConfigParser::EXTRA_TOKENS_ERROR );

$t_parser = new ConfigParser( 'array(); 2' );
$t_parser->parse( ConfigParser::EXTRA_TOKENS_ERROR );
}

public function testExtraTokensIgnore() {
$t_parser = new ConfigParser( '1; 2' );
$t_result = $t_parser->parse( ConfigParser::EXTRA_TOKENS_ERROR );
$this->assertEquals( $t_result, 1 );

$t_parser = new ConfigParser( 'array(); 2' );
$t_result = $t_parser->parse( ConfigParser::EXTRA_TOKENS_IGNORE );
$this->assertEquals( $t_result, array() );
}

public function testSyntaxError() {
$this->setExpectedExceptionRegExp('Exception', '/^Syntax error/');

$t_parser = new ConfigParser( 'array(' );
$t_parser->parse();
}

public function testInvalidTokensError() {
$this->setExpectedExceptionRegExp('Exception', '/^Unexpected token/');

$t_parser = new ConfigParser( 'echo 1;' );
$t_parser->parse();
}

public function testUnknownConstantError() {
$this->setExpectedExceptionRegExp('Exception', '/^Unknown string literal/');

# Make sure we have a string that is not a defined constant
$t_constant = 'UNDEFINED_CONSTANT';
while( defined($t_constant) ) {
$t_constant .= '_' . rand(0, 9999);
}
$t_parser = new ConfigParser( $t_constant );
$t_parser->parse();
}

private function errorMessage( $p_text ) {
return "Original input:\n"
. ">>>------------------------\n"
Expand Down

0 comments on commit a55cb58

Please sign in to comment.