-
Notifications
You must be signed in to change notification settings - Fork 4
/
atomgenerator_test.go
138 lines (120 loc) · 3.5 KB
/
atomgenerator_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
package atomgenerator
import (
"bytes"
"regexp"
"testing"
"time"
)
func TestFullXmlFeed(t *testing.T) {
pubDate, _ := time.Parse("2006-01-02 15:04", "2008-09-10 11:12")
f := Feed{
Title: "title",
PubDate: pubDate,
Link: "http://www.myblog.bogus",
authors: []Author{
Author{
Name: "author name",
Email: "author email",
Uri: "author uri",
},
},
}
entryPubDate, _ := time.Parse("2006-01-02 15:04", "2009-10-11 12:13")
entry := &Entry{
Title: "entry title",
PubDate: entryPubDate,
Link: "http://www.myblog.bogus/entry",
Description: "entry description",
Content: "<p>entry content</p>",
authors: []Author{
Author{
Name: "entry author name",
Email: "entry author email",
Uri: "entry author uri",
},
},
}
entry.AddCategory(Category{Term: "entry category 1"})
entry.AddCategory(Category{Term: "entry category 2"})
f.AddEntry(entry)
atom, err := f.GenXml()
if err != nil {
t.Error(err)
}
expected := []byte(`<?xml version="1.0" encoding="UTF-8"?> <feed xmlns="http://www.w3.org/2005/Atom">
<title>title</title>
<link href="http://www.myblog.bogus" rel="alternate"></link>
<id>http://www.myblog.bogus</id>
<updated>2008-09-10T11:12:00Z</updated>
<author>
<name>author name</name>
<email>author email</email>
<uri>author uri</uri>
</author>
<entry>
<title>entry title</title>
<link href="http://www.myblog.bogus/entry" rel="alternate"></link>
<updated>2009-10-11T12:13:00Z</updated>
<id>tag:www.myblog.bogus,2009-10-11:/entry</id>
<summary type="html">entry description</summary>
<content type="html"><p>entry content</p></content>
<author>
<name>entry author name</name>
<email>entry author email</email>
<uri>entry author uri</uri>
</author>
<category term="entry category 1"></category>
<category term="entry category 2"></category>
</entry>
</feed>`)
whitespace := regexp.MustCompile(`\s+`)
noWs := func(b []byte) []byte {
return whitespace.ReplaceAll(b, []byte(" "))
}
if !bytes.Equal(noWs(atom), noWs(expected)) {
t.Errorf("XML differs: expected %s, got %s.\n", expected, atom)
}
}
func TestValidation(t *testing.T) {
now := time.Now()
f := Feed{Title: "title"}
if errs := f.Validate(); len(errs) != 1 {
t.Error("Expected an error for a feed without PubDate.")
}
f = Feed{PubDate: now}
if errs := f.Validate(); len(errs) != 1 {
t.Error("Expected an error for a feed without Title.")
}
f = Feed{
Title: "title",
PubDate: now,
}
if errs := f.Validate(); len(errs) != 0 {
t.Error("Expected no errors for a feed with Title&PubDate and no entries.")
}
e := &Entry{Title: "entry title"}
f.AddEntry(e)
if errs := f.Validate(); len(errs) != 2 {
t.Error("Expected two errors for lack of author and no entry PubDate.")
}
e.PubDate = now
if errs := f.Validate(); len(errs) != 1 {
t.Error("Expected an error for lack of author.")
}
f.AddAuthor(Author{Name: "name"})
if errs := f.Validate(); len(errs) != 0 {
t.Error("Expected no errors for complete feed.")
}
f.AddAuthor(Author{Email: "email"})
if errs := f.Validate(); len(errs) != 1 {
t.Error("Expected an error for entry Author without Name.")
}
f.authors[1].Name = "foo"
if errs := f.Validate(); len(errs) != 0 {
t.Error("Expected no errors after fixing second author's name.")
}
e.AddCategory(Category{Scheme: "foo"})
if errs := f.Validate(); len(errs) != 1 {
t.Error("Expected an error for lack of Term in category.")
}
}