-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharray_comments_test.go
More file actions
286 lines (247 loc) · 6.9 KB
/
Copy patharray_comments_test.go
File metadata and controls
286 lines (247 loc) · 6.9 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package tomledit
import (
"strings"
"testing"
)
// TestArrayComments_RoundTripInlineComments verifies that parsing an array with
// inline comments on elements and round-tripping via Bytes() preserves them
// exactly (clean path via raw bytes).
func TestArrayComments_RoundTripInlineComments(t *testing.T) {
input := `arr = [
1, # first
2, # second
3,
]
`
doc, err := Parse([]byte(input))
if err != nil {
t.Fatalf("parse: %v", err)
}
got := string(doc.Bytes())
if got != input {
t.Errorf("round-trip mismatch\ngot:\n%s\nwant:\n%s", got, input)
}
}
// TestArrayComments_RoundTripLeadingComments verifies round-trip of arrays
// with standalone comment lines between elements.
func TestArrayComments_RoundTripLeadingComments(t *testing.T) {
input := `arr = [
1,
# between
2,
3,
]
`
doc, err := Parse([]byte(input))
if err != nil {
t.Fatalf("parse: %v", err)
}
got := string(doc.Bytes())
if got != input {
t.Errorf("round-trip mismatch\ngot:\n%s\nwant:\n%s", got, input)
}
}
// TestArrayComments_SetPreservesOtherComments sets an array element and
// verifies that comments on other elements survive re-rendering.
func TestArrayComments_SetPreservesOtherComments(t *testing.T) {
input := `arr = [
1, # first
# between
2, # second
3,
]
`
doc, err := Parse([]byte(input))
if err != nil {
t.Fatalf("parse: %v", err)
}
// Set arr[1] = 42 (the element with "# second" inline comment)
if err := doc.Set("arr[1]", 42); err != nil {
t.Fatalf("Set: %v", err)
}
got := string(doc.Bytes())
// The inline comment "# first" on the first element should survive.
if !strings.Contains(got, "# first") {
t.Errorf("lost '# first' comment\ngot:\n%s", got)
}
// The standalone "# between" comment should survive.
if !strings.Contains(got, "# between") {
t.Errorf("lost '# between' comment\ngot:\n%s", got)
}
// The replaced element value should be 42.
if !strings.Contains(got, "42") {
t.Errorf("missing new value 42\ngot:\n%s", got)
}
// The output must be valid TOML.
doc2, err := Parse([]byte(got))
if err != nil {
t.Fatalf("re-parse failed: %v\noutput:\n%s", err, got)
}
// Verify the semantic value is correct.
node, ok := doc2.Lookup("arr[1]")
if !ok {
t.Fatalf("Get arr[1]: not found")
}
if node.(Scalar).Value() != int64(42) {
t.Errorf("arr[1] = %v, want 42", node.(Scalar).Value())
}
}
// TestArrayComments_FormatPreservesComments parses a multi-line array with
// comments, formats it, and verifies comments appear in the output.
func TestArrayComments_FormatPreservesComments(t *testing.T) {
input := `arr = [
1, # first
# a comment
2, # second
3,
]
`
doc, err := Parse([]byte(input))
if err != nil {
t.Fatalf("parse: %v", err)
}
// Force array dirty by setting element 2 (third element, index 2).
if err := doc.Set("arr[2]", 99); err != nil {
t.Fatalf("Set: %v", err)
}
got := string(doc.Bytes())
if !strings.Contains(got, "# first") {
t.Errorf("lost '# first' comment\ngot:\n%s", got)
}
if !strings.Contains(got, "# a comment") {
t.Errorf("lost '# a comment' comment\ngot:\n%s", got)
}
if !strings.Contains(got, "# second") {
t.Errorf("lost '# second' comment\ngot:\n%s", got)
}
if !strings.Contains(got, "99") {
t.Errorf("missing new value 99\ngot:\n%s", got)
}
// Must be valid TOML.
if _, err := Parse([]byte(got)); err != nil {
t.Fatalf("re-parse failed: %v\noutput:\n%s", err, got)
}
}
// TestArrayComments_SetPreservesInlineOnUnchanged sets one element and verifies
// that inline comments on the unchanged elements are preserved.
func TestArrayComments_SetPreservesInlineOnUnchanged(t *testing.T) {
input := `arr = [
"a", # alpha
"b", # beta
"c", # gamma
]
`
doc, err := Parse([]byte(input))
if err != nil {
t.Fatalf("parse: %v", err)
}
// Change the middle element only.
if err := doc.Set("arr[1]", "B"); err != nil {
t.Fatalf("Set: %v", err)
}
got := string(doc.Bytes())
// Comments on unchanged elements must survive.
if !strings.Contains(got, "# alpha") {
t.Errorf("lost '# alpha' comment\ngot:\n%s", got)
}
if !strings.Contains(got, "# gamma") {
t.Errorf("lost '# gamma' comment\ngot:\n%s", got)
}
// The changed element's value should be "B".
if !strings.Contains(got, `"B"`) {
t.Errorf("missing new value \"B\"\ngot:\n%s", got)
}
// Must be valid TOML.
doc2, err := Parse([]byte(got))
if err != nil {
t.Fatalf("re-parse failed: %v\noutput:\n%s", err, got)
}
node, ok := doc2.Lookup("arr[1]")
if !ok {
t.Fatalf("Get arr[1]: not found")
}
if node.(Scalar).Value() != "B" {
t.Errorf("arr[1] = %v, want \"B\"", node.(Scalar).Value())
}
}
// TestArrayComments_TrailingCommentBeforeClose verifies that a comment after the
// last element but before ']' is preserved.
func TestArrayComments_TrailingCommentBeforeClose(t *testing.T) {
input := `arr = [
1,
2,
# trailing note
]
`
doc, err := Parse([]byte(input))
if err != nil {
t.Fatalf("parse: %v", err)
}
// Clean round-trip.
got := string(doc.Bytes())
if got != input {
t.Errorf("clean round-trip mismatch\ngot:\n%s\nwant:\n%s", got, input)
}
// Dirty round-trip: modify an element.
if err := doc.Set("arr[0]", 10); err != nil {
t.Fatalf("Set: %v", err)
}
got = string(doc.Bytes())
if !strings.Contains(got, "# trailing note") {
t.Errorf("lost '# trailing note' comment\ngot:\n%s", got)
}
// Must be valid TOML.
if _, err := Parse([]byte(got)); err != nil {
t.Fatalf("re-parse failed: %v\noutput:\n%s", err, got)
}
}
// TestArrayComments_InlineOnlyNoLeading tests an array where elements only have
// inline comments and no leading standalone comments.
func TestArrayComments_InlineOnlyNoLeading(t *testing.T) {
input := `arr = [
10, # ten
20, # twenty
]
`
doc, err := Parse([]byte(input))
if err != nil {
t.Fatalf("parse: %v", err)
}
// Set the first element.
if err := doc.Set("arr[0]", 100); err != nil {
t.Fatalf("Set: %v", err)
}
got := string(doc.Bytes())
// Second element's inline comment should survive.
if !strings.Contains(got, "# twenty") {
t.Errorf("lost '# twenty' comment\ngot:\n%s", got)
}
if !strings.Contains(got, "100") {
t.Errorf("missing new value 100\ngot:\n%s", got)
}
// Must be valid TOML.
if _, err := Parse([]byte(got)); err != nil {
t.Fatalf("re-parse failed: %v\noutput:\n%s", err, got)
}
}
// TestArrayComments_SimpleArrayNoComments ensures that arrays without comments
// still render in compact form.
func TestArrayComments_SimpleArrayNoComments(t *testing.T) {
input := "arr = [1, 2, 3]\n"
doc, err := Parse([]byte(input))
if err != nil {
t.Fatalf("parse: %v", err)
}
// Set an element to force dirty.
if err := doc.Set("arr[0]", 10); err != nil {
t.Fatalf("Set: %v", err)
}
got := string(doc.Bytes())
// Should still be compact (single line), not multiline.
if strings.Count(got, "\n") > 1 {
t.Errorf("expected compact array, got multiline:\n%s", got)
}
if !strings.Contains(got, "[10, 2, 3]") {
t.Errorf("unexpected output:\n%s", got)
}
}