liyujie
2025-08-28 d9927380ed7c8366f762049be9f3fee225860833
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
 
package doc_test
 
import (
   "bytes"
   "go/ast"
   "go/doc"
   "go/format"
   "go/parser"
   "go/token"
   "strings"
   "testing"
)
 
const exampleTestFile = `
package foo_test
 
import (
   "flag"
   "fmt"
   "log"
   "sort"
   "os/exec"
)
 
func ExampleHello() {
   fmt.Println("Hello, world!")
   // Output: Hello, world!
}
 
func ExampleImport() {
   out, err := exec.Command("date").Output()
   if err != nil {
       log.Fatal(err)
   }
   fmt.Printf("The date is %s\n", out)
}
 
func ExampleKeyValue() {
   v := struct {
       a string
       b int
   }{
       a: "A",
       b: 1,
   }
   fmt.Print(v)
   // Output: a: "A", b: 1
}
 
func ExampleKeyValueImport() {
   f := flag.Flag{
       Name: "play",
   }
   fmt.Print(f)
   // Output: Name: "play"
}
 
var keyValueTopDecl = struct {
   a string
   b int
}{
   a: "B",
   b: 2,
}
 
func ExampleKeyValueTopDecl() {
   fmt.Print(keyValueTopDecl)
   // Output: a: "B", b: 2
}
 
// Person represents a person by name and age.
type Person struct {
    Name string
    Age  int
}
 
// String returns a string representation of the Person.
func (p Person) String() string {
    return fmt.Sprintf("%s: %d", p.Name, p.Age)
}
 
// ByAge implements sort.Interface for []Person based on
// the Age field.
type ByAge []Person
 
// Len returns the number of elements in ByAge.
func (a (ByAge)) Len() int { return len(a) }
 
// Swap swaps the elements in ByAge.
func (a ByAge) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age }
 
// people is the array of Person
var people = []Person{
   {"Bob", 31},
   {"John", 42},
   {"Michael", 17},
   {"Jenny", 26},
}
 
func ExampleSort() {
    fmt.Println(people)
    sort.Sort(ByAge(people))
    fmt.Println(people)
    // Output:
    // [Bob: 31 John: 42 Michael: 17 Jenny: 26]
    // [Michael: 17 Jenny: 26 Bob: 31 John: 42]
}
`
 
var exampleTestCases = []struct {
   Name, Play, Output string
}{
   {
       Name:   "Hello",
       Play:   exampleHelloPlay,
       Output: "Hello, world!\n",
   },
   {
       Name: "Import",
       Play: exampleImportPlay,
   },
   {
       Name:   "KeyValue",
       Play:   exampleKeyValuePlay,
       Output: "a: \"A\", b: 1\n",
   },
   {
       Name:   "KeyValueImport",
       Play:   exampleKeyValueImportPlay,
       Output: "Name: \"play\"\n",
   },
   {
       Name:   "KeyValueTopDecl",
       Play:   exampleKeyValueTopDeclPlay,
       Output: "a: \"B\", b: 2\n",
   },
   {
       Name:   "Sort",
       Play:   exampleSortPlay,
       Output: "[Bob: 31 John: 42 Michael: 17 Jenny: 26]\n[Michael: 17 Jenny: 26 Bob: 31 John: 42]\n",
   },
}
 
const exampleHelloPlay = `package main
 
import (
   "fmt"
)
 
func main() {
   fmt.Println("Hello, world!")
}
`
const exampleImportPlay = `package main
 
import (
   "fmt"
   "log"
   "os/exec"
)
 
func main() {
   out, err := exec.Command("date").Output()
   if err != nil {
       log.Fatal(err)
   }
   fmt.Printf("The date is %s\n", out)
}
`
 
const exampleKeyValuePlay = `package main
 
import (
   "fmt"
)
 
func main() {
   v := struct {
       a string
       b int
   }{
       a: "A",
       b: 1,
   }
   fmt.Print(v)
}
`
 
const exampleKeyValueImportPlay = `package main
 
import (
   "flag"
   "fmt"
)
 
func main() {
   f := flag.Flag{
       Name: "play",
   }
   fmt.Print(f)
}
`
 
const exampleKeyValueTopDeclPlay = `package main
 
import (
   "fmt"
)
 
var keyValueTopDecl = struct {
   a string
   b int
}{
   a: "B",
   b: 2,
}
 
func main() {
   fmt.Print(keyValueTopDecl)
}
`
 
const exampleSortPlay = `package main
 
import (
   "fmt"
   "sort"
)
 
// Person represents a person by name and age.
type Person struct {
   Name string
   Age  int
}
 
// String returns a string representation of the Person.
func (p Person) String() string {
   return fmt.Sprintf("%s: %d", p.Name, p.Age)
}
 
// ByAge implements sort.Interface for []Person based on
// the Age field.
type ByAge []Person
 
// Len returns the number of elements in ByAge.
func (a ByAge) Len() int { return len(a) }
 
// Swap swaps the elements in ByAge.
func (a ByAge) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age }
 
// people is the array of Person
var people = []Person{
   {"Bob", 31},
   {"John", 42},
   {"Michael", 17},
   {"Jenny", 26},
}
 
func main() {
   fmt.Println(people)
   sort.Sort(ByAge(people))
   fmt.Println(people)
}
`
 
