Skip to content

Commit d4ad3ee

Browse files
lvan100lianghuan
authored andcommitted
refactor(gs): using AddTester
1 parent 02f2c69 commit d4ad3ee

File tree

15 files changed

+145
-325
lines changed

15 files changed

+145
-325
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/go-spring/barky v1.0.3
88
github.com/go-spring/gs-assert v1.0.2
99
github.com/go-spring/gs-mock v0.0.4
10-
github.com/go-spring/log v0.0.5
10+
github.com/go-spring/log v0.0.6
1111
github.com/magiconair/properties v1.8.10
1212
github.com/pelletier/go-toml v1.9.5
1313
github.com/spf13/cast v1.9.2

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ github.com/go-spring/gs-assert v1.0.2 h1:9vDppl7ZwMvQE4c83ac7GzN0VxZC5BrQue7dND7
88
github.com/go-spring/gs-assert v1.0.2/go.mod h1:FfibkqWz4HUBpbig1cKMlzW8Ha7RywTB93f1Q/NuF9I=
99
github.com/go-spring/gs-mock v0.0.4 h1:f34YN+ntXflfn13aLa3ZVCB78IG7wWZGK4y5tB+OGpI=
1010
github.com/go-spring/gs-mock v0.0.4/go.mod h1:QK0PqZ+Vu9F+BU97zl8fip5XKibvDSoN+ofky413Z6Q=
11-
github.com/go-spring/log v0.0.5 h1:a8yiGmZTS7MPYvYvePXtc0hIdaQ76pLdsXt8iJwgQBQ=
12-
github.com/go-spring/log v0.0.5/go.mod h1:9SWgPEVWSGgloRTGR7niBliqfwC5UCjPUOl2jyJOimM=
11+
github.com/go-spring/log v0.0.6 h1:R+0mKWCNzaEIZtqdfCc4e/Ha4FBhr01If5oJXjEwRO0=
12+
github.com/go-spring/log v0.0.6/go.mod h1:WrLbwbjmU8Vk5ampzBXjG4rTv+BoxmmTXxX82L2hugg=
1313
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
1414
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
1515
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=

