From b684f87574166a42d3305c005896bf2aa7cd37b3 Mon Sep 17 00:00:00 2001 From: kou Date: Sun, 28 Nov 2004 06:53:41 +0000 Subject: [PATCH] * lib/rss/rss.rb (RSS::NotSetError): added. * lib/rss/maker/{1.0,0.9,2.0}.rb: changed RSS Maker to raise RSS::NotSetError if required values of maker.channel are not set. * test/rss/test_maker_{1.0,0.9,2.0}.rb: changed tests to check RSS Maker raises or not. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7401 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 11 +++++ lib/rss/maker/0.9.rb | 28 +++++------ lib/rss/maker/1.0.rb | 43 +++++++---------- lib/rss/maker/2.0.rb | 4 ++ lib/rss/maker/base.rb | 82 +++++++++++++++++++------------ lib/rss/rss.rb | 9 ++++ test/rss/rss-assertions.rb | 12 +++++ test/rss/test_maker_0.9.rb | 98 ++++++++++++++++++++------------------ test/rss/test_maker_1.0.rb | 84 +++++++++++++++++--------------- test/rss/test_maker_2.0.rb | 79 ++++++++++++++++-------------- 10 files changed, 256 insertions(+), 194 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0164abb503..248e7a0f29 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +Sun Nov 28 15:51:40 2004 Kouhei Sutou + + * lib/rss/rss.rb (RSS::NotSetError): added. + + * lib/rss/maker/{1.0,0.9,2.0}.rb: changed RSS Maker to raise + RSS::NotSetError if required values of maker.channel are not + set. + + * test/rss/test_maker_{1.0,0.9,2.0}.rb: changed tests to check RSS + Maker raises or not. + Sun Nov 28 12:14:47 2004 Kazuo Saito * regparse.c (fetch_token): fixed test failure on HP-UX ia64 diff --git a/lib/rss/maker/0.9.rb b/lib/rss/maker/0.9.rb index 11f518ca27..b82585fb96 100644 --- a/lib/rss/maker/0.9.rb +++ b/lib/rss/maker/0.9.rb @@ -11,23 +11,15 @@ module RSS super end - def to_rss - rss = Rss.new(@rss_version, @version, @encoding, @standalone) - setup_xml_stylesheets(rss) - setup_channel(rss) - setup_other_elements(rss) - if rss.channel - rss - else - nil - end - end - private - def setup_channel(rss) - @channel.to_rss(rss) + def make_rss + Rss.new(@rss_version, @version, @encoding, @standalone) end - + + def setup_elements(rss) + setup_channel(rss) + end + class Channel < ChannelBase def to_rss(rss) @@ -44,6 +36,8 @@ module RSS else nil end + elsif variable_is_set? + raise NotSetError.new("maker.channel", not_set_required_variables) end end @@ -68,6 +62,10 @@ module RSS super + ["pubDate"] end + def required_variable_names + %w(title link description language) + end + class SkipDays < SkipDaysBase def to_rss(rss, channel) unless @days.empty? diff --git a/lib/rss/maker/1.0.rb b/lib/rss/maker/1.0.rb index ae22eb4fc4..e38a2f89e0 100644 --- a/lib/rss/maker/1.0.rb +++ b/lib/rss/maker/1.0.rb @@ -11,41 +11,22 @@ module RSS super("1.0") end - def to_rss - rss = RDF.new(@version, @encoding, @standalone) - setup_xml_stylesheets(rss) + private + def make_rss + RDF.new(@version, @encoding, @standalone) + end + + def setup_elements(rss) setup_channel(rss) setup_image(rss) setup_items(rss) setup_textinput(rss) - setup_other_elements(rss) - if rss.channel - rss - else - nil - end - end - - private - def setup_channel(rss) - @channel.to_rss(rss) - end - - def setup_image(rss) - @image.to_rss(rss) - end - - def setup_items(rss) - @items.to_rss(rss) - end - - def setup_textinput(rss) - @textinput.to_rss(rss) end class Channel < ChannelBase def to_rss(rss) + set = false if @about channel = RDF::Channel.new(@about) set = setup_values(channel) @@ -57,12 +38,16 @@ module RSS setup_other_elements(rss) end end + + if (!@about or !set) and variable_is_set? + raise NotSetError.new("maker.channel", not_set_required_variables) + end end def have_required_values? @about and @title and @link and @description end - + private def setup_items(rss) items = RDF::Channel::Items.new @@ -86,6 +71,10 @@ module RSS end end + def required_variable_names + %w(about title link description) + end + class SkipDays < SkipDaysBase def to_rss(*args) end diff --git a/lib/rss/maker/2.0.rb b/lib/rss/maker/2.0.rb index 8ac5e7bbb3..9094b1e73b 100644 --- a/lib/rss/maker/2.0.rb +++ b/lib/rss/maker/2.0.rb @@ -17,6 +17,10 @@ module RSS @title and @link and @description end + def required_variable_names + %w(title link description) + end + class SkipDays < RSS09::Channel::SkipDays class Day < RSS09::Channel::SkipDays::Day end diff --git a/lib/rss/maker/base.rb b/lib/rss/maker/base.rb index 7301e53eb6..9b633f4695 100644 --- a/lib/rss/maker/base.rb +++ b/lib/rss/maker/base.rb @@ -105,6 +105,23 @@ module RSS name end end + + def variable_is_set? + variables.find {|var| !__send__(var).nil?} + end + + def not_set_required_variables + required_variable_names.find_all do |var| + __send__(var).nil? + end + end + + def required_variables_are_set? + required_variable_names.each do |var| + return false if __send__(var).nil? + end + true + end end @@ -117,9 +134,22 @@ module RSS end end - attr_reader :rss_version, :xml_stylesheets - attr_reader :channel, :image, :items, :textinput + %w(xml_stylesheets channel image items textinput).each do |element| + attr_reader element + add_need_initialize_variable(element, "make_#{element}") + module_eval(<<-EOC, __FILE__, __LINE__) + private + def setup_#{element}(rss) + @#{element}.to_rss(rss) + end + + def make_#{element} + self.class::#{element[0,1].upcase}#{element[1..-1]}.new(self) + end +EOC + end + attr_reader :rss_version attr_accessor :version, :encoding, :standalone def initialize(rss_version) @@ -128,47 +158,39 @@ module RSS @version = "1.0" @encoding = "UTF-8" @standalone = nil - @xml_stylesheets = make_xml_stylesheets - @channel = make_channel - @image = make_image - @items = make_items - @textinput = make_textinput end - def make(&block) - block.call(self) if block - to_rss + def make + if block_given? + yield(self) + to_rss + else + nil + end end + def to_rss + rss = make_rss + setup_xml_stylesheets(rss) + setup_elements(rss) + setup_other_elements(rss) + if rss.channel + rss + else + nil + end + end + def current_element(rss) rss end private + undef make_xml_stylesheets def make_xml_stylesheets XMLStyleSheets.new(self) end - def make_channel - self.class::Channel.new(self) - end - - def make_image - self.class::Image.new(self) - end - - def make_items - self.class::Items.new(self) - end - - def make_textinput - self.class::Textinput.new(self) - end - - def setup_xml_stylesheets(rss) - @xml_stylesheets.to_rss(rss) - end - end class XMLStyleSheets diff --git a/lib/rss/rss.rb b/lib/rss/rss.rb index b6f67b86e8..9bf94986e7 100644 --- a/lib/rss/rss.rb +++ b/lib/rss/rss.rb @@ -144,6 +144,15 @@ module RSS end end + class NotSetError < Error + attr_reader :name, :variables + def initialize(name, variables) + @name = name + @variables = variables + super("required variables of #{@name} are not set: #{@variables.join(', ')}") + end + end + module BaseModel include Utils diff --git a/test/rss/rss-assertions.rb b/test/rss/rss-assertions.rb index a51fc1fd60..569e9621d2 100644 --- a/test/rss/rss-assertions.rb +++ b/test/rss/rss-assertions.rb @@ -95,6 +95,18 @@ module RSS end end + def assert_not_set_error(name, variables) + _wrap_assertion do + begin + yield + flunk("Not raise NotSetError") + rescue ::RSS::NotSetError => e + assert_equal(name, e.name) + assert_equal(variables.sort, e.variables.sort) + end + end + end + def assert_xml_declaration(version, encoding, standalone, rss) _wrap_assertion do assert_equal(version, rss.version) diff --git a/test/rss/test_maker_0.9.rb b/test/rss/test_maker_0.9.rb index 7dfe0da241..50c311ad94 100644 --- a/test/rss/test_maker_0.9.rb +++ b/test/rss/test_maker_0.9.rb @@ -121,39 +121,43 @@ module RSS link = "http://hoge.com" description = "fugafugafugafuga" language = "ja" - - rss = RSS::Maker.make("0.91") do |maker| - # maker.channel.title = title - maker.channel.link = link - maker.channel.description = description - maker.channel.language = language - end - assert_nil(rss) - rss = RSS::Maker.make("0.91") do |maker| - maker.channel.title = title - # maker.channel.link = link - maker.channel.link = nil - maker.channel.description = description - maker.channel.language = language + assert_not_set_error("maker.channel", %w(title)) do + RSS::Maker.make("0.91") do |maker| + # maker.channel.title = title + maker.channel.link = link + maker.channel.description = description + maker.channel.language = language + end end - assert_nil(rss) - rss = RSS::Maker.make("0.91") do |maker| - maker.channel.title = title - maker.channel.link = link - # maker.channel.description = description - maker.channel.language = language + assert_not_set_error("maker.channel", %w(link)) do + RSS::Maker.make("0.91") do |maker| + maker.channel.title = title + # maker.channel.link = link + maker.channel.link = nil + maker.channel.description = description + maker.channel.language = language + end end - assert_nil(rss) - rss = RSS::Maker.make("0.91") do |maker| - maker.channel.title = title - maker.channel.link = link - maker.channel.description = description - # maker.channel.language = language + assert_not_set_error("maker.channel", %w(description)) do + RSS::Maker.make("0.91") do |maker| + maker.channel.title = title + maker.channel.link = link + # maker.channel.description = description + maker.channel.language = language + end + end + + assert_not_set_error("maker.channel", %w(language)) do + RSS::Maker.make("0.91") do |maker| + maker.channel.title = title + maker.channel.link = link + maker.channel.description = description + # maker.channel.language = language + end end - assert_nil(rss) end def test_image @@ -182,17 +186,18 @@ module RSS assert_equal(height, image.height) assert_equal(description, image.description) - rss = RSS::Maker.make("0.91") do |maker| - # setup_dummy_channel(maker) - maker.channel.link = link + assert_not_set_error("maker.channel", %w(description title language)) do + RSS::Maker.make("0.91") do |maker| + # setup_dummy_channel(maker) + maker.channel.link = link - maker.image.title = title - maker.image.url = url - maker.image.width = width - maker.image.height = height - maker.image.description = description + maker.image.title = title + maker.image.url = url + maker.image.width = width + maker.image.height = height + maker.image.description = description + end end - assert_nil(rss) end def test_not_valid_image @@ -215,18 +220,19 @@ module RSS end assert_nil(rss.channel.image) - rss = RSS::Maker.make("0.91") do |maker| - setup_dummy_channel(maker) - # maker.channel.link = link - maker.channel.link = nil + assert_not_set_error("maker.channel", %w(link)) do + RSS::Maker.make("0.91") do |maker| + setup_dummy_channel(maker) + # maker.channel.link = link + maker.channel.link = nil - maker.image.title = title - maker.image.url = url - maker.image.width = width - maker.image.height = height - maker.image.description = description + maker.image.title = title + maker.image.url = url + maker.image.width = width + maker.image.height = height + maker.image.description = description + end end - assert_nil(rss) rss = RSS::Maker.make("0.91") do |maker| setup_dummy_channel(maker) diff --git a/test/rss/test_maker_1.0.rb b/test/rss/test_maker_1.0.rb index 8f80f6e2fe..1fa86662e1 100644 --- a/test/rss/test_maker_1.0.rb +++ b/test/rss/test_maker_1.0.rb @@ -85,37 +85,41 @@ module RSS link = "http://hoge.com" description = "fugafugafugafuga" - rss = RSS::Maker.make("1.0") do |maker| - # maker.channel.about = about - maker.channel.title = title - maker.channel.link = link - maker.channel.description = description + assert_not_set_error("maker.channel", %w(about)) do + RSS::Maker.make("1.0") do |maker| + # maker.channel.about = about + maker.channel.title = title + maker.channel.link = link + maker.channel.description = description + end end - assert_nil(rss) - rss = RSS::Maker.make("1.0") do |maker| - maker.channel.about = about - # maker.channel.title = title - maker.channel.link = link - maker.channel.description = description + assert_not_set_error("maker.channel", %w(title)) do + RSS::Maker.make("1.0") do |maker| + maker.channel.about = about + # maker.channel.title = title + maker.channel.link = link + maker.channel.description = description + end end - assert_nil(rss) - rss = RSS::Maker.make("1.0") do |maker| - maker.channel.about = about - maker.channel.title = title - # maker.channel.link = link - maker.channel.description = description + assert_not_set_error("maker.channel", %w(link)) do + RSS::Maker.make("1.0") do |maker| + maker.channel.about = about + maker.channel.title = title + # maker.channel.link = link + maker.channel.description = description + end end - assert_nil(rss) - rss = RSS::Maker.make("1.0") do |maker| - maker.channel.about = about - maker.channel.title = title - maker.channel.link = link - # maker.channel.description = description + assert_not_set_error("maker.channel", %w(description)) do + RSS::Maker.make("1.0") do |maker| + maker.channel.about = about + maker.channel.title = title + maker.channel.link = link + # maker.channel.description = description + end end - assert_nil(rss) end @@ -138,14 +142,15 @@ module RSS assert_equal(link, image.link) assert_equal(url, image.url) - rss = RSS::Maker.make("1.0") do |maker| - # setup_dummy_channel(maker) - maker.channel.link = link - - maker.image.title = title - maker.image.url = url + assert_not_set_error("maker.channel", %w(about title description)) do + RSS::Maker.make("1.0") do |maker| + # setup_dummy_channel(maker) + maker.channel.link = link + + maker.image.title = title + maker.image.url = url + end end - assert_nil(rss) end def test_not_valid_image @@ -173,15 +178,16 @@ module RSS assert_nil(rss.channel.image) assert_nil(rss.image) - rss = RSS::Maker.make("1.0") do |maker| - setup_dummy_channel(maker) - # maker.channel.link = link - maker.channel.link = nil - - maker.image.url = url - maker.image.title = title + assert_not_set_error("maker.channel", %w(link)) do + RSS::Maker.make("1.0") do |maker| + setup_dummy_channel(maker) + # maker.channel.link = link + maker.channel.link = nil + + maker.image.url = url + maker.image.title = title + end end - assert_nil(rss) end def test_items diff --git a/test/rss/test_maker_2.0.rb b/test/rss/test_maker_2.0.rb index dc08ada34b..e2d5e32280 100644 --- a/test/rss/test_maker_2.0.rb +++ b/test/rss/test_maker_2.0.rb @@ -137,29 +137,32 @@ module RSS description = "fugafugafugafuga" language = "ja" - rss = RSS::Maker.make("2.0") do |maker| - # maker.channel.title = title - maker.channel.link = link - maker.channel.description = description - maker.channel.language = language + assert_not_set_error("maker.channel", %w(title)) do + RSS::Maker.make("2.0") do |maker| + # maker.channel.title = title + maker.channel.link = link + maker.channel.description = description + maker.channel.language = language + end end - assert_nil(rss) - rss = RSS::Maker.make("2.0") do |maker| - maker.channel.title = title - # maker.channel.link = link - maker.channel.description = description - maker.channel.language = language + assert_not_set_error("maker.channel", %w(link)) do + RSS::Maker.make("2.0") do |maker| + maker.channel.title = title + # maker.channel.link = link + maker.channel.description = description + maker.channel.language = language + end end - assert_nil(rss) - rss = RSS::Maker.make("2.0") do |maker| - maker.channel.title = title - maker.channel.link = link - # maker.channel.description = description - maker.channel.language = language + assert_not_set_error("maker.channel", %w(description)) do + RSS::Maker.make("2.0") do |maker| + maker.channel.title = title + maker.channel.link = link + # maker.channel.description = description + maker.channel.language = language + end end - assert_nil(rss) rss = RSS::Maker.make("2.0") do |maker| maker.channel.title = title @@ -285,17 +288,18 @@ module RSS assert_equal(height, image.height) assert_equal(description, image.description) - rss = RSS::Maker.make("2.0") do |maker| - # setup_dummy_channel(maker) - maker.channel.link = link + assert_not_set_error("maker.channel", %w(title description)) do + RSS::Maker.make("2.0") do |maker| + # setup_dummy_channel(maker) + maker.channel.link = link - maker.image.title = title - maker.image.url = url - maker.image.width = width - maker.image.height = height - maker.image.description = description + maker.image.title = title + maker.image.url = url + maker.image.width = width + maker.image.height = height + maker.image.description = description + end end - assert_nil(rss) end def test_not_valid_image @@ -318,18 +322,19 @@ module RSS end assert_nil(rss.image) - rss = RSS::Maker.make("2.0") do |maker| - setup_dummy_channel(maker) - # maker.channel.link = link - maker.channel.link = nil + assert_not_set_error("maker.channel", %w(link)) do + RSS::Maker.make("2.0") do |maker| + setup_dummy_channel(maker) + # maker.channel.link = link + maker.channel.link = nil - maker.image.title = title - maker.image.url = url - maker.image.width = width - maker.image.height = height - maker.image.description = description + maker.image.title = title + maker.image.url = url + maker.image.width = width + maker.image.height = height + maker.image.description = description + end end - assert_nil(rss) rss = RSS::Maker.make("2.0") do |maker| setup_dummy_channel(maker)