Skip to content

Commit

Permalink
feat: update test function
Browse files Browse the repository at this point in the history
  • Loading branch information
dapeng committed May 30, 2024
1 parent 0e1f492 commit 701ca46
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 121 deletions.
10 changes: 0 additions & 10 deletions cemetery.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,16 +321,6 @@ func (c *cemetery) reviveSpecialTypeFields(field reflect.StructField, v reflect.
return
}

func (c *cemetery) reviveDependence(tomb Tomb) (deps []Tomb, err error) {
deps, err = c.reviveOneAndItsDeps(tomb)
if err != nil {
return
}

err = c.prophesy(append(deps, tomb)...)
return
}

func (c *cemetery) reviveOneAndItsDeps(tomb Tomb) (deps []Tomb, err error) {
deps, err = c.reviveOneFromTomb(tomb)
if err != nil {
Expand Down
7 changes: 0 additions & 7 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,8 @@ type DefaultLogger interface {

// Cemetery which is for burying and reviving Goner
type Cemetery interface {
//DefaultLogger

Goner

//bury(goner Goner, ids ...GonerId) Tomb

//Bury a Goner to the Cemetery
Bury(Goner, ...GonerOption) Cemetery

Expand All @@ -74,9 +70,6 @@ type Cemetery interface {
//ReviveAllFromTombs Revive all Goner from the Cemetery
ReviveAllFromTombs() error

//reviveOneFromTomb(tomb Tomb) (deps []Tomb, err error)
reviveDependence(tomb Tomb) (deps []Tomb, err error)

//GetTomById return the Tomb by the GonerId
GetTomById(GonerId) Tomb

Expand Down
98 changes: 2 additions & 96 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,102 +4,7 @@ import (
"reflect"
)

type TestHeaven[T Goner] interface {
Heaven
WithId(id GonerId) TestHeaven[T]
WithPriest(priests ...Priest) TestHeaven[T]
Run()
}

type testHeaven[T Goner] struct {
*heaven
testFn func(T)
testGonerId GonerId
}

func (h *testHeaven[T]) WithId(id GonerId) TestHeaven[T] {
h.testGonerId = id
return h
}

func (h *testHeaven[T]) WithPriest(priests ...Priest) TestHeaven[T] {
h.heaven = New(priests...).(*heaven)
return h
}

func (h *testHeaven[T]) installAngelHook(deps []Tomb) {
angleTombs := Tombs(deps).GetTomByType(getAngelType())
for _, tomb := range angleTombs {
angel := tomb.GetGoner().(Angel)
h.BeforeStart(angel.Start)
h.BeforeStop(angel.Stop)
}
}

func (h *testHeaven[T]) run(tomb Tomb, fn func(T)) {
deps, err := h.cemetery.reviveDependence(tomb)
if err != nil {
panic(err)
}
deps = append(deps, tomb)
h.installAngelHook(deps)
h.startFlow()
fn(tomb.GetGoner().(T))
h.stopFlow()
}

func (h *testHeaven[T]) getTestGonerType() reflect.Type {
t := new(T)
return reflect.TypeOf(t).Elem()
}

func (h *testHeaven[T]) Run() {
//将自己安葬了,便于其他组件引用 和 感知自己在TestKit
h.cemetery.Bury(h, IdGoneTestKit)

h.burial()

paramType := h.getTestGonerType()
var tomb Tomb = nil
if h.testGonerId != "" {
tomb = h.cemetery.GetTomById(h.testGonerId)
if tomb == nil {
panic(CannotFoundGonerByIdError(h.testGonerId))
}
if tomb != nil && !isCompatible(paramType, tomb.GetGoner()) {
panic(NotCompatibleError(paramType, reflect.TypeOf(tomb.GetGoner()).Elem()))
}
} else {
list := h.cemetery.GetTomByType(paramType)
if len(list) > 0 {
if len(list) > 1 {
h.Warnf("more than one Goner found by type")
}
tomb = list[0]
}

if tomb == nil {
panic(CannotFoundGonerByTypeError(paramType))
}
}
h.run(tomb, h.testFn)
return
}

func TestKit[T Goner](fn func(T)) TestHeaven[T] {
return &testHeaven[T]{testFn: fn}
}

//// Test 用于编写测试用例,参考[示例](https://github.com/gone-io/gone/blob/main/example/test/goner_test.go)
//func Test[T Goner](fn func(T), priests ...Priest) {
// TestKit(fn).WithPriest(priests...).Run()
//}
//
//// TestAt 用于编写测试用例,测试某个特定ID的Goner
//func TestAt[T Goner](id GonerId, fn func(T), priests ...Priest) {
// TestKit(fn).WithId(id).WithPriest(priests...).Run()
//}

// Test Use for writing test cases, refer to [example](https://github.com/gone-io/gone/blob/main/example/test/goner_test.go)
func Test[T Goner](fn func(goner T), priests ...Priest) {
Prepare(priests...).Run(func(in struct {
cemetery Cemetery `gone:"*"`
Expand All @@ -114,6 +19,7 @@ func Test[T Goner](fn func(goner T), priests ...Priest) {
})
}

// TestAt Use for writing test cases, test a specific ID of Goner
func TestAt[T Goner](id GonerId, fn func(goner T), priests ...Priest) {
Prepare(priests...).Run(func(in struct {
cemetery Cemetery `gone:"*"`
Expand Down
8 changes: 0 additions & 8 deletions test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,6 @@ func Test_TestAt(t *testing.T) {
})
}

func Test_testHeaven_WithId(t *testing.T) {
test := &testHeaven[*Point]{}
result := test.WithId("point-a")
assert.Equal(t, test, result)

assert.Equal(t, "point-a", string(test.testGonerId))
}

type angel struct {
Flag
x int
Expand Down

0 comments on commit 701ca46

Please sign in to comment.