-
Notifications
You must be signed in to change notification settings - Fork 3
/
fields_test.go
76 lines (64 loc) · 2.11 KB
/
fields_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
package logger_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/gol4ng/logger"
)
func TestFields_Add(t *testing.T) {
fields := logger.Fields{}
fields.
Add("my_key", "my_value").
Add("my_key", int8(2))
assert.Equal(t, "my_value", fields[0].Value)
assert.Equal(t, int8(2), fields[1].Value)
}
func TestFields_Skip(t *testing.T) {
fields := logger.Fields{}
fields.Skip("my_key", "my_value")
assert.Equal(t, "my_value", fields[0].Value)
assert.Equal(t, logger.SkipType, fields[0].Type)
}
func TestFields_Binary(t *testing.T) {
fields := logger.Fields{}
fields.Binary("my_key", []byte{1, 2, 3})
assert.Equal(t, []byte{1, 2, 3}, fields[0].Value)
assert.Equal(t, logger.BinaryType, fields[0].Type)
}
func TestFields_ByteString(t *testing.T) {
fields := logger.Fields{}
fields.ByteString("my_key", []byte("my_value"))
assert.Equal(t, []byte("my_value"), fields[0].Value)
assert.Equal(t, logger.ByteStringType, fields[0].Type)
}
func TestFields_String(t *testing.T) {
tests := []struct {
name string
fields *logger.Fields
string string
}{
{name: "Empty fields", fields: &logger.Fields{}, string: " "},
{name: "Simple fields", fields: logger.NewFields("my_key", "my_value"), string: "<my_key:my_value>"},
{name: "Simple fields", fields: logger.NewFields("my_key", "my_value").Add("my_int_val", 3), string: "<my_key:my_value> <my_int_val:3>"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.string, tt.fields.String())
})
}
}
func TestFields_GoString(t *testing.T) {
tests := []struct {
name string
fields *logger.Fields
goString string
}{
{name: "Empty fields", fields: &logger.Fields{}, goString: "logger.Fields[ ]"},
{name: "Simple fields", fields: logger.NewFields("my_key", "my_value"), goString: "logger.Fields[<my_key:my_value>]"},
{name: "Simple fields", fields: logger.NewFields("my_key", "my_value").Add("my_int_val", 3), goString: "logger.Fields[<my_key:my_value> <my_int_val:3>]"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.goString, tt.fields.GoString())
})
}
}