1package expect23import (4 "strings"56 "gno.land/p/nt/ufmt/v0"7)89const defaultAssertFailMsg = "assert expectation failed"1011// NewContext creates a new testing context.12func NewContext(t TestingT) Context {13 return Context{t: t}14}1516// Context preserves the current testing context.17type Context struct {18 t TestingT19 negated bool20 prefix string21}2223// T returns context's testing T instance.24func (c Context) T() TestingT {25 if c.t == nil {26 panic("expect: context is not initialized")27 }28 return c.t29}3031// Prefix returns context's error prefix.32func (c Context) Prefix() string {33 return c.prefix34}3536// IsNegated checks if current context negates current assert expectations.37func (c Context) IsNegated() bool {38 return c.negated39}4041// CheckExpectation checks an assert expectation and calls a callback on fail.42// It returns true when the asserted expectation fails.43// Callback is called when a negated assertion succeeds or when non negated assertion fails.44func (c Context) CheckExpectation(success bool, cb func(Context) string) bool {45 failed := (c.negated && success) || (!c.negated && !success)46 if failed {47 msg := cb(c)48 if strings.TrimSpace(msg) == "" {49 msg = defaultAssertFailMsg50 }5152 c.Fail(msg)53 }54 return failed55}5657// Fail makes the current test fail with a custom message.58func (c Context) Fail(msg string, args ...any) {59 if c.prefix != "" {60 msg = c.prefix + " - " + msg61 }6263 c.t.Fatalf(msg, args...)64}6566// TestingT defines a minimal interface for `testing.T` instances.67type TestingT interface {68 Helper()69 Fatal(args ...any)70 Fatalf(format string, args ...any)71}7273// MockTestingT creates a new testing mock that writes testing output to a string builder.74func MockTestingT(output *strings.Builder) TestingT {75 return &testingT{output}76}7778type testingT struct{ buf *strings.Builder }7980func (testingT) Helper() {}81func (t testingT) Fatal(args ...any) { t.buf.WriteString(ufmt.Sprintln(args...)) }82func (t testingT) Fatalf(fmt string, args ...any) { t.buf.WriteString(ufmt.Sprintf(fmt+"\n", args...)) }83Signatures reconstructed verbatim from vm/qfuncs — interface params keep their inline definitions.