gas/ui
ui/uitest
import "github.com/gasmod/gas/ui/uitest"Package uitest holds a mock of ui.Provider, MockUI, for tests of code that renders pages.
The mock records each call, and returns what the function in the field named after the method returns:
m := &uitest.MockUI{ RenderFn: func(_ context.Context, w http.ResponseWriter, name string, _ any, _ ...ui.RenderOption) error { _, err := io.WriteString(w, "<h1>"+name+"</h1>") return err },}err := m.Render(ctx, w, "home", nil, ui.WithStatus(http.StatusAccepted))// w holds <h1>home</h1>, err is nil, and m.CallCount("Render") is 1The mock keeps none of the rules of the ui package: it checks no name or option and compiles nothing. To test against those rules, build a ui.Service over a gas/template/memory store.
Defaults
Section titled “Defaults”With a field nil, its method writes nothing to the response writer and returns nil:
- Render and RenderFragment render nothing.
- RegisterFuncs accepts every map, nil and invalid functions included.
Each method appends a Call with its name and its arguments after the context and the response writer, before it calls its field. The arguments recorded:
- Render and RenderFragment: the name, the data and the options. The data is recorded as given. The options are resolved by ui.ApplyRenderOptions into a ui.RenderOptions.
- RegisterFuncs: a copy of the function map, nil for nil.
To read the record:
- Calls returns a copy of the calls in the order they were made, with copies of the function maps, so that changing what it returns changes no recorded call.
- 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.
API reference
Section titled “API reference”type Call
Section titled “type Call”type Call struct { // Method is the name of the method called. Method string // Args are the method's arguments after the context and the response // writer, in order. Args []any}Call is one call of the mock’s method.
type MockUI
Section titled “type MockUI”type MockUI struct { // RenderFn is what Render returns. When it is nil, Render writes // nothing and returns nil. RenderFn func(ctx context.Context, w http.ResponseWriter, name string, data any, opts ...ui.RenderOption) error // RenderFragmentFn is what RenderFragment returns. When it is nil, // RenderFragment writes nothing and returns nil. RenderFragmentFn func( ctx context.Context, w http.ResponseWriter, name string, data any, opts ...ui.RenderOption, ) error // RegisterFuncsFn is what RegisterFuncs returns. When it is nil, // RegisterFuncs returns nil. RegisterFuncsFn func(funcs template.FuncMap) error // contains filtered or unexported fields}MockUI is a ui.Provider that records its calls, as described in the package documentation.
func (*MockUI) CallCount
Section titled “func (*MockUI) CallCount”func (m *MockUI) CallCount(method string) intCallCount returns how many calls were made to method.
func (*MockUI) Calls
Section titled “func (*MockUI) Calls”func (m *MockUI) Calls() []CallCalls returns a copy of the calls, in the order they were made: changing a function map it returns changes no recorded call.
func (*MockUI) RegisterFuncs
Section titled “func (*MockUI) RegisterFuncs”func (m *MockUI) RegisterFuncs(funcs template.FuncMap) errorRegisterFuncs records the call and returns RegisterFuncsFn’s result.
func (*MockUI) Render
Section titled “func (*MockUI) Render”func (m *MockUI) Render( ctx context.Context, w http.ResponseWriter, name string, data any, opts ...ui.RenderOption,) errorRender records the call and returns RenderFn’s result.
func (*MockUI) RenderFragment
Section titled “func (*MockUI) RenderFragment”func (m *MockUI) RenderFragment( ctx context.Context, w http.ResponseWriter, name string, data any, opts ...ui.RenderOption,) errorRenderFragment records the call and returns RenderFragmentFn’s result.
func (*MockUI) Reset
Section titled “func (*MockUI) Reset”func (m *MockUI) Reset()Reset forgets every call.