ctxerr

package
v1.9.2 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2021 License: BSD-2-Clause Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// BooleanError is the *Error returned when an error occurs in a
	// boolean context.
	BooleanError = &Error{}

	// ErrTooManyErrors is chained to the last error encountered when
	// the maximum number of errors has been reached.
	ErrTooManyErrors = &Error{
		Message: "Too many errors (use TESTDEEP_MAX_ERRORS=-1 to see all)",
	}
)

Functions

This section is empty.

Types

type Context

type Context struct {
	Path        Path
	Visited     visited.Visited
	CurOperator location.GetLocationer
	Depth       int
	// 0 ≤ MaxErrors ≤ 1 stops when first error encoutered (without the
	// "Too many errors" error);
	// MaxErrors > 1 stops when MaxErrors'th error encoutered (with a
	// last "Too many errors" error);
	// < 0 do not stop until comparison ends.
	MaxErrors int
	Errors    *[]*Error
	Anchors   *anchors.Info
	Hooks     *hooks.Info
	// If true, the contents of the returned *Error will not be
	// checked. Can be used to avoid filling Error{} with expensive
	// computations.
	BooleanError bool
	// See ContextConfig.FailureIsFatal for details.
	FailureIsFatal bool
	// See ContextConfig.UseEqual for details.
	UseEqual bool
	// See ContextConfig.BeLax for details.
	BeLax bool
}

Context is used internally to keep track of the CmpDeeply in-Depth traversal.

func (Context) AddArrayIndex

func (c Context) AddArrayIndex(index int) (new Context)

AddArrayIndex creates a new Context from current one plus an array dereference for index-th item.

func (Context) AddCustomLevel added in v1.1.0

func (c Context) AddCustomLevel(pathAdd string) (new Context)

AddCustomLevel creates a new Context from current one plus pathAdd.

func (Context) AddField added in v1.1.0

func (c Context) AddField(field string) (new Context)

AddField creates a new Context from current one plus "." + field.

func (Context) AddFunctionCall

func (c Context) AddFunctionCall(fn string) (new Context)

AddFunctionCall creates a new Context from current one inside a function call.

func (Context) AddMapKey

func (c Context) AddMapKey(key interface{}) (new Context)

AddMapKey creates a new Context from current one plus a map dereference for key key.

func (Context) AddPtr

func (c Context) AddPtr(num int) (new Context)

AddPtr creates a new Context from current one plus a pointer dereference.

func (Context) CannotCompareError added in v1.1.1

func (c Context) CannotCompareError() *Error

CannotCompareError returns a generic error used when the access of unexported fields cannot be overridden.

func (Context) CollectError

func (c Context) CollectError(err *Error) *Error

CollectError collects an error in the context. It returns an error if the collector is full, nil otherwise.

In boolean context, ignore the passed error and return the BooleanError.

func (*Context) InitErrors

func (c *Context) InitErrors()

InitErrors initializes Context *Errors slice, if MaxErrors < 0 or MaxErrors > 1.

func (Context) MergeErrors

func (c Context) MergeErrors() *Error

MergeErrors merges all collected errors in the first one and returns it. It returns nil if no errors have been collected.

func (Context) ResetErrors

func (c Context) ResetErrors() (new Context)

ResetErrors returns a new Context without any Error set.

func (Context) ResetPath

func (c Context) ResetPath(newRoot string) (new Context)

ResetPath creates a new Context from current one but reinitializing Path.

type Error

type Error struct {
	// Context when the error occurred
	Context Context
	// Message describes the error
	Message string
	// Got value
	Got interface{}
	// Expected value
	Expected interface{}
	// If not nil, Summary is used to display summary instead of using
	// Got + Expected fields
	Summary ErrorSummary
	// If initialized, location of TestDeep operator originator of the error
	Location location.Location
	// If defined, the current Error comes from this Error
	Origin *Error
	// If defined, points to the next Error
	Next *Error
}

Error represents errors generated by td (go-testdeep) functions.

func TypeMismatch added in v1.8.0

func TypeMismatch(got, expected reflect.Type) *Error

TypeMismatch returns a "type mismatch" error. It is the caller responsibility to check that both types differ.

If they resolve to the same name (via their String method), it tries to deeply dump the full package name of each type.

It works pretty well with the exception of identical anomymous structs in 2 different packages with the same last name: in this case reflect does not allow us to retrieve the package from which each type comes.

package foo // in a/
var Foo struct { a int }

package foo // in b/
var Foo struct { a int }

