gt

package module
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

gt: Generics based Test library for Go

Go Reference test gosec lint

gt is test library leveraging Go generics to check variable type in IDE and compiler.

color := "blue"

// gt.Value(t, color).Equal(5) // <- Compile error

gt.Value(t, color).Equal("orange") // <- Fail
gt.Value(t, color).Equal("blue")   // <- Pass
colors := ["red", "blue"]

// gt.Array(t, colors).Equal("red")       // <- Compile error
// gt.Array(t, colors).Equal([]int{1, 2}) // <- Compile error

gt.Array(t, colors).Equal([]string{"red", "blue"}) // <- Pass
gt.Array(t, colors).Have("orange")                 // <- Fail

Motivation

Existing test libraries in Go such as testify strongly support writing unit test. Many test libraries uses reflect package to identify and compare variable type and value and test functions of the libraries accept any type by interface{} or any. However the approach has two problems:

  • Variable types mismatch between expected and actual can not be detected before running the test.
  • IDE can not support variable completion because types can not be determined before running the test.

On the other hand, Go started to provide Generics feature by version 1.18. It can be leveraged to support type completion and checking types before running a test.

Usage

In many cases, a developer does not care Go generics in using gt. However, a developer need to specify generic type (Value, Array, Map, Error, etc.) explicitly to use specific test functions for each types.

See reference for more detail.

Value

Generic test type has a minimum set of test methods.

type user struct {
    Name string
}
u1 := user{Name: "blue"}

// gt.Value(u1).Equal(1)                  // Compile error
// gt.Value(u1).Equal("blue")             // Compile error
// gt.Value(u1).Equal(&user{Name:"blue"}) // Compile error

gt.Value(u1).Equal(user{Name:"blue"}) // Pass
Number

Accepts only number types: int, uint, int64, float64, etc.

var f float64 = 12.5
gt.Number(t, f).
    Equal(12.5).         // Pass
    Greater(12).         // Pass
    Less(10).            // Fail
    GreaterOrEqual(12.5) // Pass
Array

Accepts array of any type not only primitive type but also struct.

colors := []string{"red", "blue", "yellow"}

gt.Array(t, colors).
    Equal([]string{"red", "blue", "yellow"}) // Pass
    Equal([]string{"red", "blue"})           // Fail
    // Equal([]int{1, 2})                    // Compile error
    Contain([]string{"red", "blue"})         // Pass
    Have("yellow")                           // Pass
    Length(3)                                // Pass

gt.Array(t, colors).Must().Have("orange") // Fail and stop test
Map
colorMap := map[string]int{
    "red": 1,
    "yellow": 2,
    "blue": 5,
}

gt.Map(t, colorMap)
    .HaveKey("blue")           // Pass
    .HaveValue(5)              // Pass
    // .HaveValue("red")       // Compile error
    .HaveKeyValue("yellow", 2) // Pass

gt.Map(t, colorMap).Must().HaveKey("orange") // Fail and stop test
Cast
type user struct {
    Name string
}
var v any = &user{
    Name: "blue",
}

u1 := gt.Cast[user](t, v).NotNil()  // Fail (because v is *user, not user)
gt.Cast[*user](t, v).Nil()          // Fail (because v is not nil)

u2 := gt.Cast[*user](t, v).NotNil() // Pass
gt.Value(t, u2.Name).Equal("blue")       // Pass

License

Apache License 2.0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var Diff = func(expect, actual any) string {
	switch reflect.ValueOf(actual).Kind() {
	case reflect.Pointer, reflect.UnsafePointer,
		reflect.Array, reflect.Slice,
		reflect.Struct, reflect.Map:
		return "diff:\n" + cmp.Diff(expect, actual, cmp.Exporter(func(t reflect.Type) bool { return true }))

	default:
		return strings.Join([]string{
			fmt.Sprintf("actual: %+v", actual),
			fmt.Sprintf("expect: %+v", expect),
		}, "\n")
	}
}
View Source
var DumpError = func(err error) string {
	return err.Error()
}
View Source
var EvalCompare = func(a, b any) bool {
	return reflect.DeepEqual(a, b)
}

