assert

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2020 License: BSD-3-Clause Imports: 6 Imported by: 1

Documentation

Overview

Package assert provides some assertion functions for testing.

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

Index

Examples

Constants

This section is empty.

Variables

View Source
var IncludeFilePosition = true

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

Functions

func Equal

func Equal(t testing.TB, name string, act, exp interface{}) bool
Example
// 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

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

func False

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

func NoError

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

func NoErrorOrDie

func NoErrorOrDie(t testing.TB, err error)

func NotEqual

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

func Panic

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

func Should

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

func ShouldOrDie

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

func StringEqual

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).

Example
// 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

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

func ValueShould

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.

Example
// 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)

Types

This section is empty.

Jump to

Keyboard shortcuts

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