2017-12-28 01:56:23 +00:00
package pongo2_test
2015-07-30 15:43:22 +00:00
import (
"testing"
2017-12-28 01:56:23 +00:00
"github.com/flosch/pongo2"
2015-07-30 15:43:22 +00:00
. "gopkg.in/check.v1"
)
// Hook up gocheck into the "go test" runner.
func Test ( t * testing . T ) { TestingT ( t ) }
type TestSuite struct {
2017-12-28 01:56:23 +00:00
tpl * pongo2 . Template
2015-07-30 15:43:22 +00:00
}
var (
2017-12-28 01:56:23 +00:00
_ = Suite ( & TestSuite { } )
testSuite2 = pongo2 . NewSet ( "test suite 2" , pongo2 . MustNewLocalFileSystemLoader ( "" ) )
2015-07-30 15:43:22 +00:00
)
2017-12-28 01:56:23 +00:00
func parseTemplate ( s string , c pongo2 . Context ) string {
t , err := testSuite2 . FromString ( s )
2015-07-30 15:43:22 +00:00
if err != nil {
panic ( err )
}
out , err := t . Execute ( c )
if err != nil {
panic ( err )
}
return out
}
2017-12-28 01:56:23 +00:00
func parseTemplateFn ( s string , c pongo2 . Context ) func ( ) {
2015-07-30 15:43:22 +00:00
return func ( ) {
parseTemplate ( s , c )
}
}
func ( s * TestSuite ) TestMisc ( c * C ) {
// Must
// TODO: Add better error message (see issue #18)
2017-12-28 01:56:23 +00:00
c . Check (
func ( ) { pongo2 . Must ( testSuite2 . FromFile ( "template_tests/inheritance/base2.tpl" ) ) } ,
2015-07-30 15:43:22 +00:00
PanicMatches ,
2017-12-28 01:56:23 +00:00
` \[Error \(where: fromfile\) in .*template_tests/inheritance/doesnotexist.tpl | Line 1 Col 12 near 'doesnotexist.tpl'\] open .*template_tests/inheritance/doesnotexist.tpl: no such file or directory ` ,
)
2015-07-30 15:43:22 +00:00
// Context
2017-12-28 01:56:23 +00:00
c . Check ( parseTemplateFn ( "" , pongo2 . Context { "'illegal" : nil } ) , PanicMatches , ".*not a valid identifier.*" )
2015-07-30 15:43:22 +00:00
// Registers
2017-12-28 01:56:23 +00:00
c . Check ( pongo2 . RegisterFilter ( "escape" , nil ) . Error ( ) , Matches , ".*is already registered" )
c . Check ( pongo2 . RegisterTag ( "for" , nil ) . Error ( ) , Matches , ".*is already registered" )
2015-07-30 15:43:22 +00:00
// ApplyFilter
2017-12-28 01:56:23 +00:00
v , err := pongo2 . ApplyFilter ( "title" , pongo2 . AsValue ( "this is a title" ) , nil )
2015-07-30 15:43:22 +00:00
if err != nil {
c . Fatal ( err )
}
c . Check ( v . String ( ) , Equals , "This Is A Title" )
c . Check ( func ( ) {
2017-12-28 01:56:23 +00:00
_ , err := pongo2 . ApplyFilter ( "doesnotexist" , nil , nil )
2015-07-30 15:43:22 +00:00
if err != nil {
panic ( err )
}
} , PanicMatches , ` \[Error \(where: applyfilter\)\] Filter with name 'doesnotexist' not found. ` )
}
2017-12-28 01:56:23 +00:00
func ( s * TestSuite ) TestImplicitExecCtx ( c * C ) {
tpl , err := pongo2 . FromString ( "{{ ImplicitExec }}" )
if err != nil {
c . Fatalf ( "Error in FromString: %v" , err )
}
val := "a stringy thing"
res , err := tpl . Execute ( pongo2 . Context {
"Value" : val ,
"ImplicitExec" : func ( ctx * pongo2 . ExecutionContext ) string {
return ctx . Public [ "Value" ] . ( string )
} ,
} )
if err != nil {
c . Fatalf ( "Error executing template: %v" , err )
}
c . Check ( res , Equals , val )
// The implicit ctx should not be persisted from call-to-call
res , err = tpl . Execute ( pongo2 . Context {
"ImplicitExec" : func ( ) string {
return val
} ,
} )
if err != nil {
c . Fatalf ( "Error executing template: %v" , err )
}
c . Check ( res , Equals , val )
}