* gcc.dg/guality/guality.exp: Skip on AIX.
[official-gcc.git] / libgo / go / go / doc / example_test.go
blobe0477e3f6930b81d529e770b1304ae3b1391de5d
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.
5 package doc_test
7 import (
8 "bytes"
9 "go/doc"
10 "go/format"
11 "go/parser"
12 "go/token"
13 "strings"
14 "testing"
17 const exampleTestFile = `
18 package foo_test
20 import (
21 "flag"
22 "fmt"
23 "log"
24 "os/exec"
27 func ExampleHello() {
28 fmt.Println("Hello, world!")
29 // Output: Hello, world!
32 func ExampleImport() {
33 out, err := exec.Command("date").Output()
34 if err != nil {
35 log.Fatal(err)
37 fmt.Printf("The date is %s\n", out)
40 func ExampleKeyValue() {
41 v := struct {
42 a string
43 b int
45 a: "A",
46 b: 1,
48 fmt.Print(v)
49 // Output: a: "A", b: 1
52 func ExampleKeyValueImport() {
53 f := flag.Flag{
54 Name: "play",
56 fmt.Print(f)
57 // Output: Name: "play"
60 var keyValueTopDecl = struct {
61 a string
62 b int
64 a: "B",
65 b: 2,
68 func ExampleKeyValueTopDecl() {
69 fmt.Print(keyValueTopDecl)
73 var exampleTestCases = []struct {
74 Name, Play, Output string
77 Name: "Hello",
78 Play: exampleHelloPlay,
79 Output: "Hello, world!\n",
82 Name: "Import",
83 Play: exampleImportPlay,
86 Name: "KeyValue",
87 Play: exampleKeyValuePlay,
88 Output: "a: \"A\", b: 1\n",
91 Name: "KeyValueImport",
92 Play: exampleKeyValueImportPlay,
93 Output: "Name: \"play\"\n",
96 Name: "KeyValueTopDecl",
97 Play: "<nil>",
101 const exampleHelloPlay = `package main
103 import (
104 "fmt"
107 func main() {
108 fmt.Println("Hello, world!")
111 const exampleImportPlay = `package main
113 import (
114 "fmt"
115 "log"
116 "os/exec"
119 func main() {
120 out, err := exec.Command("date").Output()
121 if err != nil {
122 log.Fatal(err)
124 fmt.Printf("The date is %s\n", out)
128 const exampleKeyValuePlay = `package main
130 import (
131 "fmt"
134 func main() {
135 v := struct {
136 a string
137 b int
139 a: "A",
140 b: 1,
142 fmt.Print(v)
146 const exampleKeyValueImportPlay = `package main
148 import (
149 "flag"
150 "fmt"
153 func main() {
154 f := flag.Flag{
155 Name: "play",
157 fmt.Print(f)
161 func TestExamples(t *testing.T) {
162 fs := token.NewFileSet()
163 file, err := parser.ParseFile(fs, "test.go", strings.NewReader(exampleTestFile), parser.ParseComments)
164 if err != nil {
165 t.Fatal(err)
167 for i, e := range doc.Examples(file) {
168 c := exampleTestCases[i]
169 if e.Name != c.Name {
170 t.Errorf("got Name == %q, want %q", e.Name, c.Name)
172 if w := c.Play; w != "" {
173 var g string // hah
174 if e.Play == nil {
175 g = "<nil>"
176 } else {
177 b := new(bytes.Buffer)
178 if err := format.Node(b, fs, e.Play); err != nil {
179 t.Fatal(err)
181 g = b.String()
183 if g != w {
184 t.Errorf("%s: got Play == %q, want %q", c.Name, g, w)
187 if g, w := e.Output, c.Output; g != w {
188 t.Errorf("%s: got Output == %q, want %q", c.Name, g, w)