1
0
mirror of https://github.com/Luzifer/cloudkeys-go.git synced 2024-09-19 23:52:57 +00:00
cloudkeys-go/vendor/github.com/flosch/pongo2/pongo2_template_test.go

535 lines
13 KiB
Go
Raw Normal View History

2017-12-28 01:56:23 +00:00
package pongo2_test
2015-07-30 15:43:22 +00:00
import (
"bytes"
"fmt"
"io/ioutil"
"path/filepath"
"regexp"
"strings"
"testing"
"time"
2017-12-28 01:56:23 +00:00
"github.com/flosch/pongo2"
2015-07-30 15:43:22 +00:00
)
2017-12-28 01:56:23 +00:00
var adminList = []string{"user2"}
2015-07-30 15:43:22 +00:00
var time1 = time.Date(2014, 06, 10, 15, 30, 15, 0, time.UTC)
var time2 = time.Date(2011, 03, 21, 8, 37, 56, 12, time.UTC)
type post struct {
Text string
Created time.Time
}
type user struct {
Name string
Validated bool
}
type comment struct {
Author *user
Date time.Time
Text string
}
2017-12-28 01:56:23 +00:00
func isAdmin(u *user) bool {
for _, a := range adminList {
2015-07-30 15:43:22 +00:00
if a == u.Name {
return true
}
}
return false
}
2017-12-28 01:56:23 +00:00
func (u *user) Is_admin() *pongo2.Value {
return pongo2.AsValue(isAdmin(u))
2015-07-30 15:43:22 +00:00
}
func (u *user) Is_admin2() bool {
2017-12-28 01:56:23 +00:00
return isAdmin(u)
2015-07-30 15:43:22 +00:00
}
func (p *post) String() string {
return ":-)"
}
/*
* Start setup sandbox
*/
type tagSandboxDemoTag struct {
}
2017-12-28 01:56:23 +00:00
func (node *tagSandboxDemoTag) Execute(ctx *pongo2.ExecutionContext, writer pongo2.TemplateWriter) *pongo2.Error {
writer.WriteString("hello")
2015-07-30 15:43:22 +00:00
return nil
}
2017-12-28 01:56:23 +00:00
func tagSandboxDemoTagParser(doc *pongo2.Parser, start *pongo2.Token, arguments *pongo2.Parser) (pongo2.INodeTag, *pongo2.Error) {
2015-07-30 15:43:22 +00:00
return &tagSandboxDemoTag{}, nil
}
2017-12-28 01:56:23 +00:00
func BannedFilterFn(in *pongo2.Value, params *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
2015-07-30 15:43:22 +00:00
return in, nil
}
func init() {
2017-12-28 01:56:23 +00:00
pongo2.DefaultSet.Debug = true
2015-07-30 15:43:22 +00:00
2017-12-28 01:56:23 +00:00
pongo2.RegisterFilter("banned_filter", BannedFilterFn)
pongo2.RegisterFilter("unbanned_filter", BannedFilterFn)
pongo2.RegisterTag("banned_tag", tagSandboxDemoTagParser)
pongo2.RegisterTag("unbanned_tag", tagSandboxDemoTagParser)
2015-07-30 15:43:22 +00:00
2017-12-28 01:56:23 +00:00
pongo2.DefaultSet.BanFilter("banned_filter")
pongo2.DefaultSet.BanTag("banned_tag")
2015-07-30 15:43:22 +00:00
f, err := ioutil.TempFile("/tmp/", "pongo2_")
if err != nil {
panic("cannot write to /tmp/")
}
f.Write([]byte("Hello from pongo2"))
2017-12-28 01:56:23 +00:00
pongo2.DefaultSet.Globals["temp_file"] = f.Name()
2015-07-30 15:43:22 +00:00
}
/*
* End setup sandbox
*/
2017-12-28 01:56:23 +00:00
var tplContext = pongo2.Context{
2015-07-30 15:43:22 +00:00
"number": 11,
"simple": map[string]interface{}{
2017-12-28 01:56:23 +00:00
"number": 42,
"name": "john doe",
"included_file": "INCLUDES.helper",
"included_file_not_exists": "INCLUDES.helper.not_exists",
"nil": nil,
"uint": uint(8),
"float": float64(3.1415),
"str": "string",
2015-07-30 15:43:22 +00:00
"chinese_hello_world": "你好世界",
"bool_true": true,
"bool_false": false,
"newline_text": `this is a text
with a new line in it`,
"long_text": `This is a simple text.
This too, as a paragraph.
Right?
Yep!`,
"escape_js_test": `escape sequences \r\n\'\" special chars "?!=$<>`,
"one_item_list": []int{99},
"multiple_item_list": []int{1, 1, 2, 3, 5, 8, 13, 21, 34, 55},
2017-12-28 01:56:23 +00:00
"unsorted_int_list": []int{192, 581, 22, 1, 249, 9999, 1828591, 8271},
"fixed_item_list": [...]int{1, 2, 3, 4},
2015-07-30 15:43:22 +00:00
"misc_list": []interface{}{"Hello", 99, 3.14, "good"},
"escape_text": "This is \\a Test. \"Yep\". 'Yep'.",
"xss": "<script>alert(\"uh oh\");</script>",
"intmap": map[int]string{
1: "one",
5: "five",
2017-12-28 01:56:23 +00:00
2: "two",
},
"strmap": map[string]string{
"abc": "def",
"bcd": "efg",
"zab": "cde",
"gh": "kqm",
"ukq": "qqa",
"aab": "aba",
2015-07-30 15:43:22 +00:00
},
"func_add": func(a, b int) int {
return a + b
},
"func_add_iface": func(a, b interface{}) interface{} {
return a.(int) + b.(int)
},
"func_variadic": func(msg string, args ...interface{}) string {
return fmt.Sprintf(msg, args...)
},
"func_variadic_sum_int": func(args ...int) int {
// Create a sum
s := 0
for _, i := range args {
s += i
}
return s
},
2017-12-28 01:56:23 +00:00
"func_variadic_sum_int2": func(args ...*pongo2.Value) *pongo2.Value {
2015-07-30 15:43:22 +00:00
// Create a sum
s := 0
for _, i := range args {
s += i.Integer()
}
2017-12-28 01:56:23 +00:00
return pongo2.AsValue(s)
2015-07-30 15:43:22 +00:00
},
},
"complex": map[string]interface{}{
2017-12-28 01:56:23 +00:00
"is_admin": isAdmin,
2015-07-30 15:43:22 +00:00
"post": post{
Text: "<h2>Hello!</h2><p>Welcome to my new blog page. I'm using pongo2 which supports {{ variables }} and {% tags %}.</p>",
Created: time2,
},
"comments": []*comment{
&comment{
Author: &user{
Name: "user1",
Validated: true,
},
Date: time1,
Text: "\"pongo2 is nice!\"",
},
&comment{
Author: &user{
Name: "user2",
Validated: true,
},
Date: time2,
Text: "comment2 with <script>unsafe</script> tags in it",
},
&comment{
Author: &user{
Name: "user3",
Validated: false,
},
Date: time1,
Text: "<b>hello!</b> there",
},
},
"comments2": []*comment{
&comment{
Author: &user{
Name: "user1",
Validated: true,
},
Date: time2,
Text: "\"pongo2 is nice!\"",
},
&comment{
Author: &user{
Name: "user1",
Validated: true,
},
Date: time1,
Text: "comment2 with <script>unsafe</script> tags in it",
},
&comment{
Author: &user{
Name: "user3",
Validated: false,
},
Date: time1,
Text: "<b>hello!</b> there",
},
},
},
}
func TestTemplates(t *testing.T) {
// Add a global to the default set
2017-12-28 01:56:23 +00:00
pongo2.Globals["this_is_a_global_variable"] = "this is a global text"
2015-07-30 15:43:22 +00:00
matches, err := filepath.Glob("./template_tests/*.tpl")
if err != nil {
t.Fatal(err)
}
for idx, match := range matches {
t.Logf("[Template %3d] Testing '%s'", idx+1, match)
2017-12-28 01:56:23 +00:00
tpl, err := pongo2.FromFile(match)
2015-07-30 15:43:22 +00:00
if err != nil {
t.Fatalf("Error on FromFile('%s'): %s", match, err.Error())
}
2017-12-28 01:56:23 +00:00
testFilename := fmt.Sprintf("%s.out", match)
testOut, rerr := ioutil.ReadFile(testFilename)
2015-07-30 15:43:22 +00:00
if rerr != nil {
2017-12-28 01:56:23 +00:00
t.Fatalf("Error on ReadFile('%s'): %s", testFilename, rerr.Error())
2015-07-30 15:43:22 +00:00
}
2017-12-28 01:56:23 +00:00
tplOut, err := tpl.ExecuteBytes(tplContext)
2015-07-30 15:43:22 +00:00
if err != nil {
t.Fatalf("Error on Execute('%s'): %s", match, err.Error())
}
2017-12-28 01:56:23 +00:00
if bytes.Compare(testOut, tplOut) != 0 {
t.Logf("Template (rendered) '%s': '%s'", match, tplOut)
errFilename := filepath.Base(fmt.Sprintf("%s.error", match))
err := ioutil.WriteFile(errFilename, []byte(tplOut), 0600)
2015-07-30 15:43:22 +00:00
if err != nil {
t.Fatalf(err.Error())
}
2017-12-28 01:56:23 +00:00
t.Logf("get a complete diff with command: 'diff -ya %s %s'", testFilename, errFilename)
2015-07-30 15:43:22 +00:00
t.Errorf("Failed: test_out != tpl_out for %s", match)
}
}
}
func TestExecutionErrors(t *testing.T) {
2017-12-28 01:56:23 +00:00
//debug = true
2015-07-30 15:43:22 +00:00
matches, err := filepath.Glob("./template_tests/*-execution.err")
if err != nil {
t.Fatal(err)
}
for idx, match := range matches {
t.Logf("[Errors %3d] Testing '%s'", idx+1, match)
2017-12-28 01:56:23 +00:00
testData, err := ioutil.ReadFile(match)
tests := strings.Split(string(testData), "\n")
2015-07-30 15:43:22 +00:00
2017-12-28 01:56:23 +00:00
checkFilename := fmt.Sprintf("%s.out", match)
checkData, err := ioutil.ReadFile(checkFilename)
2015-07-30 15:43:22 +00:00
if err != nil {
2017-12-28 01:56:23 +00:00
t.Fatalf("Error on ReadFile('%s'): %s", checkFilename, err.Error())
2015-07-30 15:43:22 +00:00
}
2017-12-28 01:56:23 +00:00
checks := strings.Split(string(checkData), "\n")
2015-07-30 15:43:22 +00:00
if len(checks) != len(tests) {
t.Fatal("Template lines != Checks lines")
}
for idx, test := range tests {
if strings.TrimSpace(test) == "" {
continue
}
if strings.TrimSpace(checks[idx]) == "" {
t.Fatalf("[%s Line %d] Check is empty (must contain an regular expression).",
match, idx+1)
}
2017-12-28 01:56:23 +00:00
tpl, err := pongo2.FromString(test)
2015-07-30 15:43:22 +00:00
if err != nil {
t.Fatalf("Error on FromString('%s'): %s", test, err.Error())
}
2017-12-28 01:56:23 +00:00
tpl, err = pongo2.FromBytes([]byte(test))
if err != nil {
t.Fatalf("Error on FromBytes('%s'): %s", test, err.Error())
}
2015-07-30 15:43:22 +00:00
_, err = tpl.ExecuteBytes(tplContext)
if err == nil {
t.Fatalf("[%s Line %d] Expected error for (got none): %s",
match, idx+1, tests[idx])
}
re := regexp.MustCompile(fmt.Sprintf("^%s$", checks[idx]))
if !re.MatchString(err.Error()) {
t.Fatalf("[%s Line %d] Error for '%s' (err = '%s') does not match the (regexp-)check: %s",
match, idx+1, test, err.Error(), checks[idx])
}
}
}
}
func TestCompilationErrors(t *testing.T) {
2017-12-28 01:56:23 +00:00
//debug = true
2015-07-30 15:43:22 +00:00
matches, err := filepath.Glob("./template_tests/*-compilation.err")
if err != nil {
t.Fatal(err)
}
for idx, match := range matches {
t.Logf("[Errors %3d] Testing '%s'", idx+1, match)
2017-12-28 01:56:23 +00:00
testData, err := ioutil.ReadFile(match)
tests := strings.Split(string(testData), "\n")
2015-07-30 15:43:22 +00:00
2017-12-28 01:56:23 +00:00
checkFilename := fmt.Sprintf("%s.out", match)
checkData, err := ioutil.ReadFile(checkFilename)
2015-07-30 15:43:22 +00:00
if err != nil {
2017-12-28 01:56:23 +00:00
t.Fatalf("Error on ReadFile('%s'): %s", checkFilename, err.Error())
2015-07-30 15:43:22 +00:00
}
2017-12-28 01:56:23 +00:00
checks := strings.Split(string(checkData), "\n")
2015-07-30 15:43:22 +00:00
if len(checks) != len(tests) {
t.Fatal("Template lines != Checks lines")
}
for idx, test := range tests {
if strings.TrimSpace(test) == "" {
continue
}
if strings.TrimSpace(checks[idx]) == "" {
t.Fatalf("[%s Line %d] Check is empty (must contain an regular expression).",
match, idx+1)
}
2017-12-28 01:56:23 +00:00
_, err = pongo2.FromString(test)
2015-07-30 15:43:22 +00:00
if err == nil {
t.Fatalf("[%s | Line %d] Expected error for (got none): %s", match, idx+1, tests[idx])
}
re := regexp.MustCompile(fmt.Sprintf("^%s$", checks[idx]))
if !re.MatchString(err.Error()) {
t.Fatalf("[%s | Line %d] Error for '%s' (err = '%s') does not match the (regexp-)check: %s",
match, idx+1, test, err.Error(), checks[idx])
}
}
}
}
func TestBaseDirectory(t *testing.T) {
mustStr := "Hello from template_tests/base_dir_test/"
2017-12-28 01:56:23 +00:00
fs := pongo2.MustNewLocalFileSystemLoader("")
s := pongo2.NewSet("test set with base directory", fs)
2015-07-30 15:43:22 +00:00
s.Globals["base_directory"] = "template_tests/base_dir_test/"
2017-12-28 01:56:23 +00:00
if err := fs.SetBaseDir(s.Globals["base_directory"].(string)); err != nil {
2015-07-30 15:43:22 +00:00
t.Fatal(err)
}
matches, err := filepath.Glob("./template_tests/base_dir_test/subdir/*")
if err != nil {
t.Fatal(err)
}
for _, match := range matches {
match = strings.Replace(match, "template_tests/base_dir_test/", "", -1)
tpl, err := s.FromFile(match)
if err != nil {
t.Fatal(err)
}
out, err := tpl.Execute(nil)
if err != nil {
t.Fatal(err)
}
if out != mustStr {
t.Errorf("%s: out ('%s') != mustStr ('%s')", match, out, mustStr)
}
}
}
func BenchmarkCache(b *testing.B) {
2017-12-28 01:56:23 +00:00
cacheSet := pongo2.NewSet("cache set", pongo2.MustNewLocalFileSystemLoader(""))
2015-07-30 15:43:22 +00:00
for i := 0; i < b.N; i++ {
2017-12-28 01:56:23 +00:00
tpl, err := cacheSet.FromCache("template_tests/complex.tpl")
2015-07-30 15:43:22 +00:00
if err != nil {
b.Fatal(err)
}
2017-12-28 01:56:23 +00:00
err = tpl.ExecuteWriterUnbuffered(tplContext, ioutil.Discard)
2015-07-30 15:43:22 +00:00
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkCacheDebugOn(b *testing.B) {
2017-12-28 01:56:23 +00:00
cacheDebugSet := pongo2.NewSet("cache set", pongo2.MustNewLocalFileSystemLoader(""))
cacheDebugSet.Debug = true
2015-07-30 15:43:22 +00:00
for i := 0; i < b.N; i++ {
2017-12-28 01:56:23 +00:00
tpl, err := cacheDebugSet.FromFile("template_tests/complex.tpl")
2015-07-30 15:43:22 +00:00
if err != nil {
b.Fatal(err)
}
2017-12-28 01:56:23 +00:00
err = tpl.ExecuteWriterUnbuffered(tplContext, ioutil.Discard)
2015-07-30 15:43:22 +00:00
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkExecuteComplexWithSandboxActive(b *testing.B) {
2017-12-28 01:56:23 +00:00
tpl, err := pongo2.FromFile("template_tests/complex.tpl")
2015-07-30 15:43:22 +00:00
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
2017-12-28 01:56:23 +00:00
err = tpl.ExecuteWriterUnbuffered(tplContext, ioutil.Discard)
2015-07-30 15:43:22 +00:00
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkCompileAndExecuteComplexWithSandboxActive(b *testing.B) {
buf, err := ioutil.ReadFile("template_tests/complex.tpl")
if err != nil {
b.Fatal(err)
}
preloadedTpl := string(buf)
b.ResetTimer()
for i := 0; i < b.N; i++ {
2017-12-28 01:56:23 +00:00
tpl, err := pongo2.FromString(preloadedTpl)
2015-07-30 15:43:22 +00:00
if err != nil {
b.Fatal(err)
}
2017-12-28 01:56:23 +00:00
err = tpl.ExecuteWriterUnbuffered(tplContext, ioutil.Discard)
2015-07-30 15:43:22 +00:00
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkParallelExecuteComplexWithSandboxActive(b *testing.B) {
2017-12-28 01:56:23 +00:00
tpl, err := pongo2.FromFile("template_tests/complex.tpl")
2015-07-30 15:43:22 +00:00
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
2017-12-28 01:56:23 +00:00
err := tpl.ExecuteWriterUnbuffered(tplContext, ioutil.Discard)
2015-07-30 15:43:22 +00:00
if err != nil {
b.Fatal(err)
}
}
})
}
func BenchmarkExecuteComplexWithoutSandbox(b *testing.B) {
2017-12-28 01:56:23 +00:00
s := pongo2.NewSet("set without sandbox", pongo2.MustNewLocalFileSystemLoader(""))
2015-07-30 15:43:22 +00:00
tpl, err := s.FromFile("template_tests/complex.tpl")
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
2017-12-28 01:56:23 +00:00
err = tpl.ExecuteWriterUnbuffered(tplContext, ioutil.Discard)
2015-07-30 15:43:22 +00:00
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkCompileAndExecuteComplexWithoutSandbox(b *testing.B) {
buf, err := ioutil.ReadFile("template_tests/complex.tpl")
if err != nil {
b.Fatal(err)
}
preloadedTpl := string(buf)
2017-12-28 01:56:23 +00:00
s := pongo2.NewSet("set without sandbox", pongo2.MustNewLocalFileSystemLoader(""))
2015-07-30 15:43:22 +00:00
b.ResetTimer()
for i := 0; i < b.N; i++ {
tpl, err := s.FromString(preloadedTpl)
if err != nil {
b.Fatal(err)
}
2017-12-28 01:56:23 +00:00
err = tpl.ExecuteWriterUnbuffered(tplContext, ioutil.Discard)
2015-07-30 15:43:22 +00:00
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkParallelExecuteComplexWithoutSandbox(b *testing.B) {
2017-12-28 01:56:23 +00:00
s := pongo2.NewSet("set without sandbox", pongo2.MustNewLocalFileSystemLoader(""))
2015-07-30 15:43:22 +00:00
tpl, err := s.FromFile("template_tests/complex.tpl")
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
2017-12-28 01:56:23 +00:00
err := tpl.ExecuteWriterUnbuffered(tplContext, ioutil.Discard)
2015-07-30 15:43:22 +00:00
if err != nil {
b.Fatal(err)
}
}
})
}