Skip to content

Commit

Permalink
feat: merge code
Browse files Browse the repository at this point in the history
  • Loading branch information
dapeng committed Jun 14, 2024
1 parent f044933 commit 4bf21b4
Show file tree
Hide file tree
Showing 6 changed files with 357 additions and 374 deletions.
3 changes: 0 additions & 3 deletions config/default.properties

This file was deleted.

119 changes: 119 additions & 0 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,59 @@ import (
"time"
)

// GonerIds for Gone framework inner Goners
const (
// IdGoneHeaven , The GonerId of Heaven Goner, which represents the program itself, and which is injected by default when it starts.
IdGoneHeaven GonerId = "gone-heaven"

// IdGoneCemetery , The GonerId of Cemetery Goner, which is Dependence Injection Key Goner, and which is injected by default.
IdGoneCemetery GonerId = "gone-cemetery"

// IdGoneTestKit , The GonerId of TestKit Goner, which is injected by default when using gone.Test or gone.TestAt to run test code.
IdGoneTestKit GonerId = "gone-test-kit"

//IdConfig , The GonerId of Config Goner, which can be used for Injecting Configs from files or envs.
IdConfig GonerId = "config"

//IdGoneConfigure , The GonerId of Configure Goner, which is used to read configs from devices.
IdGoneConfigure GonerId = "gone-configure"

// IdGoneTracer ,The GonerId of Tracer
IdGoneTracer GonerId = "gone-tracer"

// IdGoneLogger , The GonerId of Logger
IdGoneLogger GonerId = "gone-logger"

// IdGoneCMux , The GonerId of CMuxServer
IdGoneCMux GonerId = "gone-cmux"

// IdGoneGin , IdGoneGinRouter , IdGoneGinProcessor, IdGoneGinProxy, IdGoneGinResponser, IdHttpInjector;
// The GonerIds of Goners in goner/gin, which integrates gin framework for web request.
IdGoneGin GonerId = "gone-gin"
IdGoneGinRouter GonerId = "gone-gin-router"
IdGoneGinProcessor GonerId = "gone-gin-processor"
IdGoneGinProxy GonerId = "gone-gin-proxy"
IdGoneGinResponser GonerId = "gone-gin-responser"
IdHttpInjector GonerId = "http"

// IdGoneXorm , The GonerId of XormEngine Goner, which is for xorm engine.
IdGoneXorm GonerId = "gone-xorm"

// IdGoneRedisPool ,IdGoneRedisCache, IdGoneRedisKey, IdGoneRedisLocker, IdGoneRedisProvider
// The GonerIds of Goners in goner/redis, which integrates redis framework for cache and locker.
IdGoneRedisPool GonerId = "gone-redis-pool"
IdGoneRedisCache GonerId = "gone-redis-cache"
IdGoneRedisKey GonerId = "gone-redis-key"
IdGoneRedisLocker GonerId = "gone-redis-locker"
IdGoneRedisProvider GonerId = "gone-redis-provider"

// IdGoneSchedule , The GonerId of Schedule Goner, which is for schedule in goner/schedule.
IdGoneSchedule GonerId = "gone-schedule"

// IdGoneReq , The GonerId of urllib.Client Goner, which is for request in goner/urllib.
IdGoneReq GonerId = "gone-urllib"
)

// PanicTrace used for getting panic stack
func PanicTrace(kb int, skip int) []byte {
stack := make([]byte, kb<<10) //4KB
Expand Down Expand Up @@ -122,3 +175,69 @@ func TimeStat(name string, start time.Time, logs ...func(format string, args ...
mapRecord[name].UseTime/time.Duration(mapRecord[name].Count),
)
}

func testRun(fn any, priests ...Priest) {
Prepare(priests...).testKit().Run(fn)
}

// 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) {
testRun(func(in struct {
cemetery Cemetery `gone:"*"`
}) {
ft := reflect.TypeOf(fn)
t := ft.In(0).Elem()
theTombs := in.cemetery.GetTomByType(t)
if len(theTombs) == 0 {
panic(CannotFoundGonerByTypeError(t))
}
fn(theTombs[0].GetGoner().(T))
}, priests...)
}

// TestAt Use for writing test cases, test a specific ID of Goner
func TestAt[T Goner](id GonerId, fn func(goner T), priests ...Priest) {
testRun(func(in struct {
cemetery Cemetery `gone:"*"`
}) {
theTomb := in.cemetery.GetTomById(id)
if theTomb == nil {
panic(CannotFoundGonerByIdError(id))
}
g, ok := theTomb.GetGoner().(T)
if !ok {
panic(NotCompatibleError(reflect.TypeOf(g).Elem(), reflect.TypeOf(theTomb.GetGoner()).Elem()))
}
fn(g)
}, priests...)
}

func NewBuryMockCemeteryForTest() Cemetery {
return newCemetery()
}

func (p *Preparer) testKit() *Preparer {
type Kit struct {
Flag
}
p.heaven.(*heaven).cemetery.Bury(&Kit{}, IdGoneTestKit)
return p
}

/*
Test Use for writing test cases
example:
```go
gone.Prepare(priests...).Test(func(in struct{
cemetery Cemetery `gone:"*"`
}) {
// test code
})
```
*/
func (p *Preparer) Test(fn any) {
p.testKit().AfterStart(fn).Run()
}
238 changes: 238 additions & 0 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,241 @@ func TestBlank(t *testing.T) {
order := Order(1)
order.option()
}

