zk/internal/core/template_test.go
Mickaël Menu 50855154e2
Architecture (#27)
* Move everything under the internal package.
* Better separation between core and adapter packages, for easier unit testing.
* Simplify data models.
* Support multiple opened notebooks during runtime (useful for the LSP server).
* Proper surface API which might be exposed later as a public Go package.
2021-04-14 20:14:01 +02:00

84 lines
2.0 KiB
Go

package core
// templateLoaderMock implements an in-memory TemplateLoader for testing.
type templateLoaderMock struct {
templates map[string]*templateSpy
fileTemplates map[string]*templateSpy
styler Styler
}
func newTemplateLoaderMock() *templateLoaderMock {
return &templateLoaderMock{
templates: map[string]*templateSpy{},
fileTemplates: map[string]*templateSpy{},
styler: &stylerMock{},
}
}
func (m *templateLoaderMock) Spy(template string, result func(context interface{}) string) *templateSpy {
spy := newTemplateSpy(result)
spy.styler = m.styler
m.templates[template] = spy
return spy
}
func (m *templateLoaderMock) SpyString(content string) *templateSpy {
spy := newTemplateSpyString(content)
spy.styler = m.styler
m.templates[content] = spy
return spy
}
func (m *templateLoaderMock) SpyFile(path string, content string) *templateSpy {
spy := newTemplateSpyString(content)
spy.styler = m.styler
m.fileTemplates[path] = spy
return spy
}
func (l *templateLoaderMock) LoadTemplate(template string) (Template, error) {
tpl, ok := l.templates[template]
if !ok {
panic("no template spy for content: " + template)
}
return tpl, nil
}
func (l *templateLoaderMock) LoadTemplateAt(path string) (Template, error) {
tpl, ok := l.fileTemplates[path]
if !ok {
panic("no template spy for path: " + path)
}
return tpl, nil
}
// templateSpy implements Template and saves the provided render contexts.
type templateSpy struct {
Result func(interface{}) string
Contexts []interface{}
styler Styler
}
func newTemplateSpy(result func(interface{}) string) *templateSpy {
return &templateSpy{
Contexts: make([]interface{}, 0),
Result: result,
}
}
func newTemplateSpyString(result string) *templateSpy {
return &templateSpy{
Contexts: make([]interface{}, 0),
Result: func(_ interface{}) string { return result },
}
}
func (m *templateSpy) Styler() Styler {
return m.styler
}
func (m *templateSpy) Render(context interface{}) (string, error) {
m.Contexts = append(m.Contexts, context)
return m.Result(context), nil
}