~chrisppy/go-barefeed

ref: 022d6ea6f0215c28fa4b4b69dd9d821c8766ded8 go-barefeed/feed_test.go -rw-r--r-- 2.5 KiB
022d6ea6Chris Palmer updates from barefeed fixes 5 months ago
                                                                                
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
package barefeed

import (
	"sort"
	"testing"
	"time"
)

func testFeeds(t *testing.T, expected, actual Feeds) {
	if len(expected) != len(actual) {
		t.Errorf("Expected %d feeds, actual %d feeds", len(expected), len(actual))
	}

	for i := 0; i < len(expected); i++ {
		r := actual[i].(*FeedV1)
		e := expected[i].(*FeedV1)
		if e.Path != r.Path {
			t.Errorf("Expected %s, actual %s", e.Path, r.Path)
		}
		if r.ID != e.ID {
			t.Errorf("Expected %s, actual %s", e.ID, r.ID)
		}
		if r.Generator != e.Generator {
			if e.Generator == nil {
				t.Errorf("Expected nil, actual %s", *r.Generator)
			} else if r.Generator == nil {
				t.Errorf("Expected %s, actual nil", *e.Generator)
			} else if *r.Generator != *e.Generator {
				t.Errorf("Expected %s, actual %s", *e.Generator, *r.Generator)
			}
		}
		if r.IsAtom != e.IsAtom {
			t.Errorf("Expected %t, actual %t", e.IsAtom, r.IsAtom)
		}
		if r.Updated != e.Updated {
			t.Errorf("Expected %v, actual %v", e.Updated, r.Updated)
		}
		if r.Description != e.Description {
			if e.Description == nil {
				t.Errorf("Expected nil, actual %v", *r.Description)
			} else if r.Description == nil {
				t.Errorf("Expected %v, actual nil", *e.Description)
			} else {
				testText(t, *e.Description, *r.Description)
			}
		}

		testText(t, e.Title, r.Title)
		testPersons(t, e.Authors, r.Authors)
		testLinks(t, e.Links, r.Links)
		testEntries(t, e.Entries, r.Entries)
	}
}

func TestFeedSort(t *testing.T) {
	a := getFeed("Omega")
	b := getFeed("beta")
	c := getFeed("Alpha")

	f := Feeds{a, c, b}
	sort.Sort(f)

	if f[0].(*FeedV1).Title.Value != c.(*FeedV1).Title.Value {
		t.Errorf("Expected %v, actual %v", c.(*FeedV1).Title.Value, f[0].(*FeedV1).Title.Value)
	}
	if f[1].(*FeedV1).Title.Value != b.(*FeedV1).Title.Value {
		t.Errorf("Expected %v, actual %v", b.(*FeedV1).Title.Value, f[1].(*FeedV1).Title.Value)
	}
	if f[2].(*FeedV1).Title.Value != a.(*FeedV1).Title.Value {
		t.Errorf("Expected %v, actual %v", a.(*FeedV1).Title.Value, f[2].(*FeedV1).Title.Value)
	}
}

func getFeeds() Feeds {
	fd := getFeed("test title")
	return Feeds{fd}
}

func getFeed(title string) Feed {
	gen := "test gen"
	desc := getText()
	f := FeedV1{
		Path:        "https://example.com/feed.xml",
		ID:          "urn:uuid:22fea9cf-095c-4634-95f5-d0b72f99944f",
		Generator:   &gen,
		IsAtom:      true,
		Title:       getText(),
		Updated:     ToTimestamp(time.Now()),
		Description: &desc,
		Links:       getLinks(),
		Authors:     getPersons(),
		Entries:     getEntries(),
	}
	f.Title.Value = title

	var fd Feed = &f
	return fd
}