LEFT | RIGHT |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package testing | 5 package testing |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "fmt" | 9 "fmt" |
10 "io" | 10 "io" |
(...skipping 24 matching lines...) Expand all Loading... |
35 } | 35 } |
36 if !runExample(eg) { | 36 if !runExample(eg) { |
37 ok = false | 37 ok = false |
38 } | 38 } |
39 } | 39 } |
40 | 40 |
41 return | 41 return |
42 } | 42 } |
43 | 43 |
44 // trimLines trims leading and trailing whitespace from all | 44 // trimLines trims leading and trailing whitespace from all |
45 // lines in s and from s itself. A single \n is used to | 45 // lines in s and from s itself. \n is used to separate lines |
46 // separate lines in the returned value. | 46 // in the returned value. |
47 func trimLines(s string) string { | 47 func trimLines(s string) string { |
48 » // trim space from s before trimming individual lines; | 48 » // Trim space from s before trimming individual lines; |
49 » // this handles leading/trailing blank lines. | 49 » // this removes leading and trailing blank lines. |
50 s = strings.TrimSpace(s) | 50 s = strings.TrimSpace(s) |
51 lines := strings.Split(s, "\n") | 51 lines := strings.Split(s, "\n") |
52 trimmed := make([]string, len(lines)) | |
53 for i, l := range lines { | 52 for i, l := range lines { |
54 » » trimmed[i] = strings.TrimSpace(l) | 53 » » lines[i] = strings.TrimSpace(l) |
55 } | 54 } |
56 » return strings.Join(trimmed, "\n") | 55 » return strings.Join(lines, "\n") |
57 } | 56 } |
58 | 57 |
59 func runExample(eg InternalExample) (ok bool) { | 58 func runExample(eg InternalExample) (ok bool) { |
60 if *chatty { | 59 if *chatty { |
61 fmt.Printf("=== RUN: %s\n", eg.Name) | 60 fmt.Printf("=== RUN: %s\n", eg.Name) |
62 } | 61 } |
63 | 62 |
64 // Capture stdout. | 63 // Capture stdout. |
65 stdout := os.Stdout | 64 stdout := os.Stdout |
66 r, w, err := os.Pipe() | 65 r, w, err := os.Pipe() |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 } | 105 } |
107 if err != nil { | 106 if err != nil { |
108 panic(err) | 107 panic(err) |
109 } | 108 } |
110 }() | 109 }() |
111 | 110 |
112 // Run example. | 111 // Run example. |
113 eg.F() | 112 eg.F() |
114 return | 113 return |
115 } | 114 } |
LEFT | RIGHT |