add wireexample;

develop
Ge Song 2 years ago
parent d98d6dffef
commit 521fee9817

@ -0,0 +1,29 @@
// 展示 wire 的两种 mock 实现
package main
import "fmt"
func main() {
approachA()
approachB()
}
// approach A:
// - 需要 mock 的对象定义成接口, 真正的实现和 mock 分别实现接口;
// - injector 函数使用 接口类型参数, 测试时传入 mock 对象;
func approachA() {
rander := &MockRander{}
guess := InjectGuessWithoutMock(rander)
rander.Value = 1
fmt.Println("approachA:", guess.Guess(10))
}
// approach B:
// - mock 和 真正的实现分别定义到两组 injector 中, injector 提供1个对象包含要测试的对象和 mock;
// - 测试时调用 mock injector 返回对象(包含要测试的对象和 mock);
func approachB() {
guessWithMock := InjectMockGuess()
guessWithMock.Mock.Value = 1
fmt.Println("approachB:", guessWithMock.Guess.Guess(10))
}

@ -0,0 +1,64 @@
package main
import (
"fmt"
"math/rand"
"github.com/google/wire"
)
type Rander interface {
Rand() int
}
type DefaultRander struct{}
func (r *DefaultRander) Rand() int {
return rand.Int()
}
func NewRander() *DefaultRander {
return &DefaultRander{}
}
type MockRander struct {
Value int
}
func (r *MockRander) Rand() int {
return r.Value
}
type Guess struct {
rander Rander
}
func (g *Guess) Guess(guess int) string {
return fmt.Sprintf("you guess %d, real number is %d", guess, g.rander.Rand())
}
func NewGuess(rander Rander) *Guess {
return &Guess{
rander: rander,
}
}
type GuessWithMock struct {
Guess *Guess
Mock *MockRander
}
var GuessProviderSetWithoutMock = wire.NewSet(NewGuess)
var GuessProviderSet = wire.NewSet(
NewRander,
NewGuess,
wire.Bind(new(Rander), new(*DefaultRander)),
)
var MockGuessProviderSet = wire.NewSet(
wire.Struct(new(MockRander)),
NewGuess,
wire.Bind(new(Rander), new(*MockRander)),
wire.Struct(new(GuessWithMock), "*"),
)

@ -0,0 +1,20 @@
//go:build wireinject
package main
import "github.com/google/wire"
// Approach A
func InjectGuessWithoutMock(rander Rander) *Guess {
panic(wire.Build(GuessProviderSetWithoutMock))
}
// Approach B
func InjectRealGuess() *Guess {
panic(wire.Build(GuessProviderSet))
}
// Approach B
func InjectMockGuess() *GuessWithMock {
panic(wire.Build(MockGuessProviderSet))
}

@ -0,0 +1,33 @@
// Code generated by Wire. DO NOT EDIT.
//go:generate go run github.com/google/wire/cmd/wire
//go:build !wireinject
// +build !wireinject
package main
// Injectors from wire.go:
// Approach A
func InjectGuessWithoutMock(rander Rander) *Guess {
guess := NewGuess(rander)
return guess
}
// Approach B
func InjectRealGuess() *Guess {
defaultRander := NewRander()
guess := NewGuess(defaultRander)
return guess
}
// Approach B
func InjectMockGuess() *GuessWithMock {
mockRander := &MockRander{}
guess := NewGuess(mockRander)
guessWithMock := &GuessWithMock{
Guess: guess,
Mock: mockRander,
}
return guessWithMock
}

@ -0,0 +1,5 @@
module git.blauwelle.com/go/crate/wireexample
go 1.20
require github.com/google/wire v0.5.0

@ -0,0 +1,10 @@
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/subcommands v1.0.1/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/google/wire v0.5.0 h1:I7ELFeVBr3yfPIcc8+MWvrjk+3VjbcSzoXm3JVa+jD8=
github.com/google/wire v0.5.0/go.mod h1:ngWDr9Qvq3yZA10YrxfyGELY/AFWGVpy9c1LTRi1EoU=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190422233926-fe54fb35175b/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
Loading…
Cancel
Save