PathrockNetwork Gno Explorer
HomeBlocksTransactionsTokensRealmsPackagesValidatorsAnalytics

PathrockNetwork Gno Explorer — an independent explorer for Gno.land Mainnet (gnoland-1), operated by PathrockNetwork. Not an official Gno.land service.

gnowebarchive RPC

gno.land/p/g17khqpukees4237dtn3astzapmp462vjhsz6st4/expect

Package
Open in gnoweb ↗

Overview

Kind
Pure package
Name
expect
Namespace
g17khqpukees4237dtn3astzapmp462vjhsz6st4
Files
13 (README)(gnomod.toml)
Exported functions
n/a — not supported for pure packages by the node (vm/qfuncs)
Module
gno.land/p/g17khqpukees4237dtn3astzapmp462vjhsz6st4/expect
gno
0.9

Files (13)

  • README.mdmarkdown
  • gnomod.tomltoml
  • boolean.gnogno
  • context.gnogno
  • doc.gnogno
  • error.gnogno
  • float.gnogno
  • func.gnogno
  • int.gnogno
  • message.gnogno
  • string.gnogno
  • uint.gnogno
  • value.gnogno
  • value.gnogno
    1package expect23import (4	"strings"56	"gno.land/p/nt/ufmt/v0"7)8

    Functions

    not supported for pure packages by the node (vm/qfuncs)

    Signatures reconstructed verbatim from vm/qfuncs — interface params keep their inline definitions.

    9// Value creates a new checker values of different types.
    10func Value(t TestingT, value any) ValueChecker {
    11 return ValueChecker{
    12 ctx: NewContext(t),
    13 value: value,
    14 }
    15}
    16
    17// ValueChecker asserts values of different types.
    18type ValueChecker struct {
    19 ctx Context
    20 value any
    21}
    22
    23// WithFailPrefix assigns a prefix that will be prefixed to testing errors when an assertion fails.
    24func (c ValueChecker) WithFailPrefix(prefix string) ValueChecker {
    25 c.ctx.prefix = prefix
    26 return c
    27}
    28
    29// Not negates the next called expectation.
    30func (c ValueChecker) Not() ValueChecker {
    31 c.ctx.negated = !c.ctx.negated
    32 return c
    33}
    34
    35// ToBeNil asserts that current value is nil.
    36func (c ValueChecker) ToBeNil() {
    37 c.ctx.T().Helper()
    38 c.ctx.CheckExpectation(c.value == nil || istypednil(c.value), func(ctx Context) string {
    39 if !ctx.IsNegated() {
    40 return ufmt.Sprintf("Expected value to be nil\nGot: %v", c.value)
    41 }
    42 return "Expected a non nil value"
    43 })
    44}
    45
    46// ToEqual asserts that current value is equal to an expected value.
    47func (c ValueChecker) ToEqual(value any) {
    48 c.ctx.T().Helper()
    49
    50 // Assert error values first to allow comparing errors to string values
    51 if err, ok := c.value.(error); ok {
    52 want, ok := value.(error)
    53 if !ok {
    54 c.ctx.Fail("Failed: expected an error value\nGot: %T", value)
    55 return
    56 }
    57
    58 c.ctx.CheckExpectation(err.Error() == want.Error(), func(ctx Context) string {
    59 if !ctx.IsNegated() {
    60 return ufmt.Sprintf("Expected errors to match\nGot: %s\nWant: %s", err.Error(), want.Error())
    61 }
    62 return ufmt.Sprintf("Expected errors to be different\nGot: %s", err.Error())
    63 })
    64
    65 return
    66 }
    67
    68 switch v := value.(type) {
    69 case string:
    70 c.AsString().ToEqual(v)
    71 case []byte:
    72 c.AsString().ToEqual(string(v))
    73 case Stringer:
    74 c.AsString().ToEqual(v.String())
    75 case bool:
    76 c.AsBoolean().ToEqual(v)
    77 case float32:
    78 c.AsFloat().ToEqual(float64(v))
    79 case float64:
    80 c.AsFloat().ToEqual(v)
    81 case uint:
    82 c.AsUint().ToEqual(uint64(v))
    83 case uint8:
    84 c.AsUint().ToEqual(uint64(v))
    85 case uint16:
    86 c.AsUint().ToEqual(uint64(v))
    87 case uint32:
    88 c.AsUint().ToEqual(uint64(v))
    89 case uint64:
    90 c.AsUint().ToEqual(v)
    91 case int:
    92 c.AsInt().ToEqual(int64(v))
    93 case int8:
    94 c.AsInt().ToEqual(int64(v))
    95 case int16:
    96 c.AsInt().ToEqual(int64(v))
    97 case int32:
    98 c.AsInt().ToEqual(int64(v))
    99 case int64:
    100 c.AsInt().ToEqual(v)
    101 case error:
    102 c.ctx.Fail("Error is not equal to value\nGot: %s", v.Error())
    103 default:
    104 c.ctx.Fail("Unsupported type: %T", value)
    105 }
    106}
    107
    108// ToContainErrorString asserts that current error value contains an error string.
    109func (c ValueChecker) ToContainErrorString(msg string) {
    110 c.ctx.T().Helper()
    111
    112 err, ok := c.value.(error)
    113 if !ok {
    114 c.ctx.Fail("Failed: expected an error value\nGot: %T", c.value)
    115 return
    116 }
    117
    118 c.ctx.CheckExpectation(strings.Contains(err.Error(), msg), func(ctx Context) string {
    119 if !ctx.IsNegated() {
    120 return ufmt.Sprintf("Expected error message to contain: %s\nGot: %s", msg, err.Error())
    121 }
    122 return ufmt.Sprintf("Expected error message not to contain: %s\nGot: %s", msg, err.Error())
    123 })
    124}
    125
    126// AsString returns a checker to assert current value as a string.
    127func (c ValueChecker) AsString() StringChecker {
    128 c.ctx.T().Helper()
    129
    130 v, err := asString(c.value)
    131 if err != nil {
    132 c.ctx.Fail("Failed: %s: expected a string value\nGot: %T", err.Error(), c.value)
    133 return StringChecker{}
    134 }
    135
    136 return NewStringChecker(c.ctx, v)
    137}
    138
    139// AsBoolean returns a checker to assert current value as a boolean.
    140func (c ValueChecker) AsBoolean() BooleanChecker {
    141 c.ctx.T().Helper()
    142
    143 v, err := asBoolean(c.value)
    144 if err != nil {
    145 c.ctx.Fail("Failed: %s: expected a boolean value\nGot: %T", err.Error(), c.value)
    146 return BooleanChecker{}
    147 }
    148
    149 return NewBooleanChecker(c.ctx, v)
    150}
    151
    152// AsFloat returns a checker to assert current value as a float64.
    153func (c ValueChecker) AsFloat() FloatChecker {
    154 c.ctx.T().Helper()
    155
    156 v, err := asFloat(c.value)
    157 if err != nil {
    158 c.ctx.Fail("%s: expected a float value\nGot: %T", err.Error(), c.value)
    159 return FloatChecker{}
    160 }
    161
    162 return NewFloatChecker(c.ctx, v)
    163}
    164
    165// AsUint returns a checker to assert current value as a uint64.
    166func (c ValueChecker) AsUint() UintChecker {
    167 c.ctx.T().Helper()
    168
    169 v, err := asUint(c.value)
    170 if err != nil {
    171 c.ctx.Fail("Failed: %s: expected a uint value\nGot: %T", err.Error(), c.value)
    172 return UintChecker{}
    173 }
    174
    175 return NewUintChecker(c.ctx, v)
    176}
    177
    178// AsInt returns a checker to assert current value as a int64.
    179func (c ValueChecker) AsInt() IntChecker {
    180 c.ctx.T().Helper()
    181
    182 v, err := asInt(c.value)
    183 if err != nil {
    184 c.ctx.Fail("Failed: %s: expected an int value\nGot: %T", err.Error(), c.value)
    185 return IntChecker{}
    186 }
    187
    188 return NewIntChecker(c.ctx, v)
    189}
    190