Skip to content

Commit 55012d6

Browse files
committed
opml: export assertion functions to compare Document and Outline instances
Signed-off-by: VirtualTam <virtualtam@flibidi.net>
1 parent 791e8fe commit 55012d6

File tree

5 files changed

+138
-114
lines changed

5 files changed

+138
-114
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/) and this
66
project adheres to [Semantic Versioning](https://semver.org/).
77

8+
## [v1.1.0](https://github.com/virtualtam/opml-go/releases/tag/v1.1.0) - 2024-11-10
9+
10+
### Changed
11+
#### Testing
12+
- Export assertion functions to compare `Document` and `Outline` instances
13+
14+
815
## [v1.0.0](https://github.com/virtualtam/opml-go/releases/tag/v1.0.0) - 2024-11-07
916

1017
Initial release.

assert.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// Copyright (c) VirtualTam
2+
// SPDX-License-Identifier: MIT
3+
4+
package opml
5+
6+
import (
7+
"fmt"
8+
"testing"
9+
)
10+
11+
// AssertDocumentsEqual asserts two OPML documents are equal.
12+
func AssertDocumentsEqual(t *testing.T, got, want Document) {
13+
t.Helper()
14+
15+
// OPML metadata
16+
if got.XMLName.Local != want.XMLName.Local {
17+
t.Errorf("want XMLName.Local %q, got %q", want.XMLName.Local, got.XMLName.Local)
18+
}
19+
if got.Version != want.Version {
20+
t.Errorf("want Version %q, got %q", want.Version, got.Version)
21+
}
22+
23+
// Head
24+
if !got.Head.DateCreated.Equal(want.Head.DateCreated) {
25+
t.Errorf("want Head > DateCreated %q, got %q", want.Head.DateCreated, got.Head.DateCreated)
26+
}
27+
if !got.Head.DateModified.Equal(want.Head.DateModified) {
28+
t.Errorf("want Head > DateModified %q, got %q", want.Head.DateModified, got.Head.DateModified)
29+
}
30+
if got.Head.OwnerName != want.Head.OwnerName {
31+
t.Errorf("want Head > OwnerName %q, got %q", want.Head.OwnerName, got.Head.OwnerName)
32+
}
33+
if got.Head.OwnerEmail != want.Head.OwnerEmail {
34+
t.Errorf("want Head > OwnerEmail %q, got %q", want.Head.OwnerEmail, got.Head.OwnerEmail)
35+
}
36+
if got.Head.VertScrollState != want.Head.VertScrollState {
37+
t.Errorf("want Head > VertScrollState %d, got %d", want.Head.VertScrollState, got.Head.VertScrollState)
38+
}
39+
if got.Head.WindowTop != want.Head.WindowTop {
40+
t.Errorf("want Head > WindowTop %d, got %d", want.Head.WindowTop, got.Head.WindowTop)
41+
}
42+
if got.Head.WindowLeft != want.Head.WindowLeft {
43+
t.Errorf("want Head > WindowLeft %d, got %d", want.Head.WindowLeft, got.Head.WindowLeft)
44+
}
45+
if got.Head.WindowBottom != want.Head.WindowBottom {
46+
t.Errorf("want Head > WindowBottom %d, got %d", want.Head.WindowBottom, got.Head.WindowBottom)
47+
}
48+
if got.Head.WindowRight != want.Head.WindowRight {
49+
t.Errorf("want Head > WindowRight %d, got %d", want.Head.WindowRight, got.Head.WindowRight)
50+
}
51+
52+
// Body
53+
AssertOutlinesEqual(t, got.Body.Outlines, want.Body.Outlines)
54+
}
55+
56+
// AssertOutlinesEqual asserts two lists of OPML outlines are equal.
57+
func AssertOutlinesEqual(t *testing.T, gotOutlines, wantOutlines []Outline) {
58+
t.Helper()
59+
60+
assertOutlinesEqual(t, "", gotOutlines, wantOutlines)
61+
}
62+
63+
// AssertOutlinesEqual asserts two lists of OPML outlines are equal.
64+
func assertOutlinesEqual(t *testing.T, prefix string, gotOutlines, wantOutlines []Outline) {
65+
t.Helper()
66+
67+
if len(gotOutlines) != len(wantOutlines) {
68+
t.Fatalf("want %d Outlines, got %d", len(wantOutlines), len(gotOutlines))
69+
}
70+
71+
for index, wantOutline := range wantOutlines {
72+
gotOutline := gotOutlines[index]
73+
74+
if gotOutline.Text != wantOutline.Text {
75+
t.Errorf("want Outline %s%d Text %q, got %q", prefix, index, wantOutline.Text, gotOutline.Text)
76+
}
77+
78+
if len(gotOutline.Categories) != len(wantOutline.Categories) {
79+
t.Errorf("want Outline %s%d %d Categories, got %d", prefix, index, len(wantOutline.Categories), len(gotOutline.Categories))
80+
}
81+
82+
for cIndex, wantCategory := range wantOutline.Categories {
83+
gotCategory := gotOutline.Categories[cIndex]
84+
85+
if gotCategory != wantCategory {
86+
t.Errorf("want Outline %s%d Category %d %q, got %q", prefix, index, cIndex, wantCategory, gotCategory)
87+
}
88+
}
89+
90+
if !gotOutline.Created.Equal(wantOutline.Created) {
91+
t.Errorf("want Outline %s%d Created %q, got %q", prefix, index, wantOutline.Created, gotOutline.Created)
92+
}
93+
if gotOutline.IsBreakpoint != wantOutline.IsBreakpoint {
94+
t.Errorf("want Outline %s%d IsBreakpoint %t, got %t", prefix, index, wantOutline.IsBreakpoint, gotOutline.IsBreakpoint)
95+
}
96+
if gotOutline.IsComment != wantOutline.IsComment {
97+
t.Errorf("want Outline %s%d IsComment %t, got %t", prefix, index, wantOutline.IsComment, gotOutline.IsComment)
98+
}
99+
if gotOutline.Type != wantOutline.Type {
100+
t.Errorf("want Outline %s%d Type %q, got %q", prefix, index, wantOutline.Type, gotOutline.Type)
101+
}
102+
103+
if gotOutline.Url != wantOutline.Url {
104+
t.Errorf("want Outline %s%d URL %q, got %q", prefix, index, wantOutline.Url, gotOutline.Url)
105+
}
106+
107+
if gotOutline.Version != wantOutline.Version {
108+
t.Errorf("want Outline %s%d Version %q, got %q", prefix, index, wantOutline.Version, gotOutline.Version)
109+
}
110+
if gotOutline.Title != wantOutline.Title {
111+
t.Errorf("want Outline %s%d Title %q, got %q", prefix, index, wantOutline.Title, gotOutline.Title)
112+
}
113+
if gotOutline.Description != wantOutline.Description {
114+
t.Errorf("want Outline %s%d Description %q, got %q", prefix, index, wantOutline.Description, gotOutline.Description)
115+
}
116+
if gotOutline.Language != wantOutline.Language {
117+
t.Errorf("want Outline %s%d Language %q, got %q", prefix, index, wantOutline.Language, gotOutline.Language)
118+
}
119+
if gotOutline.HtmlUrl != wantOutline.HtmlUrl {
120+
t.Errorf("want Outline %s%d HtmlUrl %q, got %q", prefix, index, wantOutline.HtmlUrl, gotOutline.HtmlUrl)
121+
}
122+
if gotOutline.XmlUrl != wantOutline.XmlUrl {
123+
t.Errorf("want Outline %s%d XmlUrl %q, got %q", prefix, index, wantOutline.XmlUrl, gotOutline.XmlUrl)
124+
}
125+
126+
childPrefix := fmt.Sprintf("%s%d.", prefix, index)
127+
assertOutlinesEqual(t, childPrefix, gotOutline.Outlines, wantOutline.Outlines)
128+
}
129+
}

marshal_feedreader_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func TestUnmarshalFeedReader(t *testing.T) {
171171
t.Fatalf("want no error, got %q", err)
172172
}
173173

174-
assertDocumentsEqual(t, *got, tc.want)
174+
AssertDocumentsEqual(t, *got, tc.want)
175175
})
176176
}
177177
}

