-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoption_test.go
51 lines (43 loc) · 1.05 KB
/
option_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
package hx_test
import (
"errors"
"reflect"
"testing"
"github.com/izumin5210/hx"
)
func TestCombineOptions(t *testing.T) {
opt1 := hx.OptionFunc(func(c *hx.Config) error {
c.QueryParams.Add("foo", "1")
return nil
})
opt2 := hx.OptionFunc(func(c *hx.Config) error {
c.QueryParams.Add("foo", "2")
return nil
})
optErr := hx.OptionFunc(func(c *hx.Config) error {
return errors.New("error occurred")
})
t.Run("valid", func(t *testing.T) {
cfg, err := hx.NewConfig()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
err = cfg.Apply(hx.CombineOptions(opt1, opt2))
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if got, want := cfg.QueryParams["foo"], []string{"1", "2"}; !reflect.DeepEqual(got, want) {
t.Errorf("query foo is %v, want %v", got, want)
}
})
t.Run("error", func(t *testing.T) {
cfg, err := hx.NewConfig()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
err = cfg.Apply(hx.CombineOptions(opt1, optErr))
if err == nil {
t.Error("returned nil, want an error")
}
})
}