This repository has been archived by the owner on Dec 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tagfile_test.go
250 lines (214 loc) · 7.26 KB
/
tagfile_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
// tagfile_test
package bagins_test
import (
"fmt"
"github.com/APTrust/bagins"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
)
// Field TESTS
func TestNewField(t *testing.T) {
exp_label := "test-field"
exp_value := "this is my test"
f := bagins.NewTagField(exp_label, exp_value)
if f == nil { // This should test type but for the life of me I can't figure out how.
t.Error("Tag Field object not returned")
}
if f.Label() != exp_label {
t.Error("Tag Field label not created properly!")
}
if f.Value() != exp_value {
t.Error("Tag Field value not created properly!")
}
}
// Using a single test for set and get labels since they rely on one another.
func TestLabel(t *testing.T) {
f := bagins.NewTagField("label", "value")
if f.Label() != "label" {
t.Error("Tag Field label not created properly!")
}
f.SetLabel("new-label")
if f.Label() != "new-label" {
t.Error("Tag Field label not reset properly!")
}
if f.Value() != "value" {
t.Error("Tag Field value not set or retained properly when label changed!")
}
}
// Using single test for set and get values since they rely on one another.
func TestValue(t *testing.T) {
f := bagins.NewTagField("label", "value")
if f.Value() != "value" {
t.Error("Tag Field value not created properly!")
}
f.SetValue("new value")
if f.Value() != "new value" {
t.Error("Tag Field value not set or read properly!")
}
if f.Label() != "label" {
t.Error("Tag Field label value not retained when value set!")
}
}
// FieldList TESTS
func TestNewTagFieldList(t *testing.T) {
var tfl interface{} = bagins.NewTagFieldList()
if _, ok := tfl.(*bagins.TagFieldList); !ok {
t.Error("TagFieldList not returned!")
}
}
// Doing a unified test for Fields and SetFields
func TestFields(t *testing.T) {
fl := bagins.NewTagFieldList()
test_len := func(l int) { // DRY!
if len(fl.Fields()) != l {
t.Error("Expected TagField length of", l, "but", len(fl.Fields()), "was returned!")
}
}
test_len(0)
newFields := []bagins.TagField{
*bagins.NewTagField("label1", "value1"),
*bagins.NewTagField("label2", "value2"),
*bagins.NewTagField("label3", "value3"),
}
fl.SetFields(newFields)
test_len(3)
for i := 0; i < 3; i++ {
exp := fmt.Sprintf("label%d", i+1)
act := fl.Fields()[i].Label()
if exp != act {
t.Error("Expected", exp, "but returned", act)
}
}
}
func TestAddField(t *testing.T) {
fl := bagins.NewTagFieldList()
exp_len := 100
for i := 0; i < exp_len; i++ {
tmp := strconv.Itoa(i)
fl.AddField(*bagins.NewTagField(tmp, tmp))
}
if len(fl.Fields()) != exp_len {
t.Error("Expected", exp_len, "fields but returned", len(fl.Fields()), "!")
}
for i, f := range fl.Fields() {
if f.Value() != strconv.Itoa(i) {
t.Error("Expected field value of", strconv.Itoa(i), "but returned", f.Value(), "!")
}
}
}
func TestRemoveField(t *testing.T) {
fl := bagins.NewTagFieldList()
test_len := func(l int) { // DRY again!
if len(fl.Fields()) != l {
t.Error("Expected TagField length of", l, "but", len(fl.Fields()), "was returned!")
}
}
for i := 0; i < 100; i++ {
tmp := strconv.Itoa(i)
fl.AddField(*bagins.NewTagField(tmp, tmp))
}
test_len(100)
// Should error if removing out of range.
if err := fl.RemoveField(-6); err == nil {
t.Error("Trying to remove negative index does not produce expected error!")
}
if err := fl.RemoveField(100); err == nil {
t.Error("Trying to remove out of bound index does not produce expected error!")
}
test_len(100)
// Remove every other one of the first 25 and test
for i := 0; i < 50; i++ {
if i%2 == 0 {
fl.RemoveField(i)
}
}
test_len(75)
}
// TagFile TESTS
func TestNewTagFile(t *testing.T) {
_, err := bagins.NewTagFile("tagfile.txt")
if err != nil {
t.Error("Tagfile raised an error incorrectly!")
}
_, err = bagins.NewTagFile(".tagfile")
if err == nil {
t.Error("Bag tagfile name did not raise error as expected.")
}
}
func TestReadTagFile(t *testing.T) {
// Expected Data
exp_list := [][]string{
[]string{"description", strings.Repeat("test ", 40)},
[]string{"title", "This is my title"},
[]string{"description", strings.Repeat("more ", 80)},
}
// Prep the test file
testPath := filepath.Join(os.TempDir(), "_GOTEST_READTAGFILE_bagit.txt")
tagFile, _ := bagins.NewTagFile(testPath)
for _, exp := range exp_list {
tagFile.Data.AddField(*bagins.NewTagField(exp[0], exp[1]))
}
tagFile.Create()
defer os.Remove(testPath)
// Open and test parsing the file.
tf, errs := bagins.ReadTagFile(testPath)
for _, err := range errs {
t.Error(err)
}
if len(tf.Data.Fields()) != 3 {
t.Error("Expected 3 but returned", len(tf.Data.Fields()), "fields!")
}
fields := tagFile.Data.Fields()
for idx, exp := range exp_list {
if fields[idx].Label() != exp[0] {
t.Error("Tag field", idx, "label", fields[idx].Label(), "is not expected value of", exp[0])
}
if fields[idx].Value() != exp[1] {
t.Error("Tag field", idx, "value", fields[idx].Value(), "is not expected value of", exp[1])
}
}
}
func TestTagFileCreate(t *testing.T) {
testPath := filepath.Join(os.TempDir(), "golang_test_tagfiles/_GOTEST_bagit.txt")
tagFile, _ := bagins.NewTagFile(testPath)
tagFile.Data.AddField(*bagins.NewTagField("BagIt-Version", "A metadata element MUST consist of a label, a colon, and a value, each separated by optional whitespace. It is RECOMMENDED that lines not exceed 79 characters in length. Long values may be continued onto the next line by inserting a newline (LF), a carriage return (CR), or carriage return plus newline (CRLF) and indenting the next line with linear white space (spaces or tabs)."))
tagFile.Data.AddField(*bagins.NewTagField("Tag-File-Character-Encodeing", "UTF-8"))
err := tagFile.Create()
if err != nil {
t.Error(err)
}
fileInfo, err := os.Stat(testPath)
if err != nil {
t.Error("File and path", testPath, "not created!")
}
if fileInfo.Size() == 0 {
t.Error("Tag file was created but is empty.")
}
os.RemoveAll(filepath.Dir(testPath))
}
func TestTagFileToString(t *testing.T) {
testPath := filepath.Join(os.TempDir(), "golang_test_tagfiles/_GOTEST_bagit.txt")
tagFile, _ := bagins.NewTagFile(testPath)
tagFile.Data.AddField(*bagins.NewTagField("BagIt-Version", "0.97"))
tagFile.Data.AddField(*bagins.NewTagField("Tag-File-Character-Encoding", "UTF-8"))
tagFile.Data.AddField(*bagins.NewTagField("Long-Line", "A metadata element MUST consist of a label, a colon, and a value, each separated by optional whitespace. It is RECOMMENDED that lines not exceed 79 characters in length. Long values may be continued onto the next line by inserting a newline (LF), a carriage return (CR), or carriage return plus newline (CRLF) and indenting the next line with linear white space (spaces or tabs)."))
str, err := tagFile.ToString()
if err != nil {
t.Error(err)
}
expected := `BagIt-Version: 0.97
Tag-File-Character-Encoding: UTF-8
Long-Line: A metadata element MUST consist of a label, a colon, and a value,
each separated by optional whitespace. It is RECOMMENDED that lines not
exceed 79 characters in length. Long values may be continued onto the next
line by inserting a newline (LF), a carriage return (CR), or carriage
return plus newline (CRLF) and indenting the next line with linear white
space (spaces or tabs).
`
if str != expected {
t.Errorf("ToString() returned\n\n%s \nExpected\n\n%s", str, expected)
}
}