filetests

package
v0.49.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package filetests houses a test harness for evaluating templates and asserting the expected output.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TrimTrailingMultilineWhitespace

func TrimTrailingMultilineWhitespace(s string) string

TrimTrailingMultilineWhitespace returns a string with trailing whitespace trimmed from every line as well as trimmed trailing empty lines

Types

type DefaultTemplateLoader

type DefaultTemplateLoader struct {
	template.NoopCompiledTemplateLoader

	CompiledTemplate *template.CompiledTemplate
	DataValues       yamlmeta.Document
}

DefaultTemplateLoader is a template.NoopCompiledTemplateLoader that allows any standard library module to be loaded (including "@ytt:data", populated with "DataValues")

func (DefaultTemplateLoader) FindCompiledTemplate

func (l DefaultTemplateLoader) FindCompiledTemplate(_ string) *template.CompiledTemplate

FindCompiledTemplate returns the compiled template that is the subject of the current executing test.

func (DefaultTemplateLoader) Load

Load provides the ability to load any module from the ytt standard library (including `@ytt:data` populated with DefaultTemplateLoader.DataValues)

type EvaluateTemplate

type EvaluateTemplate func(src string) (MarshalableResult, *TestErr)

EvaluateTemplate is the processing desired from a source template to the final result.

type FileTests

type FileTests struct {
	PathToTests      string
	EvalFunc         EvaluateTemplate
	ShowTemplateCode bool
	DataValues       yamlmeta.Document
}

FileTests contain a suite of test cases, each described in a separate file, verifying the behavior of templates.

Test cases: - are found within the directory at "PathToTests" - conventionally have a .tpltest extension - top-half is the template; bottom-half is the expected output; divided by `+++` and a blank line.

Types of template tests: - expected output starting with `ERR:` indicate that expected output is an error message - expected output starting with `OUTPUT POSITION:` indicate that expected output is "pos" format - otherwise expected output is the literal output from template

For example:

#! my-test.tpltest
---
#@ msg = "hello"
msg: #@ msg
+++

msg: hello

func (FileTests) DefaultEvalTemplate

func (f FileTests) DefaultEvalTemplate(src string) (MarshalableResult, *TestErr)

DefaultEvalTemplate evaluates the YAML template "src"

func (FileTests) Run

func (f FileTests) Run(t *testing.T)

Run runs each tests: enumerates each file within FileTests.PathToTests; splits and evaluates using FileTests.EvalFunc optionally supplying FileTests.DataValues to that evaluation.

If an error occurs and FileTests.ShowTemplateCode is set, then the output includes the debug output of the template.

type MarshalableResult

type MarshalableResult interface {
	AsBytes() ([]byte, error)
}

MarshalableResult is a template evaluation result that can be (likely) marshaled into a slice of bytes.

type TestErr

type TestErr struct {
	// contains filtered or unexported fields
}

TestErr captures an error result from a single test.

func NewTestErr

func NewTestErr(realErr, testErr error) *TestErr

NewTestErr creates a new TestErr

func (TestErr) TestErr

func (e TestErr) TestErr() error

TestErr yields the error wrapped with helpful test context

func (TestErr) UserErr

func (e TestErr) UserErr() error

UserErr yields the error returned to the user

Jump to

Keyboard shortcuts

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