gospec

package module
v0.0.0-...-d614778 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2017 License: MIT Imports: 21 Imported by: 0

README

gospec

Testing framework for golang

Documentation

Overview

Package assert provides a set of comprehensive testing tools for use with the normal Go testing system.

Example Usage

The following is a complete example using assert in a standard test function:

	  import (
	      "testing"
	      "github.com/golib/assert"
   )

	  func TestWithGlobalAssertions(t *testing.T) {
	      var (
	          a = "Hello"
	          b = "Hello"
	      )

	      gospec.Equal(t, a, b, "The two words should be the same.")
   }

use assert instance:

	  import (
	      "testing"
	      "github.com/golib/assert"
   )

   func TestWithAssertInstance(t *testing.T) {
       assert := gospec.NewAssertion(t)

       var (
           a = "Hello"
           b = "Hello"
       )

       assert.Equal(a, b, "The two words should be the same.")
   }

use expect instance:

	  import (
	      "testing"
	      "github.com/golib/assert"
   )

   func TestWithExpectInstance(t *testing.T) {
       it := gospec.NewExpectation(t)

       var (
           a = "Hello"
           b = "Hello"
       )

       it("should be the same", func(expect *gospec.S){
           expect(a).Equal(b)
       })
   }

Assertions

Assertions allow you to easily write test code, and are global funcs in the `gospec` package. All assertion functions take, as the first argument, the `*testing.T` object provided by the testing framework. This allows the assertion funcs to write the failings and other details to the correct place.

Every assertion function also takes an optional string message as the final argument, allowing custom error messages to be appended to the message the assertion method outputs.

Index

Constants

This section is empty.

Variables

View Source
var (
	AnError = errors.New("gospec.AnError general error for testing")
)

AnError is an error instance useful for testing. If the code does not care about error specifics, and only needs to return the error for example, this error should be used to make the test code more readable.

Functions

func Condition

func Condition(t TestingT, comp Comparison, extras ...interface{}) bool

Condition uses custom Comparison to assert a complex condition.

func Contains

func Contains(t TestingT, v, element interface{}, extras ...interface{}) bool

Contains asserts that the specified string, list(array, slice, channel...) or map contains the specified substring or element.

assert.Contains(t, "Hello World", "World", "But 'Hello World' does contain 'World'")
assert.Contains(t, ["Hello", "World"], "World", "But ["Hello", "World"] does contain 'World'")
assert.Contains(t, {"Hello": "World"}, "Hello", "But {'Hello': 'World'} does contain 'Hello'")

Returns whether the assertion was successful (true) or not (false).

func ContainsElement

func ContainsElement(v, element interface{}) (ok bool)

ContainsElement try loop over the list checking if the list includes the element.

return false if impossible.
return true if element was found, false otherwise.

func DeepEqual

func DeepEqual(expected, actual interface{}) bool

DeepEqual determines if two objects are considered equal.

This function does no assertion of any kind.

func DeepEqualValues

func DeepEqualValues(expected, actual interface{}) bool

DeepEqualValues gets whether two objects are equal, or if their values are equal.

NOTE: it returns true if two values are func with the same addr.

func Empty

func Empty(t TestingT, v interface{}, extras ...interface{}) bool

Empty asserts that the specified value is empty. I.e. nil, "", false, 0 or either a slice or a channel with len == 0.

assert.Empty(t, obj)

Returns whether the assertion was successful (true) or not (false).

func Equal

func Equal(t TestingT, expected, actual interface{}, extras ...interface{}) bool

Equal asserts that two values are equal.

assert.Equal(t, 123, 123, "123 and 123 should be equal")

Returns whether the assertion was successful (true) or not (false).

Pointer variable equality is determined based on the equality of the referenced values (as opposed to the memory addresses).

func EqualErrors

func EqualErrors(t TestingT, actualErr, expectedErr interface{}, extras ...interface{}) bool

EqualErrors asserts that a value is an error (i.e. not `nil`) and it is equal to the provided error string.

_, err := Func()
assert.EqualErrors(t, err,  expectedErrorString, "An error was expected")

Returns whether the assertion was successful (true) or not (false).

func EqualJSON

func EqualJSON(t TestingT, expected, actual string, extras ...interface{}) bool

