txtartesting

package
v0.0.0-...-e7c0b78 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2024 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package txtartesting provides utilities for testing code where test cases are stored in .txtar files.

The expected format of a .txtar file is as follows:

1. Each test case is stored in a separate file. 2. The file name is the test case name. 3. The file content must contain three sections:

-- input --
-- output --
-- error --

The input section contains the input to the code under test.

The output section contains the expected output.

The error section contains the expected error message.

The output and error sections are mutually exclusive but at least one of them must be present.

No other files are allowed in the .txtar archive.

The output section should contain either the expected output from executing the code under test or a serialized JSON.

You load a test case by calling LoadTestCases with the path to the directory containing the .txtar files (typically, this is the `testdata` directory). The function returns a TestCase slice, each representing a single test case.

When the expected output is text, you can compare it with the actual output by calling the *TestCase.CompareTextOutput method on the test case. When the expected output is JSON, you can compare it with the actual output by calling *TestCase.CompareJSONOutput.

When you expect an error, you can compare it with the actual error by calling the *TestCase.CompareError method.

We strip leading and trailing white spaces from the input, output, and error sections. In particular, for output sections, we are careful to strip trailing newlines, removing only the empty lines.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TestCase

type TestCase struct {
	// Name is the file name.
	Name string

	// Input is the input to parse.
	Input string

	// Output is the expected output.
	Output string

	// Error is the expected error message.
	Error string
}

TestCase represents a single test case extracted from a .txtar file.

func LoadSingleTestCase

func LoadSingleTestCase(filePath string) (*TestCase, error)

LoadSingleTestCase loads a single `.txtar` test case from the given file path.

func LoadTestCases

func LoadTestCases(testdataDir string) ([]*TestCase, error)

LoadTestCases loads `.txtar` test cases from the given directory.

func (*TestCase) CompareError

func (tc *TestCase) CompareError(err error) error

CompareError compares the obtained error with the expected error.

func (*TestCase) CompareJSONOutput

func (tc *TestCase) CompareJSONOutput(output any) error

CompareJSONOutput compares the obtained JSON output with the expected JSON output.

func (*TestCase) CompareTextOutput

func (tc *TestCase) CompareTextOutput(output string) error

CompareTextOutput compares the obtained output with the expected output.

Jump to

Keyboard shortcuts

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