Skip to content

Commit e1b74eb

Browse files
authored
chore(examples): minor gno.land/p/jeronimoalbi/expect improvements (#4893)
Allows asserting errors using `expect.Value()`: ```go err := errors.New("foo") // Assert error expect.Value(t, err).ToEqual(errors.New("foo")) // Assert error message expect.Value(t, err).ToContainErrorString("foo") ``` Changes `ToBeNil()` to assert typed nils. Changes cast to boolean type to be consistent.
1 parent 4bdf6e3 commit e1b74eb

File tree

9 files changed

+119
-22
lines changed

9 files changed

+119
-22
lines changed

examples/gno.land/p/jeronimoalbi/expect/boolean.gno

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,32 +58,31 @@ func (c BooleanChecker) ToBeTruthy() {
5858
})
5959
}
6060

61-
func formatBoolean(value bool) string {
62-
return strconv.FormatBool(value)
63-
}
64-
6561
func asBoolean(value any) (bool, error) {
6662
if value == nil {
6763
return false, nil
6864
}
6965

66+
var s string
7067
switch v := value.(type) {
7168
case bool:
7269
return v, nil
7370
case string:
74-
if v == "" {
75-
return false, nil
76-
}
77-
return strconv.ParseBool(v)
71+
s = v
7872
case []byte:
79-
return v != nil, nil
73+
s = string(v)
8074
case Stringer:
81-
s := v.String()
82-
if s == "" {
83-
return false, nil
84-
}
85-
return strconv.ParseBool(v.String())
75+
s = v.String()
8676
default:
8777
return false, ErrIncompatibleType
8878
}
79+
80+
if s != "" {
81+
return strconv.ParseBool(s)
82+
}
83+
return false, nil
84+
}
85+
86+
func formatBoolean(value bool) string {
87+
return strconv.FormatBool(value)
8988
}

examples/gno.land/p/jeronimoalbi/expect/value.gno

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package expect
22

3-
import "gno.land/p/nt/ufmt"
3+
import (
4+
"strings"
5+
6+
"gno.land/p/nt/ufmt"
7+
)
48

