forked from mmcdole/gofeed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslator_test.go
100 lines (82 loc) · 2.53 KB
/
translator_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
package gofeed_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/mmcdole/gofeed"
"github.com/mmcdole/gofeed/atom"
"github.com/mmcdole/gofeed/rss"
"github.com/stretchr/testify/assert"
)
func TestDefaultRSSTranslator_Translate(t *testing.T) {
files, _ := filepath.Glob("testdata/translator/rss/*.xml")
for _, f := range files {
base := filepath.Base(f)
name := strings.TrimSuffix(base, filepath.Ext(base))
fmt.Printf("Testing %s... ", name)
// Get actual source feed
ff := fmt.Sprintf("testdata/translator/rss/%s.xml", name)
f, _ := os.Open(ff)
defer f.Close()
// Parse actual feed
translator := &gofeed.DefaultRSSTranslator{}
fp := &rss.Parser{}
rssFeed, _ := fp.Parse(f)
actual, _ := translator.Translate(rssFeed)
// Get json encoded expected feed result
ef := fmt.Sprintf("testdata/translator/rss/%s.json", name)
e, _ := ioutil.ReadFile(ef)
// Unmarshal expected feed
expected := &gofeed.Feed{}
json.Unmarshal(e, &expected)
if assert.Equal(t, actual, expected, "Feed file %s.xml did not match expected output %s.json", name, name) {
fmt.Printf("OK\n")
} else {
fmt.Printf("Failed\n")
}
}
}
func TestDefaultRSSTranslator_Translate_WrongType(t *testing.T) {
translator := &gofeed.DefaultRSSTranslator{}
af, err := translator.Translate("wrong type")
assert.Nil(t, af)
assert.NotNil(t, err)
}
func TestDefaultAtomTranslator_Translate(t *testing.T) {
files, _ := filepath.Glob("testdata/translator/atom/*.xml")
for _, f := range files {
base := filepath.Base(f)
name := strings.TrimSuffix(base, filepath.Ext(base))
fmt.Printf("Testing %s... ", name)
// Get actual source feed
ff := fmt.Sprintf("testdata/translator/atom/%s.xml", name)
f, _ := os.Open(ff)
defer f.Close()
// Parse actual feed
translator := &gofeed.DefaultAtomTranslator{}
fp := &atom.Parser{}
atomFeed, _ := fp.Parse(f)
actual, _ := translator.Translate(atomFeed)
// Get json encoded expected feed result
ef := fmt.Sprintf("testdata/translator/atom/%s.json", name)
e, _ := ioutil.ReadFile(ef)
// Unmarshal expected feed
expected := &gofeed.Feed{}
json.Unmarshal(e, &expected)
if assert.Equal(t, actual, expected, "Feed file %s.xml did not match expected output %s.json", name, name) {
fmt.Printf("OK\n")
} else {
fmt.Printf("Failed\n")
}
}
}
func TestDefaultAtomTranslator_Translate_WrongType(t *testing.T) {
translator := &gofeed.DefaultAtomTranslator{}
af, err := translator.Translate("wrong type")
assert.Nil(t, af)
assert.NotNil(t, err)
}