mirror of
				https://github.com/golang/go.git
				synced 2025-10-31 16:50:58 +00:00 
			
		
		
		
	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>
This commit is contained in:
		
							parent
							
								
									a9c48f3b03
								
							
						
					
					
						commit
						9323de3da7
					
				
					 5 changed files with 95 additions and 33 deletions
				
			
		|  | @ -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) | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Brady Catherman
						Brady Catherman