summaryrefslogtreecommitdiff
path: root/jni/ruby/test/rss/test_syndication.rb
blob: 17875f19544d4cf8856aafb29e825b4528fe5622 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
require "cgi"
require "rexml/document"

require_relative "rss-testcase"

require "rss/1.0"
require "rss/syndication"

module RSS
  class TestSyndication < TestCase

    def setup
      @prefix = "sy"
      @uri = "http://purl.org/rss/1.0/modules/syndication/"

      @parents = %w(channel)

      t = Time.iso8601("2000-01-01T12:00:05+00:00")
      class << t
        alias_method(:to_s, :iso8601)
      end

      @elems = {
        :updatePeriod => "hourly",
        :updateFrequency => "2",
        :updateBase => t,
      }

      @sy_nodes = @elems.collect do |name, value|
        "<#{@prefix}:#{name}>#{CGI.escapeHTML(value.to_s)}</#{@prefix}:#{name}>"
      end.join("\n")

      @rss_source = make_RDF(<<-EOR, {@prefix =>  @uri})
#{make_channel(@sy_nodes)}
#{make_image()}
#{make_item()}
#{make_textinput()}
EOR

      @rss = Parser.parse(@rss_source)
    end

    def test_parser

      assert_nothing_raised do
        Parser.parse(@rss_source)
      end

      @elems.each do |tag, value|
        assert_too_much_tag(tag.to_s, "channel") do
          Parser.parse(make_RDF(<<-EOR, {@prefix => @uri}))
#{make_channel(("<" + @prefix + ":" + tag.to_s + ">" +
  CGI.escapeHTML(value.to_s) +
  "</" + @prefix + ":" + tag.to_s + ">") * 2)}
#{make_item}
EOR
        end
      end

    end

    def test_accessor

      t = Time.iso8601("2003-01-01T12:00:23+09:00")
      class << t
        alias_method(:to_s, :iso8601)
      end

      new_value = {
        :updatePeriod => "daily",
        :updateFrequency => "11",
        :updateBase => t,
      }

      @elems.each do |name, value|
        value = value.to_i if name == :updateFrequency
        @parents.each do |parent|
          assert_equal(value, @rss.__send__(parent).__send__("sy_#{name}"))
          @rss.__send__(parent).__send__("sy_#{name}=", new_value[name])
          new_val = new_value[name]
          new_val = new_val.to_i if name == :updateFrequency
          assert_equal(new_val, @rss.__send__(parent).__send__("sy_#{name}"))
        end
      end

      %w(hourly daily weekly monthly yearly).each do |x|
        @parents.each do |parent|
          assert_nothing_raised do
            @rss.__send__(parent).sy_updatePeriod = x
          end
        end
      end

      %w(-2 0.3 -0.4).each do |x|
        @parents.each do |parent|
          assert_not_available_value("sy:updateBase", x) do
            @rss.__send__(parent).sy_updateBase = x
          end
        end
      end

    end

    def test_to_s

      @elems.each do |name, value|
        excepted = "<#{@prefix}:#{name}>#{value}</#{@prefix}:#{name}>"
        @parents.each do |parent|
          assert_equal(excepted,
                       @rss.__send__(parent).__send__("sy_#{name}_element"))
        end
      end

      REXML::Document.new(@rss_source).root.each_element do |parent|
        if @parents.include?(parent.name)
          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