I'm trying to test a template generation tool. In order to do this I was thinking that the easiest way to capture the template execution output is to use a io writer and provide it during testing. The issue is that for some reasons the receiver is not "updated" with the template output. Hopefully the code below explains better the issue I'm facing.
package main
import "fmt"
import "text/template"
type Company struct{
Name string
}
type Companies []Company
func main() {
s := new(stringer)
v := Companies{Company{Name:"Sony"}}
tmp := template.Must(template.New("main").Parse(src))
if err := tmp.Execute(s, v); err !=nil{
panic(err)
}
if *s != "this is the header template"{
fmt.Println("expected: \"this is the header template\" received: ", *s)
}else{
fmt.Println("s is %v", *s)
}
}
type stringer string
func (s *stringer)Write(b []byte)(int, error){
*s = stringer(b)
return len(b), nil
}
var src = `
this is the header template
{{range .}}
{{.Name}}
{{end}}
`