aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/go/doc/example_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/pkg/go/doc/example_test.go')
-rw-r--r--src/pkg/go/doc/example_test.go80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/pkg/go/doc/example_test.go b/src/pkg/go/doc/example_test.go
index b70efd93d4..e0477e3f69 100644
--- a/src/pkg/go/doc/example_test.go
+++ b/src/pkg/go/doc/example_test.go
@@ -18,6 +18,7 @@ const exampleTestFile = `
package foo_test
import (
+ "flag"
"fmt"
"log"
"os/exec"
@@ -35,6 +36,38 @@ func ExampleImport() {
}
fmt.Printf("The date is %s\n", out)
}
+
+func ExampleKeyValue() {
+ v := struct {
+ a string
+ b int
+ }{
+ a: "A",
+ b: 1,
+ }
+ fmt.Print(v)
+ // Output: a: "A", b: 1
+}
+
+func ExampleKeyValueImport() {
+ f := flag.Flag{
+ Name: "play",
+ }
+ fmt.Print(f)
+ // Output: Name: "play"
+}
+
+var keyValueTopDecl = struct {
+ a string
+ b int
+}{
+ a: "B",
+ b: 2,
+}
+
+func ExampleKeyValueTopDecl() {
+ fmt.Print(keyValueTopDecl)
+}
`
var exampleTestCases = []struct {
@@ -49,6 +82,20 @@ var exampleTestCases = []struct {
Name: "Import",
Play: exampleImportPlay,
},
+ {
+ Name: "KeyValue",
+ Play: exampleKeyValuePlay,
+ Output: "a: \"A\", b: 1\n",
+ },
+ {
+ Name: "KeyValueImport",
+ Play: exampleKeyValueImportPlay,
+ Output: "Name: \"play\"\n",
+ },
+ {
+ Name: "KeyValueTopDecl",
+ Play: "<nil>",
+ },
}
const exampleHelloPlay = `package main
@@ -78,6 +125,39 @@ func main() {
}
`
+const exampleKeyValuePlay = `package main
+
+import (
+ "fmt"
+)
+
+func main() {
+ v := struct {
+ a string
+ b int
+ }{
+ a: "A",
+ b: 1,
+ }
+ fmt.Print(v)
+}
+`
+
+const exampleKeyValueImportPlay = `package main
+
+import (
+ "flag"
+ "fmt"
+)
+
+func main() {
+ f := flag.Flag{
+ Name: "play",
+ }
+ fmt.Print(f)
+}
+`
+
func TestExamples(t *testing.T) {
fs := token.NewFileSet()
file, err := parser.ParseFile(fs, "test.go", strings.NewReader(exampleTestFile), parser.ParseComments)