checkers

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2024 License: LGPL-3.0 Imports: 18 Imported by: 329

Documentation

Index

Constants

This section is empty.

Variables

View Source
var After gc.Checker = &timeCompareChecker{
	CheckerInfo: &gc.CheckerInfo{Name: "After", Params: []string{"obtained", "want"}},
	compareFunc: func(t1, t2 time.Time) bool {
		return t1.After(t2)
	},
}

After checks whether the obtained time.Time is After the want time.Time.

View Source
var Almost gc.Checker = &timeCompareChecker{
	CheckerInfo: &gc.CheckerInfo{Name: "Almost", Params: []string{"obtained", "want"}},
	compareFunc: func(t1, t2 time.Time) bool {
		return math.Abs(t1.Sub(t2).Seconds()) <= 1.0
	},
}

Almost checks whether the obtained time.Time is within 1s of the want time.Time.

View Source
var Before gc.Checker = &timeCompareChecker{
	CheckerInfo: &gc.CheckerInfo{Name: "Before", Params: []string{"obtained", "want"}},
	compareFunc: func(t1, t2 time.Time) bool {
		return t1.Before(t2)
	},
}

Before checks whether the obtained time.Time is Before the want time.Time.

View Source
var Contains gc.Checker = &containsChecker{
	&gc.CheckerInfo{Name: "Contains", Params: []string{"obtained", "expected"}},
}
View Source
var DeepEquals gc.Checker = &deepEqualsChecker{
	&gc.CheckerInfo{Name: "DeepEquals", Params: []string{"obtained", "expected"}},
}

The DeepEquals checker verifies that the obtained value is deep-equal to the expected value. The check will work correctly even when facing slices, interfaces, and values of different types (which always fail the test).

For example:

c.Assert(value, DeepEquals, 42)
c.Assert(array, DeepEquals, []string{"hi", "there"})

This checker differs from gocheck.DeepEquals in that it will compare a nil slice equal to an empty slice, and a nil map equal to an empty map.

View Source
var DoesNotExist gc.Checker = &doesNotExistChecker{
	&gc.CheckerInfo{Name: "DoesNotExist", Params: []string{"obtained"}},
}
View Source
var DurationLessThan gc.Checker = &durationLessThanChecker{
	&gc.CheckerInfo{Name: "DurationLessThan", Params: []string{"obtained", "expected"}},
}
View Source
var ErrorIs gc.Checker = &errorIsChecker{
	CheckerInfo: &gc.CheckerInfo{
		Name:   "ErrorIs",
		Params: []string{"obtained", "error"},
	},
}

ErrorIs checks whether a value is an error that matches the other argument.

View Source
var ErrorIsNil gc.Checker = &errorIsNilChecker{
	&gc.CheckerInfo{Name: "ErrorIsNil", Params: []string{"value"}},
}

The ErrorIsNil checker tests whether the obtained value is nil. Explicitly tests against only `nil`.

For example:

c.Assert(err, ErrorIsNil)
View Source
var ExpectedValue = &struct{}{}

ExpectedValue if passed to MultiChecker.Add or MultiChecker.AddRegex, will be substituded with the expected value.

View Source
var GreaterThan gc.Checker = &greaterThanChecker{
	&gc.CheckerInfo{Name: "GreaterThan", Params: []string{"obtained", "expected"}},
}
View Source
var HasPrefix gc.Checker = &hasPrefixChecker{
	&gc.CheckerInfo{Name: "HasPrefix", Params: []string{"obtained", "expected"}},
}
View Source
var HasSuffix gc.Checker = &hasSuffixChecker{
	&gc.CheckerInfo{Name: "HasSuffix", Params: []string{"obtained", "expected"}},
}
View Source
var Ignore gc.Checker = &ignoreChecker{
	&gc.CheckerInfo{Name: "Ignore", Params: []string{"obtained"}},
}

Ignore always succeeds.

View Source
var IsDirectory gc.Checker = &isDirectoryChecker{
	&gc.CheckerInfo{Name: "IsDirectory", Params: []string{"obtained"}},
}
View Source
var IsFalse gc.Checker = gc.Not(IsTrue)

IsTrue checks whether a value has an underlying boolean type and is false.

View Source
var IsNonEmptyFile gc.Checker = &isNonEmptyFileChecker{
	&gc.CheckerInfo{Name: "IsNonEmptyFile", Params: []string{"obtained"}},
}
View Source
var IsSymlink gc.Checker = &isSymlinkChecker{
	&gc.CheckerInfo{Name: "IsSymlink", Params: []string{"obtained"}},
}
View Source
var IsTrue gc.Checker = &isTrueChecker{
	&gc.CheckerInfo{Name: "IsTrue", Params: []string{"obtained"}},
}

IsTrue checks whether a value has an underlying boolean type and is true.

View Source
var JSONEquals = &codecEqualChecker{
	name:      "JSONEquals",
	marshal:   json.Marshal,
	unmarshal: json.Unmarshal,
}

JSONEquals defines a checker that checks whether a byte slice, when unmarshaled as JSON, is equal to the given value. Rather than unmarshaling into something of the expected body type, we reform the expected body in JSON and back to interface{}, so we can check the whole content. Otherwise we lose information when unmarshaling.

View Source
var LessThan gc.Checker = &lessThanChecker{
	&gc.CheckerInfo{Name: "LessThan", Params: []string{"obtained", "expected"}},
}
View Source
var LogMatches gc.Checker = &logMatches{
	&gc.CheckerInfo{Name: "LogMatches", Params: []string{"obtained", "expected"}},
}