gs/app.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,22 @@ func (s *AppStarter) RunWith(fn func(ctx context.Context) error) {
5454
if err = s.initApp(); err != nil {
5555
return
5656
}
57-
if err = gs_app.GS.RunWith(fn); err != nil {
57+
if err = app.RunWith(fn); err != nil {
5858
return
5959
}
60-
if logRefreshed {
61-
log.Destroy()
62-
}
60+
log.Destroy()
6361
}
6462

6563
// RunAsync runs the app asynchronously and returns a function to stop the app.
6664
func (s *AppStarter) RunAsync() (func(), error) {
6765
if err := s.initApp(); err != nil {
6866
return nil, err
6967
}
70-
if err := gs_app.GS.Start(); err != nil {
68+
if err := app.Start(); err != nil {
7169
return nil, err
7270
}
7371
return func() {
74-
gs_app.GS.Stop()
75-
if logRefreshed {
76-
log.Destroy()
77-
}
72+
app.Stop()
73+
log.Destroy()
7874
}, nil
7975
}

gs/gs.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ func OnEnableServers() ConditionOnProperty {
164164
/************************************ ioc ************************************/
165165

166166
type (
167-
BeanID = gs.BeanID
168-
BeanMock = gs.BeanMock
167+
BeanID = gs.BeanID
169168
)
170169

171170
type (
@@ -212,6 +211,7 @@ type (
212211
)
213212

214213
var B = gs_app.NewBoot()
214+
var app = gs_app.NewApp()
215215

216216
// funcRunner is a function type that implements the Runner interface.
217217
type funcRunner func() error
@@ -260,74 +260,77 @@ func RunAsync() (func(), error) {
260260

261261
// Exiting returns a boolean indicating whether the application is exiting.
262262
func Exiting() bool {
263-
return gs_app.GS.Exiting()
263+
return app.Exiting()
264264
}
265265

266266
// ShutDown shuts down the app with an optional message.
267267
func ShutDown() {
268-
gs_app.GS.ShutDown()
268+
app.ShutDown()
269269
}
270270

271271
// Config returns the app configuration.
272272
func Config() *gs_conf.AppConfig {
273-
return gs_app.GS.P
273+
return app.P
274274
}
275275

276276
// Component registers a bean definition for a given object.
277277
func Component[T any](i T) T {
278278
b := gs_bean.NewBean(reflect.ValueOf(i))
279-
gs_app.GS.C.Register(b).Caller(1)
279+
app.C.Register(b).Caller(1)
280280
return i
281281
}
282282

283283
// RootBean registers a root bean definition.
284284
func RootBean(b *RegisteredBean) {
285-
gs_app.GS.C.RootBean(b)
285+
app.C.RootBean(b)
286286
}
287287

288288
// Object registers a bean definition for a given object.
289289
func Object(i any) *RegisteredBean {
290290
b := gs_bean.NewBean(reflect.ValueOf(i))
291-
return gs_app.GS.C.Register(b).Caller(1)
291+
return app.C.Register(b).Caller(1)
292292
}
293293

294294
// Provide registers a bean definition for a given constructor.
295295
func Provide(ctor any, args ...Arg) *RegisteredBean {
296296
b := gs_bean.NewBean(ctor, args...)
297-
return gs_app.GS.C.Register(b).Caller(1)
297+
return app.C.Register(b).Caller(1)
298298
}
299299

300300
// Register registers a bean definition.
301301
func Register(b *BeanDefinition) *RegisteredBean {
302-
return gs_app.GS.C.Register(b)
302+
return app.C.Register(b)
303303
}
304304

305305
// Module registers a module.
306306
func Module(conditions []ConditionOnProperty, fn func(p conf.Properties) error) {
307-
gs_app.GS.C.Module(conditions, fn)
307+
app.C.Module(conditions, fn)
308308
}
309309

310310
// Group registers a module for a group of beans.
311-
func Group[T any, R any](key string, fn func(c T) (R, error)) {
312-
gs_app.GS.C.Module([]ConditionOnProperty{
311+
func Group[T any, R any](key string, fn func(c T) (R, error), d func(R) error) {
312+
app.C.Module([]ConditionOnProperty{
313313
OnProperty(key),
314314
}, func(p conf.Properties) error {
315315
var m map[string]T
316316
if err := p.Bind(&m, "${"+key+"}"); err != nil {
317317
return err
318318
}
319319
for name, c := range m {
320-
Provide(fn, ValueArg(c)).Name(name)
320+
b := Provide(fn, ValueArg(c)).Name(name)
321+
if d != nil {
322+
b.Destroy(d)
323+
}
321324
}
322325
return nil
323326
})
324327
}
325328

326329
// RefreshProperties refreshes the app configuration.
327330
func RefreshProperties() error {
328-
p, err := gs_app.GS.P.Refresh()
331+
p, err := app.P.Refresh()
329332
if err != nil {
330333
return err
331334
}
332-
return gs_app.GS.C.RefreshProperties(p)
335+
return app.C.RefreshProperties(p)
333336
}

gs/gstest/testdata/biz/biz.go renamed to gs/gs_test.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,33 @@
1414
* limitations under the License.
1515
*/
1616

17-
package biz
17+
package gs_test
1818

1919
import (
2020
"fmt"
21+
"testing"
2122

2223
"github.com/go-spring/spring-core/gs"
23-
"github.com/go-spring/spring-core/gs/gstest/testdata/dao"
2424
)
2525

26-
func init() {
27-
gs.Object(&Service{})
26+
func TestMain(m *testing.M) {
27+
gs.AddTester(&Tester{})
28+
gs.Object(&Dep{})
29+
gs.TestMain(m)
2830
}
2931

30-
type Service struct {
31-
Dao *dao.Dao `autowire:""`
32+
func TestAA(t *testing.T) {
33+
34+
}
35+
36+
type Dep struct {
37+
Name string `value:"${name:=TestAA}"`
38+
}
39+
40+
type Tester struct {
41+
Dep *Dep `autowire:""`
3242
}
3343

34-
func (s *Service) Hello(name string) string {
35-
return fmt.Sprintf("hello %s", name)
44+
func (o *Tester) TestAA(t *testing.T) {
45+
fmt.Println(o.Dep.Name)
3646
}

gs/gstest/gstest.go

Lines changed: 0 additions & 149 deletions
This file was deleted.

0 commit comments

Comments
 (0)