-
Notifications
You must be signed in to change notification settings - Fork 19
/
patch_const_test.go
311 lines (271 loc) · 6.56 KB
/
patch_const_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//go:build go1.20
// +build go1.20
package patch_const
import (
"fmt"
"os"
"testing"
"unsafe"
"github.com/xhd2015/xgo/runtime/mock"
"github.com/xhd2015/xgo/runtime/test/patch_const/sub"
)
const pkgPath = "github.com/xhd2015/xgo/runtime/test/patch_const"
const subPkgPath = "github.com/xhd2015/xgo/runtime/test/patch_const/sub"
const testVersion = "1.0"
const N = 50
func TestPatchInElseShouldWork(t *testing.T) {
if os.Getenv("nothing") == "nothing" {
t.Fatalf("should go else")
} else {
mock.PatchByName(pkgPath, "N", func() int {
return 5
})
b := N
if b != 5 {
t.Fatalf("expect b to be %d,actual: %d", 5, b)
}
}
}
func TestPatchConstByNamePtrTest(t *testing.T) {
mock.PatchByName(pkgPath, "testVersion", func() string {
return "1.5"
})
version := testVersion
if version != "1.5" {
t.Fatalf("expect patched version a to be %s, actual: %s", "1.5", version)
}
}
func TestPatchConstByNameWrongTypeShouldFail(t *testing.T) {
var pe interface{}
func() {
defer func() {
pe = recover()
}()
mock.PatchByName(pkgPath, "N", func() string {
return "1.5"
})
}()
expectMsg := "replacer should have type: func() int, actual: func() string"
if pe == nil {
t.Fatalf("expect panic: %q, actual nil", expectMsg)
}
msg := fmt.Sprint(pe)
if msg != expectMsg {
t.Fatalf("expect err %q, actual: %q", expectMsg, msg)
}
}
func TestPatchConstOperationShouldCompileAndSkipMock(t *testing.T) {
// should have effect
mock.PatchByName(pkgPath, "N", func() int {
return 10
})
// because N is used inside an operation
// it's type is not yet determined, so
// should not rewrite it
size := N * unsafe.Sizeof(int(0))
if size != 80 {
t.Fatalf("expect N not patched and size to be %d, actual: %d\n", 80, size)
}
}
func TestPatchOtherPkgConstOperationShouldWork(t *testing.T) {
// should have effect
mock.PatchByName(subPkgPath, "N", func() int {
return 10
})
// because N is used inside an operation
// it's type is not yet determined, so
// should not rewrite it
size := sub.N * unsafe.Sizeof(int(0))
if size != 80 {
t.Fatalf("expect N not patched and size to be %d, actual: %d\n", 80, size)
}
}
func TestConstOperationNaked(t *testing.T) {
mock.PatchByName(pkgPath, "N", func() int {
return 10
})
var size int64 = N + 1
if size != 11 {
t.Fatalf("expect N not patched and size to be %d, actual: %d\n", 11, size)
}
}
const M = 10
func TestTwoConstAdd(t *testing.T) {
mock.PatchByName(pkgPath, "N", func() int {
return 10
})
var size int64 = (N + M) * 2
if size != 40 {
t.Fatalf("expect N not patched and size to be %d, actual: %d\n", 40, size)
}
}
func TestConstOperationParen(t *testing.T) {
mock.PatchByName(pkgPath, "N", func() int {
return 10
})
var size int64 = (N + 1) * 2
if size != 22 {
t.Fatalf("expect N not patched and size to be %d, actual: %d\n", 22, size)
}
}
// local const
func TestPatchConstInAssignmentShouldWork(t *testing.T) {
mock.PatchByName(pkgPath, "N", func() int {
return 10
})
var a int64 = N
if a != 10 {
t.Fatalf("expect a to be %d, actual: %d\n", 10, a)
}
}
func TestPatchConstInAssignmentNoDefShouldWork(t *testing.T) {
mock.PatchByName(pkgPath, "N", func() int {
return 10
})
var a int64 = 100
if os.Getenv("nothing") == "" {
a = N
}
if a != 10 {
t.Fatalf("expect a to be %d, actual: %d\n", 10, a)
}
}
func TestPatchConstInFuncArgShouldSkip(t *testing.T) {
mock.PatchByName(pkgPath, "N", func() int {
return 10
})
a := f(N)
if a != 10 {
t.Fatalf("expect a to be %d, actual: %d\n", 10, a)
}
}
func TestPatchConstInTypeConvertArgShouldWork(t *testing.T) {
mock.PatchByName(pkgPath, "N", func() int {
return 10
})
a := int64(N)
if a != 10 {
t.Fatalf("expect a to be %d, actual: %d\n", 10, a)
}
}
func f(a int64) int64 {
return a
}
func TestCaseConstShouldSkip(t *testing.T) {
n := int64(50)
switch n {
case N:
case N + 1:
t.Fatalf("should not fail to N+1")
default:
t.Fatalf("should not fall to default")
}
switch n {
case N, N + 1:
default:
t.Fatalf("should not fall to default")
}
}
func TestReturnConstShouldWork(t *testing.T) {
mock.PatchByName(pkgPath, "N", func() int {
return 10
})
n := getN()
if n != 10 {
t.Fatalf("expect a to be %d, actual: %d\n", 50, n)
}
}
func getN() int64 {
return N
}
// other package
func TestPatchOtherPackageConstInAssignmentShouldWork(t *testing.T) {
mock.PatchByName(subPkgPath, "N", func() int {
return 10
})
var a int64 = sub.N
if a != 10 {
t.Fatalf("expect a to be %d, actual: %d\n", 10, a)
}
}
func TestPatchOtherPackageConstInAssignmentNoDefShouldWork(t *testing.T) {
mock.PatchByName(subPkgPath, "N", func() int {
return 10
})
var a int64 = 100
if os.Getenv("nothing") == "" {
a = sub.N
}
if a != 10 {
t.Fatalf("expect a to be %d, actual: %d\n", 10, a)
}
}
func TestPatchOtherPackageConstInFuncArgShouldWork(t *testing.T) {
mock.PatchByName(subPkgPath, "N", func() int {
return 10
})
a := f(sub.N)
if a != 10 {
t.Fatalf("expect a to be %d, actual: %d\n", 10, a)
}
}
func TestPatchOtherPackageConstInTypeConvertArgShouldWork(t *testing.T) {
mock.PatchByName(subPkgPath, "N", func() int {
return 10
})
a := int64(sub.N)
if a != 10 {
t.Fatalf("expect a to be %d, actual: %d\n", 10, a)
}
}
const x = "123"
func TestPatchConstOverlappingNameShouldSkip(t *testing.T) {
x := make([]int, 0, 10)
x = append(x, 10)
if x[0] != 10 {
t.Fatalf("expect x[0] to be %d, actual: %d", 10, x[0])
}
}
func exampleSprintf(args ...interface{}) string {
return fmt.Sprintf("%v", args...)
}
func TestPatchLitPlusLitShouldCompile(t *testing.T) {
s := "should "
a := exampleSprintf(s + "compile")
b := exampleSprintf("should " + "compile") // this skips from wrapping
if a != b {
t.Fatalf("expect exampleSprintf result to be %q, actual: %q", b, a)
}
}
type Label string
func convertLabel(label Label) string {
return string(label)
}
func TestPatchOtherPkgConst(t *testing.T) {
label := convertLabel(sub.LabelPrefix + "v2")
if label != "label:v2" {
t.Fatalf("bad label: %s", label)
}
}
const good = 2
const reason = "test"
func TestNameConflictWithArgShouldSkip(t *testing.T) {
reasons := getReasons("good")
if len(reasons) != 2 || reasons[0] != "ok" || reasons[1] != "good" {
t.Fatalf("bad reason: %v", reasons)
}
getReasons2 := func(good string) (reason []string) {
reason = append(reason, "ok")
reason = append(reason, good)
return
}
reasons2 := getReasons2("good")
if len(reasons2) != 2 || reasons2[0] != "ok" || reasons2[1] != "good" {
t.Fatalf("bad reason2: %v", reasons2)
}
}
func getReasons(good string) (reason []string) {
reason = append(reason, "ok")
reason = append(reason, good)
return
}