1# frozen_string_literal: false
2require_relative "rss-testcase"
3
4require "rss/1.0"
5require "rss/dublincore"
6
7module RSS
8  class TestParser10 < TestCase
9    def test_RDF
10      assert_ns("", RDF::URI) do
11        Parser.parse(<<-EOR)
12#{make_xmldecl}
13<RDF/>
14EOR
15      end
16
17      assert_ns("", RDF::URI) do
18        Parser.parse(<<-EOR)
19#{make_xmldecl}
20<RDF xmlns="hoge"/>
21EOR
22      end
23
24      assert_ns("rdf", RDF::URI) do
25        Parser.parse(<<-EOR)
26#{make_xmldecl}
27<rdf:RDF xmlns:rdf="hoge"/>
28EOR
29      end
30
31      assert_parse(<<-EOR, :missing_tag, "channel", "RDF")
32#{make_xmldecl}
33<rdf:RDF xmlns:rdf="#{RSS::RDF::URI}"/>
34EOR
35
36      assert_parse(<<-EOR, :missing_tag, "channel", "RDF")
37#{make_xmldecl}
38<RDF xmlns="#{RSS::RDF::URI}"/>
39EOR
40
41      assert_parse(<<-EOR, :missing_tag, "channel", "RDF")
42#{make_xmldecl}
43<RDF xmlns="#{RSS::RDF::URI}"/>
44EOR
45
46      assert_parse(make_RDF(<<-EOR), :missing_tag, "item", "RDF")
47#{make_channel}
48EOR
49
50      assert_parse(make_RDF(<<-EOR), :missing_tag, "item", "RDF")
51#{make_channel}
52#{make_image}
53EOR
54
55      assert_parse(make_RDF(<<-EOR), :missing_tag, "item", "RDF")
56#{make_channel}
57#{make_textinput}
58EOR
59
60      assert_too_much_tag("image", "RDF") do
61        Parser.parse(make_RDF(<<-EOR))
62#{make_channel}
63#{make_image}
64#{make_image}
65#{make_item}
66#{make_textinput}
67EOR
68      end
69
70      assert_parse(make_RDF(<<-EOR), :nothing_raised)
71#{make_channel}
72#{make_item}
73#{make_image}
74#{make_textinput}
75EOR
76
77      assert_parse(make_RDF(<<-EOR), :nothing_raised)
78#{make_channel}
79#{make_item}
80#{make_textinput}
81#{make_image}
82EOR
83
84      assert_parse(make_RDF(<<-EOR), :nothing_raised)
85#{make_channel}
86#{make_image}
87#{make_item}
88EOR
89
90      assert_parse(make_RDF(<<-EOR), :nothing_raised)
91#{make_channel}
92#{make_image}
93#{make_item}
94#{make_textinput}
95EOR
96
97      1.step(15, 3) do |i|
98        rss = make_RDF() do
99          res = make_channel
100          i.times { res << make_item }
101          res
102        end
103        assert_parse(rss, :nothing_raised)
104      end
105    end
106
107    def test_undefined_entity
108      return unless RSS::Parser.default_parser.raise_for_undefined_entity?
109      assert_parse(make_RDF(<<-EOR), :raises, RSS::NotWellFormedError)
110#{make_channel}
111#{make_image}
112<item rdf:about="#{RDF_ABOUT}">
113  <title>#{TITLE_VALUE} &UNKNOWN_ENTITY;</title>
114  <link>#{LINK_VALUE}</link>
115  <description>#{DESCRIPTION_VALUE}</description>
116</item>
117#{make_textinput}
118EOR
119    end
120
121    def test_channel
122      assert_parse(make_RDF(<<-EOR), :missing_attribute, "channel", "rdf:about")
123<channel />
124EOR
125
126      assert_parse(make_RDF(<<-EOR), :missing_tag, "title", "channel")
127<channel rdf:about="http://example.com/"/>
128EOR
129
130      assert_parse(make_RDF(<<-EOR), :missing_tag, "link", "channel")
131<channel rdf:about="http://example.com/">
132  <title>hoge</title>
133</channel>
134EOR
135
136      assert_parse(make_RDF(<<EOR), :missing_tag, "description", "channel")
137<channel rdf:about="http://example.com/">
138  <title>hoge</title>
139  <link>http://example.com/</link>
140</channel>
141EOR
142
143      assert_parse(make_RDF(<<-EOR), :missing_tag, "items", "channel")
144<channel rdf:about="http://example.com/">
145  <title>hoge</title>
146  <link>http://example.com/</link>
147  <description>hogehoge</description>
148</channel>
149EOR
150
151      assert_parse(make_RDF(<<-EOR), :missing_attribute, "image", "rdf:resource")
152<channel rdf:about="http://example.com/">
153  <title>hoge</title>
154  <link>http://example.com/</link>
155  <description>hogehoge</description>
156  <image/>
157</channel>
158EOR
159
160      assert_parse(make_RDF(<<-EOR), :missing_tag, "items", "channel")
161<channel rdf:about="http://example.com/">
162  <title>hoge</title>
163  <link>http://example.com/</link>
164  <description>hogehoge</description>
165  <image rdf:resource="http://example.com/hoge.png" />
166</channel>
167EOR
168
169      rss = make_RDF(<<-EOR)
170<channel rdf:about="http://example.com/">
171  <title>hoge</title>
172  <link>http://example.com/</link>
173  <description>hogehoge</description>
174  <image rdf:resource="http://example.com/hoge.png" />
175  <items/>
176</channel>
177EOR
178
179      assert_missing_tag("Seq", "items") do
180        Parser.parse(rss)
181      end
182
183      assert_missing_tag("item", "RDF") do
184        Parser.parse(rss, false).validate
185      end
186
187      assert_parse(make_RDF(<<-EOR), :missing_tag, "item", "RDF")
188<channel rdf:about="http://example.com/">
189  <title>hoge</title>
190  <link>http://example.com/</link>
191  <description>hogehoge</description>
192  <image rdf:resource="http://example.com/hoge.png" />
193  <items>
194    <rdf:Seq>
195    </rdf:Seq>
196  </items>
197</channel>
198EOR
199
200      assert_parse(make_RDF(<<-EOR), :missing_attribute, "textinput", "rdf:resource")
201<channel rdf:about="http://example.com/">
202  <title>hoge</title>
203  <link>http://example.com/</link>
204  <description>hogehoge</description>
205  <image rdf:resource="http://example.com/hoge.png" />
206  <items>
207    <rdf:Seq>
208    </rdf:Seq>
209  </items>
210  <textinput/>
211</channel>
212EOR
213
214      assert_parse(make_RDF(<<-EOR), :missing_tag, "item", "RDF")
215<channel rdf:about="http://example.com/">
216  <title>hoge</title>
217  <link>http://example.com/</link>
218  <description>hogehoge</description>
219  <image rdf:resource="http://example.com/hoge.png" />
220  <items>
221    <rdf:Seq>
222    </rdf:Seq>
223  </items>
224  <textinput rdf:resource="http://example.com/search" />
225</channel>
226EOR
227    end
228
229    def test_rdf_li
230      rss = make_RDF(<<-EOR)
231<channel rdf:about="http://example.com/">
232  <title>hoge</title>
233  <link>http://example.com/</link>
234  <description>hogehoge</description>
235  <image rdf:resource="http://example.com/hoge.png" />
236  <items>
237    <rdf:Seq>
238      <rdf:li \#{rdf_li_attr}/>
239    </rdf:Seq>
240  </items>
241  <textinput rdf:resource="http://example.com/search" />
242</channel>
243#{make_item}
244EOR
245
246      source = Proc.new do |rdf_li_attr|
247        eval(%Q[%Q[#{rss}]], binding)
248      end
249
250      attr = %q[resource="http://example.com/hoge"]
251      assert_parse(source.call(attr), :nothing_raised)
252
253      attr = %q[rdf:resource="http://example.com/hoge"]
254      assert_parse(source.call(attr), :nothing_raised)
255
256      assert_parse(source.call(""), :missing_attribute, "li", "resource")
257    end
258
259    def test_image
260      assert_parse(make_RDF(<<-EOR), :missing_attribute, "image", "rdf:about")
261#{make_channel}
262<image>
263</image>
264EOR
265
266      assert_parse(make_RDF(<<-EOR), :missing_tag, "title", "image")
267#{make_channel}
268<image rdf:about="http://example.com/hoge.png">
269</image>
270EOR
271
272      assert_parse(make_RDF(<<-EOR), :missing_tag, "url", "image")
273#{make_channel}
274<image rdf:about="http://example.com/hoge.png">
275  <title>hoge</title>
276</image>
277EOR
278
279      assert_parse(make_RDF(<<-EOR), :missing_tag, "link", "image")
280#{make_channel}
281<image rdf:about="http://example.com/hoge.png">
282  <title>hoge</title>
283  <url>http://example.com/hoge.png</url>
284</image>
285EOR
286
287      assert_parse(make_RDF(<<-EOR), :missing_tag, "item", "RDF")
288#{make_channel}
289<image rdf:about="http://example.com/hoge.png">
290  <title>hoge</title>
291  <url>http://example.com/hoge.png</url>
292  <link>http://example.com/</link>
293</image>
294EOR
295
296      rss = make_RDF(<<-EOR)
297#{make_channel}
298<image rdf:about="http://example.com/hoge.png">
299  <link>http://example.com/</link>
300  <url>http://example.com/hoge.png</url>
301  <title>hoge</title>
302</image>
303EOR
304
305      assert_missing_tag("item", "RDF") do
306        Parser.parse(rss)
307      end
308
309      assert_missing_tag("item", "RDF") do
310        Parser.parse(rss, false).validate
311      end
312    end
313
314    def test_item
315      assert_parse(make_RDF(<<-EOR), :missing_attribute, "item", "rdf:about")
316#{make_channel}
317#{make_image}
318<item>
319</item>
320EOR
321
322      assert_parse(make_RDF(<<-EOR), :missing_tag, "title", "item")
323#{make_channel}
324#{make_image}
325<item rdf:about="http://example.com/hoge.html">
326</item>
327EOR
328
329      assert_parse(make_RDF(<<-EOR), :missing_tag, "link", "item")
330#{make_channel}
331#{make_image}
332<item rdf:about="http://example.com/hoge.html">
333  <title>hoge</title>
334</item>
335EOR
336
337      assert_too_much_tag("title", "item") do
338        Parser.parse(make_RDF(<<-EOR))
339#{make_channel}
340#{make_image}
341<item rdf:about="http://example.com/hoge.html">
342  <title>hoge</title>
343  <title>hoge</title>
344  <link>http://example.com/hoge.html</link>
345</item>
346EOR
347      end
348
349      assert_parse(make_RDF(<<-EOR), :nothing_raised)
350#{make_channel}
351#{make_image}
352<item rdf:about="http://example.com/hoge.html">
353  <title>hoge</title>
354  <link>http://example.com/hoge.html</link>
355</item>
356EOR
357
358      assert_parse(make_RDF(<<-EOR), :nothing_raised)
359#{make_channel}
360#{make_image}
361<item rdf:about="http://example.com/hoge.html">
362  <title>hoge</title>
363  <link>http://example.com/hoge.html</link>
364  <description>hogehoge</description>
365</item>
366EOR
367    end
368
369    def test_textinput
370      assert_parse(make_RDF(<<-EOR), :missing_attribute, "textinput", "rdf:about")
371#{make_channel}
372#{make_image}
373#{make_item}
374<textinput>
375</textinput>
376EOR
377
378      assert_parse(make_RDF(<<-EOR), :missing_tag, "title", "textinput")
379#{make_channel}
380#{make_image}
381#{make_item}
382<textinput rdf:about="http://example.com/search.html">
383</textinput>
384EOR
385
386      assert_parse(make_RDF(<<-EOR), :missing_tag, "description", "textinput")
387#{make_channel}
388#{make_image}
389#{make_item}
390<textinput rdf:about="http://example.com/search.html">
391  <title>hoge</title>
392</textinput>
393EOR
394
395      assert_too_much_tag("title", "textinput") do
396        Parser.parse(make_RDF(<<-EOR))
397#{make_channel}
398#{make_image}
399#{make_item}
400<textinput rdf:about="http://example.com/search.html">
401  <title>hoge</title>
402  <title>hoge</title>
403  <description>hogehoge</description>
404</textinput>
405EOR
406      end
407
408      assert_parse(make_RDF(<<-EOR), :missing_tag, "name", "textinput")
409#{make_channel}
410#{make_image}
411#{make_item}
412<textinput rdf:about="http://example.com/search.html">
413  <title>hoge</title>
414  <description>hogehoge</description>
415</textinput>
416EOR
417
418      assert_parse(make_RDF(<<-EOR), :missing_tag, "link", "textinput")
419#{make_channel}
420#{make_image}
421#{make_item}
422<textinput rdf:about="http://example.com/search.html">
423  <title>hoge</title>
424  <description>hogehoge</description>
425  <name>key</name>
426</textinput>
427EOR
428
429      assert_parse(make_RDF(<<-EOR), :nothing_raised)
430#{make_channel}
431#{make_image}
432#{make_item}
433<textinput rdf:about="http://example.com/search.html">
434  <title>hoge</title>
435  <description>hogehoge</description>
436  <name>key</name>
437  <link>http://example.com/search.html</link>
438</textinput>
439EOR
440    end
441
442    def test_ignore
443      name = "a"
444      rss = make_RDF(<<-EOR)
445#{make_channel}
446#{make_item}
447<#{name}/>
448EOR
449      assert_not_expected_tag(name, ::RSS::URI, "RDF") do
450        Parser.parse(rss, true, false)
451      end
452
453      uri = ""
454      name = "a"
455      rss = make_RDF(<<-EOR)
456#{make_channel}
457#{make_item}
458<#{name} xmlns=""/>
459EOR
460      assert_parse(rss, :nothing_raised)
461      assert_not_expected_tag(name, uri, "RDF") do
462        Parser.parse(rss, true, false)
463      end
464
465      uri = "http://example.com/"
466      name = "a"
467      rss = make_RDF(<<-EOR)
468#{make_channel}
469#{make_item}
470<x:#{name} xmlns:x="#{uri}"/>
471EOR
472      assert_parse(rss, :nothing_raised)
473      assert_not_expected_tag(name, uri, "RDF") do
474        Parser.parse(rss, true, false)
475      end
476
477      uri = ::RSS::URI
478      name = "a"
479      rss = make_RDF(<<-EOR)
480#{make_channel}
481#{make_item}
482#{make_image("<#{name}/>")}
483EOR
484      assert_parse(rss, :nothing_raised)
485      assert_not_expected_tag(name, uri, "image") do
486        Parser.parse(rss, true, false)
487      end
488
489      uri = CONTENT_URI
490      name = "encoded"
491      elem = "<#{name} xmlns='#{uri}'/>"
492      rss = make_RDF(<<-EOR)
493#{make_channel}
494#{make_item}
495#{make_image(elem)}
496EOR
497      assert_parse(rss, :nothing_raised)
498      assert_not_expected_tag(name, uri, "image") do
499        Parser.parse(rss, true, false)
500      end
501    end
502
503    def test_unknown_duplicated_element
504      xmlns = {"test" => "http://localhost/test"}
505      assert_parse(make_RDF(<<-EOR, xmlns), :nothing_raised)
506        #{make_channel("<test:string/>")}
507        #{make_item}
508        #{make_image}
509      EOR
510    end
511
512    def test_unknown_case_insensitive_duplicated_element
513      xmlns = {
514        "foaf" => "http://xmlns.com/foaf/0.1/",
515        "dc" => "http://purl.org/dc/elements/1.1/",
516      }
517      assert_parse(make_RDF(<<-EOR, xmlns), :nothing_raised)
518        #{make_channel}
519        #{make_item}
520        #{make_image}
521        <foaf:Image rdf:about="http://example.com/myself.png">
522          <dc:title>Myself</dc:title>
523          <dc:link>http://example.com/</dc:link>
524        </foaf:Image>
525      EOR
526    end
527  end
528end
529
530