evaltest

package
v0.0.0-...-91dc524 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2023 License: BSD-2-Clause Imports: 12 Imported by: 0

Documentation

Overview

Package evaltest provides a framework for testing Elvish script.

The entry point for the framework is the Test function, which accepts a *testing.T and any number of test cases.

Test cases are constructed using the That function, followed by method calls that add additional information to it.

Example:

Test(t,
    That("put x").Puts("x"),
    That("echo x").Prints("x\n"))

If some setup is needed, use the TestWithSetup function instead.

Index

Constants

View Source
const ApproximatelyThreshold = 1e-15

ApproximatelyThreshold defines the threshold for matching float64 values when using Approximately.

Variables

This section is empty.

Functions

func CmdExit

func CmdExit(v eval.ExternalCmdExit) error

CmdExit returns an error that can be passed to Case.Throws to match an eval.ExternalCmdExit ignoring the Pid field.

func ErrorWithMessage

func ErrorWithMessage(msg string) error

ErrorWithMessage returns an error that can be passed to Case.Throws to match any error with the given message.

func ErrorWithType

func ErrorWithType(v error) error

ErrorWithType returns an error that can be passed to the Case.Throws to match any error with the same type as the argument.

func OneOfErrors

func OneOfErrors(errs ...error) error

func Test

func Test(t *testing.T, tests ...Case)

Test runs test cases. For each test case, a new Evaler is created with NewEvaler.

func TestWithSetup

func TestWithSetup(t *testing.T, setup func(*eval.Evaler), tests ...Case)

TestWithSetup runs test cases. For each test case, a new Evaler is created with NewEvaler and passed to the setup function.

Types

type Approximately

type Approximately struct{ F float64 }

Approximately can be passed to Case.Puts to match a float64 within the threshold defined by ApproximatelyThreshold.

type Case

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

Case is a test case that can be used in Test.

func That

func That(lines ...string) Case

That returns a new Case with the specified source code. Multiple arguments are joined with newlines. To specify multiple pieces of code that are executed separately, use the Then method to append code pieces.

When combined with subsequent method calls, a test case reads like English. For example, a test for the fact that "put x" puts "x" reads:

That("put x").Puts("x")

func (Case) DoesNotCompile

func (c Case) DoesNotCompile() Case

DoesNotCompile returns an altered Case that requires the source code to fail compilation.

func (Case) DoesNothing

func (c Case) DoesNothing() Case

DoesNothing returns t unchanged. It is useful to mark tests that don't have any side effects, for example:

That("nop").DoesNothing()

func (Case) Passes

func (c Case) Passes(f func(t *testing.T)) Case

Puts returns an altered Case that runs an additional verification function.

func (Case) Prints

func (c Case) Prints(s string) Case

Prints returns an altered Case that requires the source code to produce the specified output in the byte pipe when evaluated.

func (Case) PrintsStderrWith

func (c Case) PrintsStderrWith(s string) Case

PrintsStderrWith returns an altered Case that requires the stderr output to contain the given text.

func (Case) Puts

func (c Case) Puts(vs ...interface{}) Case

Puts returns an altered Case that requires the source code to produce the specified values in the value channel when evaluated.

func (Case) Then

func (c Case) Then(lines ...string) Case

Then returns a new Case that executes the given code in addition. Multiple arguments are joined with newlines.

func (Case) Throws

func (c Case) Throws(reason error, stacks ...string) Case

Throws returns an altered Case that requires the source code to throw an exception with the given reason. The reason supports special matcher values constructed by functions like ErrorWithMessage.

If at least one stacktrace string is given, the exception must also have a stacktrace matching the given source fragments, frame by frame (innermost frame first). If no stacktrace string is given, the stack trace of the exception is not checked.

func (Case) WithSetup

func (c Case) WithSetup(f func(*eval.Evaler)) Case

Then returns a new Case with the given setup function executed on the Evaler before the code is executed.

type MatchingRegexp

type MatchingRegexp struct{ Pattern string }

MatchingRegexp can be passed to Case.Puts to match a any string that matches a regexp pattern. If the pattern is not a valid regexp, the test will panic.

Jump to

Keyboard shortcuts

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