-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvalid_test.go
More file actions
151 lines (123 loc) · 4.83 KB
/
invalid_test.go
File metadata and controls
151 lines (123 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package errpath_test
import (
"testing"
"github.com/MarkRosemaker/errpath"
)
func TestErrInvalid_bool(t *testing.T) {
errBool := &errpath.ErrInvalid[bool]{}
if want := `a value (false) is invalid`; errBool.Error() != want {
t.Fatalf("want: %s, got: %v", want, errBool)
}
errBool.Value = true
if want := `a value (true) is invalid`; errBool.Error() != want {
t.Fatalf("want: %s, got: %v", want, errBool)
}
errBool.Enum = []bool{false}
if want := `a value (true) is invalid, must be one of: false`; errBool.Error() != want {
t.Fatalf("want: %s, got: %v", want, errBool)
}
errBool.Message = "some reason"
if want := `a value (true) is invalid: some reason, must be one of: false`; errBool.Error() != want {
t.Fatalf("want: %s, got: %v", want, errBool)
}
}
func TestErrInvalid_int(t *testing.T) {
type num int
errInt := &errpath.ErrInvalid[num]{}
if want := `a value (0) is invalid`; errInt.Error() != want {
t.Fatalf("want: %s, got: %v", want, errInt)
}
errInt.Value = 7
if want := `a value (7) is invalid`; errInt.Error() != want {
t.Fatalf("want: %s, got: %v", want, errInt)
}
errInt.Message = "it is odd"
if want := `a value (7) is invalid: it is odd`; errInt.Error() != want {
t.Fatalf("want: %s, got: %v", want, errInt)
}
errInt.Enum = []num{2, 4, 6, 8}
if want := `a value (7) is invalid: it is odd, must be one of: 2, 4, 6, 8`; errInt.Error() != want {
t.Fatalf("want: %s, got: %v", want, errInt)
}
}
func TestErrInvalid_float(t *testing.T) {
type num float64
errInt := &errpath.ErrInvalid[num]{}
if want := `a value (0) is invalid`; errInt.Error() != want {
t.Fatalf("want: %s, got: %v", want, errInt)
}
errInt.Value = 7.4
if want := `a value (7.4) is invalid`; errInt.Error() != want {
t.Fatalf("want: %s, got: %v", want, errInt)
}
errInt.Message = "it is strange"
if want := `a value (7.4) is invalid: it is strange`; errInt.Error() != want {
t.Fatalf("want: %s, got: %v", want, errInt)
}
errInt.Enum = []num{2.3, 4.1, 6.7, 8.4}
if want := `a value (7.4) is invalid: it is strange, must be one of: 2.3, 4.1, 6.7, 8.4`; errInt.Error() != want {
t.Fatalf("want: %s, got: %v", want, errInt)
}
}
func TestErrInvalid_complex(t *testing.T) {
errInt := &errpath.ErrInvalid[complex128]{}
if want := `a value ((0+0i)) is invalid`; errInt.Error() != want {
t.Fatalf("want: %s, got: %v", want, errInt)
}
errInt.Value = 7.4 + 3i
if want := `a value ((7.4+3i)) is invalid`; errInt.Error() != want {
t.Fatalf("want: %s, got: %v", want, errInt)
}
errInt.Message = "it is strange"
if want := `a value ((7.4+3i)) is invalid: it is strange`; errInt.Error() != want {
t.Fatalf("want: %s, got: %v", want, errInt)
}
errInt.Enum = []complex128{2.3, 4.1 - 4i, 6.7 + 7i, 8.4 + 11i}
if want := `a value ((7.4+3i)) is invalid: it is strange, must be one of: (2.3+0i), (4.1-4i), (6.7+7i), (8.4+11i)`; errInt.Error() != want {
t.Fatalf("want: %s, got: %v", want, errInt)
}
}
func TestErrInvalid_struct(t *testing.T) {
type someStruct struct {
Foo string
Bar int
}
errBool := &errpath.ErrInvalid[someStruct]{}
if want := `a value is invalid`; errBool.Error() != want {
t.Fatalf("want: %s, got: %v", want, errBool)
}
errBool.Value = someStruct{Foo: "foo", Bar: 3}
if want := `a value (errpath_test.someStruct{Foo:"foo", Bar:3}) is invalid`; errBool.Error() != want {
t.Fatalf("want: %s, got: %v", want, errBool)
}
errBool.Enum = []someStruct{{Foo: "baz", Bar: 5}}
if want := `a value (errpath_test.someStruct{Foo:"foo", Bar:3}) is invalid, must be one of: errpath_test.someStruct{Foo:"baz", Bar:5}`; errBool.Error() != want {
t.Fatalf("want: %s, got: %v", want, errBool)
}
errBool.Message = "some reason"
if want := `a value (errpath_test.someStruct{Foo:"foo", Bar:3}) is invalid: some reason, must be one of: errpath_test.someStruct{Foo:"baz", Bar:5}`; errBool.Error() != want {
t.Fatalf("want: %s, got: %v", want, errBool)
}
}
func TestErrInvalid_pointer_to_struct(t *testing.T) {
type someStruct struct {
Foo string
Bar int
}
errBool := &errpath.ErrInvalid[*someStruct]{}
if want := `a value is invalid`; errBool.Error() != want {
t.Fatalf("want: %s, got: %v", want, errBool)
}
errBool.Value = &someStruct{Foo: "foo", Bar: 3}
if want := `a value (&errpath_test.someStruct{Foo:"foo", Bar:3}) is invalid`; errBool.Error() != want {
t.Fatalf("want: %s, got: %v", want, errBool)
}
errBool.Enum = []*someStruct{{Foo: "baz", Bar: 5}}
if want := `a value (&errpath_test.someStruct{Foo:"foo", Bar:3}) is invalid, must be one of: &errpath_test.someStruct{Foo:"baz", Bar:5}`; errBool.Error() != want {
t.Fatalf("want: %s, got: %v", want, errBool)
}
errBool.Message = "some reason"
if want := `a value (&errpath_test.someStruct{Foo:"foo", Bar:3}) is invalid: some reason, must be one of: &errpath_test.someStruct{Foo:"baz", Bar:5}`; errBool.Error() != want {
t.Fatalf("want: %s, got: %v", want, errBool)
}
}