Skip to content

Commit

Permalink
support go mod v2
Browse files Browse the repository at this point in the history
  • Loading branch information
agiledragon committed Oct 16, 2020
1 parent b80da72 commit e5657b5
Show file tree
Hide file tree
Showing 15 changed files with 446 additions and 449 deletions.
26 changes: 13 additions & 13 deletions dsl/behavior.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
package dsl

import . "github.com/agiledragon/gomonkey"
import . "github.com/agiledragon/gomonkey/v2"

type Behavior interface {
Apply() []Params
Apply() []Params
}

type ReturnBehavior struct {
rets []Params
params Params
rets []Params
params Params
}

func (this *ReturnBehavior) Apply() []Params {
this.rets = append(this.rets, this.params)
return this.rets
this.rets = append(this.rets, this.params)
return this.rets
}

type RepeatBehavior struct {
rets []Params
behavior Behavior
times int
rets []Params
behavior Behavior
times int
}

func (this *RepeatBehavior) Apply() []Params {
for i := 0; i < this.times; i++ {
this.rets = append(this.rets, this.behavior.Apply()[0])
}
return this.rets
for i := 0; i < this.times; i++ {
this.rets = append(this.rets, this.behavior.Apply()[0])
}
return this.rets
}
16 changes: 7 additions & 9 deletions dsl/factory.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
package dsl

import . "github.com/agiledragon/gomonkey"
import . "github.com/agiledragon/gomonkey/v2"

func Any() Constraint {
return &AnyConstraint{}
return &AnyConstraint{}
}

func Eq(x interface{}) Constraint {
return &EqConstraint{x: x}
return &EqConstraint{x: x}
}

func Return(x ...interface{}) Behavior {
r := &ReturnBehavior{rets: make([]Params, 0), params: make(Params, 0)}
r.params = append(r.params, x...)
return r
r := &ReturnBehavior{rets: make([]Params, 0), params: make(Params, 0)}
r.params = append(r.params, x...)
return r
}

func Repeat(behavior Behavior, times int) Behavior {
return &RepeatBehavior{rets: make([]Params, 0), behavior: behavior, times: times}
return &RepeatBehavior{rets: make([]Params, 0), behavior: behavior, times: times}
}


5 changes: 2 additions & 3 deletions dsl/patch_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package dsl

import (
"fmt"
. "github.com/agiledragon/gomonkey"
"reflect"

. "github.com/agiledragon/gomonkey/v2"
)

type FuncPara struct {
Expand Down Expand Up @@ -62,5 +63,3 @@ func (this *PatchBuilder) End() {
})
this.patches.ApplyCore(t, d)
}


2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/agiledragon/gomonkey
module github.com/agiledragon/gomonkey/v2

go 1.14

Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
Expand Down
4 changes: 2 additions & 2 deletions test/apply_func_seq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"runtime"
"testing"

. "github.com/agiledragon/gomonkey"
"github.com/agiledragon/gomonkey/test/fake"
. "github.com/agiledragon/gomonkey/v2"
"github.com/agiledragon/gomonkey/v2/test/fake"
. "github.com/smartystreets/goconvey/convey"
)

Expand Down
118 changes: 59 additions & 59 deletions test/apply_func_test.go
Original file line number Diff line number Diff line change
@@ -1,73 +1,73 @@
package test

import (
. "github.com/agiledragon/gomonkey"
. "github.com/smartystreets/goconvey/convey"
"testing"
"github.com/agiledragon/gomonkey/test/fake"
"encoding/json"
"encoding/json"
"testing"

. "github.com/agiledragon/gomonkey/v2"
"github.com/agiledragon/gomonkey/v2/test/fake"
. "github.com/smartystreets/goconvey/convey"
)

var (
outputExpect = "xxx-vethName100-yyy"
outputExpect = "xxx-vethName100-yyy"
)

