Skip to content
Permalink
Browse files

Adds Template::render(out) (no variable provider)

  • Loading branch information
remram44 committed Feb 14, 2015
1 parent b0f49c3 commit 37ee26dcd5ed5d615bc8cf8ddd7f722470e0fe20
Showing with 26 additions and 8 deletions.
  1. +6 −0 template.cpp
  2. +15 −3 template.h
  3. +5 −5 test_template.cpp
@@ -21,6 +21,12 @@ const char *TemplateError::what() const throw()
}


void EmptyVariableProvider::handle_variable(std::ostream &out, const std::string &name)
{
throw TemplateError("Missing value for variable " + name);
}


static void raise_error(const std::string &filename, int pos,
const std::string &error)
{
@@ -30,6 +30,14 @@ class VariableProvider {
};


class EmptyVariableProvider : public VariableProvider {

public:
void handle_variable(std::ostream &out, const std::string &name);

};


template<typename Map>
class MapVariableProvider : public VariableProvider {

@@ -49,7 +57,7 @@ class MapVariableProvider : public VariableProvider {
}
catch(std::out_of_range &e)
{
throw TemplateError("Missing value for variable" + name);
throw TemplateError("Missing value for variable " + name);
}
}

@@ -66,10 +74,9 @@ class Template {

typedef std::pair<BlockType, std::string> Block;

private:
protected:
std::vector<Block> m_Blocks;

protected:
static bool is_space(char c);

public:
@@ -90,6 +97,11 @@ class Template {
}
void render(std::ostream &out, VariableProvider &vars);
void render(std::ostream &out, std::initializer_list<const char*> vars);
inline void render(std::ostream &out)
{
EmptyVariableProvider empty;
render(out, empty);
}

};

@@ -4,6 +4,7 @@
#include <unordered_map>

#define private public
#define protected public
#include "template.h"


@@ -100,11 +101,10 @@ class Template_test : public CppUnit::TestFixture {
Item("test", "essai")
});
CPPUNIT_ASSERT_THROW(
tpl.render(
oss,
std::unordered_map<std::string, std::string>{
Item("is", "est")
}),
tpl.render(oss, {"is", "est"}),
TemplateError);
CPPUNIT_ASSERT_THROW(
tpl.render(oss),
TemplateError);
}
catch(TemplateError &e)

0 comments on commit 37ee26d

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