Skip to content

Commit d8e4cbd

Browse files
committed
Add test case
1 parent 6ab25a7 commit d8e4cbd

File tree

5 files changed

+133
-2
lines changed

5 files changed

+133
-2
lines changed

conf/internal/storage.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,13 @@ func NewStorage() *Storage {
7373

7474
// Copy returns a new copy of the *Storage object.
7575
func (s *Storage) Copy() *Storage {
76+
data := s.Data()
77+
if nil == data {
78+
data = make(map[string]string)
79+
}
7680
return &Storage{
7781
tree: s.tree.Copy(),
78-
data: s.Data(),
82+
data: data,
7983
}
8084
}
8185

dync/dync.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func (p *Properties) Bind(i interface{}, args ...conf.BindArg) error {
7575
return p.load().Bind(i, args...)
7676
}
7777

78+
// Set refresh properties value by key.
7879
func (p *Properties) Set(key, value string) error {
7980
prop := p.load().Copy()
8081
if err := prop.Set(key, value); nil != err {
@@ -84,6 +85,7 @@ func (p *Properties) Set(key, value string) error {
8485
return p.refreshKeys(prop, []string{key})
8586
}
8687

88+
// Remove delete key from properties.
8789
func (p *Properties) Remove(key string) error {
8890
prop := p.load()
8991
coped := conf.New()

dync/dync_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,27 @@ func TestDynamic(t *testing.T) {
132132
assert.Nil(t, err)
133133
})
134134

135+
t.Run("set value error", func(t *testing.T) {
136+
p, cfg, err := newTest()
137+
assert.Nil(t, err)
138+
b, _ := json.Marshal(cfg)
139+
assert.Equal(t, string(b), `{"Int":3,"Float":1.2,"Map":{},"Slice":[]}`)
140+
141+
err = p.Set("map.name", "jok")
142+
assert.Nil(t, err)
143+
144+
name, exists := cfg.Map.Value()["name"]
145+
assert.True(t, exists)
146+
assert.Equal(t, name, "jok")
147+
148+
err = p.Set("map.name[", "jok")
149+
assert.Error(t, err, `invalid key \'map\.name\[\'`)
150+
151+
err = p.Remove("map.name")
152+
assert.Nil(t, err)
153+
154+
_, exists = cfg.Map.Value()["name"]
155+
assert.False(t, exists)
156+
})
157+
135158
}

gs/app_args.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"github.com/go-spring-projects/go-spring/conf"
2525
)
2626

27-
// EnvPrefix 属性覆盖的环境变量需要携带该前缀
27+
// EnvPrefix environment variable prefix
2828
const EnvPrefix = "GS_"
2929

3030
// loadCmdArgs 加载以 -D key=value 或者 -D key[=true] 形式传入的命令行参数。

gs/arg/arg_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"reflect"
2121
"testing"
2222

23+
"github.com/go-spring-projects/go-spring/gs/cond"
2324
"github.com/go-spring-projects/go-spring/internal/utils/assert"
2425
"github.com/golang/mock/gomock"
2526
)
@@ -146,4 +147,105 @@ func TestBind(t *testing.T) {
146147
assert.Equal(t, len(values), 0)
147148
})
148149

150+
t.Run("one ctx matches", func(t *testing.T) {
151+
ctrl := gomock.NewController(t)
152+
defer ctrl.Finish()
153+
ctx := NewMockContext(ctrl)
154+
ctx.EXPECT().Matches(gomock.Any()).Return(true, nil)
155+
156+
ok, err := ctx.Matches(cond.OK())
157+
assert.Nil(t, err)
158+
assert.True(t, ok)
159+
})
160+
161+
t.Run("bind args", func(t *testing.T) {
162+
ctrl := gomock.NewController(t)
163+
defer ctrl.Finish()
164+
ctx := NewMockContext(ctrl)
165+
166+
var expectValues = []interface{}{
167+
0, 1, 2, 3, 4, 5, 6, nil,
168+
}
169+
170+
var gotValues []interface{}
171+
fn := func(r0 int, r1 int, r2 int, r3 int, r4 int, r5 int, r6 int, nilPtr *int) {
172+
gotValues = append(gotValues, r0)
173+
gotValues = append(gotValues, r1)
174+
gotValues = append(gotValues, r2)
175+
gotValues = append(gotValues, r3)
176+
gotValues = append(gotValues, r4)
177+
gotValues = append(gotValues, r5)
178+
gotValues = append(gotValues, r6)
179+
gotValues = append(gotValues, nil)
180+
}
181+
182+
c, err := Bind(fn, []Arg{
183+
R0(Value(expectValues[0])),
184+
R1(Value(expectValues[1])),
185+
R2(Value(expectValues[2])),
186+
R3(Value(expectValues[3])),
187+
R4(Value(expectValues[4])),
188+
R5(Value(expectValues[5])),
189+
R6(Value(expectValues[6])),
190+
Index(7, Nil()),
191+
}, 1)
192+
assert.Nil(t, err)
193+
194+
values, err := c.Call(ctx)
195+
if err != nil {
196+
t.Fatal(err)
197+
}
198+
199+
assert.Equal(t, len(values), 0)
200+
assert.Equal(t, gotValues, expectValues)
201+
202+
// mark coverage
203+
NewMockArg(ctrl).EXPECT()
204+
})
205+
206+
t.Run("bind options", func(t *testing.T) {
207+
ctrl := gomock.NewController(t)
208+
defer ctrl.Finish()
209+
ctx := NewMockContext(ctrl)
210+
211+
type options struct {
212+
name string
213+
age int
214+
}
215+
216+
type fnOption func(opts *options)
217+
218+
var gotOpts = &options{}
219+
220+
var optFn = func(option ...fnOption) {
221+
for _, op := range option {
222+
op(gotOpts)
223+
}
224+
}
225+
226+
var withName = func(name string) fnOption {
227+
return func(opts *options) {
228+
opts.name = name
229+
}
230+
}
231+
232+
var withAge = func(age int) fnOption {
233+
return func(opts *options) {
234+
opts.age = age
235+
}
236+
}
237+
238+
c, err := Bind(optFn, []Arg{Option(withName, Value("spring")), Option(withAge, Value(18))}, 1)
239+
assert.Nil(t, err)
240+
241+
values, err := c.Call(ctx)
242+
if err != nil {
243+
t.Fatal(err)
244+
}
245+
246+
assert.Equal(t, len(values), 0)
247+
assert.Equal(t, gotOpts.name, "spring")
248+
assert.Equal(t, gotOpts.age, 18)
249+
250+
})
149251
}

0 commit comments

Comments
 (0)