Skip to content
Permalink
Browse files

Accept single '{' in templates

  • Loading branch information
remram44 committed Feb 14, 2015
1 parent 1c20d87 commit 540767c508a3ea19cdb08d5f99b03ab38df48552
Showing with 24 additions and 5 deletions.
  1. +12 −2 template.cpp
  2. +12 −3 test_template.cpp
@@ -78,7 +78,11 @@ void Template::compile(std::istream &source, const std::string &filename)
if(c == '{')
state = OPEN_2;
else
raise_error(filename, pos, "expecting {");
{
current += '{';
state = NORMAL;
current += c;
}
break;
case OPEN_2:
if(!is_space(c))
@@ -116,8 +120,14 @@ void Template::compile(std::istream &source, const std::string &filename)
break;
}
}
if(state != NORMAL)
if(state == OPEN_1)
{
current += '{';
state = NORMAL;
}
else if(state != NORMAL)
raise_error(filename, pos, "premature end of stream");

if(!current.empty())
m_Blocks.push_back(Block(DATA, current));
}
@@ -35,8 +35,8 @@ class Template_test : public CppUnit::TestFixture {

void test_render()
{
std::istringstream stream("Th{{end }} {{ verb }}: {{ what}}!");
try {
std::istringstream stream("Th{{end }} {{ verb }}: {{ what}}!");
Template tpl(stream);
typedef std::pair<std::string, std::string> Item;

@@ -59,6 +59,16 @@ class Template_test : public CppUnit::TestFixture {
"what", "trees"
});
CPPUNIT_ASSERT(oss.str() == "These are: trees!");

oss.str("");
stream.str("Test2{");
Template(stream).render(oss);
CPPUNIT_ASSERT(oss.str() == "Test2{");

oss.str("");
stream.str("Test2{and stuff");
Template(stream).render(oss);
CPPUNIT_ASSERT(oss.str() == "Test2{and stuff");
}
catch(TemplateError &e)
{
@@ -77,9 +87,8 @@ class Template_test : public CppUnit::TestFixture {

void test_errors()
{
do_error_test("This{", true);
do_error_test("This{{", true);
do_error_test("This{ {is}}", true);
do_error_test("This{ {is}}", false);
do_error_test("This{{is} }", true);
do_error_test("This{{is}}", false);
do_error_test("This{{is}", true);

0 comments on commit 540767c

Please sign in to comment.
You can’t perform that action at this time.