-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamplerecord_test.go
More file actions
361 lines (324 loc) · 14.3 KB
/
examplerecord_test.go
File metadata and controls
361 lines (324 loc) · 14.3 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package sauce_test
import (
"bytes"
"fmt"
"log"
"github.com/bengarrett/sauce"
)
func ExampleContains() {
b, err := static.ReadFile("static/sauce.txt")
if err != nil {
fmt.Print(err)
}
fmt.Print(sauce.Contains(b))
// Output: true
}
func ExampleContains_none() {
b := []byte("This string of text does not contain any SAUCE.")
fmt.Print(sauce.Contains(b))
// Output: false
}
func ExampleIndex() {
b, err := static.ReadFile("static/sauce.txt")
if err != nil {
fmt.Print(err)
}
i := sauce.Index(b)
fmt.Printf("SAUCE metadata position index: %v", i)
// Output: SAUCE metadata position index: 1190
}
func ExampleTrim() {
b, err := static.ReadFile("static/sauce.txt")
if err != nil {
fmt.Print(err)
}
t := sauce.Trim(b)
fmt.Printf("The original size of the file is %d bytes and contains sauce? %v\n", len(b), sauce.Contains(b))
fmt.Printf("The trimmed size of the file is %d bytes and contains sauce? %v\n", len(t), sauce.Contains(t))
// Output: The original size of the file is 1318 bytes and contains sauce? true
// The trimmed size of the file is 1120 bytes and contains sauce? false
}
func ExampleTrim_comnt() {
b, err := static.ReadFile("static/sauce.txt")
if err != nil {
fmt.Print(err)
}
// normalize line endings
b = bytes.ReplaceAll(b, []byte("\r\n"), []byte("\n"))
// print the raw sauce binary data
const index = 7
if x := bytes.Split(b, []byte("\n")); len(x) >= index+1 {
fmt.Printf("\nSAUCE: %q\n\n", x[index])
}
// print the trimmed text
fmt.Printf("%s", string(sauce.Trim(b)))
// Output: SAUCE: "\x1aCOMNTAny comments go here. SAUCE00Sauce title Sauce author Sauce group 20161126\x9d\x0e\x00\x00\x01\x00\xd1\x03\t\x00\x00\x00\x00\x00\x01\x13IBM VGA\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
//
// File: static/sauce.txt
//
// This must be opened and saved as a Windows 1252 document.
//
// SAUCE package test with complete data and comment.
//
// Cras sit amet purus urna. Phasellus in dapibus ex. Proin pretium eget leo ut gravida. Praesent egestas urna at tincidunt mollis. Vivamus nec urna lorem. Vestibulum molestie accumsan lectus, in egestas metus facilisis eget. Nam consectetur, metus et sodales aliquam, mi dui dapibus metus, non cursus libero felis ac nunc. Nulla euismod, turpis sed mollis faucibus, orci elit dapibus leo, vitae placerat diam eros sed velit. Fusce convallis, lorem ut vulputate suscipit, tortor risus rhoncus mauris, a mattis metus magna at lorem. Sed molestie velit ipsum, in vulputate metus consequat eget. Fusce quis dui lacinia, laoreet lectus et, luctus velit. Pellentesque ut nisi quis orci pulvinar placerat vel ac lorem. Maecenas finibus fermentum erat, a pulvinar augue dictum mattis. Aenean vulputate consectetur velit at dictum. Donec vehicula ante quis ante venenatis, eu ultrices lectus egestas. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae;
}
func ExampleTrim_nocomnt() {
b, err := static.ReadFile("static/sauce-nocomnt.txt")
if err != nil {
fmt.Print(err)
}
// normalize line endings
b = bytes.ReplaceAll(b, []byte("\r\n"), []byte("\n"))
// print the raw sauce binary data
const index = 7
if x := bytes.Split(b, []byte("\n")); len(x) >= index+1 {
fmt.Printf("\nSAUCE: %q\n\n", x[index])
}
// print the trimmed text
fmt.Printf("%s", string(sauce.Trim(b)))
// Output: SAUCE: "\x1aSAUCE00Sauce title Sauce author Sauce group 20161126\x9d\x0e\x00\x00\x01\x00\xd1\x03\t\x00\x00\x00\x00\x00\x01\x13IBM VGA\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
//
// File: static/sauce-nocomnt.txt
//
// This must be opened and saved as a Windows 1252 document.
//
// SAUCE package test with complete data and comment.
//
// Cras sit amet purus urna. Phasellus in dapibus ex. Proin pretium eget leo ut gravida. Praesent egestas urna at tincidunt mollis. Vivamus nec urna lorem. Vestibulum molestie accumsan lectus, in egestas metus facilisis eget. Nam consectetur, metus et sodales aliquam, mi dui dapibus metus, non cursus libero felis ac nunc. Nulla euismod, turpis sed mollis faucibus, orci elit dapibus leo, vitae placerat diam eros sed velit. Fusce convallis, lorem ut vulputate suscipit, tortor risus rhoncus mauris, a mattis metus magna at lorem. Sed molestie velit ipsum, in vulputate metus consequat eget. Fusce quis dui lacinia, laoreet lectus et, luctus velit. Pellentesque ut nisi quis orci pulvinar placerat vel ac lorem. Maecenas finibus fermentum erat, a pulvinar augue dictum mattis. Aenean vulputate consectetur velit at dictum. Donec vehicula ante quis ante venenatis, eu ultrices lectus egestas. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae;
}
//nolint:lll
func ExampleDecode() {
b, err := static.ReadFile("static/sauce.txt")
if err != nil {
fmt.Print(err)
}
sr := sauce.Decode(b)
fmt.Printf("%+v", sr)
// Output: {ID:SAUCE Version:00 Title:Sauce title Author:Sauce author Group:Sauce group Date:{Value:20161126 Time:2016-11-26 00:00:00 +0000 UTC Epoch:1480118400} FileSize:{Bytes:3741 Decimal:3.7 kB Binary:3.7 KiB} Data:{Type:text or character stream Name:text or character stream} File:{Type:0 Name:ASCII text} Info:{Info1:{Value:977 Info:character width} Info2:{Value:9 Info:number of lines} Info3:{Value:0 Info:} Flags:{Decimal:19 Binary:10011 B:{Flag:non-blink mode Info:non-blink mode} LS:{Flag:no preference Info:no preference} AR:{Flag:invalid value Info:invalid value} Interpretations:} Font:IBM VGA} Desc:ASCII text file with no formatting codes or color codes. Comnt:{ID:COMNT Count:1 Index:1121 Comment:[Any comments go here. ]}}
}
//nolint:lll
func ExampleDecode_none() {
b := []byte("This string of text does not contain any SAUCE.")
sr := sauce.Decode(b)
fmt.Printf("%+v", sr)
// Output: {ID: Version: Title: Author: Group: Date:{Value: Time:0001-01-01 00:00:00 +0000 UTC Epoch:0} FileSize:{Bytes:0 Decimal: Binary:} Data:{Type:undefined Name:} File:{Type:0 Name:} Info:{Info1:{Value:0 Info:} Info2:{Value:0 Info:} Info3:{Value:0 Info:} Flags:{Decimal:0 Binary: B:{Flag:invalid value Info:} LS:{Flag:invalid value Info:} AR:{Flag:invalid value Info:} Interpretations:} Font:} Desc: Comnt:{ID: Count:0 Index:-1 Comment:[]}}
}
//nolint:lll
func ExampleRead() {
// open file
file, err := static.Open("static/sauce.txt")
if err != nil {
log.Print(err)
return
}
defer file.Close()
// create a new sauce record
sr, err := sauce.Read(file)
if err != nil {
log.Print(err)
return
}
// encode and print the sauce record as JSON
js, err := sr.JSON()
if err != nil {
log.Print(err)
return
}
fmt.Print(string(js))
// Output: {"id":"SAUCE","version":"00","title":"Sauce title","author":"Sauce author","group":"Sauce group","date":{"value":"20161126","iso":"2016-11-26T00:00:00Z","epoch":1480118400},"filesize":{"bytes":3741,"decimal":"3.7 kB","binary":"3.7 KiB"},"dataType":{"type":1,"name":"text or character stream"},"fileType":{"type":0,"name":"ASCII text"},"typeInfo":{"1":{"value":977,"info":"character width"},"2":{"value":9,"info":"number of lines"},"3":{"value":0,"info":""},"flags":{"decimal":19,"binary":"10011","nonBlinkMode":{"flag":"1","interpretation":"non-blink mode"},"letterSpacing":{"flag":"00","interpretation":"no preference"},"aspectRatio":{"flag":"11","interpretation":"invalid value"}},"fontName":"IBM VGA"},"comments":{"id":"COMNT","count":1,"lines":["Any comments go here. "]}}
}
func ExampleRead_none() {
b := []byte("This string of text does not contain any SAUCE.")
// create a new sauce record
sr, err := sauce.Read(bytes.NewReader(b))
if err != nil {
fmt.Printf("error: %v", err)
return
}
// encode and print the sauce record as JSON
js, err := sr.JSON()
if err != nil {
log.Print(err)
return
}
fmt.Print(string(js))
// Output: {"id":"","version":"","title":"","author":"","group":"","date":{"value":"","iso":"0001-01-01T00:00:00Z","epoch":0},"filesize":{"bytes":0,"decimal":"","binary":""},"dataType":{"type":0,"name":""},"fileType":{"type":0,"name":""},"typeInfo":{"1":{"value":0,"info":""},"2":{"value":0,"info":""},"3":{"value":0,"info":""},"flags":{"decimal":0,"binary":"","nonBlinkMode":{"flag":"","interpretation":""},"letterSpacing":{"flag":"","interpretation":""},"aspectRatio":{"flag":"","interpretation":""}},"fontName":""},"comments":{"id":"","count":0,"lines":[]}}
}
//nolint:lll
func ExampleRecord_JSON() {
b, err := static.ReadFile("static/sauce.txt")
if err != nil {
fmt.Print(err)
}
sr := sauce.Decode(b)
js, err := sr.JSON()
if err != nil {
fmt.Print(err)
}
fmt.Print(string(js))
// Output: {"id":"SAUCE","version":"00","title":"Sauce title","author":"Sauce author","group":"Sauce group","date":{"value":"20161126","iso":"2016-11-26T00:00:00Z","epoch":1480118400},"filesize":{"bytes":3741,"decimal":"3.7 kB","binary":"3.7 KiB"},"dataType":{"type":1,"name":"text or character stream"},"fileType":{"type":0,"name":"ASCII text"},"typeInfo":{"1":{"value":977,"info":"character width"},"2":{"value":9,"info":"number of lines"},"3":{"value":0,"info":""},"flags":{"decimal":19,"binary":"10011","nonBlinkMode":{"flag":"1","interpretation":"non-blink mode"},"letterSpacing":{"flag":"00","interpretation":"no preference"},"aspectRatio":{"flag":"11","interpretation":"invalid value"}},"fontName":"IBM VGA"},"comments":{"id":"COMNT","count":1,"lines":["Any comments go here. "]}}
}
func ExampleRecord_JSONIndent() {
b, err := static.ReadFile("static/sauce.txt")
if err != nil {
fmt.Print(err)
}
sr := sauce.Decode(b)
const indent = " "
js, err := sr.JSONIndent(indent)
if err != nil {
fmt.Print(err)
}
fmt.Print(string(js))
// Output: {
// "id": "SAUCE",
// "version": "00",
// "title": "Sauce title",
// "author": "Sauce author",
// "group": "Sauce group",
// "date": {
// "value": "20161126",
// "iso": "2016-11-26T00:00:00Z",
// "epoch": 1480118400
// },
// "filesize": {
// "bytes": 3741,
// "decimal": "3.7 kB",
// "binary": "3.7 KiB"
// },
// "dataType": {
// "type": 1,
// "name": "text or character stream"
// },
// "fileType": {
// "type": 0,
// "name": "ASCII text"
// },
// "typeInfo": {
// "1": {
// "value": 977,
// "info": "character width"
// },
// "2": {
// "value": 9,
// "info": "number of lines"
// },
// "3": {
// "value": 0,
// "info": ""
// },
// "flags": {
// "decimal": 19,
// "binary": "10011",
// "nonBlinkMode": {
// "flag": "1",
// "interpretation": "non-blink mode"
// },
// "letterSpacing": {
// "flag": "00",
// "interpretation": "no preference"
// },
// "aspectRatio": {
// "flag": "11",
// "interpretation": "invalid value"
// }
// },
// "fontName": "IBM VGA"
// },
// "comments": {
// "id": "COMNT",
// "count": 1,
// "lines": [
// "Any comments go here. "
// ]
// }
// }
}
func ExampleRecord_Valid() {
b, err := static.ReadFile("static/sauce.txt")
if err != nil {
fmt.Print(err)
}
sr := sauce.Decode(b)
fmt.Print(sr.Valid())
// Output: true
}
func ExampleRecord_Valid_invalid() {
b := []byte("This string of text does not contain any SAUCE.")
sr := sauce.Decode(b)
fmt.Print(sr.Valid())
// Output: false
}
//nolint:lll
func ExampleRecord_XML() {
b, err := static.ReadFile("static/sauce.txt")
if err != nil {
fmt.Print(err)
}
sr := sauce.Decode(b)
xm, err := sr.XML()
if err != nil {
fmt.Print(err)
}
fmt.Print(string(xm))
// Output: <Record id="SAUCE" version="00"><title>Sauce title</title><author>Sauce author</author><group>Sauce group</group><date epoch="1480118400"><value>20161126</value><date>2016-11-26T00:00:00Z</date></date><filesize decimal="3.7 kB" binary="3.7 KiB"><bytes>3741</bytes></filesize><data_type><type>1</type><name>text or character stream</name></data_type><file_type><type>0</type><name>ASCII text</name></file_type><type_info><type1 type="character width"><value>977</value></type1><type2 type="number of lines"><value>9</value></type2><type3 type=""><value>0</value></type3><flags decimal="19" binary="10011"><non_blink_mode interpretation="non-blink mode"><flag>1</flag></non_blink_mode><letter_spacing interpretation="no preference"><flag>00</flag></letter_spacing><aspect_ratio interpretation="invalid value"><flag>11</flag></aspect_ratio></flags><fontname>IBM VGA</fontname></type_info><comments id="COMNT" count="1"><line>Any comments go here. </line></comments></Record>
}
func ExampleRecord_XMLIndent() {
b, err := static.ReadFile("static/sauce.txt")
if err != nil {
fmt.Print(err)
}
sr := sauce.Decode(b)
const indent = " "
xm, err := sr.XMLIndent(indent)
if err != nil {
fmt.Print(err)
}
fmt.Print(string(xm))
// Output: <Record id="SAUCE" version="00">
// <title>Sauce title</title>
// <author>Sauce author</author>
// <group>Sauce group</group>
// <date epoch="1480118400">
// <value>20161126</value>
// <date>2016-11-26T00:00:00Z</date>
// </date>
// <filesize decimal="3.7 kB" binary="3.7 KiB">
// <bytes>3741</bytes>
// </filesize>
// <data_type>
// <type>1</type>
// <name>text or character stream</name>
// </data_type>
// <file_type>
// <type>0</type>
// <name>ASCII text</name>
// </file_type>
// <type_info>
// <type1 type="character width">
// <value>977</value>
// </type1>
// <type2 type="number of lines">
// <value>9</value>
// </type2>
// <type3 type="">
// <value>0</value>
// </type3>
// <flags decimal="19" binary="10011">
// <non_blink_mode interpretation="non-blink mode">
// <flag>1</flag>
// </non_blink_mode>
// <letter_spacing interpretation="no preference">
// <flag>00</flag>
// </letter_spacing>
// <aspect_ratio interpretation="invalid value">
// <flag>11</flag>
// </aspect_ratio>
// </flags>
// <fontname>IBM VGA</fontname>
// </type_info>
// <comments id="COMNT" count="1">
// <line>Any comments go here. </line>
// </comments>
// </Record>
}