test

package
v0.49.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2023 License: BSD-3-Clause Imports: 11 Imported by: 0

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

func Assert(w Writer, name string, exp, got interface{})

Assert compare two interfaces: exp and got for equality. If both parameters are not equal, the function will call print and try to describe the position (type and value) where value are not matched and call Fatalf.

If exp implement the extended reflect.Equaler, then it will use the method IsEqual with got as parameter.

If exp and got is a struct, it will print the first non-matched field in the following format,

!!! Assert: [<name>: ] T.<Field>: expecting <type>(<value>), got <type>(<value>)

If both exp and got types are string and its longer than 50 chars, it will use the diff.Text to show the difference between them. The diff output is as follow,

!!! <name>:
---- EXPECTED
<LINE_NUM> - "<STRING>"
...
++++ GOT
<LINE_NUM> + "<STRING>"
...
--++
<LINE_NUM> - "<LINE_EXP>"
<LINE_NUM> + "<LINE_GOT>"

Any lines after "----" indicate the lines that test expected, from `exp` parameter.

Any lines after "++++" indicate the lines that test got, from `got` parameter.

Any lines after "--++" indicate that the same line between expected and got but different content.

  • The "<LINE_NUM> - " print the expected line.
  • The "<LINE_NUM> + " print the got line.

LIMITATION: this method does not support recursive pointer, for example a node that point to parent and parent that point back to node again.

Example (String)
package main

import (
	"fmt"

	"github.com/shuLhan/share/lib/test"
)

func main() {
	var (
		tw  = test.TestWriter{}
		exp string
		got string
	)

	exp = `a string`
	got = `b string`
	test.Assert(&tw, ``, exp, got)
	fmt.Println(tw.String())

	exp = `func (tw *TestWriter) Fatal(args ...any)                 { fmt.Fprint(tw, args...) }`
	got = `func (tw *TestWriter) Fatalf(format string, args ...any) { fmt.Fprintf(tw, format, args...) }`
	tw.Reset()
	test.Assert(&tw, ``, exp, got)
	fmt.Println(tw.String())
}
Output:

!!! Assert: expecting string(a string), got string(b string)
!!! :
--++
0 - func (tw *TestWriter) Fatal(args ...any)                 { fmt.Fprint(tw, args...) }
0 + func (tw *TestWriter) Fatalf(format string, args ...any) { fmt.Fprintf(tw, format, args...) }
Example (String2)
package main

import (
	"fmt"

	"github.com/shuLhan/share/lib/test"
)

func main() {
	var (
		tw  = test.TestWriter{}
		exp string
		got string
	)

	exp = `Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Fusce cursus libero in velit dapibus tincidunt.
Vestibulum vulputate ipsum ac nisl viverra pharetra.
Sed at mi in urna lobortis bibendum.
Vivamus tempus enim in urna fermentum, non volutpat nisi lacinia.`

	got = `Fusce cursus libero in velit dapibus tincidunt.
Vestibulum vulputate ipsum ac nisl viverra pharetra.
Sed at mi in urna lobortis bibendum.
Sed pretium nisl ut dolor ullamcorper blandit.
Sed faucibus felis iaculis, sagittis erat quis, tempor nisi.`

	test.Assert(&tw, `Assert string`, exp, got)
	fmt.Println(tw.String())

}
Output:

!!! Assert string:
---- EXPECTED
0 - Lorem ipsum dolor sit amet, consectetur adipiscing elit.
++++ GOT
4 + Sed faucibus felis iaculis, sagittis erat quis, tempor nisi.
--++
4 - Vivamus tempus enim in urna fermentum, non volutpat nisi lacinia.
3 + Sed pretium nisl ut dolor ullamcorper blandit.
Example (Struct)
package main

import (
	"fmt"
	"math/big"

	"github.com/shuLhan/share/lib/test"
)

