# frozen_string_literal: false
require 'erb'
module RSS
module Assertions
def _wrap_assertion
yield
end
def assert_parse(rss, assert_method, *args)
__send__("assert_#{assert_method}", *args) do
::RSS::Parser.parse(rss)
end
__send__("assert_#{assert_method}", *args) do
::RSS::Parser.parse(rss, false).validate
end
end
def assert_ns(prefix, uri)
_wrap_assertion do
begin
yield
flunk("Not raise NSError")
rescue ::RSS::NSError => e
assert_equal(prefix, e.prefix)
assert_equal(uri, e.uri)
end
end
end
def assert_missing_tag(tag, parent)
_wrap_assertion do
begin
yield
flunk("Not raise MissingTagError")
rescue ::RSS::MissingTagError => e
assert_equal(tag, e.tag)
assert_equal(parent, e.parent)
end
end
end
def assert_too_much_tag(tag, parent)
_wrap_assertion do
begin
yield
flunk("Not raise TooMuchTagError")
rescue ::RSS::TooMuchTagError => e
assert_equal(tag, e.tag)
assert_equal(parent, e.parent)
end
end
end
def assert_missing_attribute(tag, attrname)
_wrap_assertion do
begin
yield
flunk("Not raise MissingAttributeError")
rescue ::RSS::MissingAttributeError => e
assert_equal(tag, e.tag)
assert_equal(attrname, e.attribute)
end
end
end
def assert_not_expected_tag(tag, uri, parent)
_wrap_assertion do
begin
yield
flunk("Not raise NotExpectedTagError")
rescue ::RSS::NotExpectedTagError => e
assert_equal(tag, e.tag)
assert_equal(uri, e.uri)
assert_equal(parent, e.parent)
end
end
end
def assert_not_available_value(tag, value, attribute=nil)
_wrap_assertion do
begin
yield
flunk("Not raise NotAvailableValueError")
rescue ::RSS::NotAvailableValueError => e
assert_equal(tag, e.tag)
assert_equal(value, e.value)
assert_equal(attribute, e.attribute)
end
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_kind_of(Array, variables)
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)
assert_equal(encoding, rss.encoding)
assert_equal(standalone, rss.standalone)
end
end
def assert_xml_stylesheet_attrs(attrs, xsl)
_wrap_assertion do
n_attrs = normalized_attrs(attrs)
::RSS::XMLStyleSheet::ATTRIBUTES.each do |name|
assert_equal(n_attrs[name], xsl.__send__(name))
end
end
end
def assert_xml_stylesheet(target, attrs, xsl)
_wrap_assertion do
if attrs.has_key?(:href)
if !attrs.has_key?(:type) and attrs.has_key?(:guess_type)
attrs[:type] = attrs[:guess_type]
end
assert_equal("xml-stylesheet", target)
assert_xml_stylesheet_attrs(attrs, xsl)
else
assert_nil(target)
assert_equal("", xsl.to_s)
end
end
end
def assert_xml_stylesheet_pis(attrs_ary, rss=nil)
_wrap_assertion do
if rss.nil?
rss = ::RSS::RDF.new
setup_rss10(rss)
end
xss_strs = []
attrs_ary.each do |attrs|
xss = ::RSS::XMLStyleSheet.new(attrs)
xss_strs.push(xss.to_s)
rss.xml_stylesheets.push(xss)
end
pi_str = rss.to_s.gsub(/<\?xml .*\n/, "").gsub(/\s*<[^\?].*\z/m, "")
assert_equal(xss_strs.join("\n"), pi_str)
end
end
def assert_xml_stylesheets(attrs, xss)
_wrap_assertion do
xss.each_with_index do |xs, i|
assert_xml_stylesheet_attrs(attrs[i], xs)
end
end
end
def assert_atom_person(tag_name, generator)
_wrap_assertion do
name = "Mark Pilgrim"
uri = "http://example.org/"
email = "f8dy@example.com"
assert_parse(generator.call(<<-EOA), :missing_tag, "name", tag_name)
<#{tag_name}/>
EOA
assert_parse(generator.call(<<-EOA), :missing_tag, "name", tag_name)
<#{tag_name}>
#{uri}#{email}
#{tag_name}>
EOA
assert_parse(generator.call(<<-EOA), :nothing_raised)
<#{tag_name}>
#{name}
#{tag_name}>
EOA
feed = RSS::Parser.parse(generator.call(<<-EOA))
<#{tag_name}>
#{name}#{uri}#{email}
#{tag_name}>
EOA
person = yield(feed)
assert_not_nil(person)
assert_equal(name, person.name.content)
assert_equal(uri, person.uri.content)
assert_equal(email, person.email.content)
end
end
def assert_atom_category(generator)
_wrap_assertion do
term = "Music"
scheme = "http://xmlns.com/wordnet/1.6/"
label = "music"
missing_args = [:missing_attribute, "category", "term"]
assert_parse(generator.call(<<-EOA), *missing_args)
EOA
assert_parse(generator.call(<<-EOA), *missing_args)
EOA
assert_parse(generator.call(<<-EOA), :nothing_raised)
EOA
feed = RSS::Parser.parse(generator.call(<<-EOA))
EOA
category = yield(feed)
assert_not_nil(category)
assert_equal(term, category.term)
assert_equal(scheme, category.scheme)
assert_equal(label, category.label)
end
end
def assert_atom_link(generator)
_wrap_assertion do
href = "http://example.org/feed.atom"
rel = "self"
type = "application/atom+xml"
hreflang = "en"
title = "Atom"
length = "1024"
assert_parse(generator.call(<<-EOA), :missing_attribute, "link", "href")
EOA
assert_parse(generator.call(<<-EOA), :missing_attribute, "link", "href")