summaryrefslogtreecommitdiff
path: root/test/rss/test_content.rb
blob: 45f834de0152129191a86f63804eb8a4b56411fc (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
require "cgi"
require "rexml/document"

require "rss-testcase"

require "rss/content"

module RSS
  class TestContent < TestCase
    def setup
      @prefix = "content"
      @uri = "http://purl.org/rss/1.0/modules/content/"
      
      @elems = {
        :encoded => "<em>ATTENTION</em>",
      }
      
      @content_nodes = @elems.collect do |name, value|
        "<#{@prefix}:#{name}>#{CGI.escapeHTML(value.to_s)}</#{@prefix}:#{name}>"
      end.join("\n")
      
      @rss10_source = make_RDF(<<-EOR, {@prefix => @uri})
#{make_channel()}
#{make_image()}
#{make_item(@content_nodes)}
#{make_textinput()}
EOR

      @rss10 = Parser.parse(@rss10_source)


      @rss20_source = make_rss20(<<-EOR, {@prefix =>  @uri})
#{make_channel20(make_item20(@content_nodes))}
EOR

      @rss20 = Parser.parse(@rss20_source)
    end

    def test_parser
      assert_nothing_raised do
        Parser.parse(@rss10_source)
      end

      assert_nothing_raised do
        Parser.parse(@rss20_source)
      end

      @elems.each do |tag, value|
        tag_name = "#{@prefix}:#{tag}"
        content_encodes = make_element(tag_name, {}, value) * 2

        assert_too_much_tag(tag.to_s, "item") do
          Parser.parse(make_RDF(<<-EOR, {@prefix => @uri}))
#{make_channel}
#{make_item(content_encodes)}
EOR
        end

        assert_too_much_tag(tag.to_s, "item") do
          Parser.parse(make_rss20(<<-EOR, {@prefix => @uri}))
#{make_channel20(make_item20(content_encodes))}
EOR
        end
      end
    end

    def test_accessor
      new_value = {
        :encoded => "<![CDATA[<it>hoge</it>]]>",
      }

      @elems.each do |name, value|
        [@rss10, @rss20].each do |rss|
          meth = "#{RSS::CONTENT_PREFIX}_#{name}"
          parent = rss.items.last
          assert_equal(value, parent.__send__(meth))
          parent.__send__("#{meth}=", new_value[name].to_s)
          assert_equal(new_value[name], parent.__send__(meth))
        end
      end
    end
    
    def test_to_s
      @elems.each do |name, value|
        excepted = make_element("#{@prefix}:#{name}", {}, value)
        meth = "#{RSS::CONTENT_PREFIX}_#{name}_element"
        [@rss10, @rss20].each do |rss|
          assert_equal(excepted, rss.items.last.__send!(meth))
        end
      end

      [@rss10_source, @rss20_source].each do |source|
        REXML::Document.new(source).root.each_element do |parent|
          next unless parent.name != "item"
          parent.each_element do |elem|
            if elem.namespace == @uri
              assert_equal(elem.text, @elems[elem.name.intern].to_s)
            end
          end
        end
      end
    end
  end
end