func TestExamples(t *testing.T) {
   fset := token.NewFileSet()
   file, err := parser.ParseFile(fset, "test.go", strings.NewReader(exampleTestFile), parser.ParseComments)
   if err != nil {
       t.Fatal(err)
   }
   for i, e := range doc.Examples(file) {
       c := exampleTestCases[i]
       if e.Name != c.Name {
           t.Errorf("got Name == %q, want %q", e.Name, c.Name)
       }
       if w := c.Play; w != "" {
           g := formatFile(t, fset, e.Play)
           if g != w {
               t.Errorf("%s: got Play == %q, want %q", c.Name, g, w)
           }
       }
       if g, w := e.Output, c.Output; g != w {
           t.Errorf("%s: got Output == %q, want %q", c.Name, g, w)
       }
   }
}
 
const exampleWholeFile = `package foo_test
 
type X int
 
func (X) Foo() {
}
 
func (X) TestBlah() {
}
 
func (X) BenchmarkFoo() {
}
 
func Example() {
   fmt.Println("Hello, world!")
   // Output: Hello, world!
}
`
 
const exampleWholeFileOutput = `package main
 
type X int
 
func (X) Foo() {
}
 
func (X) TestBlah() {
}
 
func (X) BenchmarkFoo() {
}
 
func main() {
   fmt.Println("Hello, world!")
}
`
 
func TestExamplesWholeFile(t *testing.T) {
   fset := token.NewFileSet()
   file, err := parser.ParseFile(fset, "test.go", strings.NewReader(exampleWholeFile), parser.ParseComments)
   if err != nil {
       t.Fatal(err)
   }
   es := doc.Examples(file)
   if len(es) != 1 {
       t.Fatalf("wrong number of examples; got %d want 1", len(es))
   }
   e := es[0]
   if e.Name != "" {
       t.Errorf("got Name == %q, want %q", e.Name, "")
   }
   if g, w := formatFile(t, fset, e.Play), exampleWholeFileOutput; g != w {
       t.Errorf("got Play == %q, want %q", g, w)
   }
   if g, w := e.Output, "Hello, world!\n"; g != w {
       t.Errorf("got Output == %q, want %q", g, w)
   }
}
 
const exampleInspectSignature = `package foo_test
 
import (
   "bytes"
   "io"
)
 
func getReader() io.Reader { return nil }
 
func do(b bytes.Reader) {}
 
func Example() {
   getReader()
   do()
   // Output:
}
 
func ExampleIgnored() {
}
`
 
const exampleInspectSignatureOutput = `package main
 
import (
   "bytes"
   "io"
)
 
func getReader() io.Reader { return nil }
 
func do(b bytes.Reader) {}
 
func main() {
   getReader()
   do()
}
`
 
func TestExampleInspectSignature(t *testing.T) {
   // Verify that "bytes" and "io" are imported. See issue #28492.
   fset := token.NewFileSet()
   file, err := parser.ParseFile(fset, "test.go", strings.NewReader(exampleInspectSignature), parser.ParseComments)
   if err != nil {
       t.Fatal(err)
   }
   es := doc.Examples(file)
   if len(es) != 2 {
       t.Fatalf("wrong number of examples; got %d want 2", len(es))
   }
   // We are interested in the first example only.
   e := es[0]
   if e.Name != "" {
       t.Errorf("got Name == %q, want %q", e.Name, "")
   }
   if g, w := formatFile(t, fset, e.Play), exampleInspectSignatureOutput; g != w {
       t.Errorf("got Play == %q, want %q", g, w)
   }
   if g, w := e.Output, ""; g != w {
       t.Errorf("got Output == %q, want %q", g, w)
   }
}
 
const exampleEmpty = `
package p
func Example() {}
func Example_a()
`
 
const exampleEmptyOutput = `package main
 
func main() {}
func main()
`
 
func TestExampleEmpty(t *testing.T) {
   fset := token.NewFileSet()
   file, err := parser.ParseFile(fset, "test.go", strings.NewReader(exampleEmpty), parser.ParseComments)
   if err != nil {
       t.Fatal(err)
   }
 
   es := doc.Examples(file)
   if len(es) != 1 {
       t.Fatalf("wrong number of examples; got %d want 1", len(es))
   }
   e := es[0]
   if e.Name != "" {
       t.Errorf("got Name == %q, want %q", e.Name, "")
   }
   if g, w := formatFile(t, fset, e.Play), exampleEmptyOutput; g != w {
       t.Errorf("got Play == %q, want %q", g, w)
   }
   if g, w := e.Output, ""; g != w {
       t.Errorf("got Output == %q, want %q", g, w)
   }
}
 
func formatFile(t *testing.T, fset *token.FileSet, n *ast.File) string {
   if n == nil {
       return "<nil>"
   }
   var buf bytes.Buffer
   if err := format.Node(&buf, fset, n); err != nil {
       t.Fatal(err)
   }
   return buf.String()
}