-
Notifications
You must be signed in to change notification settings - Fork 19
/
sitemap_test.go
141 lines (119 loc) · 2.84 KB
/
sitemap_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
package sitemap
import (
"errors"
"fmt"
"io/ioutil"
"strings"
"testing"
"time"
)
/*
* Examples
*/
func ExampleParseFromFile() {
err := ParseFromFile("./testdata/sitemap.xml", func(e Entry) error {
fmt.Println(e.GetLocation())
return nil
})
if err != nil {
panic(err)
}
}
func ExampleParseIndexFromFile() {
result := make([]string, 0, 0)
err := ParseIndexFromFile("./testdata/sitemap-index.xml", func(e IndexEntry) error {
result = append(result, e.GetLocation())
return nil
})
if err != nil {
panic(err)
}
}
/*
* Public API tests
*/
func TestParseSitemap(t *testing.T) {
var (
counter int
sb strings.Builder
)
err := ParseFromFile("./testdata/sitemap.xml", func(e Entry) error {
counter++
fmt.Fprintln(&sb, e.GetLocation())
lastmod := e.GetLastModified()
if lastmod != nil {
fmt.Fprintln(&sb, lastmod.Format(time.RFC3339))
}
fmt.Fprintln(&sb, e.GetChangeFrequency())
fmt.Fprintln(&sb, e.GetPriority())
return nil
})
if err != nil {
t.Errorf("Parsing failed with error %s", err)
}
if counter != 4 {
t.Errorf("Expected 4 elements, but given only %d", counter)
}
expected, err := ioutil.ReadFile("./testdata/sitemap.golden")
if err != nil {
t.Errorf("Can't read golden file due to %s", err)
}
if sb.String() != string(expected) {
t.Error("Unxepected result")
}
}
func TestParseSitemap_BreakingOnError(t *testing.T) {
var counter = 0
breakErr := errors.New("break error")
err := ParseFromFile("./testdata/sitemap.xml", func(e Entry) error {
counter++
return breakErr
})
if counter != 1 {
t.Error("Error didn't break parsing")
}
if breakErr != err {
t.Error("If consumer failed, ParseSitemap should return consumer error")
}
}
func TestParseSitemapIndex(t *testing.T) {
var (
counter int
sb strings.Builder
)
err := ParseIndexFromFile("./testdata/sitemap-index.xml", func(e IndexEntry) error {
counter++
fmt.Fprintln(&sb, e.GetLocation())
lastmod := e.GetLastModified()
if lastmod != nil {
fmt.Fprintln(&sb, lastmod.Format(time.RFC3339))
}
return nil
})
if err != nil {
t.Errorf("Parsing failed with error %s", err)
}
if counter != 3 {
t.Errorf("Expected 3 elements, but given only %d", counter)
}
expected, err := ioutil.ReadFile("./testdata/sitemap-index.golden")
if err != nil {
t.Errorf("Can't read golden file due to %s", err)
}
if sb.String() != string(expected) {
t.Error("Unxepected result")
}
}
/*
* Private API tests
*/
func TestParseShortDateTime(t *testing.T) {
res := parseDateTime("2015-05-07")
if res == nil {
t.Error("Date time was't parsed")
return
}
if res.Year() != 2015 || res.Month() != 05 || res.Day() != 07 {
t.Errorf("Date was parsed wrong %s", res.Format(time.RFC3339))
}
}