package raymond import ( "fmt" "testing" ) var sourceBasic = `

{{title}}

{{body}}
` var basicAST = `CONTENT[ '

' ] {{ PATH:title [] }} CONTENT[ '

' ] {{ PATH:body [] }} CONTENT[ '
' ] ` func TestNewTemplate(t *testing.T) { t.Parallel() tpl := newTemplate(sourceBasic) if tpl.source != sourceBasic { t.Errorf("Failed to instantiate template") } } func TestParse(t *testing.T) { t.Parallel() tpl, err := Parse(sourceBasic) if err != nil || (tpl.source != sourceBasic) { t.Errorf("Failed to parse template") } if str := tpl.PrintAST(); str != basicAST { t.Errorf("Template parsing incorrect: %s", str) } } func TestClone(t *testing.T) { t.Parallel() sourcePartial := `I am a {{wat}} partial` sourcePartial2 := `Partial for the {{wat}}` tpl := MustParse(sourceBasic) tpl.RegisterPartial("p", sourcePartial) if (len(tpl.partials) != 1) || (tpl.partials["p"] == nil) { t.Errorf("What?") } cloned := tpl.Clone() if (len(cloned.partials) != 1) || (cloned.partials["p"] == nil) { t.Errorf("Template partials must be cloned") } cloned.RegisterPartial("p2", sourcePartial2) if (len(cloned.partials) != 2) || (cloned.partials["p"] == nil) || (cloned.partials["p2"] == nil) { t.Errorf("Failed to register a partial on cloned template") } if (len(tpl.partials) != 1) || (tpl.partials["p"] == nil) { t.Errorf("Modification of a cloned template MUST NOT affect original template") } } func ExampleTemplate_Exec() { source := "

{{title}}

{{body.content}}

" ctx := map[string]interface{}{ "title": "foo", "body": map[string]string{"content": "bar"}, } // parse template tpl := MustParse(source) // evaluate template with context output, err := tpl.Exec(ctx) if err != nil { panic(err) } fmt.Print(output) // Output:

foo

bar

} func ExampleTemplate_MustExec() { source := "

{{title}}

{{body.content}}

" ctx := map[string]interface{}{ "title": "foo", "body": map[string]string{"content": "bar"}, } // parse template tpl := MustParse(source) // evaluate template with context output := tpl.MustExec(ctx) fmt.Print(output) // Output:

foo

bar

} func ExampleTemplate_ExecWith() { source := "

{{title}}

{{#body}}{{content}} and {{@baz.bat}}{{/body}}

" ctx := map[string]interface{}{ "title": "foo", "body": map[string]string{"content": "bar"}, } // parse template tpl := MustParse(source) // computes private data frame frame := NewDataFrame() frame.Set("baz", map[string]string{"bat": "unicorns"}) // evaluate template output, err := tpl.ExecWith(ctx, frame) if err != nil { panic(err) } fmt.Print(output) // Output:

foo

bar and unicorns

} func ExampleTemplate_PrintAST() { source := "

{{title}}

{{#body}}{{content}} and {{@baz.bat}}{{/body}}

" // parse template tpl := MustParse(source) // print AST output := tpl.PrintAST() fmt.Print(output) // Output: CONTENT[ '

' ] // {{ PATH:title [] }} // CONTENT[ '

' ] // BLOCK: // PATH:body [] // PROGRAM: // {{ PATH:content [] // }} // CONTENT[ ' and ' ] // {{ @PATH:baz/bat [] // }} // CONTENT[ '

' ] // }