EvalCompare is a function to check if actual value equals with expected value. A developer can replace EvalCompare with own evaluation function if needed.

EvalCompare(1, 2) == false
EvalCompare("abc", "axc") == false
EvalCompare([]int{1, 2, 3}, []int{1, 2, 3}) == true
View Source
var EvalIsNil = func(v any) bool {
	value := reflect.ValueOf(v)

	switch value.Kind() {
	case reflect.Invalid:
		return true

	case reflect.Array, reflect.Slice:
		return value.Len() == 0

	case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Uintptr, reflect.UnsafePointer:
		return value.IsNil()

	default:
		return false
	}
}

EvalIsNil is a function to check if actual value is nil. A developer can replace EvalIsNil with own nil check function if needed.

EvalIsNil(1) == false
EvalIsNil("abc") == false
EvalIsNil(nil) == true
EvalIsNil([]int{}) == true

Functions

func C added in v0.0.3

func C[T any](t testing.TB, v any) T

func Cast

func Cast[T any](t testing.TB, v any) T

Cast tries type assertion and will error and stop test if type assertion fails

var a any = "hello"
b := gt.Cast[string](a) // <- Pass and set "hello" to b
c := gt.Cast[int](a)    // <- Fail and stop test

func Equal added in v0.0.5

func Equal[T any](t testing.TB, actual T, expected T)

func ErrorAs

func ErrorAs[T any](t testing.TB, actual error, callback func(expect *T))

ErrorAs checks error type by errors.As() function. If type check passed, callback will be invoked and given extracted error by errors.As.

func NotEqual added in v0.0.5

func NotEqual[T any](t testing.TB, actual T, expected T)

Types

type ArrayTest

type ArrayTest[T any] struct {
	// contains filtered or unexported fields
}

func A

func A[T any](t testing.TB, actual []T) ArrayTest[T]

A is sugar syntax of Array

func Array

func Array[T any](t testing.TB, actual []T) ArrayTest[T]

Array provides ArrayTest that has not only Value test methods but also array (slice) comparison methods

Example
t := newRecorder()

a := []int{2, 3, 4}
b := []int{2, 3, 5}

gt.Value(t, a).Equal(b)

fmt.Println(t.msgs[0])
Output:

func (ArrayTest[T]) All added in v0.0.6

func (x ArrayTest[T]) All(f func(v T) bool) ArrayTest[T]

All calls f with testing.TB and each elements in the array. If f returns false, All returns immediately and test will trigger error. If f returns true for all elements, All will pass.

v := []int{1, 2, 3, 5}
gt.Array(t, v).All(func(v int) bool {
    return v < 6
}) // Pass
gt.Array(t, v).All(func(v int) bool {
    return v < 4
}) // Fail

func (ArrayTest[T]) Any added in v0.0.6

func (x ArrayTest[T]) Any(f func(v T) bool) ArrayTest[T]

Any calls f with testing.TB and each elements in the array. If f returns true, Any returns immediately and test will pass. If f returns false for all elements, Any will trigger error.

v := []int{1, 2, 3, 5}
gt.Array(t, v).Any(func(v int) bool {
    return v == 3
}) // Pass
gt.Array(t, v).Any(func(v int) bool {
    return v == 4
}) // Fail

func (ArrayTest[T]) At added in v0.0.5

func (x ArrayTest[T]) At(idx int, f func(t testing.TB, v T)) ArrayTest[T]

At calls f with testing.TB and idx th elements in the array. If idx is out of range, f is not called and test will trigger error.

v := []int{1, 2, 3, 5}
gt.Array(t, v).At(2, func(t testing.TB, v int) {
	gt.Value(t, v).Equal(3) // Pass
})

func (ArrayTest[T]) Contain

func (x ArrayTest[T]) Contain(expect []T) ArrayTest[T]

Contain check if actual has expect as sub sequence.

v := []int{1, 2, 3, 5}
gt.Array(t, v).Contain([]int{1, 2, 3})) // Pass
gt.Array(t, v).Contain([]int{1, 2, 5})) // Fail

