Imported Upstream version 4.8.1
[platform/upstream/gcc48.git] / libgo / go / go / doc / example_test.go
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.
4
5 package doc_test
6
7 import (
8         "bytes"
9         "go/doc"
10         "go/format"
11         "go/parser"
12         "go/token"
13         "strings"
14         "testing"
15 )
16
17 const exampleTestFile = `
18 package foo_test
19
20 import (
21         "fmt"
22         "log"
23         "os/exec"
24 )
25
26 func ExampleHello() {
27         fmt.Println("Hello, world!")
28         // Output: Hello, world!
29 }
30
31 func ExampleImport() {
32         out, err := exec.Command("date").Output()
33         if err != nil {
34                 log.Fatal(err)
35         }
36         fmt.Printf("The date is %s\n", out)
37 }
38 `
39
40 var exampleTestCases = []struct {
41         Name, Play, Output string
42 }{
43         {
44                 Name:   "Hello",
45                 Play:   exampleHelloPlay,
46                 Output: "Hello, world!\n",
47         },
48         {
49                 Name: "Import",
50                 Play: exampleImportPlay,
51         },
52 }
53
54 const exampleHelloPlay = `package main
55
56 import (
57         "fmt"
58 )
59
60 func main() {
61         fmt.Println("Hello, world!")
62 }
63 `
64 const exampleImportPlay = `package main
65
66 import (
67         "fmt"
68         "log"
69         "os/exec"
70 )
71
72 func main() {
73         out, err := exec.Command("date").Output()
74         if err != nil {
75                 log.Fatal(err)
76         }
77         fmt.Printf("The date is %s\n", out)
78 }
79 `
80
81 func TestExamples(t *testing.T) {
82         fs := token.NewFileSet()
83         file, err := parser.ParseFile(fs, "test.go", strings.NewReader(exampleTestFile), parser.ParseComments)
84         if err != nil {
85                 t.Fatal(err)
86         }
87         for i, e := range doc.Examples(file) {
88                 c := exampleTestCases[i]
89                 if e.Name != c.Name {
90                         t.Errorf("got Name == %q, want %q", e.Name, c.Name)
91                 }
92                 if w := c.Play; w != "" {
93                         var g string // hah
94                         if e.Play == nil {
95                                 g = "<nil>"
96                         } else {
97                                 b := new(bytes.Buffer)
98                                 if err := format.Node(b, fs, e.Play); err != nil {
99                                         t.Fatal(err)
100                                 }
101                                 g = b.String()
102                         }
103                         if g != w {
104                                 t.Errorf("%s: got Play == %q, want %q", c.Name, g, w)
105                         }
106                 }
107                 if g, w := e.Output, c.Output; g != w {
108                         t.Errorf("%s: got Output == %q, want %q", c.Name, g, w)
109                 }
110         }
111 }