-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparams_test.go
157 lines (129 loc) · 4.75 KB
/
params_test.go
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
152
153
154
155
156
157
package erk_test
import (
"encoding/json"
"errors"
"fmt"
"testing"
"github.com/JosiahWitt/ensure"
"github.com/JosiahWitt/ensure/ensurepkg"
"github.com/JosiahWitt/erk"
)
var _ erk.Paramable = &TestParamable{}
type TestParamable struct {
params erk.Params
}
func (p *TestParamable) Error() string {
return fmt.Sprint(p.params)
}
func (p *TestParamable) WithParams(params erk.Params) error {
p.params = params
return p
}
func (p *TestParamable) Params() erk.Params {
return p.params
}
func TestWithParams(t *testing.T) {
ensure := ensure.New(t)
ensure.Run("with erk.Paramable", func(ensure ensurepkg.Ensure) {
ensure.Run("setting empty params", func(ensure ensurepkg.Ensure) {
err1 := &TestParamable{}
err2 := erk.WithParams(err1, erk.Params{})
ensure(err1.params).IsEmpty()
ensure(err2).Equals(err1)
})
ensure.Run("setting two params", func(ensure ensurepkg.Ensure) {
err1 := &TestParamable{}
err2 := erk.WithParams(err1, erk.Params{"a": "hello", "b": "world"})
ensure(err1.params).Equals(erk.Params{"a": "hello", "b": "world"})
ensure(err2).Equals(err1)
})
})
ensure.Run("with erk.Error", func(ensure ensurepkg.Ensure) {
ensure.Run("setting two params", func(ensure ensurepkg.Ensure) {
err := erk.NewWith(ErkExample{}, "my message", erk.Params{"0": "hey", "1": "there"})
err = erk.WithParams(err, erk.Params{"a": "hello", "b": "world"})
ensure(erk.GetParams(err)).Equals(erk.Params{"0": "hey", "1": "there", "a": "hello", "b": "world"})
})
})
ensure.Run("with non erk.Paramable", func(ensure ensurepkg.Ensure) {
ensure.Run("setting two params", func(ensure ensurepkg.Ensure) {
err1 := errors.New("hi")
err2 := erk.WithParams(err1, erk.Params{"a": "hello", "b": "world"})
ensure(erk.GetParams(err2)).IsEmpty()
})
})
}
func TestWithParam(t *testing.T) {
ensure := ensure.New(t)
ensure.Run("with erk.Paramable", func(ensure ensurepkg.Ensure) {
err1 := &TestParamable{}
err2 := erk.WithParam(err1, "a", "hello")
ensure(err1.params).Equals(erk.Params{"a": "hello"})
ensure(err2).Equals(err1)
})
ensure.Run("with erk.Error", func(ensure ensurepkg.Ensure) {
err := erk.NewWith(ErkExample{}, "my message", erk.Params{"0": "hey", "1": "there"})
err = erk.WithParam(err, "a", "hello")
err = erk.WithParam(err, "b", "world")
ensure(erk.GetParams(err)).Equals(erk.Params{"0": "hey", "1": "there", "a": "hello", "b": "world"})
})
ensure.Run("with non erk.Paramable", func(ensure ensurepkg.Ensure) {
err1 := errors.New("hi")
err2 := erk.WithParam(err1, "a", "hello")
err2 = erk.WithParam(err2, "b", "world")
ensure(erk.GetParams(err2)).IsEmpty()
})
}
func TestGetParams(t *testing.T) {
ensure := ensure.New(t)
ensure.Run("with erk.Paramable", func(ensure ensurepkg.Ensure) {
err := &TestParamable{params: erk.Params{"0": "hey", "1": "there"}}
ensure(erk.GetParams(err)).Equals(erk.Params{"0": "hey", "1": "there"})
})
ensure.Run("with erk.Error", func(ensure ensurepkg.Ensure) {
err := erk.NewWith(ErkExample{}, "my message", erk.Params{"0": "hey", "1": "there"})
ensure(erk.GetParams(err)).Equals(erk.Params{"0": "hey", "1": "there"})
})
ensure.Run("with non erk.Paramable", func(ensure ensurepkg.Ensure) {
err := errors.New("hi")
ensure(erk.GetParams(err)).IsEmpty()
})
}
func TestParamsClone(t *testing.T) {
ensure := ensure.New(t)
ensure.Run("returns parameters", func(ensure ensurepkg.Ensure) {
params := erk.Params{"0": "hey", "1": "there"}
ensure(params.Clone()).Equals(erk.Params{"0": "hey", "1": "there"})
})
ensure.Run("returns a copy of the parameters", func(ensure ensurepkg.Ensure) {
params := erk.Params{"0": "hey", "1": "there"}
paramsCopy := params.Clone()
paramsCopy["0"] = "changed"
ensure(params.Clone()).Equals(erk.Params{"0": "hey", "1": "there"})
})
ensure.Run("returns empty params when params are nil", func(ensure ensurepkg.Ensure) {
var params erk.Params // Nil params
ensure(params.Clone()).Equals(erk.Params{})
})
}
func TestParamsMarshalJSON(t *testing.T) {
ensure := ensure.New(t)
ensure.Run("with no 'err' element", func(ensure ensurepkg.Ensure) {
params := erk.Params{"0": "hey", "1": "there"}
bytes, err := json.Marshal(params)
ensure(err).IsNotError()
ensure(string(bytes)).Equals(`{"0":"hey","1":"there"}`)
})
ensure.Run("with 'err' element that is not an error", func(ensure ensurepkg.Ensure) {
params := erk.Params{"0": "hey", "err": "there"}
bytes, err := json.Marshal(params)
ensure(err).IsNotError()
ensure(string(bytes)).Equals(`{"0":"hey","err":"there"}`)
})
ensure.Run("with 'err' element that is an error", func(ensure ensurepkg.Ensure) {
params := erk.Params{"0": "hey", "err": errors.New("my error")}
bytes, err := json.Marshal(params)
ensure(err).IsNotError()
ensure(string(bytes)).Equals(`{"0":"hey","err":"my error"}`)
})
}