summaryrefslogtreecommitdiff
path: root/jni/ruby/test/rss/test_xml-stylesheet.rb
blob: c67922f4ec5dae06d78249b3d29ee69993e5110a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
require "rexml/document"

require_relative "rss-testcase"

require "rss/1.0"
require "rss/xml-stylesheet"

module RSS
  class TestXMLStyleSheet < TestCase

    def test_accessor
      [
        {:href => "a.xsl", :type => "text/xsl"},
        {:media => "print", :title => "FOO"},
        {:charset => "UTF-8", :alternate => "yes"},
      ].each do |attrs|
        assert_xml_stylesheet_attrs(attrs, XMLStyleSheet.new(attrs))
      end
    end

    def test_to_s
      [
        {:href => "a.xsl", :type => "text/xsl"},
        {:type => "text/xsl"},
        {:href => "a.xsl", :guess_type => "text/xsl"},
        {:href => "a.css", :type => "text/css"},
        {:href => "a.css", :type => "text/xsl",
         :guess_type => "text/css"},
        {:href => "a.xsl", :type => "text/xsl",
         :title => "sample", :media => "printer",
         :charset => "UTF-8", :alternate => "yes"},
        {:href => "a.css", :guess_type => "text/css",
         :alternate => "no"},
        {:type => "text/xsl", :title => "sample",
         :media => "printer", :charset => "UTF-8",
         :alternate => "yes"},
      ].each do |attrs|
        target, contents = parse_pi(XMLStyleSheet.new(attrs).to_s)
        assert_xml_stylesheet(target, attrs, XMLStyleSheet.new(contents))
      end
    end

    def test_bad_alternate
      %w(a ___ ??? BAD_ALTERNATE).each do |value|
        xss = XMLStyleSheet.new
        assert_raise(NotAvailableValueError) do
          xss.alternate = value
        end
        xss.do_validate = false
        assert_nothing_raised do
          xss.alternate = value
        end
        assert_nil(xss.alternate)
      end
    end

    def test_parse
      [
        [{:href => "a.xsl", :type => "text/xsl"},],
        [{:media => "print", :title => "FOO"},],
        [{:charset => "UTF-8", :alternate => "yes"},],
        [{:href => "a.xsl", :type => "text/xsl"},
         {:type => "text/xsl"},
         {:href => "a.xsl", :guess_type => "text/xsl"},
         {:href => "a.css", :type => "text/css"},
         {:href => "a.css", :type => "text/xsl",
          :guess_type => "text/css"},
         {:href => "a.xsl", :type => "text/xsl",
          :title => "sample", :media => "printer",
          :charset => "UTF-8", :alternate => "yes"},
         {:href => "a.css", :guess_type => "text/css",
          :alternate => "no"},
         {:type => "text/xsl", :title => "sample",
          :media => "printer", :charset => "UTF-8",
          :alternate => "yes"},],
      ].each do |xsss|
        doc = REXML::Document.new(make_sample_RDF)
        root = doc.root
        xsss.each do |xss|
          content = xss.collect do |key, name|
            %Q[#{key}="#{name}"]
          end.join(" ")
          pi = REXML::Instruction.new("xml-stylesheet", content)
          root.previous_sibling = pi
        end
        rss = Parser.parse(doc.to_s)
        have_href_xsss = xsss.find_all {|xss| xss.has_key?(:href)}
        assert_equal(have_href_xsss.size, rss.xml_stylesheets.size)
        rss.xml_stylesheets.each_with_index do |stylesheet, i|
          target, = parse_pi(stylesheet.to_s)
          assert_xml_stylesheet(target, have_href_xsss[i], stylesheet)
        end
      end
    end

    def parse_pi(pi)
      /\A\s*<\?(\S+)([^?]*\?+(?:[^?>][^?]*\?+)*)>\s*\z/ =~ pi
      target = $1
      dummy = REXML::Document.new("<dummy #{$2.to_s.chop}/>").root
      contents = {}
      dummy.attributes.each do |name, value|
        contents[name] = value
      end
      [target, contents]
    end

  end
end