assert

package
v0.21.4 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2019 License: Apache-2.0 Imports: 8 Imported by: 18

README

GoKit - assert

Assert kits for Golang development.

Installation

go get -u github.com/likexian/gokit

Importing

import (
    "github.com/likexian/gokit/assert"
)

Documentation

Visit the docs on GoDoc

Example

assert panic
func willItPanic() {
    panic("failed")
}
assert.Panic(t, willItPanic)
assert err is nil
fp, err := os.Open("/data/dev/gokit/LICENSE")
assert.Nil(t, err)
assert equal
x := map[string]int{"a": 1, "b": 2}
y := map[string]int{"a": 1, "b": 2}
assert.Equal(t, x, y, "x shall equal to y")
check string in array
ok := assert.IsContains([]string{"a", "b", "c"}, "b")
if ok {
    fmt.Println("value in array")
} else {
    fmt.Println("value not in array")
}
check string in interface array
ok := assert.IsContains([]interface{}{0, "1", 2}, "1")
if ok {
    fmt.Println("value in array")
} else {
    fmt.Println("value not in array")
}
check object in struct array
ok := assert.IsContains([]A{A{0, 1}, A{1, 2}, A{1, 3}}, A{1, 2})
if ok {
    fmt.Println("value in array")
} else {
    fmt.Println("value not in array")
}
a := c ? x : y
a := 1
// b := a == 1 ? true : false
b := assert.If(a == 1, true, false)

LICENSE

Copyright 2012-2019 Li Kexian

Licensed under the Apache License 2.0

About

DONATE

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CMP = struct {
	LT string
	LE string
	GT string
	GE string
}{
	"<",
	"<=",
	">",
	">=",
}

CMP is compare operation

View Source
var ErrGreater = errors.New("left is greater then right")

ErrGreater is expect to be less error

View Source
var ErrInvalid = errors.New("value if invalid")

ErrInvalid is value invalid for operation

View Source
var ErrLess = errors.New("left is less the right")

ErrLess is expect to be greater error

Functions

func Author

func Author() string

Author returns package author

func Compare

func Compare(x, y interface{}, op string) error

Compare compare x and y, by operation It returns nil for true, ErrInvalid for invalid operation, err for false

Compare(1, 2, ">") // number compare -> true
Compare("a", "a", ">=") // string compare -> true
Compare([]string{"a", "b"}, []string{"a"}, "<") // slice len compare -> false

func Contains

func Contains(t *testing.T, got, exp interface{}, args ...interface{})

Contains assert test value to be contains

func Equal

func Equal(t *testing.T, got, exp interface{}, args ...interface{})

Equal assert test value to be equal

func False

func False(t *testing.T, got interface{}, args ...interface{})

False assert test value to be false

func Ge

func Ge(t *testing.T, got, exp interface{}, args ...interface{})

Ge assert test value greater than exp or equal

func Gt

func Gt(t *testing.T, got, exp interface{}, args ...interface{})

Gt assert test value greater than exp

func If

func If(c bool, x, y interface{}) interface{}

If returns x if c is true, else y

z = If(c, x, y)

equal to:

z = c ? x : y

func IsContains

func IsContains(array interface{}, value interface{}) bool

IsContains returns whether value is within array

func IsGe

func IsGe(x, y interface{}) bool

IsGe returns if x greater than or equal to y, value invalid will returns false

func IsGt

func IsGt(x, y interface{}) bool

IsGt returns if x greater than y, value invalid will returns false

func IsLe

func IsLe(x, y interface{}) bool

IsLe returns if x less than or equal to y, value invalid will returns false

func IsLt

func IsLt(x, y interface{}) bool

IsLt returns if x less than y, value invalid will returns false

func IsMatch

func IsMatch(r interface{}, v interface{}) bool

IsMatch returns if value v contains any match of pattern r

IsMatch(regexp.MustCompile("v\d+"), "v100")
IsMatch("v\d+", "v100")
IsMatch("\d+\.\d+", 100.1)

func IsZero

func IsZero(v interface{}) bool

IsZero returns value is zero value

func Le

func Le(t *testing.T, got, exp interface{}, args ...interface{})

Le assert test value less than exp or equal

func Len

func Len(t *testing.T, got interface{}, exp int, args ...interface{})

Len assert length of test vaue to be exp

func Length

func Length(v interface{}) int

Length returns length of value

func License

func License() string

License returns package license

func Lt

func Lt(t *testing.T, got, exp interface{}, args ...interface{})

Lt assert test value less than exp

func Match

func Match(t *testing.T, got, exp interface{}, args ...interface{})

Match assert test value match exp pattern

func Nil

func Nil(t *testing.T, got interface{}, args ...interface{})

Nil assert test value to be nil

func NotContains

func NotContains(t *testing.T, got, exp interface{}, args ...interface{})

NotContains assert test value to be contains

func NotEqual

func NotEqual(t *testing.T, got, exp interface{}, args ...interface{})

NotEqual assert test value to be not equal

func NotLen

func NotLen(t *testing.T, got interface{}, exp int, args ...interface{})

NotLen assert length of test vaue to be not exp

func NotMatch

func NotMatch(t *testing.T, got, exp interface{}, args ...interface{})

NotMatch assert test value not match exp pattern

func NotNil

func NotNil(t *testing.T, got interface{}, args ...interface{})

NotNil assert test value to be not nil

func NotPanic

func NotPanic(t *testing.T, fn func(), args ...interface{})

NotPanic assert testing to be panic

func NotZero

func NotZero(t *testing.T, got interface{}, args ...interface{})

NotZero assert test value to be not zero value

func Panic

func Panic(t *testing.T, fn func(), args ...interface{})

Panic assert testing to be panic

func ToFloat64

func ToFloat64(v interface{}) (float64, error)

ToFloat64 returns float64 value for int or uint or float

func ToInt64

func ToInt64(v interface{}) (int64, error)

ToInt64 returns int value for int or uint or float

func ToUint64

func ToUint64(v interface{}) (uint64, error)

ToUint64 returns uint value for int or uint or float

func True

func True(t *testing.T, got interface{}, args ...interface{})

True assert test value to be true

func Version

func Version() string

Version returns package version

func Zero

func Zero(t *testing.T, got interface{}, args ...interface{})

Zero assert test value to be zero value

Types

This section is empty.

Jump to

Keyboard shortcuts

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