func (ArrayTest[T]) Distinct added in v0.0.7

func (x ArrayTest[T]) Distinct() ArrayTest[T]

Distinct checks if all elements in the array are distinct. If not, test will trigger error.

gt.Array(t, []int{1, 2, 3, 5}).Distinct() // Pass
gt.Array(t, []int{1, 2, 3, 2}).Distinct() // Fail

func (ArrayTest[T]) Equal

func (x ArrayTest[T]) Equal(expect []T) ArrayTest[T]

Equal check if actual does not equals with expect. Default evaluation function uses reflect.DeepEqual.

v := []int{1, 2, 3, 5}
gt.Array(t, v).Equal([]int{1, 2, 3, 5}) // Pass
gt.Array(t, v).Equal([]int{1, 2, 3, 4}) // Fail

func (ArrayTest[T]) EqualAt added in v0.0.4

func (x ArrayTest[T]) EqualAt(idx int, expect T) ArrayTest[T]

EqualAt checks if actual[idx] equals expect. If idx is out of range, f is not called and test will trigger error.

v := []int{1, 2, 3, 5}
gt.Array(t, v).EqualAt(2, 3) // Pass
gt.Array(t, v).EqualAt(2, 1) // Fail
gt.Array(t, v).EqualAt(2, 5) // Fail by out of range

func (ArrayTest[T]) Have

func (x ArrayTest[T]) Have(expect T) ArrayTest[T]

Have check if actual has an expect element

v := []int{1, 2, 3, 5}
gt.Array(t, v).Have(5)) // Pass
gt.Array(t, v).Have(4)) // Fail

func (ArrayTest[T]) Length

func (x ArrayTest[T]) Length(expect int) ArrayTest[T]

Length checks if element number of actual array is expect.

v := []int{1, 2, 3, 5}
gt.Array(t, v).Length(4) // Pass

func (ArrayTest[T]) Less added in v0.0.10

func (x ArrayTest[T]) Less(expect int) ArrayTest[T]

Less checks if array length is shorter than expect.

v := []int{1, 2, 3, 5}
gt.Array(t, v).Less(5) // Pass
gt.Array(t, v).Less(4) // Fail

func (ArrayTest[T]) Longer added in v0.0.2

func (x ArrayTest[T]) Longer(expect int) ArrayTest[T]

Longer checks if array length is longer than expect.

v := []int{1, 2, 3, 5}
gt.Array(t, v).Longer(3) // Pass
gt.Array(t, v).Longer(4) // Fail

func (ArrayTest[T]) MatchThen added in v0.0.8

func (x ArrayTest[T]) MatchThen(match func(v T) bool, then func(t testing.TB, v T)) ArrayTest[T]

MatchThen calls then function with testing.TB and first element in the array that match with match. If no element matches, MatchThen will trigger error.

v := []struct{
    Name string
    Age int
}{
    {"Alice", 20},
    {"Bob", 21},
    {"Carol", 22},
}
gt.Array(t, v).MatchThen(func(v struct{Name string, Age int}) bool {
    return v.Name == "Bob"
}, func(t testing.TB, v struct{Name string, Age int}) {
    gt.Value(t, v.Age).Equal(21) // Pass
})

gt.Array(t, v).MatchThen(func(v struct{Name string, Age int}) bool {
    return v.Name == "Dave"
}, func(t testing.TB, v struct{Name string, Age int}) {
    gt.Value(t, v.Age).Equal(21) // Fail
})

func (ArrayTest[T]) Must

func (x ArrayTest[T]) Must() ArrayTest[T]

Must check if error has occurred in previous test. If errors will occur in following test, it immediately stop test by t.FailNow().

func (ArrayTest[T]) NotContain

func (x ArrayTest[T]) NotContain(expect []T) ArrayTest[T]

NotContain check if actual does not have expect as sub sequence.

v := []int{1, 2, 3, 5}
gt.Array(t, v).NotContain([]int{1, 2, 3})) // Fail
gt.Array(t, v).NotContain([]int{1, 2, 5})) // Pass

