-
Notifications
You must be signed in to change notification settings - Fork 6
/
formatter_test.go
266 lines (242 loc) · 5.73 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
/* Go IPP - IPP core protocol implementation in pure Go
*
* Copyright (C) 2020 and up by Alexander Pevzner (pzz@apevzner.com)
* See LICENSE for license terms and conditions
*
* IPP formatter test
*/
package goipp
import (
"strings"
"testing"
)
// TestFmtAttribute runs Formatter.FmtAttribute tests
func TestFmtAttribute(t *testing.T) {
type testData struct {
attr Attribute // Inpur attribute
out []string // Expected output
indent int // Indentation
}
tests := []testData{
// Simple test
{
attr: MakeAttr(
"attributes-charset",
TagCharset,
String("utf-8")),
out: []string{
`ATTR "attributes-charset" charset: utf-8`,
},
},
// Simple test with indentation
{
attr: MakeAttr(
"attributes-charset",
TagCharset,
String("utf-8")),
indent: 2,
out: []string{
` ATTR "attributes-charset" charset: utf-8`,
},
},
// Collection
{
attr: MakeAttrCollection("media-col",
MakeAttrCollection("media-size",
MakeAttribute("x-dimension",
TagInteger, Integer(10160)),
MakeAttribute("y-dimension",
TagInteger, Integer(15240)),
),
MakeAttribute("media-left-margin",
TagInteger, Integer(0)),
MakeAttribute("media-right-margin",
TagInteger, Integer(0)),
MakeAttribute("media-top-margin",
TagInteger, Integer(0)),
MakeAttribute("media-bottom-margin",
TagInteger, Integer(0)),
),
out: []string{
`ATTR "media-col" collection: {`,
` MEMBER "media-size" collection: {`,
` MEMBER "x-dimension" integer: 10160`,
` MEMBER "y-dimension" integer: 15240`,
` }`,
` MEMBER "media-left-margin" integer: 0`,
` MEMBER "media-right-margin" integer: 0`,
` MEMBER "media-top-margin" integer: 0`,
` MEMBER "media-bottom-margin" integer: 0`,
`}`,
},
},
// 1SetOf Collection
{
attr: MakeAttr("media-size-supported",
TagBeginCollection,
Collection{
MakeAttribute("x-dimension",
TagInteger, Integer(20990)),
MakeAttribute("y-dimension",
TagInteger, Integer(29704)),
},
Collection{
MakeAttribute("x-dimension",
TagInteger, Integer(14852)),
MakeAttribute("y-dimension",
TagInteger, Integer(20990)),
},
),
indent: 2,
out: []string{
` ATTR "media-size-supported" collection: {`,
` MEMBER "x-dimension" integer: 20990`,
` MEMBER "y-dimension" integer: 29704`,
` }`,
` {`,
` MEMBER "x-dimension" integer: 14852`,
` MEMBER "y-dimension" integer: 20990`,
` }`,
},
},
// Multiple values
{
attr: MakeAttr("page-delivery-supported",
TagKeyword,
String("reverse-order"),
String("same-order")),
out: []string{
`ATTR "page-delivery-supported" keyword: reverse-order same-order`,
},
},
// Values of mixed type
{
attr: Attribute{
Name: "page-ranges",
Values: Values{
{TagInteger, Integer(1)},
{TagInteger, Integer(2)},
{TagInteger, Integer(3)},
{TagRange, Range{5, 7}},
},
},
out: []string{
`ATTR "page-ranges" integer: 1 2 3 rangeOfInteger: 5-7`,
},
},
}
f := NewFormatter()
for _, test := range tests {
f.Reset()
f.SetIndent(test.indent)
expected := strings.Join(test.out, "\n") + "\n"
f.FmtAttribute(test.attr)
out := f.String()
if out != expected {
t.Errorf("output mismatch\n"+
"expected:\n%s"+
"present:\n%s",
expected, out)
}
}
}
// TestFmtRequestResponse runs Formatter.FmtRequest and
// Formatter.FmtResponse tests
func TestFmtRequestResponse(t *testing.T) {
type testData struct {
msg *Message // Input message
rq bool // This is request
out []string // Expected output
}
tests := []testData{
{
msg: &Message{
Version: MakeVersion(2, 0),
Code: Code(OpGetPrinterAttributes),
RequestID: 1,
Operation: []Attribute{
MakeAttribute(
"attributes-charset",
TagCharset,
String("utf-8")),
MakeAttribute(
"attributes-natural-language",
TagLanguage,
String("en-us")),
MakeAttribute(
"requested-attributes",
TagKeyword,
String("printer-name")),
},
},
rq: true,
out: []string{
`{`,
` REQUEST-ID 1`,
` VERSION 2.0`,
` OPERATION Get-Printer-Attributes`,
``,
` GROUP operation-attributes-tag`,
` ATTR "attributes-charset" charset: utf-8`,
` ATTR "attributes-natural-language" naturalLanguage: en-us`,
` ATTR "requested-attributes" keyword: printer-name`,
`}`,
},
},
{
msg: &Message{
Version: MakeVersion(2, 0),
Code: Code(StatusOk),
RequestID: 1,
Operation: []Attribute{
MakeAttribute(
"attributes-charset",
TagCharset,
String("utf-8")),
MakeAttribute(
"attributes-natural-language",
TagLanguage,
String("en-us")),
},
Printer: []Attribute{
MakeAttribute(
"printer-name",
TagName,
String("Kyocera_ECOSYS_M2040dn")),
},
},
rq: false,
out: []string{
`{`,
` REQUEST-ID 1`,
` VERSION 2.0`,
` STATUS successful-ok`,
``,
` GROUP operation-attributes-tag`,
` ATTR "attributes-charset" charset: utf-8`,
` ATTR "attributes-natural-language" naturalLanguage: en-us`,
``,
` GROUP printer-attributes-tag`,
` ATTR "printer-name" nameWithoutLanguage: Kyocera_ECOSYS_M2040dn`,
`}`,
},
},
}
f := NewFormatter()
for _, test := range tests {
f.Reset()
if test.rq {
f.FmtRequest(test.msg)
} else {
f.FmtResponse(test.msg)
}
out := f.String()
expected := strings.Join(test.out, "\n") + "\n"
if out != expected {
t.Errorf("output mismatch\n"+
"expected:\n%s"+
"present:\n%s",
expected, out)
}
}
}