1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
17 const exampleTestFile = `
27 fmt.Println("Hello, world!")
28 // Output: Hello, world!
31 func ExampleImport() {
32 out, err := exec.Command("date").Output()
36 fmt.Printf("The date is %s\n", out)
40 var exampleTestCases = []struct {
41 Name, Play, Output string
45 Play: exampleHelloPlay,
46 Output: "Hello, world!\n",
50 Play: exampleImportPlay,
54 const exampleHelloPlay = `package main
61 fmt.Println("Hello, world!")
64 const exampleImportPlay = `package main
73 out, err := exec.Command("date").Output()
77 fmt.Printf("The date is %s\n", out)
81 func TestExamples(t *testing.T) {
82 fs := token.NewFileSet()
83 file, err := parser.ParseFile(fs, "test.go", strings.NewReader(exampleTestFile), parser.ParseComments)
87 for i, e := range doc.Examples(file) {
88 c := exampleTestCases[i]
90 t.Errorf("got Name == %q, want %q", e.Name, c.Name)
92 if w := c.Play; w != "" {
97 b := new(bytes.Buffer)
98 if err := format.Node(b, fs, e.Play); err != nil {
104 t.Errorf("%s: got Play == %q, want %q", c.Name, g, w)
107 if g, w := e.Output, c.Output; g != w {
108 t.Errorf("%s: got Output == %q, want %q", c.Name, g, w)