@@ -331,25 +331,65 @@ func main() {
331
331
}
332
332
`
333
333
334
+ const exampleWholeFileFunction = `package foo_test
335
+
336
+ func Foo(x int) {
337
+ }
338
+
339
+ func Example() {
340
+ fmt.Println("Hello, world!")
341
+ // Output: Hello, world!
342
+ }
343
+ `
344
+
345
+ const exampleWholeFileFunctionOutput = `package main
346
+
347
+ func Foo(x int) {
348
+ }
349
+
350
+ func main() {
351
+ fmt.Println("Hello, world!")
352
+ }
353
+ `
354
+
355
+ var exampleWholeFileTestCases = []struct {
356
+ Title , Source , Play , Output string
357
+ }{
358
+ {
359
+ "Methods" ,
360
+ exampleWholeFile ,
361
+ exampleWholeFileOutput ,
362
+ "Hello, world!\n " ,
363
+ },
364
+ {
365
+ "Function" ,
366
+ exampleWholeFileFunction ,
367
+ exampleWholeFileFunctionOutput ,
368
+ "Hello, world!\n " ,
369
+ },
370
+ }
371
+
334
372
func TestExamplesWholeFile (t * testing.T ) {
335
- fset := token .NewFileSet ()
336
- file , err := parser .ParseFile (fset , "test.go" , strings .NewReader (exampleWholeFile ), parser .ParseComments )
337
- if err != nil {
338
- t .Fatal (err )
339
- }
340
- es := doc .Examples (file )
341
- if len (es ) != 1 {
342
- t .Fatalf ("wrong number of examples; got %d want 1" , len (es ))
343
- }
344
- e := es [0 ]
345
- if e .Name != "" {
346
- t .Errorf ("got Name == %q, want %q" , e .Name , "" )
347
- }
348
- if g , w := formatFile (t , fset , e .Play ), exampleWholeFileOutput ; g != w {
349
- t .Errorf ("got Play == %q, want %q" , g , w )
350
- }
351
- if g , w := e .Output , "Hello, world!\n " ; g != w {
352
- t .Errorf ("got Output == %q, want %q" , g , w )
373
+ for _ , c := range exampleWholeFileTestCases {
374
+ fset := token .NewFileSet ()
375
+ file , err := parser .ParseFile (fset , "test.go" , strings .NewReader (c .Source ), parser .ParseComments )
376
+ if err != nil {
377
+ t .Fatal (err )
378
+ }
379
+ es := doc .Examples (file )
380
+ if len (es ) != 1 {
381
+ t .Fatalf ("%s: wrong number of examples; got %d want 1" , c .Title , len (es ))
382
+ }
383
+ e := es [0 ]
384
+ if e .Name != "" {
385
+ t .Errorf ("%s: got Name == %q, want %q" , c .Title , e .Name , "" )
386
+ }
387
+ if g , w := formatFile (t , fset , e .Play ), c .Play ; g != w {
388
+ t .Errorf ("%s: got Play == %q, want %q" , c .Title , g , w )
389
+ }
390
+ if g , w := e .Output , c .Output ; g != w {
391
+ t .Errorf ("%s: got Output == %q, want %q" , c .Title , g , w )
392
+ }
353
393
}
354
394
}
355
395
0 commit comments