Add some Ace test cases
authorbep <bjorn.erik.pedersen@gmail.com>
Sun, 31 May 2015 11:01:20 +0000 (13:01 +0200)
committerbep <bjorn.erik.pedersen@gmail.com>
Sun, 31 May 2015 11:13:28 +0000 (13:13 +0200)
See #1178

tpl/template.go
tpl/template_test.go

index bb85225d4e4f8205b758074c1293caf6b6ce1f6e..36737eb77d65c5188155c0dccdd931cf27a7751f 100644 (file)
@@ -41,6 +41,7 @@ type Template interface {
        LoadTemplates(absPath string)
        LoadTemplatesWithPrefix(absPath, prefix string)
        AddTemplate(name, tpl string) error
+       AddAceTemplate(name, basePath, innerPath string, baseContent, innerContent []byte) error
        AddInternalTemplate(prefix, name, tpl string) error
        AddInternalShortcode(name, tpl string) error
        PrintErrors()
index 3c009c5998a564a6f34d443f51d404b725d17983..99c85affbd8a466089802544b4b79565099651e5 100644 (file)
@@ -1,11 +1,83 @@
 package tpl
 
 import (
+       "bytes"
        "errors"
        "io/ioutil"
+       "os"
+       "path/filepath"
        "testing"
 )
 
+// Some tests for Issue #1178 -- Ace
+func TestAceTemplates(t *testing.T) {
+
+       for i, this := range []struct {
+               basePath     string
+               innerPath    string
+               baseContent  string
+               innerContent string
+               expect       string
+               expectErr    int
+       }{
+               {"", filepath.FromSlash("_default/single.ace"), "", "{{ . }}", "DATA", 0},
+               {filepath.FromSlash("_default/baseof.ace"), filepath.FromSlash("_default/single.ace"),
+                       `= content main
+  h2 This is a content named "main" of an inner template. {{ . }}`,
+                       `= doctype html
+html lang=en
+  head
+    meta charset=utf-8
+    title Base and Inner Template
+  body
+    h1 This is a base template {{ . }}
+    = yield main`, `<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Base and Inner Template</title></head><body><h1>This is a base template DATA</h1></body></html>`, 0},
+       } {
+
+               for _, root := range []string{"", os.TempDir()} {
+
+                       templ := New()
+
+                       basePath := this.basePath
+                       innerPath := this.innerPath
+
+                       if basePath != "" && root != "" {
+                               basePath = filepath.Join(root, basePath)
+                       }
+
+                       if innerPath != "" && root != "" {
+                               innerPath = filepath.Join(root, innerPath)
+                       }
+
+                       d := "DATA"
+
+                       err := templ.AddAceTemplate("mytemplate.ace", basePath, innerPath,
+                               []byte(this.baseContent), []byte(this.innerContent))
+
+                       if err != nil && this.expectErr == 0 {
+                               t.Errorf("Test %d with root '%s' errored: %s", i, root, err)
+                       } else if err == nil && this.expectErr == 1 {
+                               t.Errorf("#1 Test %d with root '%s' should have errored", i, root)
+                       }
+
+                       var buff bytes.Buffer
+                       err = templ.ExecuteTemplate(&buff, "mytemplate.html", d)
+
+                       if err != nil && this.expectErr == 0 {
+                               t.Errorf("Test %d with root '%s' errored: %s", i, root, err)
+                       } else if err == nil && this.expectErr == 2 {
+                               t.Errorf("#2 Test with root '%s' %d should have errored", root, i)
+                       } else {
+                               result := buff.String()
+                               if result != this.expect {
+                                       t.Errorf("Test %d  with root '%s' got\n%s\nexpected\n%s", i, root, result, this.expect)
+                               }
+                       }
+               }
+       }
+
+}
+
 // Test for bugs discovered by https://github.com/dvyukov/go-fuzz
 func TestTplGoFuzzReports(t *testing.T) {