func TestApplyFunc(t *testing.T) {
Convey("TestApplyFunc", t, func() {
Convey("TestApplyFunc", t, func() {

Convey("one func for succ", func() {
patches := ApplyFunc(fake.Exec, func(_ string, _ ...string) (string, error) {
return outputExpect, nil
})
defer patches.Reset()
output, err := fake.Exec("", "")
So(err, ShouldEqual, nil)
So(output, ShouldEqual, outputExpect)
})
Convey("one func for succ", func() {
patches := ApplyFunc(fake.Exec, func(_ string, _ ...string) (string, error) {
return outputExpect, nil
})
defer patches.Reset()
output, err := fake.Exec("", "")
So(err, ShouldEqual, nil)
So(output, ShouldEqual, outputExpect)
})

Convey("one func for fail", func() {
patches := ApplyFunc(fake.Exec, func(_ string, _ ...string) (string, error) {
return "", fake.ErrActual
})
defer patches.Reset()
output, err := fake.Exec("", "")
So(err, ShouldEqual, fake.ErrActual)
So(output, ShouldEqual, "")
})
Convey("one func for fail", func() {
patches := ApplyFunc(fake.Exec, func(_ string, _ ...string) (string, error) {
return "", fake.ErrActual
})
defer patches.Reset()
output, err := fake.Exec("", "")
So(err, ShouldEqual, fake.ErrActual)
So(output, ShouldEqual, "")
})

Convey("two funcs", func() {
patches := ApplyFunc(fake.Exec, func(_ string, _ ...string) (string, error) {
return outputExpect, nil
})
defer patches.Reset()
patches.ApplyFunc(fake.Belong, func(_ string, _ []string) bool {
return true
})
output, err := fake.Exec("", "")
So(err, ShouldEqual, nil)
So(output, ShouldEqual, outputExpect)
flag := fake.Belong("", nil)
So(flag, ShouldBeTrue)
})
Convey("two funcs", func() {
patches := ApplyFunc(fake.Exec, func(_ string, _ ...string) (string, error) {
return outputExpect, nil
})
defer patches.Reset()
patches.ApplyFunc(fake.Belong, func(_ string, _ []string) bool {
return true
})
output, err := fake.Exec("", "")
So(err, ShouldEqual, nil)
So(output, ShouldEqual, outputExpect)
flag := fake.Belong("", nil)
So(flag, ShouldBeTrue)
})

Convey("input and output param", func() {
patches := ApplyFunc(json.Unmarshal, func(data []byte, v interface{}) error {
if data == nil {
panic("input param is nil!")
}
p := v.(*map[int]int)
*p = make(map[int]int)
(*p)[1] = 2
(*p)[2] = 4
return nil
})
defer patches.Reset()
var m map[int]int
err := json.Unmarshal([]byte("123"), &m)
So(err, ShouldEqual, nil)
So(m[1], ShouldEqual, 2)
So(m[2], ShouldEqual, 4)
})
})
Convey("input and output param", func() {
patches := ApplyFunc(json.Unmarshal, func(data []byte, v interface{}) error {
if data == nil {
panic("input param is nil!")
}
p := v.(*map[int]int)
*p = make(map[int]int)
(*p)[1] = 2
(*p)[2] = 4
return nil
})
defer patches.Reset()
var m map[int]int
err := json.Unmarshal([]byte("123"), &m)
So(err, ShouldEqual, nil)
So(m[1], ShouldEqual, 2)
So(m[2], ShouldEqual, 4)
})
})
}

123 changes: 61 additions & 62 deletions test/apply_func_var_seq_test.go
Original file line number Diff line number Diff line change
@@ -1,73 +1,72 @@
package test

import (
. "github.com/agiledragon/gomonkey"
. "github.com/smartystreets/goconvey/convey"
"testing"
"github.com/agiledragon/gomonkey/test/fake"
"testing"

. "github.com/agiledragon/gomonkey/v2"
"github.com/agiledragon/gomonkey/v2/test/fake"
. "github.com/smartystreets/goconvey/convey"
)

