testing: github.com/golangplus/testing/assert Index | Examples | Files

package assert

import "github.com/golangplus/testing/assert"

Package assert provides some assertion functions for testing.

Return values: true if the assert holds, false otherwise.

Index

Examples

Package Files

assert.go match.go

Variables

var IncludeFilePosition = true

Set this to false to avoid include file position in logs.

func Equal Uses

func Equal(t testing.TB, name string, act, exp interface{}) bool

Code:

// The following two lines are for test/example of assert package itself. Use
// *testing.T as t in normal testing instead.
IncludeFilePosition = false
defer func() { IncludeFilePosition = true }()
t := &testingp.WriterTB{Writer: os.Stdout}

Equal(t, "v", 1, 2)
Equal(t, "v", 1, "1")
Equal(t, "m", map[string]int{"Extra": 2, "Modified": 4}, map[string]int{"Missing": 1, "Modified": 5})

Output:

v is expected to be 2, but got 1
v is expected to be "1"(type=string), but got 1(type=int)
m is unexpected:
  m["Modified"] is expected to be 5, but got 4
  extra "Extra" -> 2
  missing "Missing" -> 1

func Error Uses

func Error(t testing.TB, err error) bool

func False Uses

func False(t testing.TB, name string, act bool) bool

func NoError Uses

func NoError(t testing.TB, err error) bool

func NoErrorOrDie Uses

func NoErrorOrDie(t testing.TB, err error)

func NotEqual Uses

func NotEqual(t testing.TB, name string, act, exp interface{}) bool

func Panic Uses

func Panic(t testing.TB, name string, f func()) bool

func Should Uses

func Should(t testing.TB, vl bool, showIfFailed string) bool

func ShouldOrDie Uses

func ShouldOrDie(t testing.TB, vl bool, showIfFailed string)

func StringEqual Uses

func StringEqual(t testing.TB, name string, act, exp interface{}) bool

StringEqual compares the string representation of the values. If act and exp are both slices, they were matched by elements and the results are presented in a diff style (if not totally equal).

Code:

// The following two lines are for test/example of assert package itself. Use
// *testing.T as t in normal testing instead.
IncludeFilePosition = false
defer func() { IncludeFilePosition = true }()
t := &testingp.WriterTB{Writer: os.Stdout}

StringEqual(t, "s", []int{2, 3}, []string{"1", "2"})
StringEqual(t, "s", `
Extra
Modified act`, `
Modified exp
Missing`)

Output:

Unexpected s: both 2 lines
  Difference(expected ---  actual +++)
    ---   1: "1"
    +++   2: "3"
Unexpected s: both 3 lines
  Difference(expected ---  actual +++)
    ---   2: "Modified exp"
    ---   3: "Missing"
    +++   2: "Extra"
    +++   3: "Modified act"

func True Uses

func True(t testing.TB, name string, act bool) bool

func ValueShould Uses

func ValueShould(t testing.TB, name string, act interface{}, expToFunc interface{}, descIfFailed string) bool

@param expToFunc could be a func with a single input value and a bool return, or a bool value directly.

Code:

// The following two lines are for test/example of assert package itself. Use
// *testing.T as t in normal testing instead.
IncludeFilePosition = false
defer func() { IncludeFilePosition = true }()
t := &testingp.WriterTB{Writer: os.Stdout}

ValueShould(t, "s", "\xff\xfe\xfd", utf8.ValidString, "is not valid UTF8")
ValueShould(t, "s", "abcd", len("abcd") <= 3, "has more than 3 bytes")

Output:

s is not valid UTF8: "\xff\xfe\xfd"(type string)
s has more than 3 bytes: "abcd"(type string)

Package assert imports 6 packages (graph). Updated 2018-03-28. Refresh now. Tools for package owners.