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
= `
28 fmt.Println("Hello, world!")
29 // Output: Hello, world!
32 func ExampleImport() {
33 out, err := exec.Command("date").Output()
37 fmt.Printf("The date is %s\n", out)
40 func ExampleKeyValue() {
49 // Output: a: "A", b: 1
52 func ExampleKeyValueImport() {
57 // Output: Name: "play"
60 var keyValueTopDecl = struct {
68 func ExampleKeyValueTopDecl() {
69 fmt.Print(keyValueTopDecl)
73 var exampleTestCases
= []struct {
74 Name
, Play
, Output
string
78 Play
: exampleHelloPlay
,
79 Output
: "Hello, world!\n",
83 Play
: exampleImportPlay
,
87 Play
: exampleKeyValuePlay
,
88 Output
: "a: \"A\", b: 1\n",
91 Name
: "KeyValueImport",
92 Play
: exampleKeyValueImportPlay
,
93 Output
: "Name: \"play\"\n",
96 Name
: "KeyValueTopDecl",
101 const exampleHelloPlay
= `package main
108 fmt.Println("Hello, world!")
111 const exampleImportPlay
= `package main
120 out, err := exec.Command("date").Output()
124 fmt.Printf("The date is %s\n", out)
128 const exampleKeyValuePlay
= `package main
146 const exampleKeyValueImportPlay
= `package main
161 func TestExamples(t
*testing
.T
) {
162 fs
:= token
.NewFileSet()
163 file
, err
:= parser
.ParseFile(fs
, "test.go", strings
.NewReader(exampleTestFile
), parser
.ParseComments
)
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
!= "" {
177 b
:= new(bytes
.Buffer
)
178 if err
:= format
.Node(b
, fs
, e
.Play
); err
!= nil {
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
)