EqualJSON asserts that two JSON strings are equivalent.

assert.EqualJSON(t, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)

Returns whether the assertion was successful (true) or not (false).

func EqualValues

func EqualValues(t TestingT, expected, actual interface{}, extras ...interface{}) bool

EqualValues asserts that two objects are equal or convertable to the same types and equal.

assert.EqualValues(t, uint32(123), int32(123), "uint32(123) and int32(123) should be equal values")

Returns whether the assertion was successful (true) or not (false).

func Error

func Error(t TestingT, v interface{}, extras ...interface{}) bool

Error asserts that a value is an error (i.e. `errors.New("some message")`).

_, err := Func()
assert.Error(t, err, "An error was expected")

Returns whether the assertion was successful (true) or not (false).

func Errorf

func Errorf(t TestingT, err string, extras ...interface{}) bool

Errorf reports a failure through and return false

func Exactly

func Exactly(t TestingT, expected, actual interface{}, extras ...interface{}) bool

Exactly asserts that two values are equal, both value and type.

assert.Exactly(t, int32(123), int64(123), "int32(123) and int64(123) should NOT be equal")

Returns whether the assertion was successful (true) or not (false).

func False

func False(t TestingT, v interface{}, extras ...interface{}) bool

False asserts that the specified value is false.

assert.False(t, myBool, "myBool should be false")

Returns whether the assertion was successful (true) or not (false).

func Implements

func Implements(t TestingT, expectedIface, actual interface{}, extras ...interface{}) bool

Implements asserts that the specified value implements the specified interface.

assert.Implements(t, (*MyInterface)(nil), new(MyObject), "MyObject should implement MyInterface")

Returns whether the assertion was successful (true) or not (false).

func InDelta

func InDelta(t TestingT, expected, actual interface{}, delta float64, extras ...interface{}) bool

InDelta asserts that the two numerals are within delta of each other.

assert.InDelta(t, math.Pi, (22 / 7.0), 0.01)

Returns whether the assertion was successful (true) or not (false).

func IsEmpty

func IsEmpty(v interface{}) bool

IsEmpty gets whether the specified value is considered empty or not.

returns true if value is nil or nil pointer
returns true if value is empty string
returns true if value is false
returns true if member of slice/map/channel is empty
returns true if value is equal to zero value of it's type

func IsNil

func IsNil(v interface{}) bool

IsNil checks if a specified value is nil or not, without Failing.

func IsType

func IsType(t TestingT, expected, actual interface{}, extras ...interface{}) bool

IsType asserts that the specified values are of the same type.

assert.IsType(t, int32, int32(123), "int32(123) should to be of type int32")

Returns whether the assertion was successful (true) or not (false).

func JSONContains

func JSONContains(t TestingT, jsonData, searchKeyPath string, extras ...interface{}) bool

JSONContains asserts that JSON strings contains specified key.

assert.JSONContains(t, `{"hello": "world", "foo": "bar"}`, "hello")

Returns whether the assertion was successful (true) or not (false).

func JSONEqualValues

func JSONEqualValues(t TestingT, jsonData, searchKeyPath string, expected interface{}, extras ...interface{}) bool

JSONEqualValues asserts that JSON strings contains value with specified key.

assert.JSONContains(t, `{"hello": "world", "foo": "bar"}`, "hello", "world")

Returns whether the assertion was successful (true) or not (false).

func Len

func Len(t TestingT, v interface{}, length int, extras ...interface{}) bool

Len asserts that the specified value has specific length. It will fail if the value has a type that len() not accept.

assert.Len(t, mySlice, 3, "The size of slice is not 3")

Returns whether the assertion was successful (true) or not (false).

func Match

func Match(t TestingT, r, v interface{}, extras ...interface{}) bool

Match asserts that a specified regexp matches the given value.

assert.Match(t, regexp.MustCompile("start"), "it's starting")
assert.Match(t, "start...$", "it's not starting")

Returns whether the assertion was successful (true) or not (false).

func Nil

func Nil(t TestingT, v interface{}, extras ...interface{}) bool

Nil asserts that the specified value is nil.

assert.Nil(t, err, "err should be nothing")

Returns whether the assertion was successful (true) or not (false).

func NotContains

func NotContains(t TestingT, v, element interface{}, extras ...interface{}) bool

