-
Notifications
You must be signed in to change notification settings - Fork 0
/
monkey_test.go
61 lines (47 loc) · 1.01 KB
/
monkey_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package monkey
import (
"testing"
)
func fnFoo() string {
return "foo"
}
var varFoo = "foo"
type foo struct{}
func (f *foo) PublicFoo(x string) string {
return "foo " + x
}
func (f *foo) privateFoo(x string) string {
return "foo " + x
}
func TestFunc(t *testing.T) {
p := Func(nil, fnFoo, func() string { return "bar" })
defer p.Reset()
if fnFoo() != "bar" {
t.Error("patch failed")
}
}
func TestVar(t *testing.T) {
p := Var(nil, &varFoo, "bar")
defer p.Reset()
if varFoo != "bar" {
t.Error("patch failed")
}
}
func TestMethod(t *testing.T) {
t.Run("public method", func(t *testing.T) {
f := new(foo)
p := Method(nil, f, f.PublicFoo, func(x string) string { return "bar " + x })
defer p.Reset()
if f.PublicFoo("x") != "bar x" {
t.Error("patch failed")
}
})
t.Run("private method", func(t *testing.T) {
f := new(foo)
p := Method(nil, f, f.privateFoo, func(x string) string { return "bar " + x })
defer p.Reset()
if f.privateFoo("x") != "bar x" {
t.Error("patch failed")
}
})
}