-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
107 lines (91 loc) · 2.15 KB
/
example_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package plug_test
import (
"fmt"
"math/rand/v2"
"net/http"
"os"
"os/user"
"testing"
"time"
"github.com/lufia/plug"
)
func Example_timeNow() {
scope := plug.CurrentScope()
defer scope.Delete()
now := time.Date(2024, time.April, 1, 10, 12, 50, 0, time.UTC)
key := plug.Func("time.Now", time.Now)
plug.Set(scope, key, func() time.Time {
return now
})
fmt.Println(time.Now().Format(time.RFC3339))
// Output: 2024-04-01T10:12:50Z
}
func Example_osUserCurrent() {
scope := plug.CurrentScope()
defer scope.Delete()
key := plug.Func("os/user.Current", user.Current)
plug.Set(scope, key, func() (*user.User, error) {
return &user.User{Uid: "100", Username: "user"}, nil
})
u, _ := user.Current()
fmt.Println(u.Username, u.Uid)
// Output: user 100
}
func Example_netHttpClientDo() {
scope := plug.CurrentScope()
defer scope.Delete()
key := plug.Func("net/http.Client.Do", (*http.Client)(nil).Do)
plug.Set(scope, key, func(req *http.Request) (*http.Response, error) {
return &http.Response{StatusCode: 200}, nil
})
resp, _ := http.Get("https://example.com")
fmt.Println(resp.StatusCode)
// Output: 200
}
func TestOsGetpid(t *testing.T) {
scope := plug.CurrentScopeFor(t)
key := plug.Func("os.Getpid", os.Getpid)
plug.Set(scope, key, func() int {
return 1
})
if w, pid := 1, os.Getpid(); pid != w {
t.Errorf("Getpid() = %d; want %d", pid, w)
}
}
func Example_osGetpid() {
scope := plug.CurrentScope()
defer scope.Delete()
key := plug.Func("os.Getpid", os.Getpid)
plug.Set(scope, key, func() int {
return 1
})
fmt.Println(os.Getpid())
// Output: 1
}
func Example_mathRandV2N() {
scope := plug.CurrentScope()
defer scope.Delete()
key := plug.Func("math/rand/v2.N", rand.N[int])
plug.Set(scope, key, func(n int) int {
return 3
})
fmt.Println(rand.N[int](10))
// Output: 3
}
func Example_recordGetenv() {
scope := plug.CurrentScope()
defer scope.Delete()
key := plug.Func("os.Getenv", os.Getenv)
var r plug.FuncRecorder[struct {
Key string
}]
plug.Set(scope, key, func(_ string) string {
return "dummy"
}).SetRecorder(&r)
_ = os.Getenv("PATH")
fmt.Println(r.Count())
fmt.Println(r.At(0).Key)
// Output:
// 1
// PATH
}