-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
93 lines (71 loc) · 2.26 KB
/
main_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
package main
import (
"testing"
)
func TestSplitArgs(t *testing.T) {
// simple test with a filter and a format
filter, format := splitArgs("foo=bar | .baz")
if filter != "foo=bar " || format != ".baz" {
t.Errorf("Got unexpected filter '%s' or format '%s'", filter, format)
}
// just a filter
filter, format = splitArgs("foo=bar")
if filter != "foo=bar" || format != "" {
t.Errorf("Got unexpected filter '%s' or format '%s'", filter, format)
}
// just a format
filter, format = splitArgs(".baz")
if filter != "" || format != ".baz" {
t.Errorf("Got unexpected filter '%s' or format '%s'", filter, format)
}
// multiple filters chained
filter, format = splitArgs("foo=bar | this=that | .baz")
if filter != "foo=bar | this=that " || format != ".baz" {
t.Errorf("Got unexpected filter '%s' or format '%s'", filter, format)
}
// multiple filters unchained
filter, format = splitArgs("foo=bar this=that | .baz")
if filter != "foo=bar this=that " || format != ".baz" {
t.Errorf("Got unexpected filter '%s' or format '%s'", filter, format)
}
// as little whitespace as possible
filter, format = splitArgs("foo=bar this=that|.baz")
if filter != "foo=bar this=that" || format != ".baz" {
t.Errorf("Got unexpected filter '%s' or format '%s'", filter, format)
}
// ambiguous argument
filter, format = splitArgs("test")
if filter != "" || format != "test" {
t.Errorf("Got unexpected filter '%s' or format '%s'", filter, format)
}
// no argument
filter, format = splitArgs("")
if filter != "" || format != "" {
t.Errorf("Got unexpected filter '%s' or format '%s'", filter, format)
}
}
func TestParseLine(t *testing.T) {
// simple line
res := parseLine(`key=value foo=bar baz="longer \"value" other=123`)
expected := map[string]string{
"foo": "bar",
"key": "value",
"baz": "longer \"value",
"other": "123",
}
if len(res) != 4 {
t.Errorf("Got unexpected number of keys: %+v", res)
}
for k, v := range expected {
if rv, ok := res[k]; !ok {
t.Errorf("%s expected to exist in %+v", k, res)
} else if rv != v {
t.Errorf("Value for %s expected to be %s, got %s", k, v, rv)
}
}
// invalid logfmt
res = parseLine(`key="unterminated quote`)
if len(res) != 0 {
t.Errorf("Invalid logfmt gave unexpected result: %+v", res)
}
}