generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
option_test.go
111 lines (91 loc) · 2.28 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
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
// SPDX-FileCopyrightText: 2023 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package praetor
import (
"errors"
"net/http"
"testing"
"github.com/hashicorp/consul/api"
"github.com/stretchr/testify/suite"
)
type OptionSuite struct {
suite.Suite
}
func (suite *OptionSuite) testAsOptionWithOption() {
suite.Run("Success", func() {
opt := Option(func(cfg *api.Config) error {
cfg.Address = testAddress
return nil
})
var cfg api.Config
err := AsOption(opt)(&cfg)
suite.NoError(err)
suite.Equal(testAddress, cfg.Address)
})
suite.Run("Fail", func() {
expectedErr := errors.New("expected")
opt := Option(func(cfg *api.Config) error {
return expectedErr
})
var cfg api.Config
err := AsOption(opt)(&cfg)
suite.ErrorIs(err, expectedErr)
})
}
func (suite *OptionSuite) testAsOptionWithClosure() {
suite.Run("Success", func() {
opt := func(cfg *api.Config) error {
cfg.Address = testAddress
return nil
}
var cfg api.Config
err := AsOption(opt)(&cfg)
suite.NoError(err)
suite.Equal(testAddress, cfg.Address)
})
suite.Run("Fail", func() {
expectedErr := errors.New("expected")
opt := func(cfg *api.Config) error {
return expectedErr
}
var cfg api.Config
err := AsOption(opt)(&cfg)
suite.ErrorIs(err, expectedErr)
})
}
func (suite *OptionSuite) testAsOptionNoError() {
opt := func(cfg *api.Config) {
cfg.Address = testAddress
}
var cfg api.Config
err := AsOption(opt)(&cfg)
suite.NoError(err)
suite.Equal(testAddress, cfg.Address)
}
func (suite *OptionSuite) testAsOptionCustomType() {
type TestFunc func(*api.Config)
var opt TestFunc = func(cfg *api.Config) {
cfg.Address = testAddress
}
var cfg api.Config
err := AsOption(opt)(&cfg)
suite.NoError(err)
suite.Equal(testAddress, cfg.Address)
}
func (suite *OptionSuite) TestAsOption() {
suite.Run("WithOption", suite.testAsOptionWithOption)
suite.Run("WithClosure", suite.testAsOptionWithClosure)
suite.Run("NoError", suite.testAsOptionNoError)
suite.Run("CustomType", suite.testAsOptionCustomType)
}
func (suite *OptionSuite) TestWithHTTPClient() {
c := new(http.Client)
var cfg api.Config
suite.NoError(
WithHTTPClient(c)(&cfg),
)
suite.Same(c, cfg.HttpClient)
}
func TestOption(t *testing.T) {
suite.Run(t, new(OptionSuite))
}