Skip to content

Commit

Permalink
Created an include function.
Browse files Browse the repository at this point in the history
  • Loading branch information
yosssi committed Feb 25, 2014
1 parent d87a16f commit 130daf9
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
20 changes: 19 additions & 1 deletion element.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
TypeBlock = "block"
TypeExpression = "expression"
TypeLiteral = "literal"
TypeInclude = "include"
)

var (
Expand Down Expand Up @@ -53,7 +54,7 @@ func (e *Element) parse() error {
return errors.New(fmt.Sprintf("The element has no tokens. (line no: %d)", e.LineNo))
}
switch {
case e.Type == TypeContent || e.Type == TypeBlock || e.Type == TypeExpression || e.Type == TypeLiteral || e.comment():
case e.Type == TypeContent || e.Type == TypeBlock || e.Type == TypeExpression || e.Type == TypeLiteral || e.Type == TypeInclude || e.comment():
default:
for i, token := range e.Tokens {
switch {
Expand Down Expand Up @@ -215,6 +216,21 @@ func (e *Element) Html(bf *bytes.Buffer) error {
return errors.New(fmt.Sprintf("The sub template does not have the %s block.", name))
}
block.Html(bf)
case e.Type == TypeInclude:
if len(e.Tokens) < 2 {
return errors.New(fmt.Sprintf("The include element does not have a path. (line no: %d)", e.LineNo))
}
tpl := e.getTemplate()
g := tpl.Generator
incTpl, err := g.Parse(tpl.Dir() + e.Tokens[1] + goldExtension)
if err != nil {
return err
}
incHtml, err := incTpl.Html()
if err != nil {
return err
}
bf.WriteString(incHtml)
default:
e.writeOpenTag(bf)
if e.hasTextValues() {
Expand Down Expand Up @@ -341,6 +357,8 @@ func (e *Element) setType() {
e.Type = TypeContent
case len(e.Tokens) > 0 && e.Tokens[0] == "block":
e.Type = TypeBlock
case len(e.Tokens) > 0 && e.Tokens[0] == "include":
e.Type = TypeInclude
case len(e.Tokens) > 0 && e.Tokens[0] == "|":
e.Type = TypeLiteral
case expression(e.Text):
Expand Down
48 changes: 46 additions & 2 deletions element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ func TestElementParse(t *testing.T) {
t.Errorf("Error(%s) occurred.", err.Error())
}

// When an element's type is TypeExpression.
// When an element's type is TypeLiteral.
e = &Element{Tokens: []string{"test"}, Type: TypeLiteral}
err = e.parse()
if err != nil {
t.Errorf("Error(%s) occurred.", err.Error())
}

// When an element's type is TypeExpression.
// When an element's type is TypeTag.
e, err = NewElement("div data-test=test test test2", 1, 0, nil, nil, nil)
if err != nil {
t.Errorf("Error(%s) occurred.", err.Error())
Expand Down Expand Up @@ -397,6 +397,43 @@ func TestElementHtml(t *testing.T) {
if bf.String() != expectedString {
t.Errorf("Buffer stirng should be %s", expectedString)
}

// When the element's type is include and tokens' length < 2.
e, err = NewElement("include", 1, 0, nil, nil, nil)
expectedErrMsg = fmt.Sprintf("The include element does not have a path. (line no: %d)", e.LineNo)
if err := e.Html(&bf); err == nil || err.Error() != expectedErrMsg {
t.Errorf("Error(%s) should be returned.", expectedErrMsg)
}

// When the element's type is include and g.Parse returns an error.
g := NewGenerator(false)
tpl = NewTemplate("path", g)
e, err = NewElement("include ./somepath/somefile", 1, 0, nil, tpl, nil)
bf = bytes.Buffer{}
expectedErrMsg = "open ././somepath/somefile.gold: no such file or directory"
if err := e.Html(&bf); err == nil || err.Error() != expectedErrMsg {
t.Errorf("Error(%s) should be returned.", expectedErrMsg)
}

// When the element's type is include and incTpl.Html returns an error.
g = NewGenerator(false)
tpl = NewTemplate("./test/TestElementHtml/somefile.gold", g)
e, err = NewElement("include ./001", 1, 0, nil, tpl, nil)
bf = bytes.Buffer{}
expectedErrMsg = "The block element does not have a name. (line no: 1)"
if err := e.Html(&bf); err == nil || err.Error() != expectedErrMsg {
t.Errorf("Error(%s) should be returned.", expectedErrMsg)
}

// When the element's type is include.
g = NewGenerator(false)
tpl = NewTemplate("./test/TestElementHtml/somefile.gold", g)
e, err = NewElement("include ./002", 1, 0, nil, tpl, nil)
bf = bytes.Buffer{}
if err := e.Html(&bf); err != nil {
t.Errorf("An error(%s) occurred.", err.Error())
}

}

func TestElementWriteOpenTag(t *testing.T) {
Expand Down Expand Up @@ -611,6 +648,13 @@ func TestElementSetType(t *testing.T) {
if e.Type != TypeTag {
t.Errorf("Type should be %s", TypeTag)
}

// When the element's text is an include element.
e = &Element{Tokens: []string{"include"}}
e.setType()
if e.Type != TypeInclude {
t.Errorf("Type should be %s", TypeInclude)
}
}

func TestElementGetBlock(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions test/TestElementHtml/001.gold
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
block
2 changes: 2 additions & 0 deletions test/TestElementHtml/002.gold
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#container
| aaa

0 comments on commit 130daf9

Please sign in to comment.