NotContains asserts that the specified string, list(array, slice, channel...) or map does NOT contain the specified substring or element.

assert.NotContains(t, "Hello World", "Earth", "But 'Hello World' does NOT contain 'Earth'")
assert.NotContains(t, ["Hello", "World"], "Earth", "But ['Hello', 'World'] does NOT contain 'Earth'")
assert.NotContains(t, {"Hello": "World"}, "Earth", "But {'Hello': 'World'} does NOT contain 'Earth'")

Returns whether the assertion was successful (true) or not (false).

func NotEmpty

func NotEmpty(t TestingT, v interface{}, extras ...interface{}) bool

NotEmpty asserts that the specified value is NOT empty. I.e. not nil, "", false, 0 or either a slice or a channel with len == 0.

assert.NotEmpty(t, obj)

Returns whether the assertion was successful (true) or not (false).

func NotEqual

func NotEqual(t TestingT, expected, actual interface{}, extras ...interface{}) bool

NotEqual asserts that the specified values are NOT equal.

assert.NotEqual(t, obj1, obj2, "two objects shouldn't be equal")

Returns whether the assertion was successful (true) or not (false).

Pointer variable equality is determined based on the equality of the referenced values (as opposed to the memory addresses).

func NotError

func NotError(t TestingT, v interface{}, extras ...interface{}) bool

NotError asserts that a value is not an error (i.e. `nil`).

_, err := Func()
assert.NotError(t, err)

Returns whether the assertion was successful (true) or not (false).

func NotMatch

func NotMatch(t TestingT, r, v interface{}, extras ...interface{}) bool

NotMatch asserts that a specified regexp does not match a stringify of the given value.

assert.NotMatch(t, regexp.MustCompile("starts"), "it's starting")
assert.NotMatch(t, "^start", "it's not starting")

Returns whether the assertion was successful (true) or not (false).

func NotNil

func NotNil(t TestingT, v interface{}, extras ...interface{}) bool

NotNil asserts that the specified value is not nil.

assert.NotNil(t, err, "err should be something")

Returns whether the assertion was successful (true) or not (false).

func NotPanics

func NotPanics(t TestingT, f PanicRecover, extras ...interface{}) bool

NotPanics asserts that the code inside the specified PanicRecover does NOT panic.

assert.NotPanics(t, func(){
  RemainCalm()
}, "Calling RemainCalm() should NOT panic")

Returns whether the assertion was successful (true) or not (false).

func NotZero

func NotZero(t TestingT, v interface{}, extras ...interface{}) bool

NotZero asserts that v is not the zero value for its type and returns the truth.

func Panics

func Panics(t TestingT, f PanicRecover, extras ...interface{}) bool

Panics asserts that the code inside the specified PanicRecover panics.

assert.Panics(t, func(){
  GoCrazy()
}, "Calling GoCrazy() should panic")

Returns whether the assertion was successful (true) or not (false).

func True

func True(t TestingT, v interface{}, extras ...interface{}) bool

True asserts that the specified value is true.

assert.True(t, myBool, "myBool should be true")

Returns whether the assertion was successful (true) or not (false).

func WithinDuration

func WithinDuration(t TestingT, expected, actual time.Time, delta time.Duration, extras ...interface{}) bool

WithinDuration asserts that the two times are within duration delta of each other.

assert.WithinDuration(t, time.Now(), time.Now(), 10*time.Second, "The difference should not be more than 10s")

Returns whether the assertion was successful (true) or not (false).

func Zero

func Zero(t TestingT, v interface{}, extras ...interface{}) bool

Zero asserts that v is the zero value for its type and returns the truth.

Types

type Comparison

type Comparison func() (success bool)

Comparison defines a custom func that returns true on success and false on failure

type PanicRecover

type PanicRecover func()

PanicRecover defines a func that should be passed to the assert.Panics and assert.NotPanics methods, and represents a simple func that takes no arguments, and returns nothing.

type TestingT

type TestingT interface {
	Errorf(format string, args ...interface{})
}

TestingT is the interface that wraps the basic Errorf method.

Errorf formats its arguments according to the format, analogous to Printf, and records the text in the error log. A final newline is added if not provided.

Jump to

Keyboard shortcuts

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