1require "rexml/document"
2
3require "rss-testcase"
4
5require "rss/1.0"
6require "rss/xml-stylesheet"
7
8module RSS
9  class TestXMLStyleSheet < TestCase
10
11    def test_accessor
12      [
13        {:href => "a.xsl", :type => "text/xsl"},
14        {:media => "print", :title => "FOO"},
15        {:charset => "UTF-8", :alternate => "yes"},
16      ].each do |attrs|
17        assert_xml_stylesheet_attrs(attrs, XMLStyleSheet.new(attrs))
18      end
19    end
20
21    def test_to_s
22      [
23        {:href => "a.xsl", :type => "text/xsl"},
24        {:type => "text/xsl"},
25        {:href => "a.xsl", :guess_type => "text/xsl"},
26        {:href => "a.css", :type => "text/css"},
27        {:href => "a.css", :type => "text/xsl",
28         :guess_type => "text/css"},
29        {:href => "a.xsl", :type => "text/xsl",
30         :title => "sample", :media => "printer",
31         :charset => "UTF-8", :alternate => "yes"},
32        {:href => "a.css", :guess_type => "text/css",
33         :alternate => "no"},
34        {:type => "text/xsl", :title => "sample",
35         :media => "printer", :charset => "UTF-8",
36         :alternate => "yes"},
37      ].each do |attrs|
38        target, contents = parse_pi(XMLStyleSheet.new(attrs).to_s)
39        assert_xml_stylesheet(target, attrs, XMLStyleSheet.new(contents))
40      end
41    end
42
43    def test_bad_alternate
44      %w(a ___ ??? BAD_ALTERNATE).each do |value|
45        xss = XMLStyleSheet.new
46        assert_raise(NotAvailableValueError) do
47          xss.alternate = value
48        end
49        xss.do_validate = false
50        assert_nothing_raised do
51          xss.alternate = value
52        end
53        assert_nil(xss.alternate)
54      end
55    end
56
57    def test_parse
58      [
59        [{:href => "a.xsl", :type => "text/xsl"},],
60        [{:media => "print", :title => "FOO"},],
61        [{:charset => "UTF-8", :alternate => "yes"},],
62        [{:href => "a.xsl", :type => "text/xsl"},
63         {:type => "text/xsl"},
64         {:href => "a.xsl", :guess_type => "text/xsl"},
65         {:href => "a.css", :type => "text/css"},
66         {:href => "a.css", :type => "text/xsl",
67          :guess_type => "text/css"},
68         {:href => "a.xsl", :type => "text/xsl",
69          :title => "sample", :media => "printer",
70          :charset => "UTF-8", :alternate => "yes"},
71         {:href => "a.css", :guess_type => "text/css",
72          :alternate => "no"},
73         {:type => "text/xsl", :title => "sample",
74          :media => "printer", :charset => "UTF-8",
75          :alternate => "yes"},],
76      ].each do |xsss|
77        doc = REXML::Document.new(make_sample_RDF)
78        root = doc.root
79        xsss.each do |xss|
80          content = xss.collect do |key, name|
81            %Q[#{key}="#{name}"]
82          end.join(" ")
83          pi = REXML::Instruction.new("xml-stylesheet", content)
84          root.previous_sibling = pi
85        end
86        rss = Parser.parse(doc.to_s)
87        have_href_xsss = xsss.find_all {|xss| xss.has_key?(:href)}
88        assert_equal(have_href_xsss.size, rss.xml_stylesheets.size)
89        rss.xml_stylesheets.each_with_index do |stylesheet, i|
90          target, = parse_pi(stylesheet.to_s)
91          assert_xml_stylesheet(target, have_href_xsss[i], stylesheet)
92        end
93      end
94    end
95
96    def parse_pi(pi)
97      /\A\s*<\?(\S+)([^?]*\?+(?:[^?>][^?]*\?+)*)>\s*\z/ =~ pi
98      target = $1
99      dummy = REXML::Document.new("<dummy #{$2.to_s.chop}/>").root
100      contents = {}
101      dummy.attributes.each do |name, value|
102        contents[name] = value
103      end
104      [target, contents]
105    end
106
107  end
108end
109