summaryrefslogtreecommitdiff
path: root/jni/ruby/test/rss/test_maker_dc.rb
blob: 977350ca75dc838ed5675bb08d81c23515dfc1a5 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
require_relative "rss-testcase"

require "rss/maker"

module RSS
  class TestMakerDublinCore < TestCase

    def setup
      @uri = "http://purl.org/dc/elements/1.1/"

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

      @elements = {
        :title => "hoge",
        :description =>
          " XML is placing increasingly heavy loads on
          the existing technical infrastructure of the Internet.",
        :creator => "Rael Dornfest (mailto:rael@oreilly.com)",
        :subject => "XML",
        :publisher => "The O'Reilly Network",
        :contributor => "hogehoge",
        :type => "fugafuga",
        :format => "hohoho",
        :identifier => "fufufu",
        :source => "barbar",
        :language => "ja",
        :relation => "cococo",
        :rights => "Copyright (c) 2000 O'Reilly &amp; Associates, Inc.",
        :date => t,
      }
    end

    def test_rss10
      rss = RSS::Maker.make("1.0") do |maker|
        setup_dummy_channel(maker)
        set_elements(maker.channel)

        setup_dummy_image(maker)
        set_elements(maker.image)

        setup_dummy_item(maker)
        item = maker.items.last
        @elements.each do |name, value|
          item.__send__("#{accessor_name(name)}=", value)
        end

        setup_dummy_textinput(maker)
        set_elements(maker.textinput)
      end
      assert_dublin_core(@elements, rss.channel)
      assert_dublin_core(@elements, rss.image)
      assert_dublin_core(@elements, rss.items.last)
      assert_dublin_core(@elements, rss.textinput)
    end

    def test_rss10_multiple
      assert_multiple_dublin_core_rss10("_list")
      assert_multiple_dublin_core_rss10("es")
    end

    def assert_multiple_dublin_core_rss10(multiple_rights_suffix)
      elems = []
      @elements.each do |name, value|
        plural = name.to_s + (name == :rights ? multiple_rights_suffix : "s")
        values = [value]
        if name == :date
          values << value + 60
        else
          values << value * 2
        end
        elems << [name, values, plural]
      end

      rss = RSS::Maker.make("1.0") do |maker|
        setup_dummy_channel(maker)
        set_multiple_elements(maker.channel, elems)

        setup_dummy_image(maker)
        set_multiple_elements(maker.image, elems)

        setup_dummy_item(maker)
        item = maker.items.last
        elems.each do |name, values, plural|
          dc_elems = item.__send__("dc_#{plural}")
          values.each do |value|
            elem = dc_elems.__send__("new_#{name}")
            elem.value = value
          end
        end

        setup_dummy_textinput(maker)
        set_multiple_elements(maker.textinput, elems)
      end
      assert_multiple_dublin_core(elems, rss.channel)
      assert_multiple_dublin_core(elems, rss.image)
      assert_multiple_dublin_core(elems, rss.items.last)
      assert_multiple_dublin_core(elems, rss.textinput)
    end

    def test_date
      t1 = Time.iso8601("2000-01-01T12:00:05+00:00")
      t2 = Time.iso8601("2005-01-01T12:00:05+00:00")

      rss = RSS::Maker.make("1.0") do |maker|
        setup_dummy_channel(maker)
        maker.channel.date = t1
        maker.channel.dc_dates.new_date do |date|
          date.value = t2
        end

        setup_dummy_item(maker)
        item = maker.items.last
        item.date = t2
        item.dc_dates.new_date do |date|
          date.value = t1
        end
      end
      assert_equal([t1, t2], rss.channel.dc_dates.collect{|x| x.value})
      assert_equal([t2, t1], rss.items.last.dc_dates.collect{|x| x.value})
    end

    private
    def accessor_name(name)
      "dc_#{name}"
    end

    def set_elements(target, elems=@elements)
      elems.each do |name, value|
        target.__send__("#{accessor_name(name)}=", value)
      end
    end

    def set_multiple_elements(target, elems)
      elems.each do |name, values, plural|
        plural ||= "#{name}s"
        dc_elems = target.__send__("dc_#{plural}")
        values.each do |value|
          dc_elems.__send__("new_#{name}") do |new_dc_elem|
            new_dc_elem.value = value
          end
        end
      end
    end

  end
end