asserter

package module
v2.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2022 License: GPL-3.0 Imports: 3 Imported by: 0

README

asserter

Purpose

Package for testing purposes. asserter struct does dynamic assertions. Facilitates test case enumeration & debugging.

Documentation

On time

Dosc in soure code (see repository).

Elegant, possibly outdated

github.com/Siiir/asserter v1
github.com/Siiir/asserter v2

Repository

https://github.com/Siiir/asserter

Documentation

Overview

Package for testing purposes.

`Asserter` struct does runtime assertions. Facilitates test case enumeration & debugging.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Asserter

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

Is a type for making & enumerating dynamic assertions, also automatically logging failers.

Private member, first construction argument, slice `counter` contains subcounters of assertions. Must have at least one subcounter automatically used (incremented) by `.Assert` method).

If assertions come in one series, then one subcounter is enough – len(`counter`)==1. If assertions come in (nested) packs, then: 1. counter[0] enumerates top-level packs and top-level packs can be switched with method `.Inc(0)` . 2. counter[i] where i>0 enumerates lower-level packs and they can be switched with method `.Inc(i)` .

Private member, second construction argument, function `fail` logs all failers with `failerMsg` provided by assert-like method (eg. `.AssertEq`). Most popular `fail` clousures:

func(failerMsg string){ t.Error(failerMsg) }

where `t` is *testing.T

func(failerMsg string){logFile.WriteString(failerMsg)}

where `logFile` is a text stream.

func(failerMsg string){ println(failerMsg) }

.

Example
a, e := New([]uint{0, 0}, func(s string) { fmt.Println(s) })
if e != nil {
	panic(fmt.Sprint("e == ", e))
}

// Assetions before a.Inc(0).
{
	// Enumeration tags be like: 0.x
	a.Assert(false)
}

// The first pack of assertions.
{
	a.Inc(0) // Enumeration tags be like: 1.x

	a.Assert(2 == 3)
	a.Assert(7+8 < 15)
	a.A(true)
	a.A(1 < 2)
}

// The second pack of assertions.
{
	a.Inc(0) // Enumeration tags be like: 2.x
	a.A(6.0 == 6e0)
	a.Assert(false)
}

// The fird pack of assertions.
{
	a.Inc(0) // Enumeration tags be like: 3.x
	a.AssertWithFailMsgAppendix(true, "my not-displayed msg appendix.")
	a.AWFMA(false, "my msg appendix.")
	a.AssertEq(8.9, 7.1)
	a.AssertEq(false, false)
	a.AE(8, 8.0)
}

a.ResetCounter()
a.AE("abc", nil)
Output:

0.0. assertion failed!
1.0. assertion failed!
1.1. assertion failed!
2.1. assertion failed!
3.1. assertion failed!my msg appendix.
3.2. assertion failed! 8.9 != 7.1
3.4. assertion failed! 8 of type `int` is not `reflect.DeepEqual` to 8 of type `float64`
0.0. assertion failed! abc != <nil>

func New

func New(
	counter []uint,
	fail func(failerMsg string),
) (a Asserter, e error)

Creates a new Asserter.

Passes its arguments to corresponding field setters stopping at first error caught and returning it.

[No error caught ⇔ `e`=nil] ⇒ Construction was successful. Returned Asserter object is valid.

func NewReseted

func NewReseted(counterLen uint, fail func(string)) (Asserter, error)

Equivalent to:

New( make([]uint,counterLen), fail )

func NewTiny

func NewTiny(
	fail func(failerMsg string),
) (a Asserter, e error)

Equivalent to: 1.

New( make([]uint,1), fail )

2.

NewReseted( 1, fail )

func PNew added in v2.1.0

func PNew(
	counter []uint,
	fail func(failerMsg string),
) Asserter

Mad version of `New(counter,fail)`. Panics on error − not returning it.

func PNewReseted added in v2.1.0

func PNewReseted(counterLen uint, fail func(string)) Asserter

Mad version of `NewReseted(counterLen,f ail)`. Panic on error − not returning it.

func PNewTiny added in v2.1.0

func PNewTiny(
	fail func(failerMsg string),
) Asserter

Mad version of `NewTiny(fail)`. Panics on error − not returning it.

func (*Asserter) A

func (a *Asserter) A(logical_val bool) bool

Alias for `.Assert(logical_val)`.

func (*Asserter) AE

