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.

float.gno

3.14 Kb · 104 lines
  1package expect
  2
  3import (
  4	"strconv"
  5
  6	"gno.land/p/nt/ufmt/v0"
  7)
  8
  9// NewFloatChecker creates a new checker of float64 values.
 10func NewFloatChecker(ctx Context, value float64) FloatChecker {
 11	return FloatChecker{ctx, value}
 12}
 13
 14// FloatChecker asserts float64 values.
 15type FloatChecker struct {
 16	ctx   Context
 17	value float64
 18}
 19
 20// Not negates the next called expectation.
 21func (c FloatChecker) Not() FloatChecker {
 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 FloatChecker) ToEqual(value float64) {
 28	c.ctx.T().Helper()
 29	c.ctx.CheckExpectation(c.value == value, func(ctx Context) string {
 30		got := formatFloat(c.value)
 31		if !ctx.IsNegated() {
 32			want := formatFloat(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 FloatChecker) ToBeGreaterThan(value float64) {
 41	c.ctx.T().Helper()
 42	c.ctx.CheckExpectation(c.value > value, func(ctx Context) string {
 43		got := formatFloat(c.value)
 44		want := formatFloat(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 FloatChecker) ToBeGreaterOrEqualThan(value float64) {
 54	c.ctx.T().Helper()
 55	c.ctx.CheckExpectation(c.value >= value, func(ctx Context) string {
 56		got := formatFloat(c.value)
 57		want := formatFloat(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 FloatChecker) ToBeLowerThan(value float64) {
 67	c.ctx.T().Helper()
 68	c.ctx.CheckExpectation(c.value < value, func(ctx Context) string {
 69		got := formatFloat(c.value)
 70		want := formatFloat(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 FloatChecker) ToBeLowerOrEqualThan(value float64) {
 80	c.ctx.T().Helper()
 81	c.ctx.CheckExpectation(c.value <= value, func(ctx Context) string {
 82		got := formatFloat(c.value)
 83		want := formatFloat(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 formatFloat(value float64) string {
 92	return strconv.FormatFloat(value, 'g', -1, 64)
 93}
 94
 95func asFloat(value any) (float64, error) {
 96	switch v := value.(type) {
 97	case float32:
 98		return float64(v), nil
 99	case float64:
100		return v, nil
101	default:
102		return 0, ErrIncompatibleType
103	}
104}