1require "rss-testcase"
2
3require "rss/maker"
4
5module RSS
6  class TestSetupMaker20 < TestCase
7
8    def test_setup_maker_channel
9      title = "fugafuga"
10      link = "http://hoge.com"
11      description = "fugafugafugafuga"
12      language = "ja"
13      copyright = "foo"
14      managingEditor = "bar"
15      webMaster = "web master"
16      rating = '(PICS-1.1 "http://www.rsac.org/ratingsv01.html" l gen true comment "RSACi North America Server" for "http://www.rsac.org" on "1996.04.16T08:15-0500" r (n 0 s 0 v 0 l 0))'
17      docs = "http://foo.com/doc"
18      skipDays = [
19        "Sunday",
20        "Monday",
21      ]
22      skipHours = [
23        "0",
24        "13",
25      ]
26      pubDate = Time.now
27      lastBuildDate = Time.now
28      categories = [
29        "Nespapers",
30        "misc",
31      ]
32      generator = "RSS Maker"
33      ttl = "60"
34
35      rss = RSS::Maker.make("2.0") do |maker|
36        maker.channel.title = title
37        maker.channel.link = link
38        maker.channel.description = description
39        maker.channel.language = language
40        maker.channel.copyright = copyright
41        maker.channel.managingEditor = managingEditor
42        maker.channel.webMaster = webMaster
43        maker.channel.rating = rating
44        maker.channel.docs = docs
45        maker.channel.pubDate = pubDate
46        maker.channel.lastBuildDate = lastBuildDate
47
48        skipDays.each do |day|
49          maker.channel.skipDays.new_day do |new_day|
50            new_day.content = day
51          end
52        end
53        skipHours.each do |hour|
54          maker.channel.skipHours.new_hour do |new_hour|
55            new_hour.content = hour
56          end
57        end
58
59
60        categories.each do |category|
61          maker.channel.categories.new_category do |new_category|
62            new_category.content = category
63          end
64        end
65
66        maker.channel.generator = generator
67        maker.channel.ttl = ttl
68      end
69
70      new_rss = RSS::Maker.make("2.0") do |maker|
71        rss.channel.setup_maker(maker)
72      end
73      channel = new_rss.channel
74
75      assert_equal(title, channel.title)
76      assert_equal(link, channel.link)
77      assert_equal(description, channel.description)
78      assert_equal(language, channel.language)
79      assert_equal(copyright, channel.copyright)
80      assert_equal(managingEditor, channel.managingEditor)
81      assert_equal(webMaster, channel.webMaster)
82      assert_equal(rating, channel.rating)
83      assert_equal(docs, channel.docs)
84      assert_equal(pubDate, channel.pubDate)
85      assert_equal(lastBuildDate, channel.lastBuildDate)
86
87      skipDays.each_with_index do |day, i|
88        assert_equal(day, channel.skipDays.days[i].content)
89      end
90      skipHours.each_with_index do |hour, i|
91        assert_equal(hour.to_i, channel.skipHours.hours[i].content)
92      end
93
94
95      channel.categories.each_with_index do |category, i|
96        assert_equal(categories[i], category.content)
97      end
98
99      assert_equal(generator, channel.generator)
100      assert_equal(ttl.to_i, channel.ttl)
101
102
103      assert(channel.items.empty?)
104      assert_nil(channel.image)
105      assert_nil(channel.textInput)
106    end
107
108    def test_setup_maker_image
109      title = "fugafuga"
110      link = "http://hoge.com"
111      url = "http://hoge.com/hoge.png"
112      width = "144"
113      height = "400"
114      description = "an image"
115
116      rss = RSS::Maker.make("2.0") do |maker|
117        setup_dummy_channel(maker)
118        maker.channel.link = link
119
120        maker.image.title = title
121        maker.image.url = url
122        maker.image.width = width
123        maker.image.height = height
124        maker.image.description = description
125      end
126
127      new_rss = RSS::Maker.make("2.0") do |maker|
128        rss.channel.setup_maker(maker)
129        rss.image.setup_maker(maker)
130      end
131
132      image = new_rss.image
133      assert_equal(title, image.title)
134      assert_equal(link, image.link)
135      assert_equal(url, image.url)
136      assert_equal(width.to_i, image.width)
137      assert_equal(height.to_i, image.height)
138      assert_equal(description, image.description)
139    end
140
141    def test_setup_maker_textinput
142      title = "fugafuga"
143      description = "text hoge fuga"
144      name = "hoge"
145      link = "http://hoge.com"
146
147      rss = RSS::Maker.make("2.0") do |maker|
148        setup_dummy_channel(maker)
149
150        maker.textinput.title = title
151        maker.textinput.description = description
152        maker.textinput.name = name
153        maker.textinput.link = link
154      end
155
156      new_rss = RSS::Maker.make("2.0") do |maker|
157        rss.channel.setup_maker(maker)
158        rss.textinput.setup_maker(maker)
159      end
160
161      textInput = new_rss.channel.textInput
162      assert_equal(title, textInput.title)
163      assert_equal(description, textInput.description)
164      assert_equal(name, textInput.name)
165      assert_equal(link, textInput.link)
166    end
167
168    def test_setup_maker_items(for_backward_compatibility=false)
169      title = "TITLE"
170      link = "http://hoge.com/"
171      description = "text hoge fuga"
172      author = "oprah@oxygen.net"
173      comments = "http://www.myblog.org/cgi-local/mt/mt-comments.cgi?entry_id=290"
174      pubDate = Time.now
175
176      guid_isPermaLink = "true"
177      guid_content = "http://inessential.com/2002/09/01.php#a2"
178
179      enclosure_url = "http://www.scripting.com/mp3s/weatherReportSuite.mp3"
180      enclosure_length = "12216320"
181      enclosure_type = "audio/mpeg"
182
183      source_url = "http://static.userland.com/tomalak/links2.xml"
184      source_content = "Tomalak's Realm"
185
186      category_domain = "http://www.fool.com/cusips"
187      category_content = "MSFT"
188
189      item_size = 5
190
191      rss = RSS::Maker.make("2.0") do |maker|
192        setup_dummy_channel(maker)
193
194        item_size.times do |i|
195          maker.items.new_item do |item|
196            item.title = "#{title}#{i}"
197            item.link = "#{link}#{i}"
198            item.description = "#{description}#{i}"
199            item.author = "#{author}#{i}"
200            item.comments = "#{comments}#{i}"
201            item.date = pubDate
202
203            item.guid.isPermaLink = guid_isPermaLink
204            item.guid.content = guid_content
205
206            item.enclosure.url = enclosure_url
207            item.enclosure.length = enclosure_length
208            item.enclosure.type = enclosure_type
209
210            item.source.url = source_url
211            item.source.content = source_content
212
213            category = item.categories.new_category
214            category.domain = category_domain
215            category.content = category_content
216          end
217        end
218      end
219
220      new_rss = RSS::Maker.make("2.0") do |maker|
221        rss.channel.setup_maker(maker)
222
223        rss.items.each do |item|
224          if for_backward_compatibility
225            item.setup_maker(maker)
226          else
227            item.setup_maker(maker.items)
228          end
229        end
230      end
231
232      assert_equal(item_size, new_rss.items.size)
233      new_rss.items.each_with_index do |item, i|
234        assert_equal("#{title}#{i}", item.title)
235        assert_equal("#{link}#{i}", item.link)
236        assert_equal("#{description}#{i}", item.description)
237        assert_equal("#{author}#{i}", item.author)
238        assert_equal("#{comments}#{i}", item.comments)
239        assert_equal(pubDate, item.pubDate)
240
241        assert_equal(guid_isPermaLink == "true", item.guid.isPermaLink)
242        assert_equal(guid_content, item.guid.content)
243
244        assert_equal(enclosure_url, item.enclosure.url)
245        assert_equal(enclosure_length.to_i, item.enclosure.length)
246        assert_equal(enclosure_type, item.enclosure.type)
247
248        assert_equal(source_url, item.source.url)
249        assert_equal(source_content, item.source.content)
250
251        assert_equal(1, item.categories.size)
252        assert_equal(category_domain, item.category.domain)
253        assert_equal(category_content, item.category.content)
254      end
255
256    end
257
258    def test_setup_maker_items_backward_compatibility
259      test_setup_maker_items(true)
260    end
261
262    def test_setup_maker
263      encoding = "EUC-JP"
264      standalone = true
265
266      href = 'a.xsl'
267      type = 'text/xsl'
268      title = 'sample'
269      media = 'printer'
270      charset = 'UTF-8'
271      alternate = 'yes'
272
273      rss = RSS::Maker.make("2.0") do |maker|
274        maker.encoding = encoding
275        maker.standalone = standalone
276
277        maker.xml_stylesheets.new_xml_stylesheet do |xss|
278          xss.href = href
279          xss.type = type
280          xss.title = title
281          xss.media = media
282          xss.charset = charset
283          xss.alternate = alternate
284        end
285
286        setup_dummy_channel(maker)
287      end
288
289      new_rss = RSS::Maker.make("2.0") do |maker|
290        rss.setup_maker(maker)
291      end
292
293      assert_equal("2.0", new_rss.rss_version)
294      assert_equal(encoding, new_rss.encoding)
295      assert_equal(standalone, new_rss.standalone)
296
297      xss = rss.xml_stylesheets.first
298      assert_equal(1, rss.xml_stylesheets.size)
299      assert_equal(href, xss.href)
300      assert_equal(type, xss.type)
301      assert_equal(title, xss.title)
302      assert_equal(media, xss.media)
303      assert_equal(charset, xss.charset)
304      assert_equal(alternate, xss.alternate)
305    end
306
307  end
308end
309