LogMatches checks whether a given TestLogValues actually contains the log messages we expected. If you compare it against a list of strings, we only compare that the strings in the messages are correct. You can alternatively pass a slice of SimpleMessage and we will check that the log levels are also correct.

The log may contain additional messages before and after each of the specified expected messages.

View Source
var SameContents gc.Checker = &sameContents{
	&gc.CheckerInfo{Name: "SameContents", Params: []string{"obtained", "expected"}},
}

SameContents checks that the obtained slice contains all the values (and same number of values) of the expected slice and vice versa, without respect to order or duplicates. Uses DeepEquals on contents to compare. Content types do not need to be hashable, but must satisfy reflect.DeepEquals.

View Source
var SamePath gc.Checker = &samePathChecker{
	&gc.CheckerInfo{Name: "SamePath", Params: []string{"obtained", "expected"}},
}

SamePath checks paths to see whether they're the same, can follow symlinks and is OS independent

View Source
var Satisfies gc.Checker = &satisfiesChecker{
	&gc.CheckerInfo{
		Name:   "Satisfies",
		Params: []string{"obtained", "func(T) bool"},
	},
}

Satisfies checks whether a value causes the argument function to return true. The function must be of type func(T) bool where the value being checked is assignable to T.

View Source
var SymlinkDoesNotExist gc.Checker = &symlinkDoesNotExistChecker{
	&gc.CheckerInfo{Name: "SymlinkDoesNotExist", Params: []string{"obtained"}},
}
View Source
var YAMLEquals = &codecEqualChecker{
	name:      "YAMLEquals",
	marshal:   yaml.Marshal,
	unmarshal: yaml.Unmarshal,
}

YAMLEquals defines a checker that checks whether a byte slice, when unmarshaled as YAML, is equal to the given value. Rather than unmarshaling into something of the expected body type, we reform the expected body in YAML and back to interface{}, so we can check the whole content. Otherwise we lose information when unmarshaling.

Functions

func DeepEqual

func DeepEqual(a1, a2 interface{}) (bool, error)

DeepEqual tests for deep equality. It uses normal == equality where possible but will scan elements of arrays, slices, maps, and fields of structs. In maps, keys are compared with == but elements use deep equality. DeepEqual correctly handles recursive types. Functions are equal only if they are both nil.

DeepEqual differs from reflect.DeepEqual in two ways: - an empty slice is considered equal to a nil slice. - two time.Time values that represent the same instant but with different time zones are considered equal.

If the two values compare unequal, the resulting error holds the first difference encountered.

func DeepEqualWithCustomCheck

func DeepEqualWithCustomCheck(a1 interface{}, a2 interface{}, customCheckFunc CustomCheckFunc) (bool, error)

DeepEqualWithCustomCheck tests for deep equality. It uses normal == equality where possible but will scan elements of arrays, slices, maps, and fields of structs. In maps, keys are compared with == but elements use deep equality. DeepEqual correctly handles recursive types. Functions are equal only if they are both nil.

DeepEqual differs from reflect.DeepEqual in two ways: - an empty slice is considered equal to a nil slice. - two time.Time values that represent the same instant but with different time zones are considered equal.

If the two values compare unequal, the resulting error holds the first difference encountered.

If both values are interface-able and customCheckFunc is non nil, customCheckFunc will be invoked. If it returns useDefault as true, the DeepEqual continues, otherwise the result of the customCheckFunc is used.

func TimeBetween

func TimeBetween(start, end time.Time) gc.Checker

Types

type CustomCheckFunc

type CustomCheckFunc func(path string, a1 interface{}, a2 interface{}) (useDefault bool, equal bool, err error)

CustomCheckFunc should return true for useDefault if DeepEqualWithCustomCheck should behave like DeepEqual. Otherwise the result of the CustomCheckFunc is used.

type ErrorStacker

type ErrorStacker interface {
	error
	StackTrace() []string
}

type MultiChecker

type MultiChecker struct {
	*gc.CheckerInfo
	// contains filtered or unexported fields
}

MultiChecker is a deep checker that by default matches for equality. But checks can be overriden based on path (either explicit match or regexp)

func NewMultiChecker

func NewMultiChecker() *MultiChecker

NewMultiChecker creates a MultiChecker which is a deep checker that by default matches for equality. But checks can be overriden based on path (either explicit match or regexp)

func (*MultiChecker) Add

func (checker *MultiChecker) Add(path string, c gc.Checker, args ...interface{}) *MultiChecker

Add an explict checker by path.

func (*MultiChecker) AddExpr

func (checker *MultiChecker) AddExpr(expr string, c gc.Checker, args ...interface{}) *MultiChecker

AddExpr exception which matches path with go expression. Use _ for wildcard. The top level or root value must be a _ when using expression.

func (*MultiChecker) AddRegex

func (checker *MultiChecker) AddRegex(pathRegex string, c gc.Checker, args ...interface{}) *MultiChecker

AddRegex exception which matches path with regex.

func (*MultiChecker) Check

func (checker *MultiChecker) Check(params []interface{}, names []string) (result bool, errStr string)

Check for go check Checker interface.

type SimpleMessage

type SimpleMessage struct {
	Level   loggo.Level
	Message string
}

func (SimpleMessage) String

func (s SimpleMessage) String() string

type SimpleMessages

type SimpleMessages []SimpleMessage

func (SimpleMessages) GoString

func (s SimpleMessages) GoString() string

Jump to

Keyboard shortcuts

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