aboutsummaryrefslogtreecommitdiff
path: root/src/testing
diff options
context:
space:
mode:
authorBrady Catherman <brady@gmail.com>2016-02-05 14:16:31 -0700
committerAndrew Gerrand <adg@golang.org>2016-03-09 04:34:41 +0000
commit9323de3da79d240b8223f74bd8ddd0d55dab8070 (patch)
tree4b08ed7e11eaaff51fec3f262dbbdaf6b3969cdc /src/testing
parenta9c48f3b031c2dc3a3a0aa47987137e1fdf76024 (diff)
downloadgo-9323de3da79d240b8223f74bd8ddd0d55dab8070.tar.xz
testing: implement 'Unordered Output' in Examples.
Adds a type of output to Examples that allows tests to have unordered output. This is intended to help clarify when the output of a command will produce a fixed return, but that return might not be in an constant order. Examples where this is useful would be documenting the rand.Perm() call, or perhaps the (os.File).Readdir(), both of which can not guarantee order, but can guarantee the elements of the output. Fixes #10149 Change-Id: Iaf0cf1580b686afebd79718ed67ea744f5ed9fc5 Reviewed-on: https://go-review.googlesource.com/19280 Reviewed-by: Andrew Gerrand <adg@golang.org>
Diffstat (limited to 'src/testing')
-rw-r--r--src/testing/example.go26
1 files changed, 21 insertions, 5 deletions
diff --git a/src/testing/example.go b/src/testing/example.go
index 30baf27030..fd8343f3bf 100644
--- a/src/testing/example.go
+++ b/src/testing/example.go
@@ -9,14 +9,16 @@ import (
"fmt"
"io"
"os"
+ "sort"
"strings"
"time"
)
type InternalExample struct {
- Name string
- F func()
- Output string
+ Name string
+ F func()
+ Output string
+ Unordered bool
}
func RunExamples(matchString func(pat, str string) (bool, error), examples []InternalExample) (ok bool) {
@@ -41,6 +43,12 @@ func RunExamples(matchString func(pat, str string) (bool, error), examples []Int
return
}
+func sortLines(output string) string {
+ lines := strings.Split(output, "\n")
+ sort.Strings(lines)
+ return strings.Join(lines, "\n")
+}
+
func runExample(eg InternalExample) (ok bool) {
if *chatty {
fmt.Printf("=== RUN %s\n", eg.Name)
@@ -80,8 +88,16 @@ func runExample(eg InternalExample) (ok bool) {
var fail string
err := recover()
- if g, e := strings.TrimSpace(out), strings.TrimSpace(eg.Output); g != e && err == nil {
- fail = fmt.Sprintf("got:\n%s\nwant:\n%s\n", g, e)
+ got := strings.TrimSpace(out)
+ want := strings.TrimSpace(eg.Output)
+ if eg.Unordered {
+ if sortLines(got) != sortLines(want) && err == nil {
+ fail = fmt.Sprintf("got:\n%s\nwant (unordered):\n%s\n", out, eg.Output)
+ }
+ } else {
+ if got != want && err == nil {
+ fail = fmt.Sprintf("got:\n%s\nwant:\n%s\n", got, want)
+ }
}
if fail != "" || err != nil {
fmt.Printf("--- FAIL: %s (%s)\n%s", eg.Name, dstr, fail)