marshal_spec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ func TestUnmarshalFileSpec(t *testing.T) {
644644
t.Fatalf("want no error, got %q", err)
645645
}
646646

647-
assertDocumentsEqual(t, *got, tc.want)
647+
AssertDocumentsEqual(t, *got, tc.want)
648648
})
649649
}
650650
}

opml_document_test.go

Lines changed: 0 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -2,115 +2,3 @@
22
// SPDX-License-Identifier: MIT
33

44
package opml
5-
6-
import "testing"
7-
8-
func assertDocumentsEqual(t *testing.T, got, want Document) {
9-
t.Helper()
10-
11-
// OPML metadata
12-
if got.XMLName.Local != want.XMLName.Local {
13-
t.Errorf("want XMLName.Local %q, got %q", want.XMLName.Local, got.XMLName.Local)
14-
}
15-
if got.Version != want.Version {
16-
t.Errorf("want Version %q, got %q", want.Version, got.Version)
17-
}
18-
19-
// Head
20-
if !got.Head.DateCreated.Equal(want.Head.DateCreated) {
21-
t.Errorf("want Head > DateCreated %q, got %q", want.Head.DateCreated, got.Head.DateCreated)
22-
}
23-
if !got.Head.DateModified.Equal(want.Head.DateModified) {
24-
t.Errorf("want Head > DateModified %q, got %q", want.Head.DateModified, got.Head.DateModified)
25-
}
26-
if got.Head.OwnerName != want.Head.OwnerName {
27-
t.Errorf("want Head > OwnerName %q, got %q", want.Head.OwnerName, got.Head.OwnerName)
28-
}
29-
if got.Head.OwnerEmail != want.Head.OwnerEmail {
30-
t.Errorf("want Head > OwnerEmail %q, got %q", want.Head.OwnerEmail, got.Head.OwnerEmail)
31-
}
32-
if got.Head.VertScrollState != want.Head.VertScrollState {
33-
t.Errorf("want Head > VertScrollState %d, got %d", want.Head.VertScrollState, got.Head.VertScrollState)
34-
}
35-
if got.Head.WindowTop != want.Head.WindowTop {
36-
t.Errorf("want Head > WindowTop %d, got %d", want.Head.WindowTop, got.Head.WindowTop)
37-
}
38-
if got.Head.WindowLeft != want.Head.WindowLeft {
39-
t.Errorf("want Head > WindowLeft %d, got %d", want.Head.WindowLeft, got.Head.WindowLeft)
40-
}
41-
if got.Head.WindowBottom != want.Head.WindowBottom {
42-
t.Errorf("want Head > WindowBottom %d, got %d", want.Head.WindowBottom, got.Head.WindowBottom)
43-
}
44-
if got.Head.WindowRight != want.Head.WindowRight {
45-
t.Errorf("want Head > WindowRight %d, got %d", want.Head.WindowRight, got.Head.WindowRight)
46-
}
47-
48-
// Body
49-
assertOutlinesEqual(t, got.Body.Outlines, want.Body.Outlines)
50-
}
51-
52-
func assertOutlinesEqual(t *testing.T, gotOutlines, wantOutlines []Outline) {
53-
t.Helper()
54-
55-
if len(gotOutlines) != len(wantOutlines) {
56-
t.Fatalf("want %d Outlines, got %d", len(wantOutlines), len(gotOutlines))
57-
}
58-
59-
for index, wantOutline := range wantOutlines {
60-
gotOutline := gotOutlines[index]
61-
62-
if gotOutline.Text != wantOutline.Text {
63-
t.Errorf("want Outline %d Text %q, got %q", index, wantOutline.Text, gotOutline.Text)
64-
}
65-
66-
if len(gotOutline.Categories) != len(wantOutline.Categories) {
67-
t.Errorf("want Outline %d %d Categories, got %d", index, len(wantOutline.Categories), len(gotOutline.Categories))
68-
}
69-
70-
for cIndex, wantCategory := range wantOutline.Categories {
71-
gotCategory := gotOutline.Categories[cIndex]
72-
73-
if gotCategory != wantCategory {
74-
t.Errorf("want Outline %d Category %d %q, got %q", index, cIndex, wantCategory, gotCategory)
75-
}
76-
}
77-
78-
if !gotOutline.Created.Equal(wantOutline.Created) {
79-
t.Errorf("want Outline %d Created %q, got %q", index, wantOutline.Created, gotOutline.Created)
80-
}
81-
if gotOutline.IsBreakpoint != wantOutline.IsBreakpoint {
82-
t.Errorf("want Outline %d IsBreakpoint %t, got %t", index, wantOutline.IsBreakpoint, gotOutline.IsBreakpoint)
83-
}
84-
if gotOutline.IsComment != wantOutline.IsComment {
85-
t.Errorf("want Outline %d IsComment %t, got %t", index, wantOutline.IsComment, gotOutline.IsComment)
86-
}
87-
if gotOutline.Type != wantOutline.Type {
88-
t.Errorf("want Outline %d Type %q, got %q", index, wantOutline.Type, gotOutline.Type)
89-
}
90-
91-
if gotOutline.Url != wantOutline.Url {
92-
t.Errorf("want Outline %d URL %q, got %q", index, wantOutline.Url, gotOutline.Url)
93-
}
94-
95-
if gotOutline.Version != wantOutline.Version {
96-
t.Errorf("want Outline %d Version %q, got %q", index, wantOutline.Version, gotOutline.Version)
97-
}
98-
if gotOutline.Title != wantOutline.Title {
99-
t.Errorf("want Outline %d Title %q, got %q", index, wantOutline.Title, gotOutline.Title)
100-
}
101-
if gotOutline.Description != wantOutline.Description {
102-
t.Errorf("want Outline %d Description %q, got %q", index, wantOutline.Description, gotOutline.Description)
103-
}
104-
if gotOutline.Language != wantOutline.Language {
105-
t.Errorf("want Outline %d Language %q, got %q", index, wantOutline.Language, gotOutline.Language)
106-
}
107-
if gotOutline.HtmlUrl != wantOutline.HtmlUrl {
108-
t.Errorf("want Outline %d HtmlUrl %q, got %q", index, wantOutline.HtmlUrl, gotOutline.HtmlUrl)
109-
}
110-
if gotOutline.XmlUrl != wantOutline.XmlUrl {
111-
t.Errorf("want Outline %d XmlUrl %q, got %q", index, wantOutline.XmlUrl, gotOutline.XmlUrl)
112-
}
113-
114-
assertOutlinesEqual(t, gotOutline.Outlines, wantOutline.Outlines)
115-
}
116-
}

0 commit comments

Comments
 (0)