/** * @covers Cradle\Handlebars\HandlebarsException::forCompileError */ public function testForCompileError() { $actual = null; $code = "Line 1\nLine 2\nLine 3\nLine 4\n"; $error = ['line' => 2, 'message' => 'foobar']; try { throw HandlebarsException::forCompileError($error, $code, 1); } catch (HandlebarsException $e) { $actual = $e->getMessage(); } $expected = "foobar on line 2 \n```\n2: Line 2\n3: Line 3\n```\n"; $this->assertEquals($expected, $actual); }
/** * Partially renders the section close tokens * * @param *array $node * @param *array $open * * @return string */ protected function generateClose(array $node, array &$open) { $node['value'] = trim($node['value']); if ($this->findSection($open, $node['value']) === false) { throw HandlebarsException::forUnknownEnd($node['value'], $node['line']); } $buffer = ''; $i = $this->findSection($open); if (!isset($open[$i]['else'])) { $buffer .= $this->prettyPrint(self::BLOCK_OPTIONS_FN_BODY_5, -1); $buffer .= $this->prettyPrint(self::BLOCK_OPTIONS_FN_BODY_6); $buffer .= $this->prettyPrint(self::BLOCK_OPTIONS_FN_BODY_7); $buffer .= $this->prettyPrint(self::BLOCK_OPTIONS_FN_CLOSE); $buffer .= $this->prettyPrint(self::BLOCK_OPTIONS_INVERSE_EMPTY); } else { $buffer .= $this->prettyPrint(self::BLOCK_OPTIONS_INVERSE_BODY_5); $buffer .= $this->prettyPrint(self::BLOCK_OPTIONS_INVERSE_BODY_6); $buffer .= $this->prettyPrint(self::BLOCK_OPTIONS_INVERSE_BODY_7); $buffer .= $this->prettyPrint(self::BLOCK_OPTIONS_INVERSE_CLOSE, -1); } unset($open[$i]); $buffer .= $this->prettyPrint(self::BLOCK_OPTIONS_CLOSE, -1); $buffer .= $this->prettyPrint(self::BLOCK_ESCAPE_HELPER_CLOSE, -1); return $buffer; }
/** * Returns a very nice error message * * @param *string $code * * @return HandlebarsHandler */ protected function checkEval($code) { $error = error_get_last(); if (isset($error['message']) && isset($error['line']) && $error['message'] === 'parse error') { $code = explode("\n", $code); $start = $error['line'] - 25; if ($start < 0) { $start = 0; } $code = array_splice($code, $start, 50); foreach ($code as $i => $line) { $code[$i] = ++$start . ': ' . $line; } throw HandlebarsException::forCompileError($error, $code); } return $this; }