Skip to content Search Apps Documentation Source Content File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

int.gno

3.16 Kb · 110 lines
  1package expect
  2
  3import (
  4	"strconv"
  5
  6	"gno.land/p/nt/ufmt/v0"
  7)
  8
  9// NewIntChecker creates a new checker of int64 values.
 10func NewIntChecker(ctx Context, value int64) IntChecker {
 11	return IntChecker{ctx, value}
 12}
 13
 14// IntChecker asserts int64 values.
 15type IntChecker struct {
 16	ctx   Context
 17	value int64
 18}
 19
 20// Not negates the next called expectation.
 21func (c IntChecker) Not() IntChecker {
 22	c.ctx.negated = !c.ctx.negated
 23	return c
 24}
 25
 26// ToEqual asserts that current value is equal to an expected value.
 27func (c IntChecker) ToEqual(value int64) {
 28	c.ctx.T().Helper()
 29	c.ctx.CheckExpectation(c.value == value, func(ctx Context) string {
 30		got := formatInt(c.value)
 31		if !ctx.IsNegated() {
 32			want := formatInt(value)
 33			return ufmt.Sprintf("Expected values to match\nGot: %s\nWant: %s", got, want)
 34		}
 35		return ufmt.Sprintf("Expected value to be different\nGot: %s", got)
 36	})
 37}
 38
 39// ToBeGreaterThan asserts that current value is greater than an expected value.
 40func (c IntChecker) ToBeGreaterThan(value int64) {
 41	c.ctx.T().Helper()
 42	c.ctx.CheckExpectation(c.value > value, func(ctx Context) string {
 43		got := formatInt(c.value)
 44		want := formatInt(value)
 45		if !ctx.IsNegated() {
 46			return ufmt.Sprintf("Expected values to be gerater than %s\nGot: %s", want, got)
 47		}
 48		return ufmt.Sprintf("Expected value to not to be greater than %s\nGot: %s", want, got)
 49	})
 50}
 51
 52// ToBeGreaterOrEqualThan asserts that current value is greater or equal than an expected value.
 53func (c IntChecker) ToBeGreaterOrEqualThan(value int64) {
 54	c.ctx.T().Helper()
 55	c.ctx.CheckExpectation(c.value >= value, func(ctx Context) string {
 56		got := formatInt(c.value)
 57		want := formatInt(value)
 58		if !ctx.IsNegated() {
 59			return ufmt.Sprintf("Expected values to be greater or equal than %s\nGot: %s", want, got)
 60		}
 61		return ufmt.Sprintf("Expected value to not to be greater or equal than %s\nGot: %s", want, got)
 62	})
 63}
 64
 65// ToBeLowerThan asserts that current value is lower than an expected value.
 66func (c IntChecker) ToBeLowerThan(value int64) {
 67	c.ctx.T().Helper()
 68	c.ctx.CheckExpectation(c.value < value, func(ctx Context) string {
 69		got := formatInt(c.value)
 70		want := formatInt(value)
 71		if !ctx.IsNegated() {
 72			return ufmt.Sprintf("Expected values to be lower than %s\nGot: %s", want, got)
 73		}
 74		return ufmt.Sprintf("Expected value to not to be lower than %s\nGot: %s", want, got)
 75	})
 76}
 77
 78// ToBeLowerOrEqualThan asserts that current value is lower or equal than an expected value.
 79func (c IntChecker) ToBeLowerOrEqualThan(value int64) {
 80	c.ctx.T().Helper()
 81	c.ctx.CheckExpectation(c.value <= value, func(ctx Context) string {
 82		got := formatInt(c.value)
 83		want := formatInt(value)
 84		if !ctx.IsNegated() {
 85			return ufmt.Sprintf("Expected values to be lower or equal than %s\nGot: %s", want, got)
 86		}
 87		return ufmt.Sprintf("Expected value to not to be lower or equal than %s\nGot: %s", want, got)
 88	})
 89}
 90
 91func formatInt(value int64) string {
 92	return strconv.FormatInt(value, 10)
 93}
 94
 95func asInt(value any) (int64, error) {
 96	switch v := value.(type) {
 97	case int:
 98		return int64(v), nil
 99	case int8:
100		return int64(v), nil
101	case int16:
102		return int64(v), nil
103	case int32:
104		return int64(v), nil
105	case int64:
106		return v, nil
107	default:
108		return 0, ErrIncompatibleType
109	}
110}