-
Notifications
You must be signed in to change notification settings - Fork 2
/
log_test.go
101 lines (84 loc) · 2.12 KB
/
log_test.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
package structsconv
import (
"bytes"
"log"
"os"
"reflect"
"strings"
"testing"
)
func Test_Log_on(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
defer log.SetOutput(os.Stderr)
SetLogWarning(true)
rk := rulesKey{
source: reflect.TypeOf(struct {
A int
}{}),
target: reflect.TypeOf(struct {
B int
}{}),
}
wantContains := "(struct { A int } -> struct { B int }) No mapping found for name 'A'"
logTargetFieldWithoutMappingValueInSource(rk, "A")
if !strings.Contains(buf.String(), wantContains) {
t.Errorf("log.Printf() = %q; want %q", buf.String(), wantContains)
}
}
func Test_logTargetFieldWithoutMappingValueInSource_off(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
defer log.SetOutput(os.Stderr)
logWarning = false
defer func() { logWarning = true }()
rk := rulesKey{
source: reflect.TypeOf(struct {
A int
}{}),
target: reflect.TypeOf(struct {
B int
}{}),
}
wantContains := ""
logTargetFieldWithoutMappingValueInSource(rk, "A")
if !strings.Contains(buf.String(), wantContains) {
t.Errorf("log.Printf() = %q; want %q", buf.String(), wantContains)
}
}
func Test_logIgnoringMappingForIncompatibleTypes_off(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
defer log.SetOutput(os.Stderr)
logWarning = false
defer func() { logWarning = true }()
source := struct {
A int
}{}
target := struct {
B int
}{}
rk := rulesKey{
source: reflect.TypeOf(source),
target: reflect.TypeOf(target),
}
wantContains := ""
logIgnoringMappingForIncompatibleTypes(rk, "A", reflect.ValueOf(source), reflect.ValueOf(target))
if !strings.Contains(buf.String(), wantContains) {
t.Errorf("log.Printf() = %q; want %q", buf.String(), wantContains)
}
}
func Test_logPassingZeroValue_off(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
defer log.SetOutput(os.Stderr)
logWarning = false
defer func() { logWarning = true }()
wantContains := ""
ft := reflect.TypeOf(func(i int) {})
at := reflect.TypeOf(1)
logPassingZeroValue(ft, at, 0)
if !strings.Contains(buf.String(), wantContains) {
t.Errorf("log.Printf() = %q; want %q", buf.String(), wantContains)
}
}