| 1 |
// Licensed under the MIT license, see LICENSE file for details. |
| 2 |
|
| 3 |
package quicktest |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"fmt" |
| 8 |
"go/ast" |
| 9 |
"go/parser" |
| 10 |
"go/printer" |
| 11 |
"go/token" |
| 12 |
"io" |
| 13 |
"reflect" |
| 14 |
"runtime" |
| 15 |
"strings" |
| 16 |
"testing" |
| 17 |
) |
| 18 |
|
| 19 |
// reportParams holds parameters for reporting a test error. |
| 20 |
type reportParams struct { |
| 21 |
// argNames holds the names for the arguments passed to the checker. |
| 22 |
argNames []string |
| 23 |
// got holds the value that was checked. |
| 24 |
got interface{} |
| 25 |
// args holds all other arguments (if any) provided to the checker. |
| 26 |
args []interface{} |
| 27 |
// comment optionally holds the comment passed when performing the check. |
| 28 |
comments []Comment |
| 29 |
// notes holds notes added while doing the check. |
| 30 |
notes []note |
| 31 |
// format holds the format function that must be used when outputting |
| 32 |
// values. |
| 33 |
format formatFunc |
| 34 |
} |
| 35 |
|
| 36 |
// Unquoted indicates that the string must not be pretty printed in the failure |
| 37 |
// output. This is useful when a checker calls note and does not want the |
| 38 |
// provided value to be quoted. |
| 39 |
type Unquoted string |
| 40 |
|
| 41 |
// SuppressedIfLong indicates that the value must be suppressed if verbose |
| 42 |
// testing is off and the pretty printed version of the value is long. This is |
| 43 |
// useful when a checker calls note and does not want the provided value to be |
| 44 |
// printed in non-verbose test runs if the value is too long. |
| 45 |
type SuppressedIfLong struct { |
| 46 |
// Value holds the original annotated value. |
| 47 |
Value interface{} |
| 48 |
} |
| 49 |
|
| 50 |
// longValueLines holds the number of lines after which a value is long. |
| 51 |
const longValueLines = 10 |
| 52 |
|
| 53 |
// report generates a failure report for the given error, optionally including |
| 54 |
// in the output the checker arguments, comment and notes included in the |
| 55 |
// provided report parameters. |
| 56 |
func report(err error, p reportParams) string { |
| 57 |
var buf bytes.Buffer |
| 58 |
buf.WriteByte('\n') |
| 59 |
writeError(&buf, err, p) |
| 60 |
writeStack(&buf) |
| 61 |
return buf.String() |
| 62 |
} |
| 63 |
|
| 64 |
// writeError writes a pretty formatted output of the given error using the |
| 65 |
// provided report parameters. |
| 66 |
func writeError(w io.Writer, err error, p reportParams) { |
| 67 |
ptrs := make(map[string]interface{}) |
| 68 |
values := make(map[string]string) |
| 69 |
|
| 70 |
printPair := func(key string, value interface{}) { |
| 71 |
fmt.Fprintln(w, key+":") |
| 72 |
var v string |
| 73 |
|
| 74 |
if u, ok := value.(Unquoted); ok { |
| 75 |
// Output the raw string without quotes. |
| 76 |
v = string(u) |
| 77 |
} else if s, ok := value.(SuppressedIfLong); ok { |
| 78 |
// Check whether the output is too long and must be suppressed. |
| 79 |
v = p.format(s.Value) |
| 80 |
if !testingVerbose() { |
| 81 |
if n := strings.Count(v, "\n"); n > longValueLines { |
| 82 |
fmt.Fprint(w, prefixf(prefix, "<suppressed due to length (%d lines), use -v for full output>", n)) |
| 83 |
return |
| 84 |
} |
| 85 |
} |
| 86 |
} else { |
| 87 |
// Check whether the output has been already seen. |
| 88 |
v = p.format(value) |
| 89 |
isPtr := reflect.ValueOf(value).Kind() == reflect.Ptr |
| 90 |
if k := values[v]; k != "" { |
| 91 |
if previousValue, ok := ptrs[k]; ok && isPtr && previousValue != value { |
| 92 |
fmt.Fprint(w, prefixf(prefix, "<same as %q but different pointer value>", k)) |
| 93 |
return |
| 94 |
} |
| 95 |
fmt.Fprint(w, prefixf(prefix, "<same as %q>", k)) |
| 96 |
return |
| 97 |
} |
| 98 |
if isPtr { |
| 99 |
ptrs[key] = value |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
values[v] = key |
| 104 |
fmt.Fprint(w, prefixf(prefix, "%s", v)) |
| 105 |
} |
| 106 |
|
| 107 |
// Write the checker error. |
| 108 |
if err != ErrSilent { |
| 109 |
printPair("error", Unquoted(err.Error())) |
| 110 |
} |
| 111 |
|
| 112 |
// Write comments if provided. |
| 113 |
for _, c := range p.comments { |
| 114 |
if comment := c.String(); comment != "" { |
| 115 |
printPair("comment", Unquoted(comment)) |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// Write notes if present. |
| 120 |
for _, n := range p.notes { |
| 121 |
printPair(n.key, n.value) |
| 122 |
} |
| 123 |
if IsBadCheck(err) || err == ErrSilent { |
| 124 |
// For errors in the checker invocation or for silent errors, do not |
| 125 |
// show output from args. |
| 126 |
return |
| 127 |
} |
| 128 |
|
| 129 |
// Write provided args. |
| 130 |
for i, arg := range append([]interface{}{p.got}, p.args...) { |
| 131 |
printPair(p.argNames[i], arg) |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
// testingVerbose is defined as a variable for testing. |
| 136 |
var testingVerbose = func() bool { |
| 137 |
return testing.Verbose() |
| 138 |
} |
| 139 |
|
| 140 |
// writeStack writes the traceback information for the current failure into the |
| 141 |
// provided writer. |
| 142 |
func writeStack(w io.Writer) { |
| 143 |
fmt.Fprintln(w, "stack:") |
| 144 |
pc := make([]uintptr, 8) |
| 145 |
sg := &stmtGetter{ |
| 146 |
fset: token.NewFileSet(), |
| 147 |
files: make(map[string]*ast.File, 8), |
| 148 |
config: &printer.Config{ |
| 149 |
Mode: printer.UseSpaces, |
| 150 |
Tabwidth: 4, |
| 151 |
}, |
| 152 |
} |
| 153 |
runtime.Callers(5, pc) |
| 154 |
frames := runtime.CallersFrames(pc) |
| 155 |
thisPackage := reflect.TypeOf(C{}).PkgPath() + "." |
| 156 |
for { |
| 157 |
frame, more := frames.Next() |
| 158 |
if strings.HasPrefix(frame.Function, "testing.") { |
| 159 |
// Stop before getting back to stdlib test runner calls. |
| 160 |
break |
| 161 |
} |
| 162 |
if fname := strings.TrimPrefix(frame.Function, thisPackage); fname != frame.Function { |
| 163 |
if ast.IsExported(fname) { |
| 164 |
// Continue without printing frames for quicktest exported API. |
| 165 |
continue |
| 166 |
} |
| 167 |
// Stop when entering quicktest internal calls. |
| 168 |
// This is useful for instance when using qtsuite. |
| 169 |
break |
| 170 |
} |
| 171 |
fmt.Fprint(w, prefixf(prefix, "%s:%d", frame.File, frame.Line)) |
| 172 |
if strings.HasSuffix(frame.File, ".go") { |
| 173 |
stmt, err := sg.Get(frame.File, frame.Line) |
| 174 |
if err != nil { |
| 175 |
fmt.Fprint(w, prefixf(prefix+prefix, "<%s>", err)) |
| 176 |
} else { |
| 177 |
fmt.Fprint(w, prefixf(prefix+prefix, "%s", stmt)) |
| 178 |
} |
| 179 |
} |
| 180 |
if !more { |
| 181 |
// There are no more callers. |
| 182 |
break |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
type stmtGetter struct { |
| 188 |
fset *token.FileSet |
| 189 |
files map[string]*ast.File |
| 190 |
config *printer.Config |
| 191 |
} |
| 192 |
|
| 193 |
// Get returns the lines of code of the statement at the given file and line. |
| 194 |
func (sg *stmtGetter) Get(file string, line int) (string, error) { |
| 195 |
f := sg.files[file] |
| 196 |
if f == nil { |
| 197 |
var err error |
| 198 |
f, err = parser.ParseFile(sg.fset, file, nil, parser.ParseComments) |
| 199 |
if err != nil { |
| 200 |
return "", fmt.Errorf("cannot parse source file: %s", err) |
| 201 |
} |
| 202 |
sg.files[file] = f |
| 203 |
} |
| 204 |
var stmt string |
| 205 |
ast.Inspect(f, func(n ast.Node) bool { |
| 206 |
if n == nil || stmt != "" { |
| 207 |
return false |
| 208 |
} |
| 209 |
pos := sg.fset.Position(n.Pos()).Line |
| 210 |
end := sg.fset.Position(n.End()).Line |
| 211 |
// Go < v1.9 reports the line where the statements ends, not the line |
| 212 |
// where it begins. |
| 213 |
if line == pos || line == end { |
| 214 |
var buf bytes.Buffer |
| 215 |
// TODO: include possible comment after the statement. |
| 216 |
sg.config.Fprint(&buf, sg.fset, &printer.CommentedNode{ |
| 217 |
Node: n, |
| 218 |
Comments: f.Comments, |
| 219 |
}) |
| 220 |
stmt = buf.String() |
| 221 |
return false |
| 222 |
} |
| 223 |
return pos < line && line <= end |
| 224 |
}) |
| 225 |
return stmt, nil |
| 226 |
} |
| 227 |
|
| 228 |
// prefixf formats the given string with the given args. It also inserts the |
| 229 |
// final newline if needed and indentation with the given prefix. |
| 230 |
func prefixf(prefix, format string, args ...interface{}) string { |
| 231 |
var buf []byte |
| 232 |
s := strings.TrimSuffix(fmt.Sprintf(format, args...), "\n") |
| 233 |
for _, line := range strings.Split(s, "\n") { |
| 234 |
buf = append(buf, prefix...) |
| 235 |
buf = append(buf, line...) |
| 236 |
buf = append(buf, '\n') |
| 237 |
} |
| 238 |
return string(buf) |
| 239 |
} |
| 240 |
|
| 241 |
// note holds a key/value annotation. |
| 242 |
type note struct { |
| 243 |
key string |
| 244 |
value interface{} |
| 245 |
} |
| 246 |
|
| 247 |
// prefix is the string used to indent blocks of output. |
| 248 |
const prefix = " " |