-
Notifications
You must be signed in to change notification settings - Fork 18
/
printer.go
116 lines (102 loc) · 2.66 KB
/
printer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Tideland Go Library - Audit
//
// Copyright (C) 2012-2017 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package audit
//--------------------
// IMPORTS
//--------------------
import (
"fmt"
"reflect"
"sync"
)
//--------------------
// PRINTER
//--------------------
var testNames = []string{
Invalid: "invalid",
True: "true",
False: "false",
Nil: "nil",
NotNil: "not nil",
Equal: "equal",
Different: "different",
Contents: "contents",
About: "about",
Range: "range",
Substring: "substring",
Case: "case",
Match: "match",
ErrorMatch: "error match",
Implementor: "implementor",
Assignable: "assignable",
Unassignable: "unassignable",
Empty: "empty",
NotEmpty: "not empty",
Length: "length",
Panics: "panics",
PathExists: "path exists",
Wait: "wait",
Retry: "retry",
Fail: "fail",
}
func (t Test) String() string {
if int(t) < len(testNames) {
return testNames[t]
}
return "invalid"
}
// ValueDescription returns a description of a value as string.
func ValueDescription(value interface{}) string {
rvalue := reflect.ValueOf(value)
kind := rvalue.Kind()
switch kind {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
return kind.String() + " of " + rvalue.Type().Elem().String()
case reflect.Func:
return kind.String() + " " + rvalue.Type().Name() + "()"
case reflect.Interface, reflect.Struct:
return kind.String() + " " + rvalue.Type().Name()
case reflect.Ptr:
return kind.String() + " to " + rvalue.Type().Elem().String()
}
// Default.
return kind.String()
}
// Printer allows to switch between different outputs.
type Printer interface {
// Printf prints a formatted information.
Printf(format string, args ...interface{})
}
// printerBackend is the globally printer used during
// the assertions.
var (
printerBackend Printer = &fmtPrinter{}
mux sync.Mutex
)
// fmtPrinter uses the standard fmt package for printing.
type fmtPrinter struct{}
// Printf implements the Printer interface.
func (p *fmtPrinter) Printf(format string, args ...interface{}) {
fmt.Printf(format, args...)
}
// SetPrinter sets a new global printer and returns the
// current one.
func SetPrinter(p Printer) Printer {
mux.Lock()
defer mux.Unlock()
cp := printerBackend
printerBackend = p
return cp
}
// backendPrintf uses the printer backend for output. It is used
// in the types below.
func backendPrintf(format string, args ...interface{}) {
mux.Lock()
defer mux.Unlock()
printerBackend.Printf(format, args...)
}
// EOF