type errProphet struct {
Flag
}

func (e *errProphet) AfterRevive() error {
return errors.New("AfterReviveError")
}

func Test_Test(t *testing.T) {
t.Run("suc", func(t *testing.T) {
type Line struct {
Flag
A XPoint `gone:"point-a"`
b XPoint `gone:"point-b"`
}

var a = &Point{x: 1}
var b = &Point{x: 2}

var executed = false
Test(func(l *Line) {
assert.Equal(t, a, l.A)
assert.Equal(t, b, l.b)

executed = true
}, func(cemetery Cemetery) error {
cemetery.Bury(a, GonerId("point-a"))
cemetery.Bury(b, GonerId("point-b"))
cemetery.Bury(&Line{})
return nil
})
assert.True(t, executed)
})

t.Run("failed: CannotFoundGonerById", func(t *testing.T) {
var executed = false
func() {
defer func() {
a := recover()
assert.Equal(t, CannotFoundGonerById, a.(Error).Code())
executed = true
}()
TestAt("point-a", func(p *Point) {

}, func(cemetery Cemetery) error {
return nil
})
}()

assert.True(t, executed)
})

t.Run("failed: CannotFoundGonerByType", func(t *testing.T) {
var executed = false
func() {
defer func() {
a := recover()
assert.Equal(t, CannotFoundGonerByType, a.(Error).Code())
executed = true
}()
Test(func(p *Point) {

}, func(cemetery Cemetery) error {
return nil
})
}()

assert.True(t, executed)
})

t.Run("failed: AfterRevive err", func(t *testing.T) {
var executed = false

func() {
defer func() {
a := recover()
assert.Equal(t, "AfterReviveError", a.(error).Error())
executed = true
}()
Test(func(p *errProphet) {

}, func(cemetery Cemetery) error {
cemetery.Bury(&errProphet{})
return nil
})
}()

assert.True(t, executed)
})
}

func Test_TestAt(t *testing.T) {
t.Run("suc", func(t *testing.T) {
var executed = false
type Line struct {
Flag
A XPoint `gone:"point-a"`
b XPoint `gone:"point-b"`
}

var a = &Point{x: 1}
var b = &Point{x: 2}

TestAt("point-a", func(p *Point) {
assert.Equal(t, p, a)

executed = true
}, func(cemetery Cemetery) error {
cemetery.Bury(a, GonerId("point-a"))
cemetery.Bury(b, GonerId("point-b"))
cemetery.Bury(&Line{})
return nil
})
assert.True(t, executed)
})

t.Run("suc: more than one Goner found by type", func(t *testing.T) {
var executed = false
a := &Point{}
b := &Point{}

Test(func(p *Point) {
executed = true
assert.Equal(t, a, p)
}, func(cemetery Cemetery) error {
cemetery.Bury(a)
cemetery.Bury(b)
return nil
})

assert.True(t, executed)
})

t.Run("failed: NotCompatible", func(t *testing.T) {
var executed = false
func() {
defer func() {
a := recover()
assert.Equal(t, NotCompatible, a.(Error).Code())
executed = true
}()

type Line struct {
Flag
}
TestAt("point-a", func(p *Point) {

}, func(cemetery Cemetery) error {
cemetery.Bury(&Line{}, GonerId("point-a"))
return nil
})
}()

assert.True(t, executed)
})

t.Run("failed: NotCompatible", func(t *testing.T) {
var executed = false
func() {
defer func() {
a := recover()
assert.Equal(t, NotCompatible, a.(Error).Code())
executed = true
}()

type Line struct {
Flag
}
TestAt("point-a", func(p *Point) {

}, func(cemetery Cemetery) error {

cemetery.Bury(&Line{}, GonerId("point-a"))
return nil
})
}()

assert.True(t, executed)
})
}

type angel struct {
Flag
x int
}

func (i *angel) Start(Cemetery) error {
i.x = 100
return nil
}

func (i *angel) Stop(Cemetery) error {
return nil
}

func (i *angel) X() int {
return i.x
}

func Test_testHeaven_installAngelHook(t *testing.T) {
type UseAngel struct {
Flag
angel *angel `gone:"*"`
}
var executed = false
Test(func(u *UseAngel) {
assert.Equal(t, 100, u.angel.X())
executed = true
}, func(cemetery Cemetery) error {
cemetery.Bury(&angel{})
cemetery.Bury(&UseAngel{})
return nil
})
assert.True(t, executed)
}

func TestPreparer_Test(t *testing.T) {
Prepare().Test(func(in struct {
cemetery Cemetery `gone:"gone-cemetery"`
}) {
assert.NotNil(t, in.cemetery)
})
}

func TestBuryMockCemetery_Bury(t *testing.T) {
cemetery := NewBuryMockCemeteryForTest()
point, id := &Point{}, "point-x"
cemetery.Bury(point, GonerId(id))

cemetery.Bury(&Point{x: 100})

tomb := cemetery.GetTomById(GonerId(id))
assert.Equal(t, point, tomb.GetGoner())

tombs := cemetery.GetTomByType(reflect.TypeOf(*point))
assert.Equal(t, 2, len(tombs))
}
Loading

0 comments on commit 4bf21b4

Please sign in to comment.