assertion

package
v1.1.6 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package assertion is the implementation detail of package assert. One can use API to create a customized assert function with this package

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assert

func Assert(t *testing.T, expr interface{}, trigger *Trigger)

Assert tests expr and call `t.Fatalf` to terminate test case if expr is false-equivalent value.

func AssertEqual

func AssertEqual(t *testing.T, v1, v2 interface{}, trigger *Trigger)

AssertEqual uses `reflect.DeepEqual` to test v1 and v2 equality.

func AssertNilError

func AssertNilError(t *testing.T, result []interface{}, trigger *Trigger)

AssertNilError expects a function return a nil error. Otherwise, it will terminate the test case using `t.Fatalf`.

func AssertNonNilError

func AssertNonNilError(t *testing.T, result []interface{}, trigger *Trigger)

AssertNonNilError expects a function return a non-nil error. Otherwise, it will terminate the test case using `t.Fatalf`.

func AssertNotEqual

func AssertNotEqual(t *testing.T, v1, v2 interface{}, trigger *Trigger)

AssertNotEqual uses `reflect.DeepEqual` to test v1 and v2 equality.

func IsIncluded added in v1.1.0

func IsIncluded(parent, child string) bool

IsIncluded checks whether child var is a children of parent var. Regarding the child var `a.b.c`, it's the children of `a`, `a.b` and `a.b.c`.

func IsVar added in v1.1.0

func IsVar(expr ast.Expr) bool

IsVar returns true if expr is an ident or a selector expr like `a.b`.

Types

type FalseKind

type FalseKind int

FalseKind is the kind of a false-equivalent value.

const (
	Positive FalseKind = iota
	Nil
	False
	Zero
	EmptyString
)

Valid kinds for all false-equivalent values.

func ParseFalseKind

func ParseFalseKind(expr interface{}) FalseKind

ParseFalseKind checks expr value and return false when expr is `false`, 0, `nil` and empty string. Otherwise, return true.

type Func added in v1.1.0

type Func struct {
	FileSet *token.FileSet
	Func    *ast.FuncDecl
	Caller  *ast.CallExpr
	Args    []ast.Expr

	Filename string
	Line     int
}

Func represents AST information of an assertion function.

type Info added in v1.1.0

type Info struct {
	Source string   // Source code of the caller.
	Args   []string // Selected arguments.

	// The last assignments related to Args.
	// The len(Assignments) is guaranteed to be the same as len(Args).
	Assignments [][]string

	// RelatedVars is the list of variables which are referenced in argument expression
	// or in the assignment statements containing arguments.
	// For instance, consider following code.
	//
	//     for i, c := range cases {
	//         a := 1
	//         Assert(t, a+1 == c.Value)
	//     }
	//
	// After parsing `Assert`, RelatedVars contains `a`, `c.Value` and `i`.
	// Note that, `i` is listed in related vars because of the value of `i` is assigned in
	// `i, c := range cases` in which `c` is also assigned.
	RelatedVars []string
}

Info represents code analysis information of an assertion function.

type Parser added in v1.1.4

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

Parser represents a source file parser.

func (*Parser) AddExcluded added in v1.1.4

func (p *Parser) AddExcluded(expr *ast.CallExpr)

AddExcluded adds an expr to excluded expr list so that this expr will not be inspected when finding related assignments.

func (*Parser) ParseArgs added in v1.1.4

func (p *Parser) ParseArgs(name string, skip int, argIndex []int) (f *Func, err error)

ParseArgs parses caller's source code, finds out the right call expression by name and returns the argument source AST.

Skip is the stack frame calling an assert function. If skip is 0, the stack frame for ParseArgs is selected. In most cases, caller should set skip to 1 to skip ParseArgs itself.

func (*Parser) ParseInfo added in v1.1.4

func (p *Parser) ParseInfo(f *Func) (info *Info)

ParseInfo returns more context related information about this f. See document of Info for details.

type Trigger

type Trigger struct {
	Parser   *Parser
	FuncName string
	Skip     int
	Args     []int
	Vars     map[string]interface{}
}

Trigger represents the method which triggers assertion.

func (*Trigger) P added in v1.1.4

func (t *Trigger) P() *Parser

P returns a valid parser.

Jump to

Keyboard shortcuts

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