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 ¶
LoadSingleTestCase loads a single `.txtar` test case from the given file path.
func LoadTestCases ¶
LoadTestCases loads `.txtar` test cases from the given directory.
func (*TestCase) CompareError ¶
CompareError compares the obtained error with the expected error.
func (*TestCase) CompareJSONOutput ¶
CompareJSONOutput compares the obtained JSON output with the expected JSON output.
func (*TestCase) CompareTextOutput ¶
CompareTextOutput compares the obtained output with the expected output.