-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b80da72
commit e5657b5
Showing
15 changed files
with
446 additions
and
449 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,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 | ||
} |
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,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} | ||
} | ||
|
||
|
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
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,4 +1,4 @@ | ||
module github.com/agiledragon/gomonkey | ||
module github.com/agiledragon/gomonkey/v2 | ||
|
||
go 1.14 | ||
|
||
|
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
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
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,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) | ||
}) | ||
}) | ||
} | ||
|
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,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) | ||
}) | ||
|
||
}) | ||
}) | ||
} | ||
|
||
|
Oops, something went wrong.