func (ArrayTest[T]) NotEqual

func (x ArrayTest[T]) NotEqual(expect []T) ArrayTest[T]

NotEqual check if actual does not equals with expect. Default evaluation function uses reflect.DeepEqual.

v := []int{1, 2, 3, 5}
gt.Array(t, v).NotEqual([]int{1, 2, 3, 5}) // Fail
gt.Array(t, v).NotEqual([]int{1, 2, 3, 4}) // Pass

func (ArrayTest[T]) NotEqualAt added in v0.0.4

func (x ArrayTest[T]) NotEqualAt(idx int, expect T) ArrayTest[T]

NotEqualAt checks if actual[idx] equals expect. If idx is out of range, f is not called and test will trigger error.

v := []int{1, 2, 3, 5}
gt.Array(t, v).NotEqualAt(2, 1) // Pass
gt.Array(t, v).NotEqualAt(2, 3) // Fail
gt.Array(t, v).NotEqualAt(2, 5) // Fail by out of range

func (ArrayTest[T]) NotHave

func (x ArrayTest[T]) NotHave(expect T) ArrayTest[T]

NotHave check if actual does not have an expect element

v := []int{1, 2, 3, 5}
gt.Array(t, v).Have(5)) // Fail
gt.Array(t, v).Have(4)) // Pass

type BoolTest

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

func B

func B(t *testing.T, actual bool) BoolTest

func Bool

func Bool(t *testing.T, actual bool) BoolTest

func False added in v0.0.8

func False(t *testing.T, actual bool) BoolTest

func True added in v0.0.8

func True(t *testing.T, actual bool) BoolTest

func (BoolTest) False

func (x BoolTest) False() BoolTest

func (BoolTest) True

func (x BoolTest) True() BoolTest

type ErrorTest

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

func Error

func Error(t testing.TB, actual error) ErrorTest

Value provides ErrorTest that is specialized for error testing

func (ErrorTest) Is

func (x ErrorTest) Is(expected error)

Is checks error object equality by errors.Is() function.

func (ErrorTest) IsNot

func (x ErrorTest) IsNot(expected error)

IsNot checks error object not-equality by errors.Is() function.

func (ErrorTest) Must

func (x ErrorTest) Must() ErrorTest

Must checks if error has occurred in previous test. If errors will occur in following test, it immediately stop test by t.FailNow().

type MapTest

