Skip to content
Gasv0.10.0

gas/template

template/templatetest

import "github.com/gasmod/gas/template/templatetest"

Package templatetest holds a mock of gas.TemplateProvider, MockTemplate, for tests of code that reads or registers templates.

The mock records each call, and returns what the function in the field named after the method returns:

m := &templatetest.MockTemplate{
GetFn: func(context.Context, string) ([]byte, error) {
return []byte("<p>hi</p>"), nil
},
}
b, err := m.Get(ctx, "home.html")
// b is <p>hi</p>, err is nil, and m.CallCount("Get") is 1

With a field nil, its method behaves as an empty store:

  • Get returns an error wrapping template.ErrTemplateNotFound, template: not found: and the quoted name.
  • List returns an empty, non-nil list.
  • Register and RegisterFS return nil.

Each method appends a Call with its name and its arguments after the context, before it calls its field. The arguments recorded:

  • Get: the name.
  • List: none.
  • Register: the name and the content. The content is recorded as a copy, nil for nil, so that a caller that reuses its slice does not change a recorded call.
  • RegisterFS: the fs.FS.

To read the record:

  • Calls returns a copy of the calls in the order they were made.
  • CallCount counts those of one method.
  • Reset forgets them all.

The methods may be called from several goroutines at once, as the calls are kept under a lock, which Calls, CallCount and Reset take too. Setting a field while a method runs is a data race.

type Call struct {
// Method is the name of the method called.
Method string
// Args are the method's arguments after the context, in order.
Args []any
}

Call is one call of the mock’s method.

type MockTemplate struct {
// GetFn is what Get returns. When it is nil, Get returns an error
// wrapping template.ErrTemplateNotFound.
GetFn func(ctx context.Context, name string) ([]byte, error)
// ListFn is what List returns. When it is nil, List returns an empty
// list.
ListFn func(ctx context.Context) ([]string, error)
// RegisterFn is what Register returns. When it is nil, Register
// returns nil.
RegisterFn func(ctx context.Context, name string, content []byte) error
// RegisterFSFn is what RegisterFS returns. When it is nil, RegisterFS
// returns nil.
RegisterFSFn func(ctx context.Context, fsys fs.FS) error
// contains filtered or unexported fields
}

MockTemplate is a gas.TemplateProvider that records its calls, as described in the package documentation.

func (m *MockTemplate) CallCount(method string) int

CallCount returns how many calls were made to method.

func (m *MockTemplate) Calls() []Call

Calls returns a copy of the calls, in the order they were made.

func (m *MockTemplate) Get(ctx context.Context, name string) ([]byte, error)

Get records the call and returns GetFn’s result.

func (m *MockTemplate) List(ctx context.Context) ([]string, error)

List records the call and returns ListFn’s result.

func (m *MockTemplate) Register(ctx context.Context, name string, content []byte) error

Register records the call and returns RegisterFn’s result.

func (m *MockTemplate) RegisterFS(ctx context.Context, fsys fs.FS) error

RegisterFS records the call and returns RegisterFSFn’s result.

func (m *MockTemplate) Reset()

Reset forgets every call.