forked from mirror/gin
add FuncMap testing
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
e38c36ee0d
commit
a40699e07f
|
@ -0,0 +1 @@
|
||||||
|
Date: {[{.now | formatAsDate}]}
|
29
gin_test.go
29
gin_test.go
|
@ -6,6 +6,7 @@ package gin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
@ -15,6 +16,11 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func formatAsDate(t time.Time) string {
|
||||||
|
year, month, day := t.Date()
|
||||||
|
return fmt.Sprintf("%d/%02d/%02d", year, month, day)
|
||||||
|
}
|
||||||
|
|
||||||
func setupHTMLFiles(t *testing.T) func() {
|
func setupHTMLFiles(t *testing.T) func() {
|
||||||
go func() {
|
go func() {
|
||||||
router := New()
|
router := New()
|
||||||
|
@ -32,12 +38,21 @@ func setupHTMLFiles(t *testing.T) func() {
|
||||||
|
|
||||||
func setupHTMLGlob(t *testing.T) func() {
|
func setupHTMLGlob(t *testing.T) func() {
|
||||||
go func() {
|
go func() {
|
||||||
|
SetMode(DebugMode)
|
||||||
router := New()
|
router := New()
|
||||||
router.Delims("{[{", "}]}")
|
router.Delims("{[{", "}]}")
|
||||||
|
router.SetFuncMap(template.FuncMap{
|
||||||
|
"formatAsDate": formatAsDate,
|
||||||
|
})
|
||||||
router.LoadHTMLGlob("./fixtures/basic/*")
|
router.LoadHTMLGlob("./fixtures/basic/*")
|
||||||
router.GET("/test", func(c *Context) {
|
router.GET("/test", func(c *Context) {
|
||||||
c.HTML(http.StatusOK, "hello.tmpl", map[string]string{"name": "world"})
|
c.HTML(http.StatusOK, "hello.tmpl", map[string]string{"name": "world"})
|
||||||
})
|
})
|
||||||
|
router.GET("/raw", func(c *Context) {
|
||||||
|
c.HTML(http.StatusOK, "raw.tmpl", map[string]interface{}{
|
||||||
|
"now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),
|
||||||
|
})
|
||||||
|
})
|
||||||
router.Run(":8888")
|
router.Run(":8888")
|
||||||
}()
|
}()
|
||||||
t.Log("waiting 1 second for server startup")
|
t.Log("waiting 1 second for server startup")
|
||||||
|
@ -59,6 +74,20 @@ func TestLoadHTMLGlob(t *testing.T) {
|
||||||
td()
|
td()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLoadHTMLFromFuncMap(t *testing.T) {
|
||||||
|
time.Now()
|
||||||
|
td := setupHTMLGlob(t)
|
||||||
|
res, err := http.Get("http://127.0.0.1:8888/raw")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, _ := ioutil.ReadAll(res.Body)
|
||||||
|
assert.Equal(t, "Date: 2017/07/01\n", string(resp[:]))
|
||||||
|
|
||||||
|
td()
|
||||||
|
}
|
||||||
|
|
||||||
// func (engine *Engine) LoadHTMLFiles(files ...string) {
|
// func (engine *Engine) LoadHTMLFiles(files ...string) {
|
||||||
// func (engine *Engine) RunTLS(addr string, cert string, key string) error {
|
// func (engine *Engine) RunTLS(addr string, cert string, key string) error {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue