1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
kou 2004-11-28 06:53:41 +00:00
parent 49d454ea2e
commit b684f87574
10 changed files with 256 additions and 194 deletions

View file

@ -1,3 +1,14 @@
Sun Nov 28 15:51:40 2004 Kouhei Sutou <kou@cozmixng.org>
* 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 <ksaito@uranus.dti.ne.jp>
* regparse.c (fetch_token): fixed test failure on HP-UX ia64

View file

@ -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?

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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)

View file

@ -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

View file

@ -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)