-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatting_test.go
136 lines (109 loc) · 2.77 KB
/
formatting_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"bytes"
"io"
"os"
"testing"
"text/template"
)
func TestReadFormat(t *testing.T) {
var buf bytes.Buffer
testData := map[string]string{
"msg": "it worked",
"test": "test",
"example": "foobar",
}
// simple single-argument format
if err := readFormat(".msg").Execute(&buf, testData); err != nil {
t.Errorf("Error executing simple template: %v", err)
}
if buf.String() != "it worked" {
t.Errorf("Simple test got %s", buf.String())
}
buf.Reset()
// two-argument format
if err := readFormat(".msg .example").Execute(&buf, testData); err != nil {
t.Errorf("Error executing double template: %v", err)
}
if buf.String() != "it worked foobar" {
t.Errorf("Double template got %s", buf.String())
}
buf.Reset()
// leading and trailing characters
if err := readFormat("[.msg] (.test) <.example>").Execute(&buf, testData); err != nil {
t.Errorf("Error executing template: %v", err)
}
if buf.String() != "[it worked] (test) <foobar>" {
t.Errorf("Leading and trialing characters got %s", buf.String())
}
buf.Reset()
// missing keys
if err := readFormat("(.missing) .msg").Execute(&buf, testData); err != nil {
t.Errorf("Error executing missing keys template: %v", err)
}
if buf.String() != "() it worked" {
t.Errorf("Missing keys test got %s", buf.String())
}
buf.Reset()
// chained dots
if err := readFormat(".msg.example").Execute(&buf, testData); err != nil {
t.Errorf("Error executing trailing dots template: %v", err)
}
if buf.String() != "it worked.example" {
t.Errorf("Trailing dots test got %s", buf.String())
}
buf.Reset()
}
// captureStdout returns the stdout of the evalued function
func captureStdout(f func() error) (string, error) {
orig := os.Stdout
out, in, _ := os.Pipe()
os.Stdout = in
err := f()
if err != nil {
return "", err
}
os.Stdout = orig
in.Close()
res, err := io.ReadAll(out)
if err != nil {
return "", err
}
return string(res), nil
}
func TestFormatLine(t *testing.T) {
testData := map[string]string{
"msg": "it worked",
"key": "value",
}
tem, err := template.New("test").Parse("{{.msg}}")
if err != nil {
t.Errorf("Failed to parse test template: %v", err)
}
if res, err := captureStdout(
func() error {
formatLine(testData, tem)
return nil
},
); err != nil {
t.Errorf("Error running formatLine: %v", err)
} else if res != "it worked\n" {
t.Errorf("formatLine got unexpected result: '%s'", res)
}
}
func TestDump(t *testing.T) {
testData := map[string]string{
"msg": "it worked",
"key": "value",
}
if res, err := captureStdout(
func() error {
dump(testData)
return nil
},
); err != nil {
t.Errorf("Error running formatLine: %v", err)
} else if res != `key=value msg="it worked"`+"\n" {
t.Errorf("dump got unexpected result: '%s'", res)
}
}