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.
18 const exampleTestFile
= `
30 fmt.Println("Hello, world!")
31 // Output: Hello, world!
34 func ExampleImport() {
35 out, err := exec.Command("date").Output()
39 fmt.Printf("The date is %s\n", out)
42 func ExampleKeyValue() {
51 // Output: a: "A", b: 1
54 func ExampleKeyValueImport() {
59 // Output: Name: "play"
62 var keyValueTopDecl = struct {
70 func ExampleKeyValueTopDecl() {
71 fmt.Print(keyValueTopDecl)
72 // Output: a: "B", b: 2
75 // Person represents a person by name and age.
81 // String returns a string representation of the Person.
82 func (p Person) String() string {
83 return fmt.Sprintf("%s: %d", p.Name, p.Age)
86 // ByAge implements sort.Interface for []Person based on
90 // Len returns the number of elements in ByAge.
91 func (a (ByAge)) Len() int { return len(a) }
93 // Swap swaps the elements in ByAge.
94 func (a ByAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
95 func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age }
97 // people is the array of Person
98 var people = []Person{
107 sort.Sort(ByAge(people))
110 // [Bob: 31 John: 42 Michael: 17 Jenny: 26]
111 // [Michael: 17 Jenny: 26 Bob: 31 John: 42]
115 var exampleTestCases
= []struct {
116 Name
, Play
, Output
string
120 Play
: exampleHelloPlay
,
121 Output
: "Hello, world!\n",
125 Play
: exampleImportPlay
,
129 Play
: exampleKeyValuePlay
,
130 Output
: "a: \"A\", b: 1\n",
133 Name
: "KeyValueImport",
134 Play
: exampleKeyValueImportPlay
,
135 Output
: "Name: \"play\"\n",
138 Name
: "KeyValueTopDecl",
139 Play
: exampleKeyValueTopDeclPlay
,
140 Output
: "a: \"B\", b: 2\n",
144 Play
: exampleSortPlay
,
145 Output
: "[Bob: 31 John: 42 Michael: 17 Jenny: 26]\n[Michael: 17 Jenny: 26 Bob: 31 John: 42]\n",
149 const exampleHelloPlay
= `package main
156 fmt.Println("Hello, world!")
159 const exampleImportPlay
= `package main
168 out, err := exec.Command("date").Output()
172 fmt.Printf("The date is %s\n", out)
176 const exampleKeyValuePlay
= `package main
194 const exampleKeyValueImportPlay
= `package main
209 const exampleKeyValueTopDeclPlay
= `package main
215 var keyValueTopDecl = struct {
224 fmt.Print(keyValueTopDecl)
228 const exampleSortPlay
= `package main
235 // Person represents a person by name and age.
241 // String returns a string representation of the Person.
242 func (p Person) String() string {
243 return fmt.Sprintf("%s: %d", p.Name, p.Age)
246 // ByAge implements sort.Interface for []Person based on
250 // Len returns the number of elements in ByAge.
251 func (a ByAge) Len() int { return len(a) }
253 // Swap swaps the elements in ByAge.
254 func (a ByAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
255 func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age }
257 // people is the array of Person
258 var people = []Person{
267 sort.Sort(ByAge(people))
272 func TestExamples(t
*testing
.T
) {
273 fset
:= token
.NewFileSet()
274 file
, err
:= parser
.ParseFile(fset
, "test.go", strings
.NewReader(exampleTestFile
), parser
.ParseComments
)
278 for i
, e
:= range doc
.Examples(file
) {
279 c
:= exampleTestCases
[i
]
280 if e
.Name
!= c
.Name
{
281 t
.Errorf("got Name == %q, want %q", e
.Name
, c
.Name
)
283 if w
:= c
.Play
; w
!= "" {
284 g
:= formatFile(t
, fset
, e
.Play
)
286 t
.Errorf("%s: got Play == %q, want %q", c
.Name
, g
, w
)
289 if g
, w
:= e
.Output
, c
.Output
; g
!= w
{
290 t
.Errorf("%s: got Output == %q, want %q", c
.Name
, g
, w
)
295 const exampleWholeFile
= `package foo_test
302 func (X) TestBlah() {
305 func (X) BenchmarkFoo() {
309 fmt.Println("Hello, world!")
310 // Output: Hello, world!
314 const exampleWholeFileOutput
= `package main
321 func (X) TestBlah() {
324 func (X) BenchmarkFoo() {
328 fmt.Println("Hello, world!")
332 func TestExamplesWholeFile(t
*testing
.T
) {
333 fset
:= token
.NewFileSet()
334 file
, err
:= parser
.ParseFile(fset
, "test.go", strings
.NewReader(exampleWholeFile
), parser
.ParseComments
)
338 es
:= doc
.Examples(file
)
340 t
.Fatalf("wrong number of examples; got %d want 1", len(es
))
344 t
.Errorf("got Name == %q, want %q", e
.Name
, "")
346 if g
, w
:= formatFile(t
, fset
, e
.Play
), exampleWholeFileOutput
; g
!= w
{
347 t
.Errorf("got Play == %q, want %q", g
, w
)
349 if g
, w
:= e
.Output
, "Hello, world!\n"; g
!= w
{
350 t
.Errorf("got Output == %q, want %q", g
, w
)
354 func formatFile(t
*testing
.T
, fset
*token
.FileSet
, n
*ast
.File
) string {
359 if err
:= format
.Node(&buf
, fset
, n
); err
!= nil {