templatetest

package
v0.5.5 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2022 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package templatetest exposes a mock fs.FS that implements basic file operations. Used in unit tests for the purposes of avoiding the use of testdata/ directories when unit testing template rendering.

Cribbed from Mark Bates: https://www.gopherguides.com/articles/golang-1.16-io-fs-improve-test-performance

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMockFS

func NewMockFS(tmpls ...FileMocker) fs.FS

func NewParser

func NewParser(tmpls ...FileMocker) template.Parser

NewParser constructs a template.Parser with the mocked files.

Types

type FileMocker

type FileMocker interface {
	fs.File
	fs.FileInfo
}

func NewMockFile

func NewMockFile(name string, data []byte) FileMocker

type MockFS

type MockFS []FileMocker

func (MockFS) Glob

func (mfs MockFS) Glob(pattern string) ([]string, error)

Glob checks whether the pattern matches the file after removing all directory paths from the respective parts.

Buyer beware: Glob is a simplistic implementation of fs.GlobFS.

i.e., pattern: some/long/path/* will match all of the following - some/long/path/myfile.txt - some/long/otherfile.txt - totally/different/tree/somefile.txt - /rootfile.txt ... etc.

func (MockFS) Open

func (mfs MockFS) Open(name string) (fs.File, error)

type MockFile

type MockFile struct {
	FS MockFS
	// contains filtered or unexported fields
}

func (*MockFile) Close

func (m *MockFile) Close() error

func (*MockFile) Info

func (m *MockFile) Info() (fs.FileInfo, error)

func (*MockFile) IsDir

func (m *MockFile) IsDir() bool

func (*MockFile) ModTime

func (m *MockFile) ModTime() time.Time

func (*MockFile) Mode

func (m *MockFile) Mode() os.FileMode

func (*MockFile) Name

func (m *MockFile) Name() string

func (*MockFile) Read

func (m *MockFile) Read(p []byte) (int, error)

func (*MockFile) Size

func (m *MockFile) Size() int64

func (*MockFile) Stat

func (m *MockFile) Stat() (fs.FileInfo, error)

func (*MockFile) Sys

func (m *MockFile) Sys() any

func (*MockFile) Type

func (m *MockFile) Type() fs.FileMode

Jump to

Keyboard shortcuts

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