gno icon indicating copy to clipboard operation
gno copied to clipboard

feat(examples): yet another unit and file testing package `p/jeronimoabi/expect`

Open jeronimoalbi opened this issue 5 months ago • 2 comments

The package expect provides testing support for packages and realms.

The opinionated approach taken on this package for testing is to use function chaining and semantics.

Focus is not on speed as there are other packages that would run tests faster like the official uassert or urequire packages.

Values can be asserted using the Value() function, for example:

func TestFoo(t *testing.T) {
  got := 42
  expect.Value(t, got).ToEqual(42)
  expect.Value(t, got).Not().ToEqual(0)

  expect.Value(t, "foo").ToEqual("foo")
  expect.Value(t, 42).AsInt().Not().ToBeGreaterThan(50)
  expect.Value(t, "TRUE").AsBoolean().ToBeTruthy()
}

Functions can also be used to assert returned values, errors or panics.

Package supports four type of functions:

  • func()
  • func() any
  • func() error
  • func() (any, error)

Functions can be asserted using the Func() function, for example:

func TestFoo(t *testing.T) {
  expect.Func(t, func() {
    panic("Boom!")
  }).ToPanic().WithMessage("Boom!")

  wantErr := errors.New("Boom!")
  expect.Func(t, func() error {
    return wantErr
  }).ToFail().WithMessage("Boom!")

  expect.Func(t, func() error {
    return wantErr
  }).ToFail().WithError(wantErr)
}

jeronimoalbi avatar May 20 '25 10:05 jeronimoalbi