File: object_spec.rb

package info (click to toggle)
ruby-roxml 4.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 800 kB
  • sloc: ruby: 4,133; xml: 1,013; makefile: 7
file content (82 lines) | stat: -rw-r--r-- 2,412 bytes parent folder | download | duplicates (3)
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
require 'spec_helper'

describe ROXML::XMLObjectRef do
  class SubObject
    include ROXML

    xml_reader :value, :from => :attr

    def initialize(value = nil)
      @value = value
    end
  end

  before do
    @xml = ROXML::XML.parse_string %(
<myxml>
  <node>
    <name value="first" />
    <name value="second" />
    <name value="third" />
  </node>
</myxml>)
  end

  context "plain vanilla" do
    before do
      @ref = ROXML::XMLObjectRef.new(OpenStruct.new(:name => 'name', :wrapper => 'node', :array? => false, :sought_type => SubObject), RoxmlObject.new)
    end

    it "should return one instance" do
      expect(@ref.value_in(@xml).value).to eq("first")
    end
    it "should output one instance"
  end
  
  context "with :as => []" do
    before do
      @ref = ROXML::XMLObjectRef.new(OpenStruct.new(:name => 'name', :wrapper => 'node', :array? => true, :sought_type => SubObject), RoxmlObject.new)
    end

    it "should collect all instances" do
      expect(@ref.value_in(@xml).map(&:value)).to eq(["first", "second", "third"])
    end

    it "should output all instances" do
      xml = ROXML::XML.new_node('myxml')
      @ref.update_xml(xml, ["first", "second", "third"].map {|value| SubObject.new(value) })
      expect(xml.to_s.squeeze(' ')).to eq(@xml.root.to_s.squeeze(' '))
    end
  end
  
  context "when the namespaces are different" do
    before do
      @xml = ROXML::XML.parse_string %(
      <myxml xmlns="http://example.com/three" xmlns:one="http://example.com/one" xmlns:two="http://example.com/two">
        <node>
          <one:name>first</one:name>
          <two:name>second</two:name>
          <name>third</name>
        </node>
      </myxml>)
    end

    context "with :namespace => '*'" do
      before do
        @ref = ROXML::XMLObjectRef.new(OpenStruct.new(:name => 'name', :wrapper => 'node', :array? => true, :namespace => '*', :sought_type => SubObject), RoxmlObject.new)
      end

      it "should collect all instances" do
        skip "Test bug?"
        expect(@ref.value_in(@xml).map(&:value)).to eq(["first", "second", "third"])
      end

      it "should output all instances with namespaces" do
        skip "Full namespace write support"
        xml = ROXML::XML.new_node('myxml')
        @ref.update_xml(xml, ["first", "second", "third"].map {|value| SubObject.new(value) })
        expect(xml).to eq(@xml.root)
      end
    end
  end
end