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
5package doc_test
6
7import (
8	"bytes"
9	"go/doc"
10	"go/format"
11	"go/parser"
12	"go/token"
13	"strings"
14	"testing"
15)
16
17const exampleTestFile = `
18package foo_test
19
20import (
21	"flag"
22	"fmt"
23	"log"
24	"os/exec"
25)
26
27func ExampleHello() {
28	fmt.Println("Hello, world!")
29	// Output: Hello, world!
30}
31
32func ExampleImport() {
33	out, err := exec.Command("date").Output()
34	if err != nil {
35		log.Fatal(err)
36	}
37	fmt.Printf("The date is %s\n", out)
38}
39
40func ExampleKeyValue() {
41	v := struct {
42		a string
43		b int
44	}{
45		a: "A",
46		b: 1,
47	}
48	fmt.Print(v)
49	// Output: a: "A", b: 1
50}
51
52func ExampleKeyValueImport() {
53	f := flag.Flag{
54		Name: "play",
55	}
56	fmt.Print(f)
57	// Output: Name: "play"
58}
59
60var keyValueTopDecl = struct {
61	a string
62	b int
63}{
64	a: "B",
65	b: 2,
66}
67
68func ExampleKeyValueTopDecl() {
69	fmt.Print(keyValueTopDecl)
70}
71`
72
73var exampleTestCases = []struct {
74	Name, Play, Output string
75}{
76	{
77		Name:   "Hello",
78		Play:   exampleHelloPlay,
79		Output: "Hello, world!\n",
80	},
81	{
82		Name: "Import",
83		Play: exampleImportPlay,
84	},
85	{
86		Name:   "KeyValue",
87		Play:   exampleKeyValuePlay,
88		Output: "a: \"A\", b: 1\n",
89	},
90	{
91		Name:   "KeyValueImport",
92		Play:   exampleKeyValueImportPlay,
93		Output: "Name: \"play\"\n",
94	},
95	{
96		Name: "KeyValueTopDecl",
97		Play: "<nil>",
98	},
99}
100
101const exampleHelloPlay = `package main
102
103import (
104	"fmt"
105)
106
107func main() {
108	fmt.Println("Hello, world!")
109}
110`
111const exampleImportPlay = `package main
112
113import (
114	"fmt"
115	"log"
116	"os/exec"
117)
118
119func main() {
120	out, err := exec.Command("date").Output()
121	if err != nil {
122		log.Fatal(err)
123	}
124	fmt.Printf("The date is %s\n", out)
125}
126`
127
128const exampleKeyValuePlay = `package main
129
130import (
131	"fmt"
132)
133
134func main() {
135	v := struct {
136		a string
137		b int
138	}{
139		a: "A",
140		b: 1,
141	}
142	fmt.Print(v)
143}
144`
145
146const exampleKeyValueImportPlay = `package main
147
148import (
149	"flag"
150	"fmt"
151)
152
153func main() {
154	f := flag.Flag{
155		Name: "play",
156	}
157	fmt.Print(f)
158}
159`
160
161func TestExamples(t *testing.T) {
162	fset := token.NewFileSet()
163	file, err := parser.ParseFile(fset, "test.go", strings.NewReader(exampleTestFile), parser.ParseComments)
164	if err != nil {
165		t.Fatal(err)
166	}
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)
171		}
172		if w := c.Play; w != "" {
173			var g string // hah
174			if e.Play == nil {
175				g = "<nil>"
176			} else {
177				var buf bytes.Buffer
178				if err := format.Node(&buf, fset, e.Play); err != nil {
179					t.Fatal(err)
180				}
181				g = buf.String()
182			}
183			if g != w {
184				t.Errorf("%s: got Play == %q, want %q", c.Name, g, w)
185			}
186		}
187		if g, w := e.Output, c.Output; g != w {
188			t.Errorf("%s: got Output == %q, want %q", c.Name, g, w)
189		}
190	}
191}
192