plate

package module
v0.0.0-...-4008059 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2015 License: MIT Imports: 3 Imported by: 0

README

Plate

Build Status GoDoc

Plate is a wrapper for Go's html/template (or text/template) that helps you test your template execution.

Plate makes it easier to check if the correct template has been rendered, whether the correct context has been passed to it and catch errors that occur.

Usage

First, rewrite your template variables to have the type plate.Executor. In short, transform this:

var tmpl *template.Template
// or
var templates map[string]*template.Template

To this:

var tmpl plate.Executor
// or
var templates map[string]plate.Executor

Your templates will implement plate.Executor automatically. Then, in your tests, wrap your template in a plate.Recorder:

recorder := plate.New(tmpl)
tmpl = recorder
// or
recorder := plate.New(tmpl)
templates["index.html"] = recorder

The template will execute as before, except for one thing: the recorder will accumulate the result of all executions: the output that template produced, the context passed to it and an error returned from an Execute*() call.

That information can be checked later to find out any faults in the execution of your template.

tmpl.Execute(...)
if recorder.LastExecution().Context == nil {
    t.Error("A nil context has been passed to the template")
}

Documentation

Overview

Package plate provides a Recorder for testing Go's HTML templates.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Execution

type Execution struct {
	Output  []byte
	Context interface{}

	Error error
}

Execution represents one occurence of template being executed. It provides access to the output produced, the context that was passed to the template and the error returned from the Execute*() function, if any.

type Executor

type Executor interface {
	Execute(wr io.Writer, data interface{}) error
	ExecuteTemplate(wr io.Writer, name string, data interface{}) error
}

Executor is an interface comprised of the metods that html/template and text/template use to render themselves. Thus any *template.Template implements Executor automatically.

type Recorder

type Recorder struct {
	// The original template to wrap.
	Template Executor
	// contains filtered or unexported fields
}

Recorder wraps an Executor and records results of executions for later checks.

func New

func New(tmpl Executor) *Recorder

New() returns a new Recorder wrapping the given template. It is equivalent to:

&Recorder{Template: tmpl}

func (*Recorder) Execute

func (r *Recorder) Execute(wr io.Writer, data interface{}) error

Execute executes the wrapped template, saving information into the Recorder.

func (*Recorder) ExecuteTemplate

func (r *Recorder) ExecuteTemplate(wr io.Writer, name string, data interface{}) error

ExecuteTemplate is like Execute, but for named teplates.

func (*Recorder) Executions

func (r *Recorder) Executions() []Execution

Executions returns all executions that have occured since the construction of a Recorder (or since Reset()).

func (*Recorder) FailedExecutions

func (r *Recorder) FailedExecutions() []Execution

FailedExecutions returns all executions that have Error != nil

func (*Recorder) LastExecution

func (r *Recorder) LastExecution() Execution

LastExecution returns the information of the latest execution. LastExecution PANICS if no executions have occured yet.

func (*Recorder) Reset

func (r *Recorder) Reset()

Reset clears all executions. Recorder is thus restored to its initial state.

func (*Recorder) TimesExecuted

func (r *Recorder) TimesExecuted() int

TimesExecuted returns the count of times template has been executed since construction or calling Reset().

Directories

Path Synopsis
examples
webapp
Package webapp demonstrates the use of plate in detail.
Package webapp demonstrates the use of plate in detail.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL