list; } /** * Gets the errors as an array. * * @param Lexer|Parser $obj Object containing the errors. * * @return array */ public function getErrorsAsArray($obj) { $ret = array(); foreach ($obj->errors as $err) { $ret[] = $obj instanceof Lexer ? array($err->getMessage(), $err->ch, $err->pos, $err->getCode()) : array($err->getMessage(), $err->token, $err->getCode()); } return $ret; } /** * Gets test's input and expected output. * * @param string $name The name of the test. * * @return array */ public function getData($name) { $input = file_get_contents('tests/data/' . $name . '.in'); $output = unserialize(file_get_contents('tests/data/' . $name . '.out')); return array($input, $output); } /** * Tests the `Lexer`. * * @param string $name The name of the test. * * @return Lexer */ public function runLexerTest($name) { list($input, $output) = $this->getData($name); $lexer = new Lexer($input); $errors = $this->getErrorsAsArray($lexer); $lexer->errors = array(); $this->assertEquals($output['errors'], $errors); $this->assertEquals($output['lexer'], $lexer); return $lexer; } /** * Tests the `Parser`. * * @param string $name The name of the test. * * @return Parser */ public function runParserTest($name) { list($input, $output) = $this->getData($name); $parser = new Parser($input); $errors = $this->getErrorsAsArray($parser); $parser->errors = array(); $this->assertEquals($output['errors'], $errors); $this->assertEquals($output['parser'], $parser); return $parser; } }