package ctxerr
import(
  a_foo "a/foo"
  b_foo "b/foo"
)
…
TypeMismatch(reflect.TypeOf(a_foo.Foo), reflect.TypeOf(b_foo.Foo))

returns an error producing:

type mismatch
     got: struct { a int }
expected: struct { a int }

func (*Error) Append

func (e *Error) Append(buf *bytes.Buffer, prefix string)

Append appends the Error contents to "buf" using prefix "prefix" for each line.

func (*Error) Error

func (e *Error) Error() string

Error implements error interface.

func (*Error) ExpectedString

func (e *Error) ExpectedString() string

ExpectedString returns the string corresponding to the Expected field. Returns the empty string if the Error Summary field is not nil.

func (*Error) GotString

func (e *Error) GotString() string

GotString returns the string corresponding to the Got field. Returns the empty string if the Error Summary field is not nil.

func (*Error) SummaryString

func (e *Error) SummaryString() string

SummaryString returns the string corresponding to the Summary field. Returns the empty string if the Error Summary field is nil.

type ErrorSummary added in v1.1.0

type ErrorSummary interface {
	AppendSummary(buf *bytes.Buffer, prefix string)
}

ErrorSummary is the interface used to render error summaries. See Error.Summary.

func NewSummary added in v1.1.0

func NewSummary(s string) ErrorSummary

NewSummary returns an ErrorSummary composed by the simple string s.

func NewSummaryReason added in v1.1.0

func NewSummaryReason(got interface{}, reason string) ErrorSummary

NewSummaryReason returns an ErrorSummary meaning that the value got failed for an (optional) reason.

With a given reason "it is not nil", the generated summary will be:

        value: the_got_value
it failed coz: it is not nil

If reason is empty, the generated summary will be:

  value: the_got_value
it failed but didn't say why

type ErrorSummaryItem added in v1.1.0

type ErrorSummaryItem struct {
	Label       string
	Value       string
	Explanation string
}

ErrorSummaryItem implements the ErrorSummary interface and allows to render a labeled value.

With explanation set:

Label: value
Explanation

With an empty explantion:

Label: value

func (ErrorSummaryItem) AppendSummary added in v1.1.0

func (s ErrorSummaryItem) AppendSummary(buf *bytes.Buffer, prefix string)

AppendSummary implements the ErrorSummary interface.

type ErrorSummaryItems added in v1.1.0

type ErrorSummaryItems []ErrorSummaryItem

ErrorSummaryItems implements the ErrorSummary interface and allows to render summaries with several labeled values. For example:

Missing 6 items: the 6 items...
  Extra 2 items: the 2 items...

func (ErrorSummaryItems) AppendSummary added in v1.1.0

func (s ErrorSummaryItems) AppendSummary(buf *bytes.Buffer, prefix string)

AppendSummary implements ErrorSummary interface.

type Path added in v1.1.0

type Path []pathLevel

Path defines a structure depth path, typically used to mark a position during a deep traversal in case of error.

func NewPath added in v1.1.0

func NewPath(root string) Path

NewPath returns a new Path initialized with "root" root node.

func (Path) AddArrayIndex added in v1.1.0

func (p Path) AddArrayIndex(index int) Path

AddArrayIndex adds a level corresponding to an array index.

func (Path) AddCustomLevel added in v1.1.0

func (p Path) AddCustomLevel(custom string) Path

AddCustomLevel adds a custom level.

func (Path) AddField added in v1.1.0

func (p Path) AddField(field string) Path

AddField adds a level corresponding to a struct field.

func (Path) AddFunctionCall added in v1.1.0

func (p Path) AddFunctionCall(fn string) Path

AddFunctionCall adds a level corresponding to a function call.

func (Path) AddMapKey added in v1.1.0

func (p Path) AddMapKey(key interface{}) Path

AddMapKey adds a level corresponding to a map key.

func (Path) AddPtr added in v1.1.0

func (p Path) AddPtr(num int) Path

AddPtr adds "num" pointers levels.

func (Path) Copy added in v1.1.0

func (p Path) Copy() Path

Copy returns a new Path, exact but independent copy of "p".

func (Path) Equal added in v1.1.0

func (p Path) Equal(o Path) bool

Equal returns true if "p" and "o" are equal, false otherwise.

func (Path) Len added in v1.1.0

func (p Path) Len() int

Len returns the number of levels, excluding pointers ones.

func (Path) String added in v1.1.0

func (p Path) String() string

Jump to

Keyboard shortcuts

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