Documentation
¶
Index ¶
- Constants
- type Assertion
- func (a *Assertion) Add(key string, values ...interface{}) *Assertion
- func (a Assertion) Commit()
- func (a *Assertion) Compare(value interface{}, op string, expect ...interface{}) *Assertion
- func (a *Assertion) CompareRaw(value interface{}, op string, expect ...interface{}) *Assertion
- func (a *Assertion) Critical() *Assertion
- func (a *Assertion) Error(args ...interface{})
- func (a *Assertion) Expect(op string, values ...interface{}) *Assertion
- func (a *Assertion) ExpectRaw(op string, values ...interface{}) *Assertion
- func (a *Assertion) Fatal(args ...interface{})
- func (a *Assertion) Got(values ...interface{}) *Assertion
- func (a *Assertion) Log(args ...interface{})
- func (a *Assertion) Print(args ...interface{}) *Assertion
- func (a Assertion) PrintPretty(value interface{})
- func (a *Assertion) Printf(format string, args ...interface{}) *Assertion
- func (a *Assertion) Println(args ...interface{}) *Assertion
- func (a *Assertion) Raw(args ...interface{}) *Assertion
- func (a *Assertion) Rawln(args ...interface{}) *Assertion
- func (a *Assertion) Test(condition bool) bool
- func (a *Assertion) TestCustomDeepDiff(value, expect interface{}, c compare.Custom) bool
- func (a *Assertion) TestCustomDeepEqual(value, expect interface{}, c compare.Custom) bool
- func (a *Assertion) TestCustomDeepNotEqual(value, expect interface{}, c compare.Custom) bool
- func (a *Assertion) TestDeepDiff(value, expect interface{}) bool
- func (a *Assertion) TestDeepEqual(value, expect interface{}) bool
- func (a *Assertion) TestDeepNotEqual(value, expect interface{}) bool
- func (a Assertion) That(value interface{}) OnValue
- func (a Assertion) ThatBoolean(value bool) OnBoolean
- func (a Assertion) ThatDuration(value time.Duration) OnDuration
- func (a Assertion) ThatEnum(enum Enum) OnEnum
- func (a Assertion) ThatError(err error) OnError
- func (a Assertion) ThatFloat(value float64) OnFloat
- func (a Assertion) ThatInteger(value int) OnInteger
- func (a Assertion) ThatMap(mp interface{}) OnMap
- func (a Assertion) ThatSlice(slice interface{}) OnSlice
- func (a Assertion) ThatString(value interface{}) OnString
- type Enum
- type Manager
- type OnBoolean
- type OnDuration
- type OnEnum
- type OnError
- type OnFloat
- type OnInteger
- type OnMap
- func (o OnMap) DeepEquals(expected interface{}) bool
- func (o OnMap) Equals(expected interface{}) bool
- func (o OnMap) EqualsWithComparator(expected interface{}, same func(a, b interface{}) bool) bool
- func (o OnMap) IsEmpty() bool
- func (o OnMap) IsLength(length int) bool
- func (o OnMap) IsNotEmpty() bool
- type OnSlice
- func (o OnSlice) DeepEquals(expected interface{}) bool
- func (o OnSlice) Equals(expected interface{}) bool
- func (o OnSlice) EqualsOneOf(expecteds interface{}) bool
- func (o OnSlice) EqualsWithComparator(expected interface{}, same func(a, b interface{}) bool) bool
- func (o OnSlice) IsEmpty() bool
- func (o OnSlice) IsLength(length int) bool
- func (o OnSlice) IsNotEmpty() bool
- type OnString
- type OnValue
- func (o OnValue) CustomDeepEquals(expect interface{}, c compare.Custom) bool
- func (o OnValue) DeepEquals(expect interface{}) bool
- func (o OnValue) DeepNotEquals(test interface{}) bool
- func (o OnValue) Equals(expect interface{}) bool
- func (o OnValue) Implements(iface reflect.Type) bool
- func (o OnValue) IsNil() bool
- func (o OnValue) IsNotNil() bool
- func (o OnValue) NotEquals(test interface{}) bool
- type Output
Examples ¶
Constants ¶
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 assert.For.
func (Assertion) Commit ¶
func (a Assertion) Commit()
Commit writes the output lines to the main output object.
func (*Assertion) Compare ¶
Compare adds both the "Got" and "Expect" entries to the output buffer, with the operator being prepended to the expect list.
func (*Assertion) CompareRaw ¶
CompareRaw is like Compare except it does not push the values through the pretty printer.
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) ExpectRaw ¶
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) 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 ¶
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) Println ¶
Println prints the values using Print and then starts a new indented line.
func (*Assertion) Raw ¶
Raw writes a set of values to the output buffer, joined by tabs. It does not use the pretty printer.
func (*Assertion) Rawln ¶
Println prints the values using Print and then starts a new indented line.
func (*Assertion) TestCustomDeepDiff ¶
TestCustomDeepDiff is equivalent to TestCustomDeepEqual except it also prints
a diff.
func (*Assertion) TestCustomDeepEqual ¶
TestCustomDeepEqual adds the entries for Got and Expect, then tests if they are the same using c.DeepEqual, commiting if they are not.
func (*Assertion) TestCustomDeepNotEqual ¶
TestCustomDeepNotEqual adds the entries for Got and Expect, then tests if they are the same using c.DeepEqual, commiting if they are.
func (*Assertion) TestDeepDiff ¶
TestDeepDiff is equivalent to TestDeepEqual except it also prints a diff.
func (*Assertion) TestDeepEqual ¶
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 ¶
TestDeepNotEqual adds the entries for Got and Expect, then tests if they are the same using compare.DeepEqual, commiting if they are.
func (Assertion) ThatBoolean ¶
ThatBoolean returns an OnBoolean for boolean based assertions.
func (Assertion) ThatDuration ¶
func (a Assertion) ThatDuration(value time.Duration) OnDuration
ThatDuration returns an OnDuration for time duration based assertions.
func (Assertion) ThatInteger ¶
ThatInteger returns an OnInteger for integer based assertions.
func (Assertion) ThatMap ¶
ThatMap returns an OnMap for assertions on map type objects. Calling this with a non map type will result in panics.
func (Assertion) ThatSlice ¶
ThatSlice returns an OnSlice for assertions on slice type objects. Calling this with a non slice type will result in panics.
func (Assertion) ThatString ¶
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() {
assert := assert.To(nil)
for _, test := range enumTests {
assert.For(test.name).ThatEnum(&test.value).HasName(test.name)
}
var enum testEnum
assert.For(`"A"`).ThatEnum(&enum).CannotUnmarshal(`"A"`)
assert.For("0").ThatEnum(&enum).CannotUnmarshal(`0`)
}
Output: Error:BadParse For enum BadParse Bad Parse UNKNOWN Error:FailedParse For enum FailedParse Unexpected parse error `badparse not in testEnum` Error:BadJsonMarshal For enum BadJsonMarshal Bad JSON "badjson" Expect "BadJsonMarshal" Error:FailedJsonMarshal For enum FailedJsonMarshal JSON marshal error `json: error calling MarshalJSON for type *assert_test.testEnum: FailedJsonMarshal` Error:BadJsonUnmarshal For enum BadJsonUnmarshal Bad JSON Unmarshal UNKNOWN Error:FailedJsonUnmarshal For enum FailedJsonUnmarshal JSON unmarshal error `FailedJsonUnmarshal` Error:B For enum A Expected name `B` Error:"A" 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
type OnBoolean ¶
type OnBoolean struct {
Assertion
// contains filtered or unexported fields
}
OnBoolean is the result of calling ThatBoolean on an Assertion. It provides boolean assertion tests.
func (OnBoolean) Equals ¶
Equals asserts that the supplied boolean is equal to the expected boolean.
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.
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 ¶
CannotUnmarshal asserts that the enum type cannot unmarshal the supplied string without error.
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 ¶
DeepEquals asserts that the error value matches the expected error using compare.DeepEqual.
func (OnError) HasMessage ¶
HasMessage asserts that the error string matches the expected message.
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.
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 ¶
Equals asserts that the supplied integer is equal to the expected integer.
type OnMap ¶
type OnMap struct {
Assertion
// contains filtered or unexported fields
}
OnMap is the result of calling ThatMap on an Assertion. It provides assertion tests that are specific to map types.
func (OnMap) DeepEquals ¶
DeepEquals asserts the array or map matches expected using a deep-equal comparison.
func (OnMap) EqualsWithComparator ¶
EqualsWithComparator asserts the array or map matches expected using a comparator function
func (OnMap) IsNotEmpty ¶
IsNotEmpty asserts that the map has elements
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 ¶
DeepEquals asserts the array or slice matches expected using a deep-equal comparison.
func (OnSlice) EqualsOneOf ¶
Equals asserts the array or slice matches one of the expected array or slice
func (OnSlice) EqualsWithComparator ¶
EqualsWithComparator asserts the array or slice matches expected using a comparator function
func (OnSlice) IsLength ¶
IsLength asserts that the slice has exactly the specified number of elements
func (OnSlice) IsNotEmpty ¶
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) DoesNotContain ¶
DoesNotContain asserts that the supplied string does not contain substr.
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) CustomDeepEquals ¶
CustomDeepEquals asserts that the supplied value is equal to the expected value using compare.Diff and the custom comparators.
func (OnValue) DeepEquals ¶
DeepEquals asserts that the supplied value is equal to the expected value using compare.Diff.
func (OnValue) DeepNotEquals ¶
DeepNotEquals asserts that the supplied value is not equal to the test value using a deep comparison.
func (OnValue) Implements ¶
Implements asserts that the supplied value implements the specified interface.
func (OnValue) IsNil ¶
IsNil asserts that the supplied value was a nil. Typed nils are also be allowed.