func main() {
	type ADT struct {
		BigRat *big.Rat
		Int    int
		Bytes  []byte
	}

	var cases = []struct {
		desc string
		exp  ADT
		got  ADT
	}{{
		desc: `On field struct`,
		exp: ADT{
			BigRat: big.NewRat(123, 456),
		},
		got: ADT{
			BigRat: big.NewRat(124, 456),
		},
	}, {
		desc: `On field int`,
		exp: ADT{
			BigRat: big.NewRat(1, 2),
			Int:    1,
		},
		got: ADT{
			BigRat: big.NewRat(1, 2),
			Int:    2,
		},
	}, {
		desc: `On field []byte`,
		exp: ADT{
			Bytes: []byte(`hello, world`),
		},
		got: ADT{
			Bytes: []byte(`hello, world!`),
		},
	}, {
		desc: `On field []byte, same length`,
		exp: ADT{
			Bytes: []byte(`heelo, world!`),
		},
		got: ADT{
			Bytes: []byte(`hello, world!`),
		},
	}}

	var (
		tw = test.TestWriter{}
	)

	for _, c := range cases {
		test.Assert(&tw, c.desc, c.exp, c.got)
		fmt.Println(tw.String())
		tw.Reset()
	}
}
Output:

!!! Assert: On field struct: ADT.BigRat: Rat.a: Int.abs: nat[0]: expecting Word(41), got Word(31)
!!! Assert: On field int: ADT.Int: expecting int(1), got int(2)
!!! Assert: On field []byte: ADT.Bytes: len(): expecting 12, got 13
!!! Assert: On field []byte, same length: ADT.Bytes: [2]: expecting uint8(101), got uint8(108)

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]
LF
">>>" [INPUT_NAME] LF
INPUT_CONTENT
LF
"<<<" [OUTPUT_NAME] LF
OUTPUT_CONTENT

The Data can contains zero or more flag. A Flag is map of key and value separated by ":". The Flag's key must not contain spaces.

The Data may contain description, to describe the content of test file.

The line that start with "\n>>>" (new line followed by three '>') define 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<<<" (new line followed by three '<') 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 at the end, to separated them with each others. If the content of Input or Output itself expecting empty 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.

Articles

func LoadData added in v0.40.0

func LoadData(file string) (data *Data, err error)

LoadData load data from file.

Example
package main

import (
	"fmt"
	"log"

	"github.com/shuLhan/share/lib/test"
)

func main() {
	var (
		data    *test.Data
		name    string
		content []byte
		err     error
	)

	// Content of data1_test.txt,
	//
	//	key: value
	//	Description of test1.
	//	>>>
	//	input.
	//
	//	<<<
	//	output.

	data, err = test.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

func LoadDataDir(path string) (listData []*Data, err error)

LoadDataDir load all data inside a directory. Only file that has file name suffix "_text.txt" will be loaded.

Example
package main

import (
	"fmt"
	"log"

	"github.com/shuLhan/share/lib/test"
)

func main() {
	var (
		listData []*test.Data
		data     *test.Data
		err      error
		name     string
		content  []byte
	)

	listData, err = test.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.

type TestWriter added in v0.47.0

type TestWriter struct {
	bytes.Buffer
}

TestWriter write Errorx, Fatalx, and Logx to bytes.Buffer.

func (*TestWriter) Error added in v0.47.0

func (tw *TestWriter) Error(args ...any)

func (*TestWriter) Errorf added in v0.47.0

func (tw *TestWriter) Errorf(format string, args ...any)

func (*TestWriter) Fatal added in v0.47.0

func (tw *TestWriter) Fatal(args ...any)

func (*TestWriter) Fatalf added in v0.47.0

func (tw *TestWriter) Fatalf(format string, args ...any)

func (*TestWriter) Log added in v0.47.0

func (tw *TestWriter) Log(args ...any)

func (*TestWriter) Logf added in v0.47.0

func (tw *TestWriter) Logf(format string, args ...any)

type Writer added in v0.41.1

type Writer interface {
	Error(args ...any)
	Errorf(format string, args ...any)
	Fatal(args ...any)
	Fatalf(format string, args ...any)
	Log(args ...any)
	Logf(format string, args ...any)
}

Writer contains common methods between testing.T and testing.B, a subset of testing.TB that cannot be used do private method.

Directories

Path Synopsis
Package mock provide a mocking for standard output and standard error.
Package mock provide a mocking for standard output and standard error.

Jump to

Keyboard shortcuts

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