summaryrefslogtreecommitdiff
path: root/test/rss/test_parser_atom_feed.rb
blob: 626062d23aeca4d6f0d209d353cd95d871277dc3 (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# frozen_string_literal: false
require_relative "rss-testcase"

require "rss/atom"

module RSS
  class TestParserAtomFeed < TestCase
    def test_feed_validation
      assert_ns("", Atom::URI) do
        Parser.parse(<<-EOA)
<feed/>
EOA
      end

      assert_ns("", Atom::URI) do
        Parser.parse(<<-EOA)
<feed xmlns="hoge"/>
EOA
      end

      assert_parse(<<-EOA, :missing_tag, "id", "feed") do
<feed xmlns="#{Atom::URI}"/>
EOA
      end

      assert_parse(<<-EOA, :missing_tag, "title", "feed") do
<feed xmlns="#{Atom::URI}">
  <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
</feed>
EOA
      end

      assert_parse(<<-EOA, :missing_tag, "updated", "feed") do
<feed xmlns="#{Atom::URI}">
  <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
  <title>Example Feed</title>
</feed>
EOA
      end

      assert_parse(<<-EOA, :missing_tag, "author", "feed") do
<feed xmlns="#{Atom::URI}">
  <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
  <title>Example Feed</title>
  <updated>2003-12-13T18:30:02Z</updated>
</feed>
EOA
      end

      assert_parse(<<-EOA, :nothing_raised) do
<feed xmlns="#{Atom::URI}">
  <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
  <title>Example Feed</title>
  <updated>2003-12-13T18:30:02Z</updated>
  <author>
    <name>A person</name>
  </author>
</feed>
EOA
      end
    end

    def test_lang
      feed = RSS::Parser.parse(<<-EOA)
<feed xmlns="#{Atom::URI}" xml:lang="ja">
  <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
  <title xml:lang="en">Example Feed</title>
  <updated>2003-12-13T18:30:02Z</updated>
  <author xml:lang="en">
    <name>A person</name>
  </author>
</feed>
EOA

      assert_equal("ja", feed.lang)
      assert_equal("ja", feed.id.lang)
      assert_equal("en", feed.title.lang)
      assert_equal("ja", feed.updated.lang)
      assert_equal("en", feed.author.lang)
      assert_equal("en", feed.author.name.lang)
    end

    def test_base
      feed = RSS::Parser.parse(<<-EOA)
<feed xmlns="#{Atom::URI}" xml:base="http://example.com/">
  <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
  <title xml:lang="en">Example Feed</title>
  <updated>2003-12-13T18:30:02Z</updated>
  <generator uri="generator">Generator</generator>
  <link hreflang="ja" href="http://example.org/link1"/>
  <link hreflang="en" href="link2"/>
  <link hreflang="fr" xml:base="http://example.net/" href="link3"/>
  <author>
    <name>A person</name>
    <uri>person</uri>
  </author>
</feed>
EOA

      assert_equal("http://example.com/", feed.base)
      assert_equal("http://example.com/", feed.id.base)
      assert_equal("http://example.com/", feed.title.base)
      assert_equal("http://example.com/", feed.updated.base)
      assert_equal("http://example.com/", feed.generator.base)
      assert_equal("http://example.com/generator", feed.generator.uri)

      assert_equal("http://example.com/", feed.links[0].base)
      assert_equal("http://example.org/link1", feed.links[0].href)
      assert_equal("http://example.com/", feed.links[1].base)
      assert_equal("http://example.com/link2", feed.links[1].href)
      assert_equal("http://example.net/", feed.links[2].base)
      assert_equal("http://example.net/link3", feed.links[2].href)
      assert_equal("http://example.com/person", feed.author.uri.content)
    end

    def test_feed_author
      assert_atom_person("author", method(:make_feed)) do |feed|
        assert_equal(2, feed.authors.size)
        feed.authors[1]
      end
    end

    def test_entry_author
      generator = method(:make_feed_with_open_entry)
      assert_atom_person("author", generator) do |feed|
        assert_equal(1, feed.entries.size)
        assert_equal(1, feed.entry.authors.size)
        feed.entry.author
      end
    end

    def test_feed_category
      assert_atom_category(method(:make_feed)) do |feed|
        assert_equal(1, feed.categories.size)
        feed.category
      end
    end

    def test_entry_category
      assert_atom_category(method(:make_feed_with_open_entry)) do |feed|
        assert_equal(1, feed.entries.size)
        assert_equal(1, feed.entry.categories.size)
        feed.entry.category
      end
    end

    def test_entry_content
      assert_atom_content(method(:make_feed_with_open_entry)) do |feed|
        assert_equal(1, feed.entries.size)
        feed.entry.content
      end
    end

    def test_feed_contributor
      assert_atom_person("contributor", method(:make_feed)) do |feed|
        assert_equal(1, feed.contributors.size)
        feed.contributor
      end
    end

    def test_entry_contributor
      generator = method(:make_feed_with_open_entry)
      assert_atom_person("contributor", generator) do |feed|
        assert_equal(1, feed.entries.size)
        assert_equal(1, feed.entry.contributors.size)
        feed.entry.contributor
      end
    end

    def test_feed_generator
      assert_atom_generator(method(:make_feed)) do |feed|
        feed.generator
      end
    end

    def test_feed_icon
      assert_atom_icon(method(:make_feed)) do |feed|
        feed.icon
      end
    end

    def test_feed_id
      feed = RSS::Parser.parse(make_feed(''))
      assert_equal(FEED_ID, feed.id.content)
    end

    def test_entry_id
      feed = RSS::Parser.parse(make_feed(''))
      assert_equal(ENTRY_ID, feed.entry.id.content)
    end

    def test_feed_link
      assert_atom_link(method(:make_feed)) do |feed|
        assert_equal(1, feed.links.size)
        feed.link
      end
    end

    def test_entry_link
      assert_atom_link(method(:make_feed_with_open_entry)) do |feed|
        assert_equal(1, feed.entries.size)
        assert_equal(1, feed.entry.links.size)
        feed.entry.link
      end
    end

    def test_feed_logo
      assert_atom_logo(method(:make_feed)) do |feed|
        feed.logo
      end
    end

    def test_feed_rights
      assert_atom_text_construct("rights", method(:make_feed)) do |feed|
        feed.rights
      end
    end

    def test_entry_rights
      generator = method(:make_feed_with_open_entry)
      assert_atom_text_construct("rights", generator) do |feed|
        assert_equal(1, feed.entries.size)
        feed.entry.rights
      end
    end

    def test_entry_source
      assert_atom_source(method(:make_feed_with_open_entry_source)) do |feed|
        assert_equal(1, feed.entries.size)
        assert_not_nil(feed.entry.source)
        feed.entry.source
      end
    end

    def test_feed_subtitle
      assert_atom_text_construct("subtitle", method(:make_feed)) do |feed|
        feed.subtitle
      end
    end

    def test_feed_title
      feed = RSS::Parser.parse(make_feed(''))
      assert_equal(FEED_TITLE, feed.title.content)
    end

    def test_entry_title
      feed = RSS::Parser.parse(make_feed(''))
      assert_equal(ENTRY_TITLE, feed.entry.title.content)
    end

    def test_feed_updated
      feed = RSS::Parser.parse(make_feed(''))
      assert_equal(Time.parse(FEED_UPDATED), feed.updated.content)
    end

    def test_entry_updated
      feed = RSS::Parser.parse(make_feed(''))
      assert_equal(Time.parse(ENTRY_UPDATED), feed.entry.updated.content)
    end

    def test_entry_published
      generator = method(:make_feed_with_open_entry)
      assert_atom_date_construct("published", generator) do |feed|
        assert_equal(1, feed.entries.size)
        feed.entry.published
      end
    end

    def test_entry_summary
      generator = method(:make_feed_with_open_entry)
      assert_atom_text_construct("summary", generator) do |feed|
        assert_equal(1, feed.entries.size)
        feed.entry.summary
      end
    end
  end
end