compare

package module
v0.0.0-...-a691ca9 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2022 License: MIT Imports: 9 Imported by: 0

README

go-diff

unit-tests codecov

A library that can compare data types based on different "match" criterias.

Supported match criterias:

  • less than
  • less than or equal
  • greater than
  • greater than or equal
  • percentage deviation
  • regex
  • range
  • equal
  • not equal
  • not empty
  • contains

Example

func main() {
    val := Validation{
        MatchType: MatchTypeEqual
        ExpectedValue: "foo",
    }
    isMatch, err := val.Matches("foo")
    if err != nil {
        panic(err)
    }
    fmt.Printf("Compared %q against %q and got %v", "foo", "foo", isMatch)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidMatchType = errors.New("invalid match type")
	ErrValueNotANumber  = errors.New("value is not a number")
)
View Source
var (
	// ErrValueExceedsRange is returned when the value exceeds the supported range.
	ErrValueExceedsRange = errors.New("value exceeds supported range")

	// ErrAorBIsZero is returned when a or b is 0.
	ErrAorBIsZero = errors.New("a or b is 0")
)
View Source
var (
	ErrAExceedsRangeOfB = errors.New("a must be smaller than b")
)
View Source
var (
	ErrInvalidPercentageFormat = fmt.Errorf("invalid percentage format")
)

Functions

This section is empty.

Types

type MatchType

type MatchType string

MatchType defines the type of match to be performed.

const (
	// MatchTypeLessThan is used to compare the response with the expected value.
	// If the response is less than the expected value the validation is successful.
	// If the response is equal or greater than the expected value the validation is not successful.
	// If the response is not a number the validation is not successful.
	MatchTypeLessThan MatchType = "lt"
	// MatchTypeLessThanOrEqual is used to compare the response with the expected value.
	// If the response is less than or equal to the expected value the validation is successful.
	// If the response is greater than the expected value the validation is not successful.
	// If the response is not a number the validation is not successful.
	MatchTypeLessThanOrEqual MatchType = "lte"
	// MatchTypeGreaterThan is used to compare the response with the expected value.
	// If the response is greater than the expected value the validation is successful.
	// If the response is equal or smaller than the expected value the validation is not successful.
	// If the response is not a number the validation is not successful.
	MatchTypeGreaterThan MatchType = "gt"
	// MatchTypeGreaterThanOrEqual is used to compare the response with the expected value.
	// If the response is greater than or equal to the expected value the validation is successful.
	// If the response is smaller than the expected value the validation is not successful.
	// If the response is not a number the validation is not successful.
	MatchTypeGreaterThanOrEqual MatchType = "gte"
	// MatchTypePercentageDeviation is used to compare the response with the expected value.
	// A percentage offset is calculated from the expected value and the response.
	// If the percentage offset is smaller than the calculated offset, the validation is successful.
	// The defined offset is added to the expected value and defines a failure tolerance.
	MatchTypePercentageDeviation MatchType = "pd"
	// MatchTypeAbsoluteOffset is used to compare the response with the expected value.
	// The defined regex is used to match the response.
	MatchTypeRegex MatchType = "re"
	// MatchTypeRange is used to compare the response with the expected value.
	// The defined range is used to match the response.
	// The value must be in between the range.
	// If the response is not a number the validation is not successful.
	MatchTypeRange MatchType = "rg"
	// MatchTypeEqual is used to compare the response with the expected value.
	// If the response is equal to the expected value the validation is successful.
	// If the response is not equal to the expected value the validation is not successful.
	MatchTypeEqual MatchType = "eq"
	// MatchTypeNotEqual is used to compare the response with the expected value.
	// If the response is not equal to the expected value the validation is successful.
	// If the response is equal to the expected value the validation is not successful.
	MatchTypeNotEqual MatchType = "neq"
	// MatchTypeEmpty is used to compare the response with the expected value.
	// If the response is empty the validation is successful.
	// If the response is not empty the validation is not successful.
	MatchTypeEmpty MatchType = "et"
	// MatchTypeNotEmpty is used to compare the response with the expected value.
	// If the response is not empty the validation is successful.
	// If the response is empty the validation is not successful.
	MatchTypeNotEmpty MatchType = "ne"
	// MatchTypeContains is used to compare the response with the expected value.
	// If the response contains the expected value the validation is successful.
	// If the response does not contain the expected value the validation is not successful.
	MatchTypeContains MatchType = "ct"
)

type Percent

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

Percent represents a custom type that defines a specific allowed value range from 0.0 to 100.0.

func NewPercent

func NewPercent(value float64) (*Percent, error)

NewPercent returns a new instance of type Percent.

func NewPercentFromFloats

func NewPercentFromFloats(a, b int) (*Percent, error)

NewPercentFromInts calculates the percent difference between a and b.

func ParsePercentageValueFromString

func ParsePercentageValueFromString(in string) (*Percent, error)

ParsePercentageValueFromString parses a percentage value from a string and returns the value as type Percent. If the string is not a valid percentage value, an error is returned.

func (Percent) Get

func (p Percent) Get() float64

Get returns the value of the Percent type. If the value is not within the supported range, 0.0 is returned.

func (*Percent) Set

func (p *Percent) Set(value float64) error

Set sets the value of the Percent type.

type Report

type Report struct {
	// Type defines the actual type of A and B
	// This field has no functional purpose and only exists for discovery.
	Type string
	// Index is the index of the byte that differs
	Index int
	// Original represents the value of the byte at index in A
	Original *byte
	// New defines the value of b passed to the Equal function.
	New byte
}

func ByteDifferent

func ByteDifferent(a, b byte) *Report

ByteDifferent returns the difference between a and b.+ If a and b are the same, nil is returned.

type Reports

type Reports []Report

func BytesDifferent

func BytesDifferent(a, b []byte) (Reports, error)

BytesDifferent returns the difference between a and b.

type Validation

type Validation struct {
	// MatchType defines the type of the validation.
	// Possible values:
	// - lt: less than
	// - lte: less than or equal
	// - gt: greater than
	// - gte: greater than or equal
	// - pd: percentual offset
	// - re: regex
	// - rg: range
	// - eq: equals
	// - neq: not equals
	// - ne: not empty
	// - et: empty
	// - ct: contains
	MatchType MatchType
	// MatchValue defines the value operation.
	// Must only be set for "percentual offset" and "range" definitions.
	// Possible values:
	// - [0-9]-[0-9]: range definition
	// - [0-9]%: percentual offset
	// - any: regex
	MatchValue *string
	// ExpectedValue defines the expected value.
	ExpectedValue interface{}
}

Validation defines the validation specification to execute a test.

func (Validation) Matches

func (d Validation) Matches(value interface{}) (bool, error)

Matches validates the argument value against the validation specification. If the validation is successful the method returns true as validation and nil as error.

Jump to

Keyboard shortcuts

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