type MapTest[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func M

func M[K comparable, V any](t testing.TB, actual map[K]V) MapTest[K, V]

M is sugar syntax of Map

m := map[string]int{
	"blue": 5,
}
gt.M(t, m).HaveKey("blue").HaveValue(5)

func Map

func Map[K comparable, V any](t testing.TB, actual map[K]V) MapTest[K, V]

Map provides MapTest that has not only Value test methods but also key-value test

m := map[string]int{
	"blue": 5,
}
gt.Map(t, m).HaveKey("blue").HaveValue(5)
Example
t := newRecorder()

type user struct {
	ID   int
	Name string
}
a := &user{ID: 1, Name: "Alice"}
b := &user{ID: 2, Name: "Bob"}

gt.Value(t, a).Equal(b)

fmt.Println(t.msgs[0])
Output:

Example (Nil)
t := newRecorder()

type user struct {
	ID   int
	Name string
}
a := &user{ID: 1, Name: "Alice"}

gt.Value(t, a).Equal(nil)

fmt.Println(t.msgs[0])
Output:

func (MapTest[K, V]) At added in v0.0.5

func (x MapTest[K, V]) At(key K, f func(t testing.TB, v V)) MapTest[K, V]

At calls f with testing.TB and idx th elements in the array. If idx is out of range, f is not called and test will trigger error.

m := map[string]int{
	"blue": 5,
}
gt.Map(t, m).At("blue", func(t testing.TB, v int) {
	gt.Value(t, v).Equal(5) // <- pass
})

func (MapTest[K, V]) Equal

func (x MapTest[K, V]) Equal(expect map[K]V) MapTest[K, V]

Equal checks if expect completely equals given actual map.

m := map[string]int{
	"blue": 5,
}
gt.Map(t, m).Equal(map[string]int{"blue": 5}) // <- Pass
gt.Map(t, m).Equal(map[string]int{"blue": 0}) // <- Fail

func (MapTest[K, V]) EqualAt added in v0.0.4

func (x MapTest[K, V]) EqualAt(key K, expect V) MapTest[K, V]

EqualAt checks if actual[key] equals expect. If key is not found, test will fail.

m := map[string]int{
	"blue": 5,
}
gt.Map(t, m).NotEqualAt("blue", 5)   // Pass
gt.Map(t, m).NotEqualAt("blue", 1)   // Fail
gt.Map(t, m).NotEqualAt("orange", 5) // Fail by key not found

func (MapTest[K, V]) HaveKey

func (x MapTest[K, V]) HaveKey(expect K) MapTest[K, V]

HaveKey checks if the map has expect key.

m := map[string]int{
	"blue": 5,
}
gt.Map(t, m).HaveKey("blue")   // <- pass
gt.Map(t, m).HaveKey("orange") // <- fail

func (MapTest[K, V]) HaveKeyValue

func (x MapTest[K, V]) HaveKeyValue(expectKey K, expectValue V) MapTest[K, V]

HaveKeyValue checks if the map has expect a pair of key and value.

m := map[string]int{
	"blue": 5,
}
gt.Map(t, m).HaveKeyValue("blue", 5)   // <- pass
gt.Map(t, m).HaveKeyValue("blue", 0)   // <- fail
gt.Map(t, m).HaveKeyValue("orange", 5) // <- fail

func (MapTest[K, V]) HaveValue

func (x MapTest[K, V]) HaveValue(expect V) MapTest[K, V]

HaveValue checks if the map has expect key.

m := map[string]int{
	"blue": 5,
}
gt.Map(t, m).HaveValue(5) // <- pass
gt.Map(t, m).HaveValue(7) // <- fail

func (MapTest[K, V]) Length

func (x MapTest[K, V]) Length(expect int) MapTest[K, V]

Length checks number of a pair of keys.

m := map[string]int{
	"blue": 5,
	"orange: 0,
}
gt.Map(t, m).Length(2) // <- pass
gt.Map(t, m).Length(0) // <- pass

func (MapTest[K, V]) Must

func (x MapTest[K, V]) Must() MapTest[K, V]

Must check if error has occurred in previous test. If errors will occur in following test, it immediately stop test by t.Failed().

m := map[string]int{
	"blue": 5,
}
gt.Must().Map(t, m).
	HaveKey("blue", 0).      // <- fail
	HaveKey("blue", 5).      // <- will not be tested
gt.Map(t, m).HaveKey("blue") // <- will not be tested

func (MapTest[K, V]) NotEqual

func (x MapTest[K, V]) NotEqual(expect map[K]V) MapTest[K, V]

NotEqual checks if expect does not equal given actual map.

m := map[string]int{
	"blue": 5,
}
gt.Map(t, m).Equal(map[string]int{ // <- Pass
	"blue": 0,
})
gt.Map(t, m).Equal(map[string]int{ // <- Pass
	"blue": 5,
	"orange": 9,
})

func (MapTest[K, V]) NotEqualAt added in v0.0.4

func (x MapTest[K, V]) NotEqualAt(key K, expect V) MapTest[K, V]

NotEqualAt checks if actual[key] equals expect. If key is not found, test will fail.

m := map[string]int{
	"blue": 5,
}
gt.Map(t, m).NotEqualAt("blue", 1)   // Pass
gt.Map(t, m).NotEqualAt("blue", 5)   // Fail
gt.Map(t, m).NotEqualAt("orange", 5) // Fail by key not found

func (MapTest[K, V]) NotHaveKey

func (x MapTest[K, V]) NotHaveKey(expect K) MapTest[K, V]

NotHaveKey checks if the map does not have expect key.

m := map[string]int{
	"blue": 5,
}
gt.Map(t, m).NotHaveKey("orange") // <- pass
gt.Map(t, m).NotHaveKey("blue")   // <- fail

func (MapTest[K, V]) NotHaveKeyValue

func (x MapTest[K, V]) NotHaveKeyValue(expectKey K, expectValue V) MapTest[K, V]

NotHaveKeyValue checks if the map does not have expect a pair of key and value.

m := map[string]int{
	"blue": 5,
}
gt.Map(t, m).NotHaveKeyValue("blue", 5)   // <- fail
gt.Map(t, m).NotHaveKeyValue("blue", 0)   // <- pass
gt.Map(t, m).NotHaveKeyValue("orange", 5) // <- pass

func (MapTest[K, V]) NotHaveValue

func (x MapTest[K, V]) NotHaveValue(expect V) MapTest[K, V]

NotHaveValue checks if the map has expect key.

m := map[string]int{
	"blue": 5,
}
gt.Map(t, m).NotHaveValue(5) // <- fail
gt.Map(t, m).NotHaveValue(7) // <- pass

type NoErrorTest added in v0.0.3

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

func NoError added in v0.0.3

func NoError(t testing.TB, actual error) NoErrorTest

NoError checks if error does not occur.

func (NoErrorTest) Must added in v0.0.3

func (x NoErrorTest) Must()

type NumberTest

type NumberTest[T number] struct {
	// contains filtered or unexported fields
}

func N

func N[T number](t testing.TB, actual T) NumberTest[T]

N is sugar syntax of Number

func Number

func Number[T number](t testing.TB, actual T) NumberTest[T]

Number provides NumberTest that has not only Value test methods but also large-small comparison methods

func (NumberTest[T]) Equal

func (x NumberTest[T]) Equal(expect T) NumberTest[T]

Equal checks if expect equals given actual value.

n := 2
gt.Number(t, n).Equal(2)

func (NumberTest[T]) Greater

func (x NumberTest[T]) Greater(expect T) NumberTest[T]

Greater checks if actual value is greater than expect

n := 5
gt.Number(t, n).Greater(3) // Pass
gt.Number(t, n).Greater(5) // Fail

func (NumberTest[T]) GreaterOrEqual

func (x NumberTest[T]) GreaterOrEqual(expect T) NumberTest[T]

GreaterOrEqual checks if actual value is expect or greater

n := 5
gt.Number(t, n).GreaterOrEqual(3) // Pass
gt.Number(t, n).GreaterOrEqual(5) // Pass
gt.Number(t, n).GreaterOrEqual(6) // Fail

func (NumberTest[T]) Less

func (x NumberTest[T]) Less(expect T) NumberTest[T]

Less checks if actual value is less than expect

n := 5
gt.Number(t, n).Less(6) // Pass
gt.Number(t, n).Less(5) // Fail

func (NumberTest[T]) LessOrEqual

func (x NumberTest[T]) LessOrEqual(expect T) NumberTest[T]

LessOrEqual checks if actual value is expect or Less

n := 5
gt.Number(t, n).LessOrEqual(6) // Pass
gt.Number(t, n).LessOrEqual(5) // Pass
gt.Number(t, n).LessOrEqual(3) // Fail

func (NumberTest[T]) Must

func (x NumberTest[T]) Must() NumberTest[T]

Must check if error has occurred in previous test. If errors will occur in following test, it immediately stop test by t.FailNow().

func (NumberTest[T]) NotEqual

func (x NumberTest[T]) NotEqual(expect T) NumberTest[T]

NotEqual checks if expect does not equal given actual value.

n := 5
gt.Number(t, n).NotEqual(1) // Pass
gt.number(t, n).Equal(5)    // Fail

type Return1Test added in v0.0.3

type Return1Test[T1 any] struct {
	// contains filtered or unexported fields
}

func R1 added in v0.0.3

func R1[T1 any](r1 T1, err error) Return1Test[T1]

func Return1 added in v0.0.3

func Return1[T1 any](r1 T1, err error) Return1Test[T1]

Return1 creates a test for returned variables (one value and one error)

f := func() (string, error) {
	return "ok", nil
}
gt.Return1(f()).Error()               // Fail
gt.Return1(f()).NoError().Equal("ok") // Pass

func (Return1Test[T1]) Error added in v0.0.3

func (x Return1Test[T1]) Error(t testing.TB) ErrorTest

Error check if the function returned error. If error is nil, it will fail. If error is not nil, it provides ErrorTest

func (Return1Test[T1]) NoError added in v0.0.3

func (x Return1Test[T1]) NoError(t testing.TB) T1

NoError check if the function returned no error. If error is not nil, it will fail. If error is nil, it provides 1st returned value.

type Return2Test added in v0.0.3

type Return2Test[T1, T2 any] struct {
	// contains filtered or unexported fields
}

func R2 added in v0.0.3

func R2[T1, T2 any](r1 T1, r2 T2, err error) Return2Test[T1, T2]

func Return2 added in v0.0.3

func Return2[T1, T2 any](r1 T1, r2 T2, err error) Return2Test[T1, T2]

Return2 creates a test for returned variables (two values and one error)

f := func() (string, int, error) {
	return "ok", 1, nil
}
gt.Return2(f()).Error()           // Fail
s, i := gt.Return1(f()).NoError() // Pass

s.Equal("ok") // Pass
i.Equal(1)    // Pass

func (Return2Test[T1, T2]) Error added in v0.0.3

func (x Return2Test[T1, T2]) Error(t testing.TB) ErrorTest

Error check if the function returned error. If error is nil, it will fail. If error is not nil, it provides ErrorTest

func (Return2Test[T1, T2]) NoError added in v0.0.3

func (x Return2Test[T1, T2]) NoError(t testing.TB) (T1, T2)

NoError check if the function returned no error. If error is not nil, it will fail. If error is nil, it provides 1st and 2nd returned value.

type Return3Test added in v0.0.3

type Return3Test[T1, T2, T3 any] struct {
	// contains filtered or unexported fields
}

func R3 added in v0.0.3

func R3[T1, T2, T3 any](r1 T1, r2 T2, r3 T3, err error) Return3Test[T1, T2, T3]

func Return3 added in v0.0.3

func Return3[T1, T2, T3 any](r1 T1, r2 T2, r3 T3, err error) Return3Test[T1, T2, T3]

Return3 creates a test for returned variables (three values and one error)

f := func() (string, int, bool, error) {
	return "ok", 1, true, nil
}
gt.Return3(f()).Error()           // Fail
s, i, b := gt.Return1(f()).NoError() // Pass

s.Equal("ok") // Pass
i.Equal(1)    // Pass
b.Equal(true) // Pass

func (Return3Test[T1, T2, T3]) Error added in v0.0.3

func (x Return3Test[T1, T2, T3]) Error(t testing.TB) ErrorTest

Error check if the function returned error. If error is nil, it will fail. If error is not nil, it provides ErrorTest

func (Return3Test[T1, T2, T3]) NoError added in v0.0.3

func (x Return3Test[T1, T2, T3]) NoError(t testing.TB) (T1, T2, T3)

NoError check if the function returned no error. If error is not nil, it will fail. If error is nil, it provides 1st, 2nd and 3rd returned value.

type StringTest added in v0.0.5

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

func S added in v0.0.5

func S(t testing.TB, actual string) StringTest

S is sugar syntax of String

func String added in v0.0.5

func String(t testing.TB, actual string) StringTest

String provides StringTest that has basic comparison methods

func (StringTest) Contains added in v0.0.5

func (x StringTest) Contains(sub string) StringTest

Contains check if actual contains expected.

func (StringTest) Equal added in v0.0.5

func (x StringTest) Equal(expect string) StringTest

Equal check if actual equals with expect. Default evaluation function uses reflect.DeepEqual.

func (StringTest) HasPrefix added in v0.0.5

func (x StringTest) HasPrefix(prefix string) StringTest

HasPrefix check if actual has prefix expected.

func (StringTest) HasSuffix added in v0.0.5

func (x StringTest) HasSuffix(suffix string) StringTest

HasSuffix check if actual has suffix expected.

func (StringTest) IsEmpty added in v0.0.5

func (x StringTest) IsEmpty() StringTest

IsEmpty check if actual is empty.

func (StringTest) IsNotEmpty added in v0.0.5

func (x StringTest) IsNotEmpty() StringTest

IsNotEmpty check if actual is not empty.

func (StringTest) Match added in v0.0.5

func (x StringTest) Match(pattern string) StringTest

Match check if actual matches with expected regular expression.

func (StringTest) Must added in v0.0.5

func (x StringTest) Must() StringTest

Must check if error has occurred in previous test. If errors will occur in following test, it immediately stop test by t.FailNow().

func (StringTest) NotContains added in v0.0.5

func (x StringTest) NotContains(sub string) StringTest

NotContains check if actual does not contain expected.

func (StringTest) NotEqual added in v0.0.5

func (x StringTest) NotEqual(expect string) StringTest

NotEqual check if actual does not equals with expect. Default evaluation function uses reflect.DeepEqual.

func (StringTest) NotHasPrefix added in v0.0.5

func (x StringTest) NotHasPrefix(prefix string) StringTest

NotHasPrefix check if actual does not have prefix expected.

func (StringTest) NotHasSuffix added in v0.0.5

func (x StringTest) NotHasSuffix(suffix string) StringTest

NotHasSuffix check if actual does not have suffix expected.

func (StringTest) NotMatch added in v0.0.5

func (x StringTest) NotMatch(pattern string) StringTest

NotMatch check if actual matches with expected regular expression.

type ValueTest

type ValueTest[T any] struct {
	// contains filtered or unexported fields
}

func V

func V[T any](t testing.TB, actual T) ValueTest[T]

V is sugar syntax of Value

func Value

func Value[T any](t testing.TB, actual T) ValueTest[T]

Value provides ValueTest that has basic comparison methods

Example (Nil)

nolint

t := newRecorder()

a := "test"

gt.Value(t, &a).Nil()

fmt.Println(t.msgs[0])
Output:

func (ValueTest[T]) Equal

func (x ValueTest[T]) Equal(expect T) ValueTest[T]

Equal check if actual equals with expect. Default evaluation function uses reflect.DeepEqual.

type user struct {
  Name string
}
u1 := user{Name: "blue"}
gt.Value(t, u1).Equal(user{Name: "blue"}) // Pass
gt.Value(t, u1).Equal(user{Name: "orange"}) // Fail

func (ValueTest[T]) In added in v0.0.6

func (x ValueTest[T]) In(expects ...T) ValueTest[T]

In checks actual is in expects. Default evaluation function uses reflect.DeepEqual.

func (ValueTest[T]) Must

func (x ValueTest[T]) Must() ValueTest[T]

Must check if error has occurred in previous test. If errors will occur in following test, it immediately stop test by t.Failed().

name := "Alice"
gt.Value(name).Equal("Bob").Must() // Test will stop here

func (ValueTest[T]) Nil

func (x ValueTest[T]) Nil() ValueTest[T]

Nil checks if actual is nil.

var u *User
gt.Value(t, u).Nil() // Pass
u = &User{}
gt.Value(t, u).Nil() // Fail

func (ValueTest[T]) NotEqual

func (x ValueTest[T]) NotEqual(expect T) ValueTest[T]

NotEqual check if actual does not equals with expect. Default evaluation function uses reflect.DeepEqual.

type user struct {
  Name string
}
u1 := user{Name: "blue"}
gt.Value(t, u1).NotEqual(user{Name: "blue"})   // Fail
gt.Value(t, u1).NotEqual(user{Name: "orange"}) // Pass

func (ValueTest[T]) NotNil

func (x ValueTest[T]) NotNil() ValueTest[T]

NotNil checks if actual is not nil.

var u *User
gt.Value(t, u).Nil() // Fail
u = &User{}
gt.Value(t, u).Nil() // Pass

Jump to

Keyboard shortcuts

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