-
Notifications
You must be signed in to change notification settings - Fork 8
/
fmt_test.go
123 lines (96 loc) · 2.89 KB
/
fmt_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
package format
import (
"fmt"
"reflect"
"testing"
)
func TestReformattingSingleNamedPattern(t *testing.T) {
pat := "%<name_me>x"
f, n := reformat(pat)
if f != "%x" {
t.Errorf("pattern should be %%x but %v", f)
}
if !reflect.DeepEqual(n, []string{"name_me"}) {
t.Errorf("named var should be {name_me} but %v", n)
}
}
func TestReformattingMultipleNamedPattern(t *testing.T) {
pat := "%<name_me>x and %<another_name>v"
f, n := reformat(pat)
if f != "%x and %v" {
t.Errorf("pattern should be %%x and %%v but %v", f)
}
if !reflect.DeepEqual(n, []string{"name_me", "another_name"}) {
t.Errorf("named var should be {name_me, another_name} but %v", n)
}
}
func TestReformattingRepeatedNamedPattern(t *testing.T) {
pat := "%<name_me>x and %<another_name>v and %<name_me>v"
f, n := reformat(pat)
if f != "%x and %v and %v" {
t.Errorf("pattern should be %%x and %%v and %%v but %v", f)
}
if !reflect.DeepEqual(n, []string{"name_me", "another_name", "name_me"}) {
t.Errorf("named var should be {name_me, another_name, name_me} but %v", n)
}
}
func TestSprintf(t *testing.T) {
pat := "%<brother>s loves %<sister>s. %<sister>s also loves %<brother>s."
params := map[string]interface{}{
"sister": "Susan",
"brother": "Louis",
}
s := Sprintf(pat, params)
if s != "Louis loves Susan. Susan also loves Louis." {
t.Errorf("result should be Louis loves Susan. Susan also love Louis. but %v", s)
}
}
func TestSprintfStruct(t *testing.T) {
pat := "%<Brother>s loves %<Sister>s. %<Sister>s also loves %<Brother>s."
params := struct {
Brother string
Sister string
}{
Brother: "Louis",
Sister: "Susan",
}
s := Sprintf(pat, params)
if s != "Louis loves Susan. Susan also loves Louis." {
t.Errorf("result should be Louis loves Susan. Susan also love Louis. but %v", s)
}
}
func TestSprintfln(t *testing.T) {
pat := "%<brother>s loves %<sister>s. %<sister>s also loves %<brother>s."
params := map[string]interface{}{
"sister": "Susan",
"brother": "Louis",
}
s := Sprintfln(pat, params)
if s != "Louis loves Susan. Susan also loves Louis."+fmt.Sprintln() {
t.Errorf("result should be Louis loves Susan. Susan also love Louis"+fmt.Sprintln()+". but %v", s)
}
}
func TestSprintfFloatsWithPrecision(t *testing.T) {
pat := "%<float>f / %<floatprecision>.1f / %<long>g / %<longprecision>.3g"
params := map[string]interface{}{
"float": 5.034560,
"floatprecision": 5.03456,
"long": 5.03456,
"longprecision": 5.03456,
}
s := Sprintf(pat, params)
expectedresult := "5.034560 / 5.0 / 5.03456 / 5.03"
if s != expectedresult {
t.Errorf("result should be (%v) but is (%v)", expectedresult, s)
}
}
func BenchmarkSprintln(b *testing.B) {
pat := "%<brother>s loves %<sister>s. %<sister>s also loves %<brother>s."
params := map[string]interface{}{
"sister": "Susan",
"brother": "Louis",
}
for i := 0; i < b.N; i++ {
_ = Sprintfln(pat, params)
}
}