Templates

How to test

On panic, templates render as an empty string. Thus to test if a gos template is rendering, the result must be compared with an empty string, if the two are similar, fail the test.

Which function to render with?

A template abstraction exports a function to render itself with one parameter. The parameter has the type specified in the template’s struct attribute. The function will have the name specified in the template’s name attribute.

Sample GXML :

The GXML code below will declare a template named Foo that uses data type types.MyType.

<gos>
	...
	<template name="Foo" path="bar" struct="types.MyType" />
	...
</gos>

Sample test

The Go code below is a test of template Foo.

package templates

import (
	"my/project/app/types"
)

func TestFoo(t *testing.T) {
	
	// render template
	res := Foo(types.MyType{})

	// check if empty
	if res == "" {
		t.Error("Empty string...")
	}
}