@@ -1,11 +1,61 @@
11package main
22
33import (
4+ "bytes"
5+ "io/ioutil"
6+ "log"
7+ "os"
8+ "path/filepath"
49 "testing"
510
611 "github.com/stretchr/testify/assert"
712)
813
14+func TestLoadTemplates(t *testing.T) {
15+ data := struct {
16+ TestString string
17+ }{
18+ TestString: "Testing",
19+ }
20+
21+ templates := loadTemplates("")
22+ assert.NotEqual(t, templates, nil)
23+
24+ var defaultSignin bytes.Buffer
25+ templates.ExecuteTemplate(&defaultSignin, "sign_in.html", data)
26+ assert.Equal(t, "\n<!DOCTYPE html>", defaultSignin.String()[0:16])
27+
28+ var defaultError bytes.Buffer
29+ templates.ExecuteTemplate(&defaultError, "error.html", data)
30+ assert.Equal(t, "\n<!DOCTYPE html>", defaultError.String()[0:16])
31+
32+ dir, err := ioutil.TempDir("", "templatetest")
33+ if err != nil {
34+ log.Fatal(err)
35+ }
36+ defer os.RemoveAll(dir)
37+
38+ templateHTML := `{{.TestString}} {{.TestString | ToLower}} {{.TestString | ToUpper}}`
39+ signInFile := filepath.Join(dir, "sign_in.html")
40+ if err := ioutil.WriteFile(signInFile, []byte(templateHTML), 0666); err != nil {
41+ log.Fatal(err)
42+ }
43+ errorFile := filepath.Join(dir, "error.html")
44+ if err := ioutil.WriteFile(errorFile, []byte(templateHTML), 0666); err != nil {
45+ log.Fatal(err)
46+ }
47+ templates = loadTemplates(dir)
48+ assert.NotEqual(t, templates, nil)
49+
50+ var sitpl bytes.Buffer
51+ templates.ExecuteTemplate(&sitpl, "sign_in.html", data)
52+ assert.Equal(t, "Testing testing TESTING", sitpl.String())
53+
54+ var errtpl bytes.Buffer
55+ templates.ExecuteTemplate(&errtpl, "error.html", data)
56+ assert.Equal(t, "Testing testing TESTING", errtpl.String())
57+}
58+
959func TestTemplatesCompile(t *testing.T) {
1060 templates := getTemplates()
1161 assert.NotEqual(t, templates, nil)