File: namespace_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 (289 lines) | stat: -rw-r--r-- 13,682 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
require_relative './../spec_helper.rb'

describe ROXML, "with namespaces" do
  describe "for writing" do
    before do
      @xml = <<EOS
<?xml version="1.0" encoding="UTF-8"?>
<gronk:VApp name="My new vApp" status="1" href="https://vcloud.example.com/vapp/833" type="application/vnd.vmware.vcloud.vapp+xml" xmlns:vmw="http://foo.example.com" xmlns:gronk="http://gronk.example.com">
  <gronk:NetworkConfig name="Network 1">
    <vmw:FenceMode>allowInOut</vmw:FenceMode>
    <vmw:Dhcp>true</vmw:Dhcp>
    <gronk:errors>
      <gronk:error>OhNo!</gronk:error>
      <gronk:error>Another!</gronk:error>
    </gronk:errors>
  </gronk:NetworkConfig>
  <foo />
  <bar>
    gronk
  </bar>
</gronk:VApp>
EOS
    end

    class NetworkConfig
      include ROXML
      xml_namespace :gronk

      xml_name 'NetworkConfig'
      xml_reader    :name, :from => '@name'
      xml_reader    :errors, :as => [], :from => 'gronk:error'
      xml_accessor  :fence_mode, :from => 'vmw:FenceMode'
      xml_accessor  :dhcp?, :from => 'vmw:Dhcp'
    end

    class VApp
      include ROXML
      xml_namespace :gronk

      xml_name "VApp"
      xml_reader    :name,    :from => '@name'
      xml_reader    :status,  :from => '@status'
      xml_reader    :href,    :from => '@href'
      xml_reader    :type,    :from => '@type'
      xml_accessor  :foo,     :from => 'foo', :namespace => false
      xml_accessor  :bar,     :from => 'bar', :namespace => false
      xml_accessor  :network_configs, :as => [NetworkConfig], :namespace => :gronk
    end

    describe "#to_xml" do
      xit "should reproduce the input xml" do
        output = ROXML::XML::Document.new
        output.root = VApp.from_xml(@xml).to_xml(:namespaces => {'vmw' => "http://foo.example.com", 'gronk' => "http://gronk.example.com"})
        expect(ROXML::XML.parse_string(output.to_s)).to be_equivalent_to(ROXML::XML.parse_string(@xml))
      end
    end
  end

  shared_examples_for "roxml namespacey declaration" do
    context "with a namespacey :from" do
      context "and an explicit :namespace" do
        it "should raise" do
          expect do
            Class.new do
              include ROXML
              xml_reader :default_namespace_with_namespacey_from_and_explicit_namespace, :from => 'namespacey:with_namespacey_from', :namespace => 'explicit'
            end
          end.to raise_error(ROXML::ContradictoryNamespaces)
        end
      end

      context "and :namespace => false" do
        it "should raise" do
          expect do
            Class.new do
              include ROXML
              xml_reader :default_namespace_with_namespacey_from_and_namespace_false, :from => 'namespacey:with_namespacey_from', :namespace => false
            end
          end.to raise_error(ROXML::ContradictoryNamespaces)
        end
      end
    end
  end

  shared_examples_for "roxml namespacey declaration with default" do
    it_should_behave_like "roxml namespacey declaration"
    
    it "should use the default namespace" do
      expect(@instance.default_namespace).to eq('default namespace node')
    end
    
    context "and :namespace => false" do
      it "should find the namespace-less node" do
        expect(@instance.default_namespace_with_namespace_false).to eq('namespaceless node')
      end
    end

    context "with an explicit :namespace" do
      it "should use the explicit namespace" do
        @instance.default_and_explicit_namespace == 'explicit namespace node'
      end
    end

    context "with a namespace-less :from" do
      it "should use the default namespace" do
        expect(@instance.default_namespace_with_namespaceless_from).to eq('default namespace node')
      end
      
      context "and :namespace => false" do
        it "should find the namespace-less node" do
          expect(@instance.default_namespace_with_namespaceless_from_and_namespace_false).to eq('namespaceless node')
        end
      end

      context "and an explicit :namespace" do
        it "should use the explicit namespace" do
          expect(@instance.default_namespace_with_namespaceless_from_and_explicit_namespace).to eq('explicit namespace node')
        end
      end
    end

    context "with a namespacey :from" do
      it "should use the :from namespace" do
        expect(@instance.default_namespace_with_namespacey_from).to eq('namespacey node')
      end
    end

    context "with an namespacey XPath :from" do
      it "should use the given namespace" do
        expect(@instance.default_namespace_with_namespacey_xpath_from).to eq('namespacey xpath node')
      end
    end

    context "with an XPath :from" do
      it "should use the default namespace" do
        expect(@instance.default_namespace_with_xpath_from).to eq('default namespace with xpath node')
      end
    end
  end

  context "with a default namespace declared" do
    class DefaultNamespaceyObject
      include ROXML
      xml_namespace :default_declared

      xml_reader :default_namespace
      xml_reader :default_namespace_with_namespace_false, :namespace => false
      xml_reader :default_and_explicit_namespace, :namespace => 'explicit'
      xml_reader :default_namespace_with_namespaceless_from, :from => 'with_namespaceless_from'
      xml_reader :default_namespace_with_namespaceless_from_and_explicit_namespace, :from => 'with_namespaceless_from', :namespace => 'explicit'
      xml_reader :default_namespace_with_namespaceless_from_and_namespace_false, :from => 'with_namespaceless_from', :namespace => false
      xml_reader :default_namespace_with_namespacey_from, :from => 'namespacey:with_namespacey_from'
      xml_reader :default_namespace_with_namespacey_xpath_from, :from => 'xpath/namespacey:default_namespace'
      xml_reader :default_namespace_with_xpath_from, :from => 'xpath/default_namespace'

      # These are handled in the "roxml namespacey declaration" shared spec
      # xml_reader :default_namespace_with_namespacey_from_and_namespace_false, :from => 'namespacey:with_namespaceless_from', :namespace => false
      # xml_reader :default_namespace_with_namespacey_from_and_explicit_namespace, :from => 'namespacey:with_namespaceless_from', :namespace => 'explicit'
    end

    before do
      @instance = DefaultNamespaceyObject.from_xml(%{
        <book xmlns:namespacey="http://www.aws.com/aws" xmlns:default_declared="http://www.aws.com/default" xmlns:explicit="http://www.aws.com/different">
          <default_declared:default_namespace>default namespace node</default_declared:default_namespace>
          <namespacey:with_namespacey_from>namespacey node</namespacey:with_namespacey_from>
          <explicit:with_namespaceless_from>explicit namespace node</explicit:with_namespaceless_from>
          <with_namespaceless_from>namespaceless node</with_namespaceless_from>
          <default_declared:with_namespaceless_from>default namespace node</default_declared:with_namespaceless_from>
          <explicit:default_and_explicit_namespace>explicit namespace node</explicit:default_and_explicit_namespace>
          <default_namespace_with_namespace_false>namespaceless node</default_namespace_with_namespace_false>
          <default_declared:xpath><namespacey:default_namespace>namespacey xpath node</namespacey:default_namespace></default_declared:xpath>
          <default_declared:xpath><default_declared:default_namespace>default namespace with xpath node</default_declared:default_namespace></default_declared:xpath>
        </book>
      })
    end
    
    it_should_behave_like "roxml namespacey declaration with default"
  end

  context "with a default namespace on the root node" do
    class XmlDefaultNamespaceyObject
      include ROXML
      xml_reader :default_namespace
      xml_reader :default_namespace_with_namespace_false, :namespace => false
      xml_reader :default_and_explicit_namespace, :namespace => 'explicit'
      xml_reader :default_namespace_with_namespaceless_from, :from => 'with_namespaceless_from'
      xml_reader :default_namespace_with_namespaceless_from_and_explicit_namespace, :from => 'with_namespaceless_from', :namespace => 'explicit'
      xml_reader :default_namespace_with_namespaceless_from_and_namespace_false, :from => 'with_namespaceless_from', :namespace => false
      xml_reader :default_namespace_with_namespacey_from, :from => 'namespacey:with_namespacey_from'
      xml_reader :default_namespace_with_namespacey_xpath_from, :from => 'xpath/namespacey:default_namespace'
      xml_reader :default_namespace_with_xpath_from, :from => 'xpath/default_namespace'

      # These are handled in the "roxml namespacey declaration" shared spec
      # xml_reader :default_namespace_with_namespacey_from_and_namespace_false, :from => 'namespacey:with_namespaceless_from', :namespace => false
      # xml_reader :default_namespace_with_namespacey_from_and_explicit_namespace, :from => 'namespacey:with_namespaceless_from', :namespace => 'explicit'
    end

    before do
      @instance = XmlDefaultNamespaceyObject.from_xml(%{
        <book xmlns="http://www.aws.com/xml_default" xmlns:namespacey="http://www.aws.com/aws" xmlns:default_declared="http://www.aws.com/default" xmlns:explicit="http://www.aws.com/different">
          <default_namespace>default namespace node</default_namespace>
          <namespacey:with_namespacey_from>namespacey node</namespacey:with_namespacey_from>
          <explicit:with_namespaceless_from>explicit namespace node</explicit:with_namespaceless_from>
          <with_namespaceless_from xmlns="">namespaceless node</with_namespaceless_from>
          <with_namespaceless_from>default namespace node</with_namespaceless_from>
          <explicit:default_and_explicit_namespace>explicit namespace node</explicit:default_and_explicit_namespace>
          <default_namespace_with_namespace_false xmlns="">namespaceless node</default_namespace_with_namespace_false>
          <xpath><namespacey:default_namespace>namespacey xpath node</namespacey:default_namespace></xpath>
          <xpath><default_namespace>default namespace with xpath node</default_namespace></xpath>
        </book>
      })
    end

    it_should_behave_like "roxml namespacey declaration with default"
  end

  context "without a default namespace" do
    class NamespaceyObject
      include ROXML

      xml_reader :no_default_namespace
      xml_reader :no_default_namespace_with_namespace_false, :namespace => false
      xml_reader :no_default_but_an_explicit_namespace, :namespace => 'explicit'
      xml_reader :no_default_namespace_with_namespaceless_from, :from => 'with_namespaceless_from'
      xml_reader :no_default_namespace_with_namespaceless_from_and_explicit_namespace, :from => 'with_namespaceless_from', :namespace => 'explicit'
      xml_reader :no_default_namespace_with_namespaceless_from_and_namespace_false, :from => 'with_namespaceless_from', :namespace => false
      xml_reader :no_default_namespace_with_namespacey_from, :from => 'namespacey:with_namespacey_from'

      # These are handled in the "roxml namespacey declaration" shared spec
      # xml_reader :no_default_namespace_with_namespacey_from_and_explicit_namespace, :from => 'namespacey:with_namespacey_from', :namespace => 'explicit'
      # xml_reader :no_default_namespace_with_namespacey_from_and_namespace_false, :from => 'namespacey:with_namespacey_from', :namespace => false
    end

    before do
      @instance = NamespaceyObject.from_xml(%{
        <book xmlns:namespacey="http://www.aws.com/aws" xmlns:explicit="http://www.aws.com/different">
          <namespacey:with_namespacey_from>namespacey node</namespacey:with_namespacey_from>
          <explicit:with_namespaceless_from>explicit namespace node</explicit:with_namespaceless_from>
          <with_namespaceless_from>namespaceless node</with_namespaceless_from>
          <explicit:no_default_but_an_explicit_namespace>explicit namespace node</explicit:no_default_but_an_explicit_namespace>
          <no_default_namespace_with_namespace_false>namespaceless node</no_default_namespace_with_namespace_false>
          <no_default_namespace>namespaceless node</no_default_namespace>
        </book>
      })
    end
    
    it_should_behave_like "roxml namespacey declaration"

    it "should find the namespace-less node" do
      expect(@instance.no_default_namespace).to eq('namespaceless node')
    end

    context "with :namespace => false" do
      it "should find the namespace-less node" do
        expect(@instance.no_default_namespace_with_namespace_false).to eq('namespaceless node')
      end
    end

    context "with an explicit :namespace" do
      it "should use the explicit namespace" do
        expect(@instance.no_default_but_an_explicit_namespace).to eq('explicit namespace node')
      end
    end

    context "with a namespace-less :from" do
      it "should find the namespace-less node" do
        expect(@instance.no_default_namespace_with_namespaceless_from).to eq('namespaceless node')
      end
    
      context "and an explicit :namespace" do
        it "should use the explicit namespace" do
          expect(@instance.no_default_namespace_with_namespaceless_from_and_explicit_namespace).to eq('explicit namespace node')
        end
      end

      context "with :namespace => false" do
        it "should find the namespace-less node" do
          expect(@instance.no_default_namespace_with_namespaceless_from_and_namespace_false).to eq('namespaceless node')
        end
      end
    end

    context "with a namespacey :from" do
      it "should use the :from namespace" do
        expect(@instance.no_default_namespace_with_namespacey_from).to eq('namespacey node')
      end
    end
  end
end