@@ -30,67 +30,89 @@ func TestDefaultWith(t *testing.T) {
30
30
}
31
31
}
32
32
33
- // NOTE TO REVIEWER: Ignore this test. The next CL revamps it.
34
- func TestCommonHandle (t * testing.T ) {
35
- tm := time .Date (2022 , 9 , 18 , 8 , 26 , 33 , 0 , time .UTC )
36
- r := NewRecord (tm , InfoLevel , "message" , 1 )
37
- r .AddAttrs (String ("a" , "one" ), Int ("b" , 2 ), Any ("" , "ignore me" ))
38
-
39
- newHandler := func (replace func (Attr ) Attr ) * commonHandler {
40
- return & commonHandler {
41
- app : textAppender {},
42
- attrSep : ' ' ,
43
- opts : HandlerOptions {ReplaceAttr : replace },
44
- }
45
- }
46
-
33
+ // Verify the common parts of TextHandler and JSONHandler.
34
+ func TestJSONAndTextHandlers (t * testing.T ) {
47
35
removeAttr := func (a Attr ) Attr { return Attr {} }
48
36
37
+ attrs := []Attr {String ("a" , "one" ), Int ("b" , 2 ), Any ("" , "ignore me" )}
38
+ preAttrs := []Attr {Int ("pre" , 3 ), String ("x" , "y" )}
39
+
49
40
for _ , test := range []struct {
50
- name string
51
- h * commonHandler
52
- want string
41
+ name string
42
+ replace func (Attr ) Attr
43
+ preAttrs []Attr
44
+ attrs []Attr
45
+ wantText string
46
+ wantJSON string
53
47
}{
54
48
{
55
- name : "basic" ,
56
- h : newHandler (nil ),
57
- want : "time=2022-09-18T08:26:33.000Z level=INFO msg=message a=one b=2" ,
49
+ name : "basic" ,
50
+ attrs : attrs ,
51
+ wantText : "time=2000-01-02T03:04:05.000Z level=INFO msg=message a=one b=2" ,
52
+ wantJSON : `{"time":"2000-01-02T03:04:05Z","level":"INFO","msg":"message","a":"one","b":2}` ,
58
53
},
59
54
{
60
- name : "cap keys" ,
61
- h : newHandler (upperCaseKey ),
62
- want : "TIME=2022-09-18T08:26:33.000Z LEVEL=INFO MSG=message A=one B=2" ,
55
+ name : "cap keys" ,
56
+ replace : upperCaseKey ,
57
+ attrs : attrs ,
58
+ wantText : "TIME=2000-01-02T03:04:05.000Z LEVEL=INFO MSG=message A=one B=2" ,
59
+ wantJSON : `{"TIME":"2000-01-02T03:04:05Z","LEVEL":"INFO","MSG":"message","A":"one","B":2}` ,
63
60
},
64
61
{
65
- name : "remove all" ,
66
- h : newHandler (removeAttr ),
67
- want : "" ,
62
+ name : "remove all" ,
63
+ replace : removeAttr ,
64
+ attrs : attrs ,
65
+ wantText : "" ,
66
+ wantJSON : `{}` ,
68
67
},
69
68
{
70
- name : "preformatted" ,
71
- h : newHandler (nil ).with ([]Attr {Int ("pre" , 3 ), String ("x" , "y" )}),
72
- want : "time=2022-09-18T08:26:33.000Z level=INFO msg=message pre=3 x=y a=one b=2" ,
69
+ name : "preformatted" ,
70
+ preAttrs : preAttrs ,
71
+ attrs : attrs ,
72
+ wantText : "time=2000-01-02T03:04:05.000Z level=INFO msg=message pre=3 x=y a=one b=2" ,
73
+ wantJSON : `{"time":"2000-01-02T03:04:05Z","level":"INFO","msg":"message","pre":3,"x":"y","a":"one","b":2}` ,
73
74
},
74
75
{
75
- name : "preformatted cap keys" ,
76
- h : newHandler (upperCaseKey ).with ([]Attr {Int ("pre" , 3 ), String ("x" , "y" )}),
77
- want : "TIME=2022-09-18T08:26:33.000Z LEVEL=INFO MSG=message PRE=3 X=y A=one B=2" ,
76
+ name : "preformatted cap keys" ,
77
+ replace : upperCaseKey ,
78
+ preAttrs : preAttrs ,
79
+ attrs : attrs ,
80
+ wantText : "TIME=2000-01-02T03:04:05.000Z LEVEL=INFO MSG=message PRE=3 X=y A=one B=2" ,
81
+ wantJSON : `{"TIME":"2000-01-02T03:04:05Z","LEVEL":"INFO","MSG":"message","PRE":3,"X":"y","A":"one","B":2}` ,
78
82
},
79
83
{
80
- name : "preformatted remove all" ,
81
- h : newHandler (removeAttr ).with ([]Attr {Int ("pre" , 3 ), String ("x" , "y" )}),
82
- want : "" ,
84
+ name : "preformatted remove all" ,
85
+ replace : removeAttr ,
86
+ preAttrs : preAttrs ,
87
+ attrs : attrs ,
88
+ wantText : "" ,
89
+ wantJSON : "{}" ,
83
90
},
84
91
} {
92
+ r := NewRecord (testTime , InfoLevel , "message" , 1 )
93
+ r .AddAttrs (test .attrs ... )
94
+ var buf bytes.Buffer
95
+ opts := HandlerOptions {ReplaceAttr : test .replace }
85
96
t .Run (test .name , func (t * testing.T ) {
86
- var buf bytes.Buffer
87
- test .h .w = & buf
88
- if err := test .h .handle (r ); err != nil {
89
- t .Fatal (err )
90
- }
91
- got := strings .TrimSuffix (buf .String (), "\n " )
92
- if got != test .want {
93
- t .Errorf ("\n got %#v\n want %#v\n " , got , test .want )
97
+ for _ , handler := range []struct {
98
+ name string
99
+ h Handler
100
+ want string
101
+ }{
102
+ {"text" , opts .NewTextHandler (& buf ), test .wantText },
103
+ {"json" , opts .NewJSONHandler (& buf ), test .wantJSON },
104
+ } {
105
+ t .Run (handler .name , func (t * testing.T ) {
106
+ h := handler .h .With (test .preAttrs )
107
+ buf .Reset ()
108
+ if err := h .Handle (r ); err != nil {
109
+ t .Fatal (err )
110
+ }
111
+ got := strings .TrimSuffix (buf .String (), "\n " )
112
+ if got != handler .want {
113
+ t .Errorf ("\n got %#v\n want %#v\n " , got , handler .want )
114
+ }
115
+ })
94
116
}
95
117
})
96
118
}
0 commit comments