assert

package
v4.15.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2019 License: MIT Imports: 7 Imported by: 175

README

V Assertion Library

Yet another assertion library for Golang.

Quick Start

import (
    . "v2ray.com/ext/assert"
)

func TestStringEquals(t *testing.T) {
    assert := With(t)

    str := "Hello" + " " + "World"
    assert(str, Equals, "Hello World")
}

Usage

The assert function take at least 2 parameters: assert(value, matcher, expectations...), where:

  • value is the value to be asserted.
  • matcher is a "function" to assert the value.
  • expectations are optional parameters that will be passed into matcher for the assertion.

There are several predefined matchers:

  • Equals
  • LessThan
  • GreaterThan
  • AtLeast
  • AtMost
  • IsNil
  • IsNotNil
  • HasSubstring

Documentation

Overview

Package assert provides basic assertions for unit tests.

Example Usage

The following examples show how to use this library:

import (
  "testing"
  . "v2ray.com/ext/assert"
)

func TestSomething(t *testing.T) {
  assert := With(t)

  a := 10
  b := 1
  assert(a, Equals, b)
}

Index

Constants

This section is empty.

Variables

View Source
var AtLeast = CreateMatcher(func(v interface{}, exp interface{}) bool {
	return !callInternal(less, v, exp)
}, "less than")
View Source
var AtMost = CreateMatcher(func(v interface{}, exp interface{}) bool {
	return !callInternal(greater, v, exp)
}, "less than or equals to")
View Source
var Equals = CreateMatcher(func(v interface{}, exp interface{}) bool {
	vt := reflect.TypeOf(v)
	op, found := equals[vt]
	if found {
		return op.call(v, []interface{}{exp})
	}
	return v == exp
}, "equals to")

Equals is a Matcher that expects two given values are equal to each other.

View Source
var GreaterThan = CreateMatcher(func(v interface{}, exp interface{}) bool {
	return callInternal(greater, v, exp)
}, "less than")
View Source
var HasDone = CreateMatcher(func(ctx context.Context) bool {
	select {
	case <-ctx.Done():
		return true
	default:
		return false
	}
}, "has done.")
View Source
var HasPrefix = CreateMatcher(func(a, b string) bool {
	return strings.HasPrefix(a, b)
}, "has prefix")
View Source
var HasStringElement = CreateMatcher(func(a []string, b string) bool {
	for _, v := range a {
		if v == b {
			return true
		}
	}
	return false
}, "contains string element")
View Source
var HasSubstring = CreateMatcher(func(a, b string) bool {
	return strings.Contains(a, b)
}, "contains substring")
View Source
var HasSuffix = CreateMatcher(func(a, b string) bool {
	return strings.HasSuffix(a, b)
}, "has suffix")
View Source
var Implements = CreateMatcher(func(v interface{}, exp interface{}) bool {
	return reflect.TypeOf(v).Implements(reflect.TypeOf(exp).Elem())
}, "implements")
View Source
var IsEmpty = CreateMatcher(func(v interface{}) bool {
	return reflect.ValueOf(v).Len() == 0
}, "is empty")
View Source
var IsFalse = CreateMatcher(func(v bool) bool {
	return !v
}, "is false")
View Source
var IsNegative = CreateMatcher(func(v interface{}, exp interface{}) bool {
	return callInternal(less, v, 0)
}, "is negative")
View Source
var IsNil = CreateMatcher(func(v interface{}) bool {
	return v == nil || reflect.ValueOf(v).IsNil()
}, "is nil")
View Source
var IsNotNil = CreateMatcher(func(v interface{}) bool {
	return v != nil
}, "is not nil")
View Source
var IsPositive = CreateMatcher(func(v interface{}, exp interface{}) bool {
	return callInternal(greater, v, 0)
}, "is positive")
View Source
var IsTrue = CreateMatcher(func(v bool) bool {
	return v
}, "is true")
View Source
var LessThan = CreateMatcher(func(v interface{}, exp interface{}) bool {
	return callInternal(less, v, exp)
}, "less than")

LessThan expects the given value is less than the expectation.

View Source
var NotEquals = CreateMatcher(func(v interface{}, exp interface{}) bool {
	vt := reflect.TypeOf(v)
	op, found := equals[vt]
	if found {
		return !op.call(v, []interface{}{exp})
	}
	return v != exp
}, "not equals to")

NotEquals is a Matcher that expects two given values are not equal to each other.

View Source
var Panics = CreateMatcher(func(v interface{}) (ret bool) {
	defer func() {
		if x := recover(); x != nil {
			ret = true
		}
	}()
	if vf, ok := v.(func()); ok {
		vf()
	}
	return false
}, "panics")

Functions

func RegisterEqualsMatcher

func RegisterEqualsMatcher(f interface{})

func RegisterGreaterThanMatcher

func RegisterGreaterThanMatcher(f interface{})

func RegisterLessThanMatcher

func RegisterLessThanMatcher(f interface{})

Types

type Assert

type Assert func(value interface{}, op *Matcher, expectations ...interface{})

Assert asserts the given value matches expectations.

func With

func With(t *testing.T) Assert

With creates wrap the testing object into an assertion.

type Matcher

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

func CreateMatcher

func CreateMatcher(fv interface{}, verb string) *Matcher

func Not

func Not(op *Matcher) *Matcher

Jump to

Keyboard shortcuts

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