assert

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2017 License: Apache-2.0 Imports: 13 Imported by: 9

Documentation

Index

Examples

Constants

View Source
const (
	// Log is the informational level.
	Log = level(iota)
	// Error is used for things that cause test failures but do not abort.
	Error
	// Fatal is used for failures that cause the running test to immediately stop.
	Fatal
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Assertion

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

Assertion is the type for the start of an assertion line. You construct an assertion from an Output using either assert.With or assert.For.

func For deprecated

func For(t interface{}, msg string, args ...interface{}) *Assertion

Deprecated: For(m) is deprecated, use assert.To() instead.

func With deprecated

func With(t interface{}) *Assertion

Deprecated: With is deprecated, use For with a message.

func (*Assertion) Add

func (a *Assertion) Add(key string, values ...interface{}) *Assertion

Add appends a key value pair to the output buffer.

func (Assertion) Commit

func (a Assertion) Commit()

Commit writes the output lines to the main output object.

func (*Assertion) Compare

func (a *Assertion) Compare(value interface{}, op string, expect ...interface{}) *Assertion

Compare adds both the "Got" and "Expect" entries to the output buffer, with the operator being prepended to the expect list.

func (*Assertion) CompareRaw

func (a *Assertion) CompareRaw(value interface{}, op string, expect ...interface{}) *Assertion

CompareRaw is like Compare except it does not push the values through the pretty printer.

func (*Assertion) Critical

func (a *Assertion) Critical() *Assertion

Critical switches this assertion from Error to Fatal.

func (*Assertion) Error

func (a *Assertion) Error(args ...interface{})

Error appends the supplied message to the cached output, and then flushes to the underlying output at Error level.

func (*Assertion) Expect

func (a *Assertion) Expect(op string, values ...interface{}) *Assertion

Expect adds the standard "Expect" entry to the output buffer.

func (*Assertion) ExpectRaw

func (a *Assertion) ExpectRaw(op string, values ...interface{}) *Assertion

ExpectRaw adds the standard "Expect" entry to the output buffer, without pretty printing.

func (*Assertion) Fatal

func (a *Assertion) Fatal(args ...interface{})

Fatal appends the supplied message to the cached output, and then flushes to the underlying output at Fatal level.

func (*Assertion) Got

func (a *Assertion) Got(values ...interface{}) *Assertion

Got adds the standard "Got" entry to the output buffer.

func (*Assertion) Log

func (a *Assertion) Log(args ...interface{})

Log appends the supplied message to the cached output, and then flushes to the underlying output at Log level.

func (*Assertion) Print

func (a *Assertion) Print(args ...interface{}) *Assertion

Print writes a set of values to the output buffer, joined by tabs. The values will be printed with PrintValue.

func (Assertion) PrintPretty

func (a Assertion) PrintPretty(value interface{})

PrintPretty writes a value to the output buffer. It performs standardised transformations (mostly quoting)

func (*Assertion) Printf

func (a *Assertion) Printf(format string, args ...interface{}) *Assertion

Printf writes a formatted unquoted string to the output buffer.

func (*Assertion) Println

func (a *Assertion) Println(args ...interface{}) *Assertion

Println prints the values using Print and then starts a new indented line.

func (*Assertion) Raw

func (a *Assertion) Raw(args ...interface{}) *Assertion

Raw writes a set of values to the output buffer, joined by tabs. It does not use the pretty printer.

func (*Assertion) Rawln

func (a *Assertion) Rawln(args ...interface{}) *Assertion

Println prints the values using Print and then starts a new indented line.

func (*Assertion) Test

func (a *Assertion) Test(condition bool) bool

Test commits the pending output if the condition is not true.

func (*Assertion) TestDeepDiff

func (a *Assertion) TestDeepDiff(value interface{}, expect interface{}) bool

TestDeepDiff is equivalent to TestDeepEqual except it also prints a diff.

func (*Assertion) TestDeepEqual

func (a *Assertion) TestDeepEqual(value interface{}, expect interface{}) bool

TestDeepEqual adds the entries for Got and Expect, then tests if they are the same using compare.DeepEqual, commiting if they are not.

func (*Assertion) TestDeepNotEqual

func (a *Assertion) TestDeepNotEqual(value interface{}, expect interface{}) bool

TestDeepNotEqual adds the entries for Got and Expect, then tests if they are the same using compare.DeepEqual, commiting if they are.

func (Assertion) That

func (a Assertion) That(value interface{}) OnValue

That returns an OnValue for the specified untyped value.

func (Assertion) ThatDuration

func (a Assertion) ThatDuration(value time.Duration) OnDuration

ThatDuration returns an OnDuration for time duration based assertions.

func (Assertion) ThatEnum

func (a Assertion) ThatEnum(enum Enum) OnEnum

ThatEnum returns an OnEnum for assertions on enumeration objects.

func (Assertion) ThatError

func (a Assertion) ThatError(err error) OnError

ThatError returns an OnError for error type assertions.

func (Assertion) ThatFloat

func (a Assertion) ThatFloat(value float64) OnFloat

ThatFloat returns an OnFloat for floating point based assertions.

func (Assertion) ThatInteger

func (a Assertion) ThatInteger(value int) OnInteger

ThatInteger returns an OnInteger for integer based assertions.

func (Assertion) ThatSlice

func (a Assertion) ThatSlice(slice interface{}) OnSlice

ThatSlice returns an OnSlice for assertions on slice type objects. Calling this with a non slice type will result in panics.

func (Assertion) ThatString

func (a Assertion) ThatString(value interface{}) OnString

ThatString returns an OnString for string based assertions. The untyped argument is converted to a string using fmt.Sprint, and the result supports string specific tests.

type Enum

type Enum interface {
	// String matches the Stringer interface, and returns the name of the value.
	String() string
	// Parse takes a name and sets the value to match the name.
	Parse(value string) error
}

Enum is the interface for something that acts like an enumeration value.

Example

An example of testing enum values

package main

import (
	"encoding/json"
	"fmt"
	"strconv"
	"strings"

	"github.com/google/gapid/core/assert"
)

type testEnum int

type enumEntry struct {
	value testEnum
	name  string
}

var (
	enums = []enumEntry{
		{0, "UNKNOWN"},
		{1, "A"},
		{2, "B"},
		{3, "BadParse"},
		{4, "FailedParse"},
		{5, "BadJsonMarshal"},
		{6, "FailedJsonMarshal"},
		{7, "BadJsonUnmarshal"},
		{8, "FailedJsonUnmarshal"},
	}
	enumTests = append(enums, []enumEntry{
		{testEnum(-1), "testEnum(-1)"},
		{1, "B"},
	}...)
)

func (v testEnum) String() string {
	for _, e := range enums {
		if e.value == v {
			return e.name
		}
	}
	return fmt.Sprintf("testEnum(%d)", v)
}

func (v *testEnum) Parse(s string) error {
	if s == "BadParse" {
		*v = 0
		return nil
	}
	if s == "FailedParse" {
		s = "badparse"
	}
	for _, e := range enums {
		if e.name == s {
			*v = e.value
			return nil
		}
	}
	trimmed := strings.TrimSuffix(strings.TrimPrefix(s, "testEnum("), ")")
	if i, err := strconv.Atoi(trimmed); err == nil {
		*v = testEnum(i)
		return nil
	}
	return fmt.Errorf("%s not in testEnum", s)
}

func (v testEnum) MarshalJSON() ([]byte, error) {
	if v.String() == "FailedJsonMarshal" {
		return nil, fmt.Errorf("FailedJsonMarshal")
	}
	if v.String() == "BadJsonMarshal" {
		return json.Marshal("badjson")
	}
	return json.Marshal(v.String())
}

func (v *testEnum) UnmarshalJSON(bytes []byte) error {
	var str string
	if err := json.Unmarshal(bytes, &str); err != nil {
		return err
	}
	if str == "FailedJsonUnmarshal" {
		return fmt.Errorf("FailedJsonUnmarshal")
	}
	if str == "BadJsonUnmarshal" {
		*v = 0
		return nil
	}
	return v.Parse(str)
}

// An example of testing enum values
func main() {
	ctx := assert.Context(nil)
	for _, test := range enumTests {
		assert.With(ctx).ThatEnum(&test.value).HasName(test.name)
	}
	var enum testEnum
	assert.With(ctx).ThatEnum(&enum).CannotUnmarshal(`"A"`)
	assert.With(ctx).ThatEnum(&enum).CannotUnmarshal(`0`)

}
Output:

Error:
    For enum   BadParse
    Bad Parse  UNKNOWN
Error:
    For enum                FailedParse
    Unexpected parse error  `badparse not in testEnum`
Error:
    For enum  BadJsonMarshal
    Bad JSON  "badjson"
    Expect    "BadJsonMarshal"
Error:
    For enum            FailedJsonMarshal
    JSON marshal error  `json: error calling MarshalJSON for type *assert_test.testEnum: FailedJsonMarshal`
Error:
    For enum            BadJsonUnmarshal
    Bad JSON Unmarshal  UNKNOWN
Error:
    For enum              FailedJsonUnmarshal
    JSON unmarshal error  `FailedJsonUnmarshal`
Error:
    For enum       A
    Expected name  `B`
Error:
    For     "A"
    Got     A
    Expect  Unmarshal to fail

type Manager

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

Manager is the root of the fluent interface. It wraps an assertion output target in something that can construct assertion objects. The output object is normally a testing.T

func Context deprecated

func Context(out Output) Manager

Deprecated: Context is deprecated, To instead.

func To

func To(out Output) Manager

To creates an assertion manager for the given output.

func (Manager) For

func (ctx Manager) For(msg string, args ...interface{}) *Assertion

For starts a new assertion with the supplied title.

func (Manager) Print deprecated

func (m Manager) Print(args ...interface{})

Deprecated: Print is deprecated, use Log instead.

type OnDuration

type OnDuration struct {
	Assertion
	// contains filtered or unexported fields
}

OnDuration is the result of calling ThatDuration on an Assertion. It provides assertion tests that are specific to the time duration.

func (OnDuration) IsAtLeast

func (o OnDuration) IsAtLeast(min time.Duration) bool

IsAtLeast asserts that the time duration is at least than the supplied minimum.

func (OnDuration) IsAtMost

func (o OnDuration) IsAtMost(max time.Duration) bool

IsAtMost asserts that the time duration is at most than the supplied maximum.

type OnEnum

type OnEnum struct {
	Assertion
	// contains filtered or unexported fields
}

OnEnum is the result of calling ThatEnum on an Assertion. It provides enumeration assertion tests.

func (OnEnum) CannotUnmarshal

func (o OnEnum) CannotUnmarshal(data string) bool

CannotUnmarshal asserts that the enum type cannot unmarshal the supplied string without error.

func (OnEnum) HasName

func (o OnEnum) HasName(name string) bool

HasName asserts that the enum value has the supplied name. It verifies both the String/Parse and JSON marshalling.

type OnError

type OnError struct {
	Assertion
	// contains filtered or unexported fields
}

OnError is the result of calling ThatError on an Assertion. It provides assertion tests that are specific to error types.

func (OnError) DeepEquals

func (o OnError) DeepEquals(expect error) bool

DeepEquals asserts that the error value matches the expected error using compare.DeepEqual.

func (OnError) Equals

func (o OnError) Equals(expect error) bool

Equals asserts that the error value matches the expected error.

func (OnError) Failed

func (o OnError) Failed() bool

Failed asserts that the error value was not nil.

func (OnError) HasCause

func (o OnError) HasCause(expect error) bool

HasCause asserts that the error cause matches expected error.

func (OnError) HasMessage

func (o OnError) HasMessage(expect string) bool

HasMessage asserts that the error string matches the expected message.

func (OnError) Succeeded

func (o OnError) Succeeded() bool

Succeeded asserts that the error value was nil.

type OnFloat

type OnFloat struct {
	Assertion
	// contains filtered or unexported fields
}

OnFloat is the result of calling ThatFloat on an Assertion. It provides numeric assertion tests.

func (OnFloat) Equals

func (o OnFloat) Equals(v, tolerance float64) bool

Equals asserts that the float equals v with ± tolerance.

func (OnFloat) IsAtLeast

func (o OnFloat) IsAtLeast(min float64) bool

IsAtLeast asserts that the float is at least the supplied minimum.

func (OnFloat) IsAtMost

func (o OnFloat) IsAtMost(max float64) bool

IsAtMost asserts that the float is at most the supplied maximum.

type OnInteger

type OnInteger struct {
	Assertion
	// contains filtered or unexported fields
}

OnInteger is the result of calling ThatInt on an Assertion. It provides numeric assertion tests.

func (OnInteger) Equals

func (o OnInteger) Equals(expect int) bool

Equals asserts that the supplied integer is equal to the expected integer.

func (OnInteger) IsAtLeast

func (o OnInteger) IsAtLeast(min int) bool

IsAtLeast asserts that the integer is at least the supplied minimum.

func (OnInteger) IsAtMost

func (o OnInteger) IsAtMost(max int) bool

IsAtMost asserts that the integer is at most the supplied maximum.

func (OnInteger) IsBetween

func (o OnInteger) IsBetween(min, max int) bool

IsBetween asserts that the integer lies within the given range (inclusive).

func (OnInteger) NotEquals

func (o OnInteger) NotEquals(test int) bool

NotEquals asserts that the supplied integer is not equal to the test integer.

type OnSlice

type OnSlice struct {
	Assertion
	// contains filtered or unexported fields
}

OnSlice is the result of calling ThatSlice on an Assertion. It provides assertion tests that are specific to slice types.

func (OnSlice) DeepEquals

func (o OnSlice) DeepEquals(expected interface{}) bool

DeepEquals asserts the array or slice matches expected using a deep-equal comparison.

func (OnSlice) Equals

func (o OnSlice) Equals(expected interface{}) bool

Equals asserts the array or slice matches expected.

func (OnSlice) EqualsWithComparator

func (o OnSlice) EqualsWithComparator(expected interface{}, same func(a, b interface{}) bool) bool

EqualsWithComparator asserts the array or slice matches expected using a comparator function

func (OnSlice) IsEmpty

func (o OnSlice) IsEmpty() bool

IsEmpty asserts that the slice was of length 0

func (OnSlice) IsLength

func (o OnSlice) IsLength(length int) bool

IsLength asserts that the slice has exactly the specified number of elements

func (OnSlice) IsNotEmpty

func (o OnSlice) IsNotEmpty() bool

IsNotEmpty asserts that the slice has elements

type OnString

type OnString struct {
	Assertion
	// contains filtered or unexported fields
}

OnString is the result of calling ThatString on an Assertion. It provides assertion tests that are specific to strings.

func (OnString) Contains

func (o OnString) Contains(substr string) bool

Contains asserts that the supplied string contains substr.

func (OnString) DoesNotContain

func (o OnString) DoesNotContain(substr string) bool

DoesNotContain asserts that the supplied string does not contain substr.

func (OnString) Equals

func (o OnString) Equals(expect string) bool

Equals asserts that the supplied string is equal to the expected string.

func (OnString) HasPrefix

func (o OnString) HasPrefix(substr string) bool

HasPrefix asserts that the supplied string start with substr.

func (OnString) HasSuffix

func (o OnString) HasSuffix(substr string) bool

HasSuffix asserts that the supplied string ends with with substr.

func (OnString) NotEquals

func (o OnString) NotEquals(test string) bool

NotEquals asserts that the supplied string is not equal to the test string.

type OnValue

type OnValue struct {
	Assertion
	// contains filtered or unexported fields
}

OnValue is the result of calling That on an Assertion. It provides generice assertion tests that work for any type.

func (OnValue) DeepEquals

func (o OnValue) DeepEquals(expect interface{}) bool

DeepEquals asserts that the supplied value is equal to the expected value using compare.Diff.

func (OnValue) DeepNotEquals

func (o OnValue) DeepNotEquals(test interface{}) bool

DeepNotEquals asserts that the supplied value is not equal to the test value using a deep comparison.

func (OnValue) Equals

func (o OnValue) Equals(expect interface{}) bool

Equals asserts that the supplied value is equal to the expected value.

func (OnValue) Implements

func (o OnValue) Implements(iface reflect.Type) bool

Implements asserts that the supplied value implements the specified interface.

func (OnValue) IsNil

func (o OnValue) IsNil() bool

IsNil asserts that the supplied value was a nil. Typed nils are also be allowed.

func (OnValue) IsNotNil

func (o OnValue) IsNotNil() bool

IsNotNil asserts that the supplied value was not a nil. Typed nils will also fail.

func (OnValue) NotEquals

func (o OnValue) NotEquals(test interface{}) bool

NotEquals asserts that the supplied value is not equal to the test value.

type Output

type Output interface {
	Fatal(...interface{})
	Error(...interface{})
	Log(...interface{})
}

Output matches the logging methods of the test host types.

Jump to

Keyboard shortcuts

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