Repository navigation
Expand file tree
/
Copy pathextract_function_range_slice_expression.txtar
More file actions
267 lines (230 loc) · 7.37 KB
/
Copy pathextract_function_range_slice_expression.txtar
File metadata and controls
267 lines (230 loc) · 7.37 KB
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
# Extract direct two-index slice-expression ranges with omitted and explicit bounds while preserving panic, iteration, and backing-array behavior.
exec semedit refactor-extract-callable --file all.go --symbol walkAll --in-function WalkAll --kind function --start-line 4 --end-line 6
cmp all.go want/all.go.txt
exec semedit refactor-extract-callable --file from.go --symbol walkFrom --in-function WalkFrom --kind function --start-line 4 --end-line 6
cmp from.go want/from.go.txt
exec semedit refactor-extract-callable --file to.go --symbol walkTo --in-function WalkTo --kind function --start-line 4 --end-line 6
cmp to.go want/to.go.txt
exec semedit refactor-extract-callable --file bounded.go --symbol walkBounded --in-function WalkBounded --kind function --start-line 4 --end-line 6
cmp bounded.go want/bounded.go.txt
exec semedit refactor-extract-callable --file shadow.go --symbol walkShadow --in-function WalkShadow --kind function --start-line 4 --end-line 6
cmp shadow.go want/shadow.go.txt
exec semedit refactor-extract-callable --file mutation.go --symbol walkMutation --in-function WalkMutation --kind function --start-line 5 --end-line 10
cmp mutation.go want/mutation.go.txt
exec semedit refactor-extract-callable --file invalid.go --symbol walkInvalid --in-function WalkInvalid --kind function --start-line 4 --end-line 6
cmp invalid.go want/invalid.go.txt
exec go test ./...
-- go.mod --
module example.com/extractrangesliceexpression
go 1.23
-- all.go --
package sample
func WalkAll(values Values) {
for index, value := range values[:] {
record("all", index, value, -1)
}
}
-- from.go --
package sample
func WalkFrom(values Values, low int) {
for index, value := range values[low:] {
record("from", index, value, low)
}
}
-- to.go --
package sample
func WalkTo(values Values, high int) {
for index, value := range values[:high] {
record("to", index, value, high)
}
}
-- bounded.go --
package sample
func WalkBounded(values Values, low, high int) {
for index, value := range values[low:high] {
record("bounded", index, value, low)
}
}
-- shadow.go --
package sample
func WalkShadow(values Values, low, high int) {
for low, value := range values[low:high] {
record("shadow", low, value, low)
}
}
-- mutation.go --
package sample
func WalkMutation(values Values, low, high int) {
alias = values[low:high]
for index, value := range values[low:high] {
if index == 0 {
mutateBacking()
}
record("mutation", index, value, low)
}
}
-- invalid.go --
package sample
func WalkInvalid(values Values, low, high int) {
for index, value := range values[low:high] {
record("invalid", index, value, low)
}
}
-- support.go --
package sample
type Values []int
type Event struct {
Kind string
Index int
Value int
Bound int
}
var events []Event
var backing Values
var alias Values
var bodyEffects int
func record(kind string, index, value, bound int) {
events = append(events, Event{Kind: kind, Index: index, Value: value, Bound: bound})
bodyEffects++
}
func mutateBacking() {
alias = alias[:1]
backing[3] = 99
alias = append(alias, 77)
}
-- service_test.go --
package sample
import (
"reflect"
"testing"
)
func TestSliceExpressionRangesPreserveGoBehavior(t *testing.T) {
events = nil
WalkAll(nil)
WalkAll(Values{})
if len(events) != 0 || bodyEffects != 0 {
t.Fatalf("nil and empty full slices: events=%v effects=%d", events, bodyEffects)
}
values := Values{100, 1, 2, 3, 4}
events = nil
WalkAll(values)
if !reflect.DeepEqual(events, []Event{{Kind: "all", Index: 0, Value: 100, Bound: -1}, {Kind: "all", Index: 1, Value: 1, Bound: -1}, {Kind: "all", Index: 2, Value: 2, Bound: -1}, {Kind: "all", Index: 3, Value: 3, Bound: -1}, {Kind: "all", Index: 4, Value: 4, Bound: -1}}) {
t.Fatalf("full-slice expression: %v", events)
}
events = nil
WalkFrom(values, 1)
if !reflect.DeepEqual(events, []Event{{Kind: "from", Index: 0, Value: 1, Bound: 1}, {Kind: "from", Index: 1, Value: 2, Bound: 1}, {Kind: "from", Index: 2, Value: 3, Bound: 1}, {Kind: "from", Index: 3, Value: 4, Bound: 1}}) {
t.Fatalf("omitted high bound: %v", events)
}
events = nil
WalkTo(values, 3)
if !reflect.DeepEqual(events, []Event{{Kind: "to", Index: 0, Value: 100, Bound: 3}, {Kind: "to", Index: 1, Value: 1, Bound: 3}, {Kind: "to", Index: 2, Value: 2, Bound: 3}}) {
t.Fatalf("omitted low bound: %v", events)
}
events = nil
WalkBounded(values, 1, 4)
if !reflect.DeepEqual(events, []Event{{Kind: "bounded", Index: 0, Value: 1, Bound: 1}, {Kind: "bounded", Index: 1, Value: 2, Bound: 1}, {Kind: "bounded", Index: 2, Value: 3, Bound: 1}}) {
t.Fatalf("explicit bounds and bound use in body: %v", events)
}
events = nil
WalkShadow(values, 1, 4)
if !reflect.DeepEqual(events, []Event{{Kind: "shadow", Index: 0, Value: 1, Bound: 0}, {Kind: "shadow", Index: 1, Value: 2, Bound: 1}, {Kind: "shadow", Index: 2, Value: 3, Bound: 2}}) {
t.Fatalf("bound name shadowed by range variable: %v", events)
}
backing = Values{100, 1, 2, 3, 4}
values = backing[1:5]
bodyEffects = 0
events = nil
WalkMutation(values, 1, 4)
wantMutation := []Event{{Kind: "mutation", Index: 0, Value: 2, Bound: 1}, {Kind: "mutation", Index: 1, Value: 77, Bound: 1}, {Kind: "mutation", Index: 2, Value: 4, Bound: 1}}
if !reflect.DeepEqual(events, wantMutation) || backing[3] != 77 || bodyEffects != 3 {
t.Fatalf("captured length/backing mutation: events=%v backing=%v effects=%d", events, backing, bodyEffects)
}
bodyEffects = 0
events = nil
func() {
defer func() {
if recover() == nil {
t.Error("invalid bounds did not panic")
}
}()
WalkInvalid(Values{1}, 0, 2)
}()
if bodyEffects != 0 || len(events) != 0 {
t.Fatalf("invalid bounds ran body effects: events=%v effects=%d", events, bodyEffects)
}
}
-- want/all.go.txt --
package sample
func WalkAll(values Values) {
walkAll(values)
}
func walkAll(values Values) {
for index, value := range values[:] {
record("all", index, value, -1)
}
}
-- want/from.go.txt --
package sample
func WalkFrom(values Values, low int) {
walkFrom(values, low)
}
func walkFrom(values Values, low int) {
for index, value := range values[low:] {
record("from", index, value, low)
}
}
-- want/to.go.txt --
package sample
func WalkTo(values Values, high int) {
walkTo(values, high)
}
func walkTo(values Values, high int) {
for index, value := range values[:high] {
record("to", index, value, high)
}
}
-- want/bounded.go.txt --
package sample
func WalkBounded(values Values, low, high int) {
walkBounded(values, low, high)
}
func walkBounded(values Values, low int, high int) {
for index, value := range values[low:high] {
record("bounded", index, value, low)
}
}
-- want/shadow.go.txt --
package sample
func WalkShadow(values Values, low, high int) {
walkShadow(values, low, high)
}
func walkShadow(values Values, low int, high int) {
for low, value := range values[low:high] {
record("shadow", low, value, low)
}
}
-- want/mutation.go.txt --
package sample
func WalkMutation(values Values, low, high int) {
alias = values[low:high]
walkMutation(values, low, high)
}
func walkMutation(values Values, low int, high int) {
for index, value := range values[low:high] {
if index == 0 {
mutateBacking()
}
record("mutation", index, value, low)
}
}
-- want/invalid.go.txt --
package sample
func WalkInvalid(values Values, low, high int) {
walkInvalid(values, low, high)
}
func walkInvalid(values Values, low int, high int) {
for index, value := range values[low:high] {
record("invalid", index, value, low)
}
}