assertion

package module
v1.0.13 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2024 License: MIT Imports: 7 Imported by: 0

README

assertions

Assertion style package, supporting custom assertions

GoDoc

This package is build on top of github.com/smarty/assertions package

it allows custom assertions on fields and types

you basically do

	customAssertions := map[string]assertions.AssertionFunc{
		"$.Field1.Field2":   assertions.SkipAssertion,
		assertion.TimeType:  assertions.AssertTimeToDuration(time.Second),
		assertion.FloatType: assertions.AssertFloat64ToDecimalPlaces(2),
		"$.Field1[][]":      customAssertionFunc,
	}

and pass this assertion map to the assert function For example

  • "$.Field1.Field2": assertions.SkipAssertion, references nested fields in root struct, and skips assertion
  • assertion.TimeType: assertions.AssertTimeToDuration(time.Second) asserts till seconds (skip milliesconds)
  • assertion.FloatType: assertions.AssertFloat64ToDecimalPlaces(2) asserts till the first 2 decimal places
  • "$.Field1[][]": customAssertionFunc Or you can build your custom assertion method

Documentation

Index

Constants

View Source
const (
	TimeType  = "time.Time"
	FloatType = "float64"
	IntType   = "int"
	Int64Type = "int64"
)

Variables

This section is empty.

Functions

func Assert

func Assert(actual any, expected any, customAssertions map[string]AssertionFunc) (bool, string)

Assert compares the actual and expected values using the custom assertions defined and returns the result and message actual is the actual value to be compared expected is the expected value to be compared customAssertions is the map of custom assertions defined for the path or type Example usage:

customAssertions := map[string]AssertionFunc{
	"field1": customAssertionFunc1,
	"field2": customAssertionFunc2,
	"int":    customAssertionFunc3,
}

match, message := Assert(actual, expected, customAssertions) returns the result and message

func SkipAssertion

func SkipAssertion(actual any, expected ...any) string

SkipAssertion is a custom assertion function that always returns true.

Types

type AssertionFunc

type AssertionFunc assertions.SoFunc

func AssertFloat64ToDecimalPlaces

func AssertFloat64ToDecimalPlaces(decimalPlaces int) AssertionFunc

AssertFloat64ToDecimalPlaces is a custom assertion function that rounds float64 to the specified decimal places before comparing. this function is rounding the float64 to the specified decimal places before comparing.

func AssertFloat64WithTolerance

func AssertFloat64WithTolerance(tolerance float64) AssertionFunc

AssertFloat64WithTolerance is a custom assertion function that compares float64 values with a tolerance. tolerance is in format of 0.0001 if tolerance is 0, it will compare the float64 values as is.

func AssertNumberWithTolerance added in v1.0.11

func AssertNumberWithTolerance[T ~int | ~int64 | ~float64 | ~float32 | ~int32](tolerance T) AssertionFunc

AssertNumberWithTolerance is a custom assertion function that compares number values with a tolerance. tolerance is in format of 0.0001 if tolerance is 0, it will compare the value as is.

func AssertStringWithCleanup

func AssertStringWithCleanup(cleanup func(string) string) AssertionFunc

AssertStringWithCleanup is a custom assertion function that cleans up the string before comparing. cleanup function is a function that takes a string and returns a string after cleanup. if cleanup is nil, it will compare the string as is.

func AssertStringWithDistance added in v1.0.12

func AssertStringWithDistance(distance int) AssertionFunc

func AssertTimeToDuration

func AssertTimeToDuration(duration time.Duration) AssertionFunc

AssertTimeToDuration is a custom assertion function that truncates time to the specified duration before comparing. this function is using time.Truncate() to truncate the time to the specified duration.

func SkipAssertionIf

func SkipAssertionIf(condition func(actual any, expected any) bool, customAssertion AssertionFunc) AssertionFunc

SkipAssertionIf is a custom assertion function that skips the assertion if the condition is met. if the condition is met, it will return an empty string. if not, it will return the result of the provided customAssertion function or default assertion function shouldEqual.

Jump to

Keyboard shortcuts

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