-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update the example of use test
- Loading branch information
dapeng
committed
May 15, 2024
1 parent
752e451
commit 7291153
Showing
7 changed files
with
918 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,51 @@ | ||
module example | ||
|
||
go 1.21.1 | ||
|
||
require ( | ||
github.com/gone-io/gone v0.3.1 | ||
github.com/stretchr/testify v1.9.0 | ||
) | ||
|
||
require ( | ||
github.com/bytedance/sonic v1.10.2 // indirect | ||
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect | ||
github.com/chenzhuoyu/iasm v0.9.1 // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/gabriel-vasile/mimetype v1.4.3 // indirect | ||
github.com/gin-contrib/sse v0.1.0 // indirect | ||
github.com/gin-gonic/gin v1.9.1 // indirect | ||
github.com/go-playground/locales v0.14.1 // indirect | ||
github.com/go-playground/universal-translator v0.18.1 // indirect | ||
github.com/go-playground/validator/v10 v10.17.0 // indirect | ||
github.com/goccy/go-json v0.10.2 // indirect | ||
github.com/golang/mock v1.6.0 // indirect | ||
github.com/golang/snappy v0.0.4 // indirect | ||
github.com/google/uuid v1.6.0 // indirect | ||
github.com/gopherjs/gopherjs v1.17.2 // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/jtolds/gls v4.20.0+incompatible // indirect | ||
github.com/klauspost/cpuid/v2 v2.2.6 // indirect | ||
github.com/leodido/go-urn v1.3.0 // indirect | ||
github.com/magiconair/properties v1.8.6 // indirect | ||
github.com/mattn/go-isatty v0.0.20 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/pelletier/go-toml/v2 v2.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/sirupsen/logrus v1.9.0 // indirect | ||
github.com/syndtr/goleveldb v1.0.0 // indirect | ||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect | ||
github.com/ugorji/go/codec v1.2.12 // indirect | ||
golang.org/x/arch v0.7.0 // indirect | ||
golang.org/x/crypto v0.22.0 // indirect | ||
golang.org/x/mod v0.17.0 // indirect | ||
golang.org/x/net v0.24.0 // indirect | ||
golang.org/x/sys v0.19.0 // indirect | ||
golang.org/x/text v0.14.0 // indirect | ||
golang.org/x/tools v0.20.0 // indirect | ||
google.golang.org/protobuf v1.33.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978 // indirect | ||
xorm.io/xorm v1.3.2 // indirect | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package test | ||
|
||
import ( | ||
"github.com/gone-io/gone" | ||
"math" | ||
) | ||
|
||
//go:gone | ||
func NewDistanceCalculator() gone.Goner { | ||
return &distanceCalculator{} | ||
} | ||
|
||
type distanceCalculator struct { | ||
gone.Flag | ||
|
||
originPoint IPoint `gone:"*"` | ||
} | ||
|
||
func (d *distanceCalculator) CalculateDistanceFromOrigin(x, y int) float64 { | ||
originX, originY := d.originPoint.GetX(), d.originPoint.GetY() | ||
return math.Sqrt(math.Pow(float64(x-originX), 2) + math.Pow(float64(y-originY), 2)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package test | ||
|
||
import ( | ||
"example/test/mock" | ||
"github.com/golang/mock/gomock" | ||
"github.com/gone-io/gone" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func Test_distanceCalculator_CalculateDistanceFromOrigin(t *testing.T) { | ||
|
||
//创建mock控制器 | ||
controller := gomock.NewController(t) | ||
defer controller.Finish() | ||
|
||
gone.Test(func(d *distanceCalculator) { | ||
distance := d.CalculateDistanceFromOrigin(3, 4) | ||
|
||
assert.Equal(t, float64(5), distance) | ||
|
||
}, func(cemetery gone.Cemetery) error { | ||
|
||
//创建mock对象 | ||
point := mock.NewMockIPoint(controller) | ||
point.EXPECT().GetX().Return(0) | ||
point.EXPECT().GetY().Return(0) | ||
|
||
//将mock对象埋葬到Cemetery | ||
cemetery.Bury(point) | ||
|
||
//被测试的对象也需要埋葬到Cemetery | ||
cemetery.Bury(NewDistanceCalculator()) | ||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package test | ||
|
||
//go:generate sh -c "mockgen -package=mock -source=$GOFILE|gone mock -o mock/$GOFILE" | ||
type IPoint interface { | ||
GetX() int | ||
GetY() int | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package test | ||
|
||
import "github.com/gone-io/gone" | ||
|
||
type originPoint struct { | ||
gone.Flag | ||
} | ||
|
||
//go:gone | ||
func NewOriginPoint() gone.Goner { | ||
return &originPoint{} | ||
} | ||
|
||
func (o *originPoint) GetX() int { | ||
return 100 | ||
} | ||
func (o *originPoint) GetY() int { | ||
return 200 | ||
} |