-
Notifications
You must be signed in to change notification settings - Fork 1
/
addrfmt_test.go
98 lines (90 loc) · 1.99 KB
/
addrfmt_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
package addrfmt_test
import (
"bytes"
"fmt"
"testing"
"github.com/LUSHDigital/addrfmt"
)
func Test_Line(t *testing.T) {
line := addrfmt.Line([2]string{"a", "b"})
equals(t, "a", line.Type())
equals(t, "b", line.Text())
}
func Test_Lines_Exists(t *testing.T) {
lines := addrfmt.Lines([][2]string{
{"a", "a"},
{"b", "b"},
})
err := lines.Exists("a", "b")
equals(t, nil, err)
err = lines.Exists("c")
if err == nil {
t.Error("should be an error")
}
}
func Test_Lines_Line(t *testing.T) {
lines := addrfmt.Lines([][2]string{
{"a", "a"},
{"b", "b"},
})
_, err := lines.Line("a")
equals(t, nil, err)
_, err = lines.Line("c")
if err == nil {
t.Error("should be an error")
}
if err.Error() == "" {
t.Error("error should have content")
}
}
func Test_Lines_Text(t *testing.T) {
lines := addrfmt.Lines([][2]string{
{"a", "a"},
{"b", "b"},
})
s := lines.Text("a")
equals(t, "a", s)
s = lines.Text("c")
equals(t, "", s)
}
func Test_Lines_Render(t *testing.T) {
lines := addrfmt.Lines([][2]string{
{"a", "a"},
{"b", "b"},
})
var buf bytes.Buffer
err := lines.Render(&buf, `{{ txt "a" }}{{ txt "c" }}{{ text "b" }}`, nil)
equals(t, nil, err)
equals(t, "ab", buf.String())
buf.Reset()
err = lines.Render(&buf, `txt "a" }}{{ txt "c" }}{{ text "b"`, nil)
if err == nil {
t.Error("should be an error")
}
err = lines.Render(&buf, `{{ hello "a" }}{{ txt "c" }}{{ text "b" }}`, map[string]interface{}{
"hello": func(s string) string {
return fmt.Sprintf("hello %s", s)
},
"txt": func(s string) string {
if s == "" {
return "c"
}
return s
},
})
equals(t, nil, err)
equals(t, "hello acb", buf.String())
}
func Test_Lines_Template(t *testing.T) {
lines := addrfmt.Lines([][2]string{})
tmpl := lines.Template()
if tmpl == nil {
t.Error("new template should not be nil")
}
}
func equals(tb testing.TB, expected, actual interface{}) {
tb.Helper()
if expected != actual {
tb.Fatalf("\n\texp: %#[1]v (%[1]T)\n\tgot: %#[2]v (%[2]T)\n", expected, actual)
}
}