1require "rss-testcase"
2
3require "rss/2.0"
4
5module RSS
6  class TestParser20 < TestCase
7    def test_rss20
8      assert_parse(make_rss20(<<-EOR), :missing_tag, "channel", "rss")
9EOR
10
11      assert_parse(make_rss20(<<-EOR), :nothing_raised)
12#{make_channel20("")}
13EOR
14    end
15
16    def test_cloud20
17      attrs = [
18        ["domain", CLOUD_DOMAIN],
19        ["port", CLOUD_PORT],
20        ["path", CLOUD_PATH],
21        ["registerProcedure", CLOUD_REGISTER_PROCEDURE],
22        ["protocol", CLOUD_PROTOCOL],
23      ]
24
25      (attrs.size + 1).times do |i|
26        missing_attr = attrs[i]
27        if missing_attr
28          meth = :missing_attribute
29          args = ["cloud", missing_attr[0]]
30        else
31          meth = :nothing_raised
32          args = []
33        end
34
35        cloud_attrs = []
36        attrs.each_with_index do |attr, j|
37          unless i == j
38            cloud_attrs << %Q[#{attr[0]}="#{attr[1]}"]
39          end
40        end
41
42        assert_parse(make_rss20(<<-EOR), meth, *args)
43#{make_channel20(%Q[<cloud #{cloud_attrs.join("\n")}/>])}
44EOR
45      end
46    end
47
48    def test_source20
49        assert_parse(make_rss20(<<-EOR), :missing_attribute, "source", "url")
50#{make_channel20(make_item20(%Q[<source>Example</source>]))}
51EOR
52
53        assert_parse(make_rss20(<<-EOR), :nothing_raised)
54#{make_channel20(make_item20(%Q[<source url="http://example.com/" />]))}
55EOR
56
57        assert_parse(make_rss20(<<-EOR), :nothing_raised)
58#{make_channel20(make_item20(%Q[<source url="http://example.com/">Example</source>]))}
59EOR
60    end
61
62    def test_enclosure20
63      attrs = [
64        ["url", ENCLOSURE_URL],
65        ["length", ENCLOSURE_LENGTH],
66        ["type", ENCLOSURE_TYPE],
67      ]
68
69      (attrs.size + 1).times do |i|
70        missing_attr = attrs[i]
71        if missing_attr
72          meth = :missing_attribute
73          args = ["enclosure", missing_attr[0]]
74        else
75          meth = :nothing_raised
76          args = []
77        end
78
79        enclosure_attrs = []
80        attrs.each_with_index do |attr, j|
81          unless i == j
82            enclosure_attrs << %Q[#{attr[0]}="#{attr[1]}"]
83          end
84        end
85
86        assert_parse(make_rss20(<<-EOR), meth, *args)
87#{make_channel20(%Q[
88#{make_item20(%Q[
89<enclosure
90  #{enclosure_attrs.join("\n")} />
91    ])}
92  ])}
93EOR
94      end
95    end
96
97    def test_category20
98      values = [nil, CATEGORY_DOMAIN]
99      values.each do |value|
100        domain = ""
101        domain << %Q[domain="#{value}"] if value
102
103        ["", "Example Text"].each do |text|
104          rss_src = make_rss20(<<-EOR)
105#{make_channel20(%Q[
106#{make_item20(%Q[
107<category #{domain}>#{text}</category>
108    ])}
109  ])}
110EOR
111          assert_parse(rss_src, :nothing_raised)
112
113          rss = RSS::Parser.parse(rss_src)
114          category = rss.items.last.categories.first
115          assert_equal(value, category.domain)
116          assert_equal(text, category.content)
117        end
118      end
119    end
120  end
121end
122
123