-
Notifications
You must be signed in to change notification settings - Fork 0
/
special_test.go
77 lines (73 loc) · 1.94 KB
/
special_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
package log
import (
"bufio"
"bytes"
"math"
"testing"
)
func Test_LogS_JSON_NaN(t *testing.T) {
// Setup
var b bytes.Buffer
w := bufio.NewWriter(&b)
SetLogLevel(LevelByName("Verbose"))
Config.LogFileAndLine = false
Config.JSON = true
Config.NoTimestamp = true
SetOutput(w)
// Start of the actual test
value := math.NaN()
zero := 0.0
S(Verbose, "Test NaN", Any("nan", value), Any("minus-inf", -1.0/zero))
_ = w.Flush()
actual := b.String()
// Note that we serialize that way but can't deserialize with go default json unmarshaller
expected := `{"level":"trace","msg":"Test NaN","nan":NaN,"minus-inf":-Inf}` + "\n"
if actual != expected {
t.Errorf("unexpected:\n%s\nvs:\n%s\n", actual, expected)
}
}
func Test_LogS_JSON_Array(t *testing.T) {
// Setup
var b bytes.Buffer
w := bufio.NewWriter(&b)
SetLogLevel(LevelByName("Verbose"))
Config.LogFileAndLine = false
Config.JSON = true
Config.NoTimestamp = true
SetOutput(w)
// Start of the actual test
S(Verbose, "Test Array", Any("arr", []interface{}{"x", 42, "y"}))
_ = w.Flush()
actual := b.String()
expected := `{"level":"trace","msg":"Test Array","arr":["x",42,"y"]}` + "\n"
if actual != expected {
t.Errorf("unexpected:\n%s\nvs:\n%s\n", actual, expected)
}
}
func Test_LogS_JSON_Map(t *testing.T) {
// Setup
var b bytes.Buffer
w := bufio.NewWriter(&b)
SetLogLevel(LevelByName("Verbose"))
Config.LogFileAndLine = false
Config.JSON = true
Config.NoTimestamp = true
SetOutput(w)
// Start of the actual test
tst := map[string]interface{}{
"str1": "val 1",
"subArray": []interface{}{
"x", 42, "y",
},
"number": 3.14,
}
S(Verbose, "Test Map", Any("map", tst), Int64("in64", 0), Bool("bool", true))
_ = w.Flush()
actual := b.String()
//nolint:lll
expected := `{"level":"trace","msg":"Test Map","map":{"number":3.14,"str1":"val 1","subArray":["x",42,"y"]},"in64":0,"bool":true}` +
"\n"
if actual != expected {
t.Errorf("unexpected:\n%s\nvs:\n%s\n", actual, expected)
}
}