func TestApplyFuncVarSeq(t *testing.T) {
Convey("TestApplyFuncVarSeq", t, func() {
Convey("TestApplyFuncVarSeq", t, func() {

Convey("default times is 1", func() {
info1 := "hello cpp"
info2 := "hello golang"
info3 := "hello gomonkey"
outputs := []OutputCell{
{Values: Params{[]byte(info1), nil}},
{Values: Params{[]byte(info2), nil}},
{Values: Params{[]byte(info3), nil}},
}
patches := ApplyFuncVarSeq(&fake.Marshal, outputs)
defer patches.Reset()
bytes, err := fake.Marshal("")
So(err, ShouldEqual, nil)
So(string(bytes), ShouldEqual, info1)
bytes, err = fake.Marshal("")
So(err, ShouldEqual, nil)
So(string(bytes), ShouldEqual, info2)
bytes, err = fake.Marshal("")
So(err, ShouldEqual, nil)
So(string(bytes), ShouldEqual, info3)
})
Convey("default times is 1", func() {
info1 := "hello cpp"
info2 := "hello golang"
info3 := "hello gomonkey"
outputs := []OutputCell{
{Values: Params{[]byte(info1), nil}},
{Values: Params{[]byte(info2), nil}},
{Values: Params{[]byte(info3), nil}},
}
patches := ApplyFuncVarSeq(&fake.Marshal, outputs)
defer patches.Reset()
bytes, err := fake.Marshal("")
So(err, ShouldEqual, nil)
So(string(bytes), ShouldEqual, info1)
bytes, err = fake.Marshal("")
So(err, ShouldEqual, nil)
So(string(bytes), ShouldEqual, info2)
bytes, err = fake.Marshal("")
So(err, ShouldEqual, nil)
So(string(bytes), ShouldEqual, info3)
})

Convey("retry succ util the third times", func() {
info1 := "hello cpp"
outputs := []OutputCell{
{Values: Params{[]byte(""), fake.ErrActual}, Times: 2},
{Values: Params{[]byte(info1), nil}},
}
patches := ApplyFuncVarSeq(&fake.Marshal, outputs)
defer patches.Reset()
bytes, err := fake.Marshal("")
So(err, ShouldEqual, fake.ErrActual)
bytes, err = fake.Marshal("")
So(err, ShouldEqual, fake.ErrActual)
bytes, err = fake.Marshal("")
So(err, ShouldEqual, nil)
So(string(bytes), ShouldEqual, info1)
})
Convey("retry succ util the third times", func() {
info1 := "hello cpp"
outputs := []OutputCell{
{Values: Params{[]byte(""), fake.ErrActual}, Times: 2},
{Values: Params{[]byte(info1), nil}},
}
patches := ApplyFuncVarSeq(&fake.Marshal, outputs)
defer patches.Reset()
bytes, err := fake.Marshal("")
So(err, ShouldEqual, fake.ErrActual)
bytes, err = fake.Marshal("")
So(err, ShouldEqual, fake.ErrActual)
bytes, err = fake.Marshal("")
So(err, ShouldEqual, nil)
So(string(bytes), ShouldEqual, info1)
})

Convey("batch operations failed on the third time", func() {
info1 := "hello gomonkey"
outputs := []OutputCell{
{Values: Params{[]byte(info1), nil}, Times: 2},
{Values: Params{[]byte(""), fake.ErrActual}},
}
patches := ApplyFuncVarSeq(&fake.Marshal, outputs)
defer patches.Reset()
bytes, err := fake.Marshal("")
So(err, ShouldEqual, nil)
So(string(bytes), ShouldEqual, info1)
bytes, err = fake.Marshal("")
So(err, ShouldEqual, nil)
So(string(bytes), ShouldEqual, info1)
bytes, err = fake.Marshal("")
So(err, ShouldEqual, fake.ErrActual)
})
Convey("batch operations failed on the third time", func() {
info1 := "hello gomonkey"
outputs := []OutputCell{
{Values: Params{[]byte(info1), nil}, Times: 2},
{Values: Params{[]byte(""), fake.ErrActual}},
}
patches := ApplyFuncVarSeq(&fake.Marshal, outputs)
defer patches.Reset()
bytes, err := fake.Marshal("")
So(err, ShouldEqual, nil)
So(string(bytes), ShouldEqual, info1)
bytes, err = fake.Marshal("")
So(err, ShouldEqual, nil)
So(string(bytes), ShouldEqual, info1)
bytes, err = fake.Marshal("")
So(err, ShouldEqual, fake.ErrActual)
})

})
})
}


Loading

0 comments on commit e5657b5

Please sign in to comment.