func (a *Asserter) AE(lhs any, rhs any) bool

Alias for `.AssertEq(lhs,rhs)`.

func (*Asserter) AWFMA

func (a *Asserter) AWFMA(logical_val bool, appendix string) bool

Alias for `Asserter.AssertWithFailMsgAppendix`.

func (*Asserter) Assert

func (a *Asserter) Assert(logical_val bool) bool

If `logical_val`=false, passes `a.GenerateFailerMsg()` to `a.fail(failerMsg)`.

Increments last subcounter in `a`.

Returns `logical_val`.

func (*Asserter) AssertEq

func (a *Asserter) AssertEq(lhs any, rhs any) bool

Like `.Assert`, but appends string-printed values of `lhs` and `rhs` in "%v" format to basic fail message, explaining they are different.

Being an inteligent function, it can decice to also append some other descriptions of `lhs` & `rhs` objects (eg. their types). This happens when both values look misleadingly similar in "%v" format.

func (*Asserter) AssertWithFailMsgAppendix

func (a *Asserter) AssertWithFailMsgAppendix(logical_val bool, appendix string) bool

If `logical_va`=false, passes ```a.GenerateFailerMsg() + appendix``` to `a.fail`.

Increments last subcounter in `a`.

Returns `logical_val`.

func (*Asserter) Clone

func (a *Asserter) Clone() Asserter

Returns a deep copy of `*a`.

func (*Asserter) Counter

func (a *Asserter) Counter() []uint

Returns private member `a.counter`.

func (*Asserter) Eq

func (lhs *Asserter) Eq(rhs *Asserter) bool

Returns true if `lhs` & `rhs` are semantically equal.

func (*Asserter) Fail

func (a *Asserter) Fail() func(failerMsg string)

Returns private member `a.fail`.

func (*Asserter) GenerateEnumerationTag

func (a *Asserter) GenerateEnumerationTag() string

Generates enumeration tag for upcoming assertion case in form "α.β.γ....ζ",

where α = `a.counter[0]`, β = `a.counter[1]`, ..., ζ = `a.counter[len(a.counter)-1]` .

Example tags: "0.0.0", "1.3", "5", "1.1.2"

func (*Asserter) GenerateFailerMsg

func (a *Asserter) GenerateFailerMsg() (failerMsg string)

Description:

return a.GenerateEnumerationTag() + " assertion failed!"

func (*Asserter) Inc

func (a *Asserter) Inc(ind uint) (indexNotPresentInCounter bool)

If possible, does a.counter[ind]++ and returns false.

Otherwise returns true.

func (*Asserter) IncLast

func (a *Asserter) IncLast()

Description:

a.counter[len(a.counter)-1]++

func (*Asserter) NewReseted

func (a *Asserter) NewReseted() Asserter

Similar to:

NewReseter(uint(len(a.counter)), a.fail)

but because it is bound to succeed it doesn't return `error` .

func (*Asserter) PSetCounter added in v2.1.0

func (a *Asserter) PSetCounter(counter []uint)

Mad version of `SetCounter(counter)`.

Panic on error − not returning it.

func (*Asserter) PSetFail added in v2.1.0

func (a *Asserter) PSetFail(fail func(failerMsg string))

Mad version of `SetFail(fail)`. Panic on error − not returning it.

func (*Asserter) ResetCounter

func (a *Asserter) ResetCounter()

Zeroes the internal counter.

func (*Asserter) SetCounter

func (a *Asserter) SetCounter(counter []uint) error

Description:

if len(counter) == 0 {
	return EmptyCounterError{}
}
a.counter = counter
return nil

func (*Asserter) SetFail

func (a *Asserter) SetFail(fail func(failerMsg string)) error

Description:

if fail == nil {
	return (NilFailError{})
}
a.fail = fail
return nil

type EmptyCounterError

type EmptyCounterError struct{}

Denotes that length of `counter` variable equals 0, which means it cannot be(come) member `.counter` & be used for assertion counting.

func (EmptyCounterError) Error

func (EmptyCounterError) Error() string

Description:

return "len(counter) == 0"

type NilFailError

type NilFailError struct{}

Denotes that `fail` variable equals nil. which means it cannot be(come) member `.fail` & be used to log failing assertions.

func (NilFailError) Error

func (NilFailError) Error() string

Description:

return "fail == nil"

Jump to

Keyboard shortcuts

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