59
// Value creates a new checker values of different types.
610
func Value(t TestingT, value any) ValueChecker {
@@ -31,7 +35,7 @@ func (c ValueChecker) Not() ValueChecker {
3135
// ToBeNil asserts that current value is nil.
3236
func (c ValueChecker) ToBeNil() {
3337
c.ctx.T().Helper()
34-
c.ctx.CheckExpectation(c.value == nil, func(ctx Context) string {
38+
c.ctx.CheckExpectation(c.value == nil || istypednil(c.value), func(ctx Context) string {
3539
if !ctx.IsNegated() {
3640
return ufmt.Sprintf("Expected value to be nil\nGot: %v", c.value)
3741
}
@@ -43,11 +47,31 @@ func (c ValueChecker) ToBeNil() {
4347
func (c ValueChecker) ToEqual(value any) {
4448
c.ctx.T().Helper()
4549

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+
4668
switch v := value.(type) {
4769
case string:
4870
c.AsString().ToEqual(v)
4971
case []byte:
5072
c.AsString().ToEqual(string(v))
73+
case Stringer:
74+
c.AsString().ToEqual(v.String())
5175
case bool:
5276
c.AsBoolean().ToEqual(v)
5377
case float32:
@@ -74,11 +98,31 @@ func (c ValueChecker) ToEqual(value any) {
7498
c.AsInt().ToEqual(int64(v))
7599
case int64:
76100
c.AsInt().ToEqual(v)
101+
case error:
102+
c.ctx.Fail("Error is not equal to value\nGot: %s", v.Error())
77103
default:
78104
c.ctx.Fail("Unsupported type: %T", value)
79105
}
80106
}
81107

108+
// ToContainErrorString asserts that current error value contains an error string.
109+
func (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+
82126
// AsString returns a checker to assert current value as a string.
83127
func (c ValueChecker) AsString() StringChecker {
84128
c.ctx.T().Helper()

examples/gno.land/p/jeronimoalbi/expect/value_test.gno

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package expect_test
22

33
import (
4+
"errors"
45
"testing"
56

67
"gno.land/p/jeronimoalbi/expect"
@@ -12,6 +13,7 @@ func TestValue(t *testing.T) {
1213

1314
expect.Value(t, "foo").ToEqual("foo")
1415
expect.Value(t, []byte("foo")).ToEqual([]byte("foo"))
16+
expect.Value(t, stringer("foo")).ToEqual(stringer("foo"))
1517
expect.Value(t, true).ToEqual(true)
1618
expect.Value(t, float32(1)).ToEqual(float32(1))
1719
expect.Value(t, float64(1)).ToEqual(float64(1))
@@ -25,13 +27,16 @@ func TestValue(t *testing.T) {
2527
expect.Value(t, int16(1)).ToEqual(int16(1))
2628
expect.Value(t, int32(1)).ToEqual(int32(1))
2729
expect.Value(t, int64(1)).ToEqual(int64(1))
30+
expect.Value(t, errors.New("foo")).ToEqual(errors.New("foo"))
31+
expect.Value(t, errors.New("foo bar")).ToContainErrorString("foo")
2832
})
2933

3034
t.Run("not to equal", func(t *testing.T) {
3135
t.Parallel()
3236

3337
expect.Value(t, "foo").Not().ToEqual("bar")
3438
expect.Value(t, []byte("foo")).Not().ToEqual([]byte("bar"))
39+
expect.Value(t, stringer("foo")).Not().ToEqual(stringer("bar"))
3540
expect.Value(t, true).Not().ToEqual(false)
3641
expect.Value(t, float32(1)).Not().ToEqual(float32(2))
3742
expect.Value(t, float64(1)).Not().ToEqual(float64(2))
@@ -45,11 +50,14 @@ func TestValue(t *testing.T) {
4550
expect.Value(t, int16(1)).Not().ToEqual(int16(2))
4651
expect.Value(t, int32(1)).Not().ToEqual(int32(2))
4752
expect.Value(t, int64(1)).Not().ToEqual(int64(2))
53+
expect.Value(t, errors.New("foo")).Not().ToEqual(errors.New("bar"))
54+
expect.Value(t, errors.New("foo")).Not().ToContainErrorString("bar")
4855
})
4956

5057
t.Run("to be nil", func(t *testing.T) {
5158
t.Parallel()
5259
expect.Value(t, nil).ToBeNil()
60+
expect.Value(t, (*int)(nil)).ToBeNil() // typed nil
5361
})
5462

5563
t.Run("not to be nil", func(t *testing.T) {
@@ -64,17 +72,19 @@ func TestValue(t *testing.T) {
6472
expect.Value(t, "TRUE").AsBoolean().ToBeTruthy()
6573
expect.Value(t, "t").AsBoolean().ToBeTruthy()
6674
expect.Value(t, "1").AsBoolean().ToBeTruthy()
67-
expect.Value(t, []byte("foo")).AsBoolean().ToBeTruthy()
75+
expect.Value(t, []byte("true")).AsBoolean().ToBeTruthy()
6876
})
6977

7078
t.Run("not to be truthy", func(t *testing.T) {
7179
t.Parallel()
7280

81+
expect.Value(t, "").AsBoolean().Not().ToBeTruthy()
7382
expect.Value(t, "false").AsBoolean().Not().ToBeTruthy()
7483
expect.Value(t, "FALSE").AsBoolean().Not().ToBeTruthy()
7584
expect.Value(t, "f").AsBoolean().Not().ToBeTruthy()
7685
expect.Value(t, "0").AsBoolean().Not().ToBeTruthy()
7786
expect.Value(t, []byte(nil)).AsBoolean().Not().ToBeTruthy()
87+
expect.Value(t, []byte("false")).AsBoolean().Not().ToBeTruthy()
7888
})
7989

8090
t.Run("to be falsy", func(t *testing.T) {
@@ -95,7 +105,7 @@ func TestValue(t *testing.T) {
95105
expect.Value(t, "TRUE").AsBoolean().Not().ToBeFalsy()
96106
expect.Value(t, "t").AsBoolean().Not().ToBeFalsy()
97107
expect.Value(t, "1").AsBoolean().Not().ToBeFalsy()
98-
expect.Value(t, []byte("foo")).AsBoolean().Not().ToBeFalsy()
108+
expect.Value(t, []byte("true")).AsBoolean().Not().ToBeFalsy()
99109
})
100110

101111
t.Run("to equal stringer", func(t *testing.T) {
@@ -110,3 +120,7 @@ func TestValue(t *testing.T) {
110120
expect.Value(t, address("foo")).AsString().Not().ToEqual("bar")
111121
})
112122
}
123+
124+
type stringer string
125+
126+
func (s stringer) String() string { return string(s) }

examples/gno.land/p/jeronimoalbi/expect/z_boolean_1_filetest.gno

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ func main() {
2525
expect.Value(t, "TRUE").AsBoolean().ToBeFalsy()
2626
expect.Value(t, "FALSE").AsBoolean().Not().ToBeFalsy()
2727

28-
expect.Value(t, []byte("")).AsBoolean().ToBeFalsy()
29-
expect.Value(t, []byte(nil)).AsBoolean().Not().ToBeFalsy()
28+
expect.Value(t, []byte("TRUE")).AsBoolean().ToBeFalsy()
29+
expect.Value(t, []byte("FALSE")).AsBoolean().Not().ToBeFalsy()
3030

3131
expect.Value(t, intStringer{1}).AsBoolean().ToBeFalsy()
3232
expect.Value(t, intStringer{0}).AsBoolean().Not().ToBeFalsy()

examples/gno.land/p/jeronimoalbi/expect/z_boolean_2_filetest.gno

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ func main() {
2525
expect.Value(t, "FALSE").AsBoolean().ToBeTruthy()
2626
expect.Value(t, "TRUE").AsBoolean().Not().ToBeTruthy()
2727

28-
expect.Value(t, []byte(nil)).AsBoolean().ToBeTruthy()
29-
expect.Value(t, []byte("")).AsBoolean().Not().ToBeTruthy()
28+
expect.Value(t, []byte("FALSE")).AsBoolean().ToBeTruthy()
29+
expect.Value(t, []byte("TRUE")).AsBoolean().Not().ToBeTruthy()
3030

3131
expect.Value(t, intStringer{0}).AsBoolean().ToBeTruthy()
3232
expect.Value(t, intStringer{1}).AsBoolean().Not().ToBeTruthy()

examples/gno.land/p/jeronimoalbi/expect/z_value_0_filetest.gno

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"errors"
45
"strings"
56

67
"gno.land/p/jeronimoalbi/expect"
@@ -11,9 +12,14 @@ var (
1112
t = expect.MockTestingT(&output)
1213
)
1314

15+
type stringer string
16+
17+
func (s stringer) String() string { return string(s) }
18+
1419
func main() {
1520
expect.Value(t, "foo").ToEqual("bar")
1621
expect.Value(t, []byte("foo")).ToEqual([]byte("bar"))
22+
expect.Value(t, stringer("foo")).ToEqual(stringer("bar"))
1723
expect.Value(t, true).ToEqual(false)
1824
expect.Value(t, float32(1)).ToEqual(float32(2))
1925
expect.Value(t, float64(1.1)).ToEqual(float64(1.2))
@@ -27,7 +33,10 @@ func main() {
2733
expect.Value(t, int16(1)).ToEqual(int16(2))
2834
expect.Value(t, int32(1)).ToEqual(int32(2))
2935
expect.Value(t, int64(1)).ToEqual(int64(2))
36+
expect.Value(t, errors.New("foo")).ToEqual(errors.New("bar"))
37+
expect.Value(t, errors.New("foo")).ToContainErrorString("bar")
3038

39+
expect.Value(t, 0).ToEqual(errors.New("foo"))
3140
expect.Value(t, 0).ToEqual([]string{})
3241

3342
println(output.String())
@@ -41,6 +50,9 @@ func main() {
4150
// Got: foo
4251
// Want: bar
4352
// Expected values to match
53+
// Got: foo
54+
// Want: bar
55+
// Expected values to match
4456
// Got: true
4557
// Want: false
4658
// Expected values to match
@@ -79,4 +91,11 @@ func main() {
7991
// Expected values to match
8092
// Got: 1
8193
// Want: 2
94+
// Expected errors to match
95+
// Got: foo
96+
// Want: bar
97+
// Expected error message to contain: bar
98+
// Got: foo
99+
// Error is not equal to value
100+
// Got: foo
82101
// Unsupported type: unknown

examples/gno.land/p/jeronimoalbi/expect/z_value_1_filetest.gno

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"errors"
45
"strings"
56

67
"gno.land/p/jeronimoalbi/expect"
@@ -11,9 +12,14 @@ var (
1112
t = expect.MockTestingT(&output)
1213
)
1314

15+
type stringer string
16+
17+
func (s stringer) String() string { return string(s) }
18+
1419
func main() {
1520
expect.Value(t, "foo").Not().ToEqual("foo")
1621
expect.Value(t, []byte("foo")).Not().ToEqual([]byte("foo"))
22+
expect.Value(t, stringer("foo")).Not().ToEqual(stringer("foo"))
1723
expect.Value(t, true).Not().ToEqual(true)
1824
expect.Value(t, float32(1)).Not().ToEqual(float32(1))
1925
expect.Value(t, float64(1)).Not().ToEqual(float64(1))
@@ -27,7 +33,10 @@ func main() {
2733
expect.Value(t, int16(1)).Not().ToEqual(int16(1))
2834
expect.Value(t, int32(1)).Not().ToEqual(int32(1))
2935
expect.Value(t, int64(1)).Not().ToEqual(int64(1))
36+
expect.Value(t, errors.New("foo")).Not().ToEqual(errors.New("foo"))
37+
expect.Value(t, errors.New("foo bar")).Not().ToContainErrorString("bar")
3038

39+
expect.Value(t, 0).Not().ToEqual(errors.New("foo"))
3140
expect.Value(t, 0).Not().ToEqual([]string{})
3241

3342
println(output.String())
@@ -39,6 +48,8 @@ func main() {
3948
// Expected values to be different
4049
// Got: foo
4150
// Expected values to be different
51+
// Got: foo
52+
// Expected values to be different
4253
// Got: true
4354
// Expected value to be different
4455
// Got: 1
@@ -64,4 +75,10 @@ func main() {
6475
// Got: 1
6576
// Expected value to be different
6677
// Got: 1
78+
// Expected errors to be different
79+
// Got: foo
80+
// Expected error message not to contain: bar
81+
// Got: foo bar
82+
// Error is not equal to value
83+
// Got: foo
6784
// Unsupported type: unknown

examples/gno.land/p/jeronimoalbi/expect/z_value_3_filetest.gno

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ var (
1313

1414
func main() {
1515
expect.Value(t, nil).Not().ToBeNil()
16+
expect.Value(t, (*int)(nil)).Not().ToBeNil()
1617

1718
println(output.String())
1819
}
1920

2021
// Output:
2122
// Expected a non nil value
23+
// Expected a non nil value

examples/gno.land/p/jeronimoalbi/expect/z_value_4_filetest.gno

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ var (
1313

1414
func main() {
1515
expect.Value(t, nil).WithFailPrefix("Foo prefix").Not().ToBeNil()
16+
expect.Value(t, (*int)(nil)).WithFailPrefix("Foo prefix").Not().ToBeNil()
1617

1718
println(output.String())
1819
}
1920

2021
// Output:
2122
// Foo prefix - Expected a non nil value
23+
// Foo prefix - Expected a non nil value

0 commit comments

Comments
 (0)