Skip to content

Commit

Permalink
cmd/go: run examples in source order, not name order
Browse files Browse the repository at this point in the history
Add Order field to doc.Example and write doc comments there.

Fixes #4662.

R=golang-dev, iant
CC=golang-dev
https://golang.org/cl/7229071
  • Loading branch information
rsc committed Feb 2, 2013
1 parent e5ea2d5 commit 18178fd
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/cmd/go/test.bash
Expand Up @@ -279,6 +279,9 @@ fi
unset GOPATH
rm -rf $d

# Only succeeds if source order is preserved.
./testgo test testdata/example[12]_test.go

# clean up
rm -rf testdata/bin testdata/bin1
rm -f testgo
Expand Down
10 changes: 9 additions & 1 deletion src/cmd/go/test.go
Expand Up @@ -809,7 +809,9 @@ func (t *testFuncs) load(filename, pkg string, seen *bool) error {
*seen = true
}
}
for _, e := range doc.Examples(f) {
ex := doc.Examples(f)
sort.Sort(byOrder(ex))
for _, e := range ex {
if e.Output == "" && !e.EmptyOutput {
// Don't run examples with no output.
continue
Expand All @@ -820,6 +822,12 @@ func (t *testFuncs) load(filename, pkg string, seen *bool) error {
return nil
}

type byOrder []*doc.Example

func (x byOrder) Len() int { return len(x) }
func (x byOrder) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func (x byOrder) Less(i, j int) bool { return x[i].Order < x[j].Order }

var testmainTmpl = template.Must(template.New("main").Parse(`
package main
Expand Down
23 changes: 23 additions & 0 deletions src/cmd/go/testdata/example1_test.go
@@ -0,0 +1,23 @@
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Make sure that go test runs Example_Z before Example_A, preserving source order.

package p

import "fmt"

var n int

func Example_Z() {
n++
fmt.Println(n)
// Output: 1
}

func Example_A() {
n++
fmt.Println(n)
// Output: 2
}
21 changes: 21 additions & 0 deletions src/cmd/go/testdata/example2_test.go
@@ -0,0 +1,21 @@
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Make sure that go test runs Example_Y before Example_B, preserving source order.

package p

import "fmt"

func Example_Y() {
n++
fmt.Println(n)
// Output: 3
}

func Example_B() {
n++
fmt.Println(n)
// Output: 4
}
5 changes: 5 additions & 0 deletions src/pkg/go/doc/example.go
Expand Up @@ -18,6 +18,7 @@ import (
"unicode/utf8"
)

// An Example represents an example function found in a source files.
type Example struct {
Name string // name of the item being exemplified
Doc string // example function doc string
Expand All @@ -26,8 +27,11 @@ type Example struct {
Comments []*ast.CommentGroup
Output string // expected output
EmptyOutput bool // expect empty output
Order int // original source code order
}

// Examples returns the examples found in the files, sorted by Name field.
// The Order fields record the order in which the examples were encountered.
func Examples(files ...*ast.File) []*Example {
var list []*Example
for _, file := range files {
Expand Down Expand Up @@ -65,6 +69,7 @@ func Examples(files ...*ast.File) []*Example {
Comments: file.Comments,
Output: output,
EmptyOutput: output == "" && hasOutput,
Order: len(flist),
})
}
if !hasTests && numDecl > 1 && len(flist) == 1 {
Expand Down

0 comments on commit 18178fd

Please sign in to comment.