uint.gno
3.30 Kb ยท 115 lines
1package expect
2
3import (
4 "strconv"
5
6 "gno.land/p/demo/ufmt"
7)
8
9// NewUintChecker creates a new checker of uint64 values.
10func NewUintChecker(ctx Context, value uint64) UintChecker {
11 return UintChecker{ctx, value}
12}
13
14// UintChecker asserts uint64 values.
15type UintChecker struct {
16 ctx Context
17 value uint64
18}
19
20// Not negates the next called expectation.
21func (c UintChecker) Not() UintChecker {
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 UintChecker) ToEqual(value uint64) {
28 c.ctx.T().Helper()
29 c.ctx.CheckExpectation(c.value == value, func(ctx Context) string {
30 if !ctx.IsNegated() {
31 got := formatUint(c.value)
32 want := formatUint(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", formatUint(c.value))
36 })
37}
38
39// ToBeGreaterThan asserts that current value is greater than an expected value.
40func (c UintChecker) ToBeGreaterThan(value uint64) {
41 c.ctx.T().Helper()
42 c.ctx.CheckExpectation(c.value > value, func(ctx Context) string {
43 got := formatUint(c.value)
44 want := formatUint(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 UintChecker) ToBeGreaterOrEqualThan(value uint64) {
54 c.ctx.T().Helper()
55 c.ctx.CheckExpectation(c.value >= value, func(ctx Context) string {
56 got := formatUint(c.value)
57 want := formatUint(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 UintChecker) ToBeLowerThan(value uint64) {
67 c.ctx.T().Helper()
68 c.ctx.CheckExpectation(c.value < value, func(ctx Context) string {
69 got := formatUint(c.value)
70 want := formatUint(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 UintChecker) ToBeLowerOrEqualThan(value uint64) {
80 c.ctx.T().Helper()
81 c.ctx.CheckExpectation(c.value <= value, func(ctx Context) string {
82 got := formatUint(c.value)
83 want := formatUint(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 formatUint(value uint64) string {
92 return strconv.FormatUint(value, 10)
93}
94
95func asUint(value any) (uint64, error) {
96 switch v := value.(type) {
97 case uint:
98 return uint64(v), nil
99 case uint8:
100 return uint64(v), nil
101 case uint16:
102 return uint64(v), nil
103 case uint32:
104 return uint64(v), nil
105 case uint64:
106 return v, nil
107 case int:
108 if v < 0 {
109 return 0, ErrIncompatibleType
110 }
111 return uint64(v), nil
112 default:
113 return 0, ErrIncompatibleType
114 }
115}