Documentation ¶
Overview ¶
Package for testing purposes.
`Asserter` struct does runtime assertions. Facilitates test case enumeration & debugging.
Index ¶
- type Asserter
- func New(counter []uint, fail func(failerMsg string)) (a Asserter, e error)
- func NewReseted(counterLen uint, fail func(string)) (Asserter, error)
- func NewTiny(fail func(failerMsg string)) (a Asserter, e error)
- func PNew(counter []uint, fail func(failerMsg string)) Asserter
- func PNewReseted(counterLen uint, fail func(string)) Asserter
- func PNewTiny(fail func(failerMsg string)) Asserter
- func (a *Asserter) A(logical_val bool) bool
- func (a *Asserter) AE(lhs any, rhs any) bool
- func (a *Asserter) AWFMA(logical_val bool, appendix string) bool
- func (a *Asserter) Assert(logical_val bool) bool
- func (a *Asserter) AssertEq(lhs any, rhs any) bool
- func (a *Asserter) AssertWithFailMsgAppendix(logical_val bool, appendix string) bool
- func (a *Asserter) Clone() Asserter
- func (a *Asserter) Counter() []uint
- func (lhs *Asserter) Eq(rhs *Asserter) bool
- func (a *Asserter) Fail() func(failerMsg string)
- func (a *Asserter) GenerateEnumerationTag() string
- func (a *Asserter) GenerateFailerMsg() (failerMsg string)
- func (a *Asserter) Inc(ind uint) (indexNotPresentInCounter bool)
- func (a *Asserter) IncLast()
- func (a *Asserter) NewReseted() Asserter
- func (a *Asserter) PSetCounter(counter []uint)
- func (a *Asserter) PSetFail(fail func(failerMsg string))
- func (a *Asserter) ResetCounter()
- func (a *Asserter) SetCounter(counter []uint) error
- func (a *Asserter) SetFail(fail func(failerMsg string)) error
- type EmptyCounterError
- type NilFailError
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 ¶
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 PNewReseted ¶ added in v2.1.0
Mad version of `NewReseted(counterLen,f ail)`. Panic on error − not returning it.
func (*Asserter) Assert ¶
If `logical_val`=false, passes `a.GenerateFailerMsg()` to `a.fail(failerMsg)`.
Increments last subcounter in `a`.
Returns `logical_val`.
func (*Asserter) AssertEq ¶
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 ¶
If `logical_va`=false, passes ```a.GenerateFailerMsg() + appendix``` to `a.fail`.
Increments last subcounter in `a`.
Returns `logical_val`.
func (*Asserter) GenerateEnumerationTag ¶
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 ¶
Description:
return a.GenerateEnumerationTag() + " assertion failed!"
func (*Asserter) Inc ¶
If possible, does a.counter[ind]++ and returns false.
Otherwise returns true.
func (*Asserter) NewReseted ¶
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
Mad version of `SetCounter(counter)`.
Panic on error − not returning it.
func (*Asserter) PSetFail ¶ added in v2.1.0
Mad version of `SetFail(fail)`. Panic on error − not returning it.
func (*Asserter) SetCounter ¶
Description:
if len(counter) == 0 { return EmptyCounterError{} } a.counter = counter 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.