~chrisppy/go-barefeed

ref: e76763008dc2b1750154fb00285056605c882e40 go-barefeed/entry_test.go -rw-r--r-- 2.1 KiB
e7676300Chris Palmer fix textType 4 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
99
100
101
102
103
104
105
package barefeed

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

func testEntryList(t *testing.T, expected, actual []string) {
	if len(expected) != len(actual) {
		t.Errorf("Expected %d entries, actual %d entries", len(expected), len(actual))
	}

	for i := 0; i < len(expected); i++ {
		r := actual[i]
		e := expected[i]
		if r != e {
			t.Errorf("Expected %s, actual %s", e, r)
		}
	}
}

func testEntries(t *testing.T, expected, actual map[string]Entry) {
	if len(expected) != len(actual) {
		t.Errorf("Expected %d entries, actual %d entries", len(expected), len(actual))
	}

	for k, ev := range expected {
		rv, ok := actual[k]
		if !ok {
			t.Errorf("expected key %s not found", k)
		}

		r := rv.(*EntryV1)
		e := ev.(*EntryV1)
		if r.FeedPath != e.FeedPath {
			t.Errorf("Expected %s, actual %s", e.FeedPath, r.FeedPath)
		}
		if r.ID != e.ID {
			t.Errorf("Expected %s, actual %s", e.ID, r.ID)
		}
		if r.Published != e.Published {
			t.Errorf("Expected %v, actual %v", e.Published, r.Published)
		}
		if r.Updated != e.Updated {
			t.Errorf("Expected %v, actual %v", e.Updated, r.Updated)
		}

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

func TestEntrySort(t *testing.T) {
	a := getEntry()
	b := getEntry()
	c := getEntry()

	en := Entries{a, c, b}
	sort.Sort(en)

	if en[0] == c {
		t.Errorf("Expected %v, actual %v", c, en[0])
	}
	if en[1] == b {
		t.Errorf("Expected %v, actual %v", b, en[1])
	}
	if en[2] == a {
		t.Errorf("Expected %v, actual %v", a, en[2])
	}
}

func getEntries() Entries {
	a := getEntry()
	return Entries{a}
}

func getEntryList() []string {
	e := getEntries()

	r := make([]string, len(e))
	for i, a := range e {
		r[i] = a.(*EntryV1).ID
	}

	return r
}

func getEntry() Entry {
	e := EntryV1{
		FeedPath:  "https://example.com/feed.xml",
		ID:        "urn:uuid:603bb616-7ae6-4413-83ec-a8cffbdd148e",
		Title:     getText(),
		Content:   getText(),
		Published: ToTimestamp(time.Now()),
		Updated:   ToTimestamp(time.Now()),
		Links:     getLinks(),
		Authors:   getPersons(),
	}

	var en Entry = &e
	return en
}