Repository navigation
Expand file tree
/
Copy pathextract_function_range_map.txtar
More file actions
134 lines (112 loc) · 2.73 KB
/
Copy pathextract_function_range_map.txtar
File metadata and controls
134 lines (112 loc) · 2.73 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
# Extract a complete direct map range and preserve named types, empty cases, map identity, and mutation visibility.
exec semedit refactor-extract-callable --file service.go --symbol visit --in-function Run --kind function --start-line 10 --end-line 12
stdout 'symbol: visit'
cmp service.go want/service.go
exec go test ./...
-- go.mod --
module example.com/extractrangemap
go 1.23
-- service.go --
package sample
type EntryMap map[int]string
var seen map[int]string
var alias EntryMap
var callbackCount int
func Run(entries EntryMap) {
for key, value := range entries {
record(key, value)
}
}
func RunLocal() {
entries := EntryMap{3: "three", 4: "four"}
for key, value := range entries {
record(key, value)
}
}
func RunAndClear(entries EntryMap) {
alias = entries
callbackCount = 0
for key, value := range entries {
recordAndClear(key, value)
}
}
func record(key int, value string) {
seen[key] = value
}
func recordAndClear(key int, value string) {
seen[key] = value
if callbackCount == 0 {
clear(alias)
}
callbackCount++
}
-- service_test.go --
package sample
import (
"reflect"
"testing"
)
func TestMapRangeExtractionPreservesNilEmptyAndEntries(t *testing.T) {
seen = map[int]string{}
Run(nil)
Run(EntryMap{})
if len(seen) != 0 {
t.Fatalf("nil and empty maps produced entries: %v", seen)
}
want := map[int]string{1: "one", 2: "two", 3: "three"}
Run(EntryMap{1: "one", 2: "two", 3: "three"})
if !reflect.DeepEqual(seen, want) {
t.Fatalf("Run entries = %v, want %v", seen, want)
}
}
func TestMapRangeExtractionPreservesLocalMapAndAliasMutation(t *testing.T) {
seen = map[int]string{}
RunLocal()
wantLocal := map[int]string{3: "three", 4: "four"}
if !reflect.DeepEqual(seen, wantLocal) {
t.Fatalf("RunLocal entries = %v, want %v", seen, wantLocal)
}
entries := EntryMap{1: "one", 2: "two", 3: "three"}
seen = map[int]string{}
RunAndClear(entries)
if callbackCount != 1 || len(entries) != 0 || len(seen) != 1 {
t.Fatalf("alias mutation: callbacks=%d entries=%v seen=%v", callbackCount, entries, seen)
}
}
-- want/service.go --
package sample
type EntryMap map[int]string
var seen map[int]string
var alias EntryMap
var callbackCount int
func Run(entries EntryMap) {
visit(entries)
}
func visit(entries EntryMap) {
for key, value := range entries {
record(key, value)
}
}
func RunLocal() {
entries := EntryMap{3: "three", 4: "four"}
for key, value := range entries {
record(key, value)
}
}
func RunAndClear(entries EntryMap) {
alias = entries
callbackCount = 0
for key, value := range entries {
recordAndClear(key, value)
}
}
func record(key int, value string) {
seen[key] = value
}
func recordAndClear(key int, value string) {
seen[key] = value
if callbackCount == 0 {
clear(alias)
}
callbackCount++
}