-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathformatter_test.go
292 lines (279 loc) · 11.8 KB
/
formatter_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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package stringFormatter_test
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/wissance/stringFormatter"
)
const _address = "grpcs://127.0.0.1"
type meteoData struct {
Int int
Str string
Double float64
Err error
}
func TestFormat(t *testing.T) {
for name, test := range map[string]struct {
template string
args []any
expected string
}{
"all args in place": {
template: "Hello i am {0}, my age is {1} and i am waiting for {2}, because i am {0}!",
args: []any{"Michael Ushakov (Evillord666)", "34", `"Great Success"`},
expected: `Hello i am Michael Ushakov (Evillord666), my age is 34 and i am waiting for "Great Success", because i am Michael Ushakov (Evillord666)!`,
},
"too large index": {
template: "We are wondering if these values would be replaced : {5}, {4}, {0}",
args: []any{"one", "two", "three"},
expected: "We are wondering if these values would be replaced : {5}, {4}, one",
},
"no args": {
template: "No args ... : {0}, {1}, {2}",
args: nil,
expected: "No args ... : {0}, {1}, {2}",
},
"format json": {
template: `
{
"Comment": "Call Lambda with GRPC",
"StartAt": "CallLambdaWithGrpc",
"States": {"CallLambdaWithGrpc": {"Type": "Task", "Resource": "{0}:get ad user", "End": true}}
}`,
args: []any{_address},
expected: `
{
"Comment": "Call Lambda with GRPC",
"StartAt": "CallLambdaWithGrpc",
"States": {"CallLambdaWithGrpc": {"Type": "Task", "Resource": "grpcs://127.0.0.1:get ad user", "End": true}}
}`,
},
"multiple nested curly brackets": {
template: `{"StartAt": "S0", "States": {"S0": {"Type": "Map" {0}, ` +
`"Iterator": {"StartAt": "SI0", "States": {"SI0": {"Type": "Pass", "End": true}}}` +
`, "End": true}}}`,
args: []any{""},
expected: `{"StartAt": "S0", "States": {"S0": {"Type": "Map" , "Iterator": {"StartAt": "SI0", "States": {"SI0": {"Type": "Pass", "End": true}}}, "End": true}}}`,
},
"indexes out of args range": {
template: "{3} - rings to the immortal elfs, {7} to dwarfs, {9} to greedy people and {1} to control everything",
args: []any{"0", "1", "2", "3"},
expected: "3 - rings to the immortal elfs, {7} to dwarfs, {9} to greedy people and 1 to control everything",
},
"format integers": {
template: `Here we are testing integers "int8": {0}, "int16": {1}, "int32": {2}, "int64": {3} and finally "int": {4}`,
args: []any{int8(8), int16(-16), int32(32), int64(-64), int(123)},
expected: `Here we are testing integers "int8": 8, "int16": -16, "int32": 32, "int64": -64 and finally "int": 123`,
},
"format unsigneds": {
template: `Here we are testing integers "uint8": {0}, "uint16": {1}, "uint32": {2}, "uint64": {3} and finally "uint": {4}`,
args: []any{uint8(8), uint16(16), uint32(32), uint64(64), uint(128)},
expected: `Here we are testing integers "uint8": 8, "uint16": 16, "uint32": 32, "uint64": 64 and finally "uint": 128`,
},
"format floats": {
template: `Here we are testing floats "float32": {0}, "float64":{1}`,
args: []any{float32(1.24), float64(1.56)},
expected: `Here we are testing floats "float32": 1.24, "float64":1.56`,
},
"format bools": {
template: `Here we are testing "bool" args: {0}, {1}`,
args: []any{false, true},
expected: `Here we are testing "bool" args: false, true`,
},
"format complex": {
template: `Here we are testing "complex64" {0} and "complex128": {1}`,
args: []any{complex64(complex(1.0, 6.0)), complex(2.3, 3.2)},
expected: `Here we are testing "complex64" (1+6i) and "complex128": (2.3+3.2i)`,
},
"doubly curly brackets": {
template: "Hello i am {{0}}, my age is {1} and i am waiting for {2}, because i am {0}!",
args: []any{"Michael Ushakov (Evillord666)", "34", `"Great Success"`},
expected: `Hello i am {0}, my age is 34 and i am waiting for "Great Success", because i am Michael Ushakov (Evillord666)!`,
},
"doubly curly brackets at the end": {
template: "At the end {{0}}",
args: []any{"s"},
expected: "At the end {0}",
},
"struct arg": {
template: "Example is: {0}",
args: []any{
meteoData{
Int: 123,
Str: "This is a test str, nothing more special",
Double: -1.098743,
Err: errors.New("main question error, is 42"),
},
},
expected: "Example is: {123 This is a test str, nothing more special -1.098743 main question error, is 42}",
},
"open bracket at the end of line of go line": {
template: "type serviceHealth struct {",
args: []any{},
expected: "type serviceHealth struct {",
},
"open bracket at the end of line of go line with {} inside": {
template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) {",
args: []any{},
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) {",
},
"close bracket at the end of line of go line with {} inside": {
template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) }",
args: []any{},
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) }",
},
"no bracket at the end of line with {} inside": {
template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) ",
args: []any{},
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) ",
},
"open bracket at the end of line of go line with multiple {} inside": {
template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}, additionalData interface{}) {",
args: []any{},
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}, additionalData interface{}) {",
},
"commentaries after bracket": {
template: "switch app.appConfig.ServerCfg.Schema { //nolint:exhaustive",
args: []any{},
expected: "switch app.appConfig.ServerCfg.Schema { //nolint:exhaustive",
},
"bracket in the middle": {
template: "in the middle - { at the end - nothing",
args: []any{},
expected: "in the middle - { at the end - nothing",
},
} {
t.Run(name, func(t *testing.T) {
assert.Equal(t, test.expected, stringFormatter.Format(test.template, test.args...))
})
}
}
func TestFormatWithArgFormatting(t *testing.T) {
for name, test := range map[string]struct {
template string
args []any
expected string
}{
"numeric_test_1": {
template: "This is the text with an only number formatting: decimal - {0} / {0 : D6}, scientific - {1} / {1 : e2}",
args: []any{123, 191.0784},
expected: "This is the text with an only number formatting: decimal - 123 / 000123, scientific - 191.0784 / 1.91e+02",
},
"numeric_test_2": {
template: "This is the text with an only number formatting: binary - {0:B} / {0 : B8}, hexadecimal - {1:X} / {1 : X4}",
args: []any{15, 250},
expected: "This is the text with an only number formatting: binary - 1111 / 00001111, hexadecimal - fa / 00fa",
},
"numeric_test_3": {
template: "This is the text with an only number formatting: decimal - {0:F} / {0 : F4} / {0:F8}",
args: []any{10.5467890},
expected: "This is the text with an only number formatting: decimal - 10.546789 / 10.5468 / 10.54678900",
},
"numeric_test_4": {
template: "This is the text with percentage format - {0:P100} / {0 : P100.5}, and non normal percentage {1:P100}",
args: []any{12, "ass"},
expected: "This is the text with percentage format - 12.00 / 11.94, and non normal percentage 0.00",
},
} {
// Run test here
t.Run(name, func(t *testing.T) {
// assert.NotNil(t, test)
assert.Equal(t, test.expected, stringFormatter.Format(test.template, test.args...))
})
}
}
// TestStrFormatWithComplicatedText - this test represents issue with complicated text
func TestFormatComplex(t *testing.T) {
for name, test := range map[string]struct {
template string
args map[string]any
expected string
}{
"numeric_test_1": {
template: `
{
"Comment": "Call Lambda with GRPC",
"StartAt": "CallLambdaWithGrpc",
"States": {"CallLambdaWithGrpc": {"Type": "Task", "Resource": "{address}:get ad user", "End": true}}
}`,
args: map[string]any{"address": _address},
expected: `
{
"Comment": "Call Lambda with GRPC",
"StartAt": "CallLambdaWithGrpc",
"States": {"CallLambdaWithGrpc": {"Type": "Task", "Resource": "grpcs://127.0.0.1:get ad user", "End": true}}
}`,
},
"key not found": {
template: "Hello: {username}, you earn {amount} $",
args: map[string]any{"amount": 1000},
expected: "Hello: {username}, you earn 1000 $",
},
"dialog": {
template: "Hello {user} what are you doing here {app} ?",
args: map[string]any{"user": "vpupkin", "app": "mn_console"},
expected: "Hello vpupkin what are you doing here mn_console ?",
},
"info message": {
template: "Current app settings are: ipAddr: {ipaddr}, port: {port}, use ssl: {ssl}.",
args: map[string]any{"ipaddr": "127.0.0.1", "port": 5432, "ssl": false},
expected: "Current app settings are: ipAddr: 127.0.0.1, port: 5432, use ssl: false.",
},
"one json line with open bracket at the end": {
template: " \"server\": {",
args: map[string]any{},
expected: " \"server\": {",
},
"open bracket at the end of line of go line with {} inside": {
template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) {",
args: map[string]any{},
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) {",
},
"open bracket at the end of line of go line with multiple {} inside": {
template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}, additionalData interface{}) {",
args: map[string]any{},
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}, additionalData interface{}) {",
},
"close bracket at the end of line of go line with {} inside": {
template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) }",
args: map[string]any{},
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) }",
},
"commentaries after bracket": {
template: "switch app.appConfig.ServerCfg.Schema { //nolint:exhaustive",
args: map[string]any{},
expected: "switch app.appConfig.ServerCfg.Schema { //nolint:exhaustive",
},
} {
t.Run(name, func(t *testing.T) {
assert.Equal(t, test.expected, stringFormatter.FormatComplex(test.template, test.args))
})
}
}
func TestFormatComplexWithArgFormatting(t *testing.T) {
for name, test := range map[string]struct {
template string
args map[string]any
expected string
}{
"numeric_test_1": {
template: "This is the text with an only number formatting: scientific - {mass} / {mass : e2}",
args: map[string]any{"mass": 191.0784},
expected: "This is the text with an only number formatting: scientific - 191.0784 / 1.91e+02",
},
"numeric_test_2": {
template: "This is the text with an only number formatting: binary - {bin:B} / {bin : B8}, hexadecimal - {hex:X} / {hex : X4}",
args: map[string]any{"bin": 15, "hex": 250},
expected: "This is the text with an only number formatting: binary - 1111 / 00001111, hexadecimal - fa / 00fa",
},
"numeric_test_3": {
template: "This is the text with an only number formatting: decimal - {float:F} / {float : F4} / {float:F8}",
args: map[string]any{"float": 10.5467890},
expected: "This is the text with an only number formatting: decimal - 10.546789 / 10.5468 / 10.54678900",
},
} {
t.Run(name, func(t *testing.T) {
assert.Equal(t, test.expected, stringFormatter.FormatComplex(test.template, test.args))
})
}
}