github.com/flyinox/gosm@v0.0.0-20171117061539-16768cb62077/src/go/doc/example_test.go (about) 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 "flag" 22 "fmt" 23 "log" 24 "os/exec" 25 ) 26 27 func ExampleHello() { 28 fmt.Println("Hello, world!") 29 // Output: Hello, world! 30 } 31 32 func 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 40 func 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 52 func ExampleKeyValueImport() { 53 f := flag.Flag{ 54 Name: "play", 55 } 56 fmt.Print(f) 57 // Output: Name: "play" 58 } 59 60 var keyValueTopDecl = struct { 61 a string 62 b int 63 }{ 64 a: "B", 65 b: 2, 66 } 67 68 func ExampleKeyValueTopDecl() { 69 fmt.Print(keyValueTopDecl) 70 } 71 ` 72 73 var 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 101 const exampleHelloPlay = `package main 102 103 import ( 104 "fmt" 105 ) 106 107 func main() { 108 fmt.Println("Hello, world!") 109 } 110 ` 111 const exampleImportPlay = `package main 112 113 import ( 114 "fmt" 115 "log" 116 "os/exec" 117 ) 118 119 func 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 128 const exampleKeyValuePlay = `package main 129 130 import ( 131 "fmt" 132 ) 133 134 func 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 146 const exampleKeyValueImportPlay = `package main 147 148 import ( 149 "flag" 150 "fmt" 151 ) 152 153 func main() { 154 f := flag.Flag{ 155 Name: "play", 156 } 157 fmt.Print(f) 158 } 159 ` 160 161 func 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 }