package progtest

import "src.elv.sh/pkg/prog/progtest"

Package progtest provides a framework for testing subprograms.

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

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

Example:

Test(t, someProgram,
     ThatElvish("-c", "echo hello").WritesStdout("hello\n"))

Index

Package Files

progtest.go

func Run

func Run(p prog.Program, args ...string) (exit int, stdout, stderr string)

Run runs a Program with the given arguments. It returns the Program's exit code and output to stdout and stderr.

func SetDeprecationLevel

func SetDeprecationLevel(c testutil.Cleanuper, level int)

SetDeprecationLevel sets prog.DeprecationLevel to the given value for the duration of a test.

func Test

func Test(t *testing.T, p prog.Program, cases ...Case)

Test runs test cases against a given program.

type Case

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

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

func ThatElvish

func ThatElvish(args ...string) Case

ThatElvish returns a new Case with the specified CLI arguments.

The new Case expects the program run to exit with 0, and write nothing to stdout or stderr.

When combined with subsequent method calls, a test case reads like English. For example, a test for the fact that "elvish -c hello" writes "hello\n" to stdout reads:

ThatElvish("-c", "hello").WritesStdout("hello\n")

func (Case) DoesNothing

func (c Case) DoesNothing() Case

DoesNothing returns c itself. It is useful to mark tests that otherwise don't have any expectations, for example:

ThatElvish("-c", "nop").DoesNothing()

func (Case) ExitsWith

func (c Case) ExitsWith(code int) Case

ExitsWith returns an altered Case that requires the program run to return with the given exit code.

func (Case) WithStdin

func (c Case) WithStdin(s string) Case

WithStdin returns an altered Case that provides the given input to stdin of the program.

func (Case) WritesStderr

func (c Case) WritesStderr(s string) Case

WritesStderr returns an altered Case that requires the program run to write exactly the given text to stderr.

func (Case) WritesStderrContaining

func (c Case) WritesStderrContaining(s string) Case

WritesStderrContaining returns an altered Case that requires the program run to write output to stderr that contains the given text as a substring.

func (Case) WritesStdout

func (c Case) WritesStdout(s string) Case

WritesStdout returns an altered Case that requires the program run to write exactly the given text to stdout.

func (Case) WritesStdoutContaining

func (c Case) WritesStdoutContaining(s string) Case

WritesStdoutContaining returns an altered Case that requires the program run to write output to stdout that contains the given text as a substring.

Package progtest imports 7 packages (graph).

Version v0.17.0 (latest) | Published Dec 7, 2021 | Platform: linux/amd64 | Updated 9 minutes ago

.
Tools for package owners.