func TestTemplates(t *testing.T) { // Add a global to the default set pongo2.Globals.Set("this_is_a_global_variable", "this is a global text") 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) tpl, err := pongo2.FromFile(match) if err != nil { t.Fatalf("Error on FromFile('%s'): %s", match, err.Error()) } testFilename := fmt.Sprintf("%s.out", match) testOut, rerr := ioutil.ReadFile(testFilename) if rerr != nil { t.Fatalf("Error on ReadFile('%s'): %s", testFilename, rerr.Error()) } tplOut, err := tpl.ExecuteBytes(tplContext) if err != nil { t.Fatalf("Error on Execute('%s'): %s", match, err.Error()) } 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) if err != nil { t.Fatalf(err.Error()) } t.Logf("get a complete diff with command: 'diff -ya %s %s'", testFilename, errFilename) t.Errorf("Failed: test_out != tpl_out for %s", match) } } }
func BenchmarkExecuteComplexWithSandboxActive(b *testing.B) { tpl, err := pongo2.FromFile("template_tests/complex.tpl") if err != nil { b.Fatal(err) } b.ResetTimer() for i := 0; i < b.N; i++ { err = tpl.ExecuteWriterUnbuffered(tplContext, ioutil.Discard) if err != nil { b.Fatal(err) } } }
func BenchmarkParallelExecuteComplexWithSandboxActive(b *testing.B) { tpl, err := pongo2.FromFile("template_tests/complex.tpl") if err != nil { b.Fatal(err) } b.ResetTimer() b.RunParallel(func(pb *testing.PB) { for pb.Next() { err := tpl.ExecuteWriterUnbuffered(tplContext, ioutil.Discard) if err != nil { b.Fatal(err) } } }) }