-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsauce_test.go
129 lines (120 loc) · 3.12 KB
/
sauce_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
// Package sauce to handle the reading and parsing of embedded SAUCE metadata.
package sauce_test
import (
"encoding/json"
"encoding/xml"
"fmt"
"os"
"reflect"
"strings"
"testing"
"github.com/bengarrett/sauce"
"github.com/bengarrett/sauce/internal/layout"
)
const example = "static/sauce.txt"
func TestTrim(t *testing.T) {
none := []byte("This is a string without any SAUCE.")
if got := sauce.Trim(none); !reflect.DeepEqual(got, none) {
t.Errorf("Trim() = %q, want %q", got, none)
}
fake := none
fake = append(fake, layout.SauceSeek...)
fake = append(fake, []byte(strings.Repeat("?", 128))...)
if got := sauce.Trim(fake); !reflect.DeepEqual(got, none) {
t.Errorf("Trim() = %q, want %q", got, none)
}
raw, err := static.ReadFile(example)
if err != nil {
t.Errorf("Trim() %v error: %v", example, err)
}
const wantL = 1119
if got := sauce.Trim(raw); len(got) != wantL {
t.Errorf("Trim() length = %d, want %d", len(got), wantL)
t.Errorf("Trim() = %q", got)
}
}
func TestDecode(t *testing.T) {
raw, err := static.ReadFile(example)
if err != nil {
t.Errorf("Decode() %v error: %v", example, err)
}
got := sauce.Decode(raw)
const wantT = "Sauce title"
if got.Title != wantT {
t.Errorf("Decode().Title = %q, want %q", got.Title, wantT)
}
const wantA = "Sauce author"
if got.Author != wantA {
t.Errorf("Decode().Author = %q, want %q", got.Author, wantA)
}
}
func TestJSON(t *testing.T) {
const id, ver = "SAUCE", "00"
raw, err := static.ReadFile(example)
if err != nil {
t.Errorf("JSON() %v error: %v", example, err)
return
}
rec := sauce.Decode(raw)
// test json
gotData, err := rec.JSON()
if err != nil {
t.Errorf("JSON() %v error: %v", example, err)
return
}
var res sauce.Record
if err := json.Unmarshal(gotData, &res); err != nil {
t.Errorf("Unmarshal error: %v", err)
}
if res.ID != id {
t.Errorf("Unmarshal ID got: %v, want %v", res.ID, id)
}
if res.Version != ver {
t.Errorf("Unmarshal Version got: %v, want %v", res.Version, ver)
}
// test json tab indent
gotData, err = rec.JSONIndent("\t")
if err != nil {
t.Errorf("JSONIndent() %v error: %v", example, err)
return
}
res = sauce.Record{}
if err := json.Unmarshal(gotData, &res); err != nil {
t.Errorf("Unmarshal error: %v", err)
}
if res.ID != id {
t.Errorf("Unmarshal ID got: %v, want %v", res.ID, id)
}
if res.Version != ver {
t.Errorf("Unmarshal Version got: %v, want %v", res.Version, ver)
}
}
func TestXML(t *testing.T) {
const id, ver = "SAUCE", "00"
raw, err := static.ReadFile(example)
if err != nil {
t.Errorf("JSON() %v error: %v", example, err)
return
}
rec := sauce.Decode(raw)
gotData, err := rec.XMLIndent(" ")
if err != nil {
t.Errorf("XML() %v error: %v", example, err)
return
}
var res sauce.Record
if err := xml.Unmarshal(gotData, &res); err != nil {
t.Errorf("Unmarshal error: %v", err)
s := strings.Split(string(gotData), "\n")
for i, row := range s {
fmt.Fprintf(os.Stderr, "%d. %s\n", i+1, row)
}
return
}
if res.ID != id {
t.Errorf("Unmarshal ID got: %v, want %v", res.ID, id)
}
if res.Version != ver {
t.Errorf("Unmarshal Version got: %v, want %v", res.Version, ver)
}
}