Documentation ¶
Overview ¶
Package test provide library for helping with testing.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Assert ¶
Assert compare two interfaces: `exp` and `got` for equality. If both parameters are not equal, the function will call Fatalf that describe the position (type and value) where value are not matched.
If `exp` implement the extended `reflect.Equaler`, then it will use the method `IsEqual()` with `got` as parameter.
WARNING: this method does not support recursive pointer, for example a node that point to parent and parent that point back to node again.
func AssertBench ¶
AssertBench will compare two interfaces: `exp` and `got` for equality. If both parameters are not equal, the function will call Fatalf that describe the position (type and value) where value are not matched.
Types ¶
type Data ¶ added in v0.40.0
type Data struct { Flag map[string]string Input map[string][]byte Output map[string][]byte // The file name of the data. Name string Desc []byte }
Data contains predefined input and output values that is loaded from file to be used during test.
The data provides zero or more flags, an optional description, zero or more input, and zero or more output.
The data content use the following format,
[FLAG_KEY ":" FLAG_VALUE LF] [LF DESCRIPTION] ">>>" [INPUT_NAME] LF INPUT_CONTENT LF "<<<" [OUTPUT_NAME] LF OUTPUT_CONTENT
The data can contains zero or more flag. A flag is key and value separated by ":". The flag key must not contain spaces.
The data may contain description.
The line that start with "\n>>>" defined the beginning of input. An input can have a name, if its empty it will be set to "default". An input can be defined multiple times, with different names.
The line that start with "\n<<<" defined the beginning of output. An output can have a name, if its empty it will be set to "default". An output also can be defined multiple times, with different names.
All of both input and output content will have one new line truncated at the end. If they expecting new line at the end, add two empty lines at the end of it.
Example ¶
The following code illustrate how to use Data when writing test.
Assume that we are writing a parser that consume []byte. First we pass the input as defined in ">>>" and then we dump the result into bytes.Buffer to be compare with output "<<<".
func TestParse(t *testing.T) { var buf bytes.Buffer tdata, _ := LoadData("testdata/data.txt") opt := tdata.Flag["env"] p, err := Parse(tdata.Input["default"], opt) if err != nil { Assert(t, "Error", tdata.Output["error"], []byte(err.Error()) } fmt.Fprintf(&buf, "%v", p) want := tdata.Output["default"] got := buf.Bytes() Assert(t, tdata.Name, want, got) }
That is the gist, the real application can consume one or more input; or generate one or more output.
func LoadData ¶ added in v0.40.0
LoadData load data from file.
Example ¶
var ( data *Data name string content []byte err error ) // Content of data1_test.txt, // // key: value // Description of test1. // >>> // input. // // <<< // output. data, err = LoadData("testdata/data1_test.txt") if err != nil { log.Fatal(err) } fmt.Printf("%s\n", data.Name) fmt.Printf(" Flags=%v\n", data.Flag) fmt.Printf(" Desc=%s\n", data.Desc) fmt.Println(" Input") for name, content = range data.Input { fmt.Printf(" %s=%s\n", name, content) } fmt.Println(" Output") for name, content = range data.Output { fmt.Printf(" %s=%s\n", name, content) }
Output: data1_test.txt Flags=map[key:value] Desc=Description of test1. Input default=input. Output default=output.
func LoadDataDir ¶ added in v0.40.0
LoadDataDir load all data inside a directory. Only file that has file name suffix "_text.txt" will be loaded.
Example ¶
var ( listData []*Data data *Data err error name string content []byte ) listData, err = LoadDataDir("testdata/") if err != nil { log.Fatal(err) } for _, data = range listData { fmt.Printf("%s\n", data.Name) fmt.Printf(" Flags=%v\n", data.Flag) fmt.Printf(" Desc=%s\n", data.Desc) fmt.Println(" Input") for name, content = range data.Input { fmt.Printf(" %s=%s\n", name, content) } fmt.Println(" Output") for name, content = range data.Output { fmt.Printf(" %s=%s\n", name, content) } }
Output: data1_test.txt Flags=map[key:value] Desc=Description of test1. Input default=input. Output default=output. data2_test.txt Flags=map[] Desc= Input default=another test input. Output default=another test output.