func_test.gno
2.08 Kb ยท 114 lines
1package expect_test
2
3import (
4 "errors"
5 "testing"
6
7 "gno.land/p/jeronimoalbi/expect"
8)
9
10func TestFunction(t *testing.T) {
11 t.Run("not to fail", func(t *testing.T) {
12 t.Parallel()
13
14 expect.Func(t, func() error {
15 return nil
16 }).Not().ToFail()
17 })
18
19 t.Run("to fail", func(t *testing.T) {
20 t.Parallel()
21
22 expect.Func(t, func() error {
23 return errors.New("Foo")
24 }).ToFail()
25 })
26
27 t.Run("to fail with mesasge", func(t *testing.T) {
28 t.Parallel()
29
30 expect.Func(t, func() error {
31 return errors.New("Foo")
32 }).ToFail().WithMessage("Foo")
33 })
34
35 t.Run("to fail with different message", func(t *testing.T) {
36 t.Parallel()
37
38 expect.Func(t, func() error {
39 return errors.New("Bar")
40 }).ToFail().Not().WithMessage("Foo")
41 })
42
43 t.Run("to fail with error", func(t *testing.T) {
44 t.Parallel()
45
46 expect.Func(t, func() error {
47 return errors.New("Foo")
48 }).ToFail().WithError(errors.New("Foo"))
49 })
50
51 t.Run("to fail with different error", func(t *testing.T) {
52 t.Parallel()
53
54 expect.Func(t, func() error {
55 return errors.New("Bar")
56 }).ToFail().Not().WithError(errors.New("Foo"))
57 })
58
59 t.Run("not to panic", func(t *testing.T) {
60 t.Parallel()
61
62 expect.Func(t, func() error {
63 return nil
64 }).Not().ToPanic()
65 })
66
67 t.Run("to panic", func(t *testing.T) {
68 t.Parallel()
69
70 expect.Func(t, func() error {
71 panic("Foo")
72 }).ToPanic()
73 })
74
75 t.Run("to panic with message", func(t *testing.T) {
76 t.Parallel()
77
78 expect.Func(t, func() error {
79 panic("Foo")
80 }).ToPanic().WithMessage("Foo")
81 })
82
83 t.Run("to panich with different message", func(t *testing.T) {
84 t.Parallel()
85
86 expect.Func(t, func() error {
87 panic("Foo")
88 }).ToPanic().Not().WithMessage("Bar")
89 })
90
91 t.Run("to return value", func(t *testing.T) {
92 t.Parallel()
93
94 expect.Func(t, func() any {
95 return "foo"
96 }).ToReturn("foo")
97
98 expect.Func(t, func() (any, error) {
99 return "foo", nil
100 }).ToReturn("foo")
101 })
102
103 t.Run("not to return value", func(t *testing.T) {
104 t.Parallel()
105
106 expect.Func(t, func() any {
107 return "foo"
108 }).Not().ToReturn("bar")
109
110 expect.Func(t, func() (any, error) {
111 return "foo", nil
112 }).Not().ToReturn("bar")
113 })
114}