xml-mapping_extensions 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 159f916e7e1a374d68857ce63626f37b763312e3
4
- data.tar.gz: 50ffcf3d3606eec904e7ce9faaecccf90905ac39
3
+ metadata.gz: 26f76a9fe36b5554f940110297aefbf62e26cc99
4
+ data.tar.gz: 30c72c8dc6b1c9c8757c493c14919a65ff4df36f
5
5
  SHA512:
6
- metadata.gz: fdd87b9bb6d862300aac1b9bf993b9023f2704c6f788347868eb9eb89cd2a5911f92c321c4bba1af4a8520d3957f80b05678d7e75302b2b9e756bf61600d355a
7
- data.tar.gz: fbc3c1cc7ba3627c21c6fe6cf2711031620f4638554a1d53d97e46c43547cb86426be36ef46eb4fb7bfbc5f78a1a341937a6342867710cc0a35bd93ad74f58c3
6
+ metadata.gz: a5d5359795785430506b583c13ad3274ba6cf5b237cd5cdb90f201da76d959bc295faf408f5e885e3d9e69b79a5bc4ea64a951a3eeb1e12405f2b40a6e12ccef
7
+ data.tar.gz: e16fabb2475ce3b7c3e15b68e57fa8a9c6acf32f89834e370ab2e6176f2c427dec7bd04422218272ef093243039245fb2a031204ddea05e7bc257f229b965f9d
@@ -1 +1 @@
1
- 2.2.2
1
+ 2.2.3
data/CHANGES.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.4.1 (18 May 2016)
2
+
3
+ - Fixed various issues with nested namespaces
4
+
1
5
  ## 0.4.0 (16 May 2016)
2
6
 
3
7
  - Renamed `NamespacedElement` to `Namespaced` and made it an explicit include rather than auto-injected
data/README.md CHANGED
@@ -183,19 +183,18 @@ Outputs:
183
183
 
184
184
  ## Namespaces
185
185
 
186
- The `Namespace` class encapsulates an XML namespace:
187
-
188
- ```ruby
189
- namespace = Namespace.new(uri: 'http://example.org/px', schema_location: 'http://example.org/px.xsd')
190
- ```
191
-
192
- The `Namespaced` module extends `XML::Mapping` to add a `namespace` attribute and write the namespace
193
- out when saving to XML.
186
+ The `Namespace` class encapsulates an XML namespace. The `Namespaced` module extends `XML::Mapping` to
187
+ add a `namespace` attribute and write the namespace out when saving to XML.
194
188
 
195
189
  ```ruby
196
190
  class MyElem
197
191
  include XML::MappingExtensions::Namespaced # instead of XML::Mapping
198
192
 
193
+ namespace Namespace.new(
194
+ prefix: 'px',
195
+ uri: 'http://example.org/px'
196
+ )
197
+
199
198
  root_element_name 'my_elem'
200
199
 
201
200
  date_node :plain_date, 'plain_date'
@@ -204,10 +203,12 @@ class MyElem
204
203
  uri_node :uri, 'uri'
205
204
  mime_type_node :mime_type, 'mime_type'
206
205
  end
206
+
207
+ MyElem.namespace
208
+ # => #<XML::MappingExtensions::Namespace:0x007fb1c6b73e80>
207
209
  ```
208
210
 
209
- Setting a namespace on a namespaced object will cause that namespace to be written out when the object is saved
210
- to XML:
211
+ The namespace will then be written out when the object is saved to XML:
211
212
 
212
213
  ```ruby
213
214
  obj = MyElem.new(...)
@@ -215,11 +216,12 @@ obj.namespace = namespace
215
216
 
216
217
  puts obj.write_xml
217
218
  ```
218
-
219
- Outputs:
220
-
221
219
  ```xml
222
- <element xmlns='http://example.org/px/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://example.org/px.xsd' attribute='123'>
220
+ <element
221
+ xmlns='http://example.org/px/'
222
+ xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
223
+ xsi:schemaLocation='http://example.org/px.xsd'
224
+ attribute='123'>
223
225
  element text
224
226
  <child>child 1</child>
225
227
  <child>child 2</child>
@@ -229,18 +231,26 @@ Outputs:
229
231
  Setting a `prefix` attribute on the namespace will set the prefix on each element in the output:
230
232
 
231
233
  ```ruby
232
- namespace = Namespace.new(prefix: 'px', uri: 'http://example.org/px', schema_location: 'http://example.org/px.xsd')
234
+
235
+ class MyElem
236
+ namespace Namespace.new(
237
+ prefix: 'px',
238
+ uri: 'http://example.org/px',
239
+ schema_location: 'http://example.org/px.xsd'
240
+ )
241
+ end
233
242
 
234
243
  obj = MyElem.new(...)
235
244
  obj.namespace = namespace
236
245
 
237
246
  puts obj.write_xml
238
247
  ```
239
-
240
- Outputs:
241
-
242
248
  ```xml
243
- <px:element xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://example.org/px.xsd' xmlns:px='http://example.org/px/' attribute='123'>
249
+ <px:element
250
+ xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
251
+ xsi:schemaLocation='http://example.org/px.xsd'
252
+ xmlns:px='http://example.org/px/'
253
+ attribute='123'>
244
254
  element text
245
255
  <px:child>child 1</px:child>
246
256
  <px:child>child 2</px:child>
@@ -6,6 +6,30 @@ module XML
6
6
  # [XML::Mapping](http://multi-io.github.io/xml-mapping/)
7
7
  module MappingExtensions
8
8
  Dir.glob(File.expand_path('../mapping_extensions/*.rb', __FILE__), &method(:require))
9
+
10
+ # Ensures that the specified argument is a URI.
11
+ # @param url [String, URI] The argument. If the argument is already
12
+ # a URI, it is returned unchanged; otherwise, the argument's string
13
+ # form (as returned by +`to_s`+) is parsed as a URI.
14
+ # @return [nil, URI] +`nil`+ if +`url`+ is nil, otherwise the URI.
15
+ # @raise [URI::InvalidURIError] if `url` is a string that is not a valid URI
16
+ def self.to_uri(url)
17
+ return nil unless url
18
+ return url if url.is_a? URI
19
+ stripped = url.respond_to?(:strip) ? url.strip : url.to_s.strip
20
+ URI.parse(stripped)
21
+ end
22
+
23
+ # Ensures that the specified argument is a URI string.
24
+ # @param url [String, URI] The argument. If the argument is already
25
+ # a URI, it is returned unchanged; otherwise, the argument's string
26
+ # form (as returned by +`to_s`+) is parsed as a URI.
27
+ # @return [nil, String] +`nil`+ if +`url`+ is nil, otherwise the URI string.
28
+ # @raise [URI::InvalidURIError] if `url` is a string that is not a valid URI
29
+ def self.to_uri_str(url)
30
+ uri = to_uri(url)
31
+ uri && uri.to_s
32
+ end
9
33
  end
10
34
 
11
35
  module Mapping
@@ -12,38 +12,20 @@ module XML
12
12
  # @return [String, nil] the namespace prefix
13
13
  attr_reader :prefix
14
14
 
15
- # @return [String, nil] the schema location URI(s), as a space-separated string list
15
+ # @return [URI, String, nil] the schema location URI
16
16
  attr_reader :schema_location
17
17
 
18
18
  # Creates a new {Namespace}
19
19
  # @param uri [URI, String] the namespace URI
20
20
  # @param prefix [String, nil] the namespace prefix
21
- # @param schema_location [String, nil] the schema location(s)
21
+ # @param schema_location [String, nil] the schema location
22
+ # @raise [URI::InvalidURIError] if `uri` is nil, or a string that is not a valid URI
23
+ # @raise [URI::InvalidURIError] if `schema_location` is a string that is not a valid URI
22
24
  def initialize(uri:, prefix: nil, schema_location: nil)
23
- @uri = uri.to_s
25
+ fail URI::InvalidURIError, 'uri cannot be nil' unless uri
26
+ @uri = MappingExtensions.to_uri_str(uri)
24
27
  @prefix = prefix
25
- @schema_location = schema_location
26
- end
27
-
28
- # Sets `uri` as the default (no-prefix) namespace on `elem`, with
29
- # `schema_location` as the schema location.
30
- # @param elem [REXML::Element] The element to set the namespace on
31
- def set_default_namespace(elem) # rubocop:disable Style/AccessorMethodName
32
- elem.add_namespace(uri)
33
- return unless schema_location
34
- # TODO: Figure out xsi:noNamespaceSchemaLocation
35
- elem.add_namespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance')
36
- elem.add_attribute('xsi:schemaLocation', schema_location)
37
- end
38
-
39
- # Sets `prefix` as the prefix for namespace `uri` on the specified document
40
- # root element, and all its descendants that have that namespace.
41
- # @param root [REXML::Element] The document root to set the namespace on
42
- def set_prefix(root) # rubocop:disable Style/AccessorMethodName
43
- return unless prefix
44
- set_prefix_recursive(root)
45
- root.add_namespace(nil) if root.attributes['xmlns'] == uri # clear the no-prefix namespace
46
- root.add_namespace(prefix, uri)
28
+ @schema_location = MappingExtensions.to_uri_str(schema_location)
47
29
  end
48
30
 
49
31
  def to_s
@@ -60,20 +42,11 @@ module XML
60
42
  state.hash
61
43
  end
62
44
 
45
+ protected
46
+
63
47
  def state
64
48
  [uri, prefix, schema_location]
65
49
  end
66
-
67
- protected :state
68
-
69
- def set_prefix_recursive(elem) # rubocop:disable Style/AccessorMethodName
70
- return unless elem.namespace == uri
71
- # name= with a prefixed name sets namespace by side effect and is the only way to actually output the prefix
72
- elem.name = "#{prefix}:#{elem.name}"
73
- elem.each_element { |e| set_prefix_recursive(e) }
74
- end
75
-
76
- private :set_prefix_recursive
77
50
  end
78
51
  end
79
52
  end
@@ -26,21 +26,59 @@ module XML
26
26
  # Hack to make sure these don't get defined till after `XML::Mapping`'s include
27
27
  # hooks have a chance to define their super methods
28
28
  module InstanceMethods
29
- # Overrides `XML::Mapping#pre_save` to set the XML namespace and schema location
30
- # on the generated element.
31
- def pre_save(options = {mapping: :_default})
32
- xml = super(options)
33
- namespace.set_default_namespace(xml) if namespace
34
- xml
29
+
30
+ # Overrides `XML::Mapping#fill_into_xml` to set the XML namespace
31
+ def fill_into_xml(xml, options = { mapping: :_default })
32
+ add_namespace(xml)
33
+ super(xml, options)
34
+ return unless namespace
35
+ set_prefix_recursive(namespace.prefix, xml)
36
+ end
37
+
38
+ private
39
+
40
+ def set_prefix_recursive(prefix, elem)
41
+ return elem unless prefix
42
+ return elem unless elem.namespace == namespace.uri || elem.namespace.to_s.empty?
43
+
44
+ set_prefix(prefix, elem)
45
+ elem.each_element { |e| set_prefix_recursive(prefix, e) }
46
+ elem
47
+ end
48
+
49
+ def add_namespace(elem)
50
+ return unless namespace
51
+ prefix, uri, schema_location = namespace.prefix, namespace.uri, namespace.schema_location # rubocop:disable Style/ParallelAssignment
52
+ if prefix
53
+ set_prefix(prefix, elem)
54
+ add_schema_location(uri, schema_location, elem.root)
55
+ elem.root.add_namespace(prefix, uri)
56
+ else
57
+ add_schema_location(uri, schema_location, elem)
58
+ elem.add_namespace(uri)
59
+ end
35
60
  end
36
61
 
37
- # Overrides `XML::Mapping#save_to_xml` to set the XML namespace prefix on
38
- # the generated element, and all its descendants that have that namespace.
39
- def save_to_xml(options = {mapping: :_default})
40
- xml = super(options)
41
- namespace.set_prefix(xml) if namespace
42
- xml
62
+ def set_prefix(prefix, elem)
63
+ # name= with a prefixed name sets namespace by side effect and is the only way to actually output the prefix
64
+ elem.name = "#{prefix}:#{elem.name}" if elem.prefix.to_s.empty?
43
65
  end
66
+
67
+ def add_schema_location(uri, schema_location, elem)
68
+ return elem unless schema_location
69
+
70
+ schema_location_attr = elem.attribute('xsi:schemaLocation')
71
+ all_declarations = schema_location_attr ? schema_location_attr.value : ''
72
+
73
+ declaration = "#{uri} #{schema_location}"
74
+ return elem if all_declarations.include?(declaration)
75
+
76
+ all_declarations << " #{declaration}"
77
+ elem.add_attribute('xsi:schemaLocation', all_declarations.strip)
78
+
79
+ elem.add_namespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance') unless elem.root && elem.root.attribute('xmlns:xsi')
80
+ end
81
+
44
82
  end
45
83
  end
46
84
  end
@@ -1,6 +1,6 @@
1
1
  module XML
2
2
  module MappingExtensions
3
3
  # The version of this gem
4
- VERSION = '0.4.0'
4
+ VERSION = '0.4.1'
5
5
  end
6
6
  end
@@ -40,42 +40,6 @@ module XML
40
40
  end
41
41
  end
42
42
 
43
- describe ':namespace' do
44
- it 'sets the namespace on save' do
45
- uri = 'http://example.org/px/'
46
- schema_location = 'http://example.org/px.xsd'
47
- namespace = MappingExtensions::Namespace.new(uri: uri, prefix: 'px', schema_location: schema_location)
48
-
49
- obj = MXSpecObject.new
50
- obj.attribute = 123
51
- obj.text = 'element text'
52
- obj.children = ['child 1', 'child 2']
53
- obj.namespace = namespace
54
-
55
- namespace_attribs = "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='#{schema_location}' xmlns:px='#{uri}'"
56
- expected = "<px:element #{namespace_attribs} attribute='123'>element text<px:child>child 1</px:child><px:child>child 2</px:child></px:element>"
57
-
58
- expect(obj.save_to_xml).to be_xml(expected)
59
- end
60
-
61
- it 'works without prefixes' do
62
- uri = 'http://example.org/px/'
63
- schema_location = 'http://example.org/px.xsd'
64
- namespace = MappingExtensions::Namespace.new(uri: uri, schema_location: schema_location)
65
-
66
- obj = MXSpecObject.new
67
- obj.attribute = 123
68
- obj.text = 'element text'
69
- obj.children = ['child 1', 'child 2']
70
- obj.namespace = namespace
71
-
72
- namespace_attribs = "xmlns='#{uri}' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='#{schema_location}'"
73
- expected = "<element #{namespace_attribs} attribute='123'>element text<child>child 1</child><child>child 2</child></element>"
74
-
75
- expect(obj.save_to_xml).to be_xml(expected)
76
- end
77
- end
78
-
79
43
  module ClassMethods
80
44
  describe '#parse_xml' do
81
45
 
@@ -3,20 +3,6 @@ require 'spec_helper'
3
3
  module XML
4
4
  module MappingExtensions
5
5
  describe Namespace do
6
- describe '#set_default_namespace' do
7
- it 'sets the default namespace'
8
- it 'sets the schema location'
9
- it 'allows a nil schema location'
10
- it 'sets the no-namespace schema location'
11
- it 'allows a nil no-namespace schema location'
12
- end
13
-
14
- describe '#set_prefix' do
15
- it 'sets the prefix'
16
- it 'clears the no-prefix namespace, if previously present'
17
- it 'leaves an unrelated no-prefix namespace intact'
18
- end
19
-
20
6
  describe '#to_s' do
21
7
  it 'includes the prefix, namespace, and schema location' do
22
8
  uri = 'http://example.org/px/'
@@ -39,6 +25,16 @@ module XML
39
25
  namespace = Namespace.new(uri: uri, prefix: prefix)
40
26
  expect(namespace.to_s).to match(/Namespace.*#{uri}.*#{prefix}.*nil/)
41
27
  end
28
+
29
+ it 'rejects a nil uri' do
30
+ expect { Namespace.new(uri: nil) }.to raise_error(URI::InvalidURIError)
31
+ end
32
+ it 'rejects an invalid uri' do
33
+ expect { Namespace.new(uri: 'I am not a URI') }.to raise_error(URI::InvalidURIError)
34
+ end
35
+ it 'rejects an invalid schema_location' do
36
+ expect { Namespace.new(uri: 'http://example.org/px/', schema_location: 'I am not a URI') }.to raise_error(URI::InvalidURIError)
37
+ end
42
38
  end
43
39
 
44
40
  describe 'equality' do
@@ -5,8 +5,13 @@ require 'xml/mapping_extensions/namespace'
5
5
  module XML
6
6
  module MappingExtensions
7
7
 
8
- UNPREFIXED = Namespace.new(uri: 'http://example.org/nse')
9
- PREFIXED = Namespace.new(uri: 'http://example.org/nse', prefix: 'px')
8
+ UNPREFIXED = Namespace.new(uri: 'http://example.org/nse')
9
+ PREFIXED = Namespace.new(uri: 'http://example.org/nse', prefix: 'px')
10
+ UNPREFIXED_SL = Namespace.new(uri: 'http://example.org/nse', schema_location: 'http://example.org/nse.xsd')
11
+ PREFIXED_SL = Namespace.new(uri: 'http://example.org/nse', schema_location: 'http://example.org/nse.xsd', prefix: 'px')
12
+ PREFIXED_SL1 = Namespace.new(uri: 'http://example.org/nse1', schema_location: 'http://example.org/nse1.xsd', prefix: 'px1')
13
+ PREFIXED_SL2 = Namespace.new(uri: 'http://example.org/nse2', schema_location: 'http://example.org/nse2.xsd', prefix: 'px2')
14
+ PREFIXED_SL3 = Namespace.new(uri: 'http://example.org/nse3', schema_location: 'http://example.org/nse3.xsd', prefix: 'px3')
10
15
 
11
16
  # TODO: both versions with schema location
12
17
 
@@ -26,28 +31,218 @@ module XML
26
31
  text_node :text, 'text()'
27
32
  end
28
33
 
34
+ class UnprefixedSL
35
+ include Namespaced
36
+ namespace UNPREFIXED_SL
37
+ root_element_name 'unprefixed'
38
+ text_node :attr, '@attr'
39
+ text_node :text, 'text()'
40
+
41
+ def initialize(attr = nil, text = nil)
42
+ self.attr = attr
43
+ self.text = text
44
+ end
45
+ end
46
+
47
+ class PrefixedSL
48
+ include Namespaced
49
+ namespace PREFIXED_SL
50
+ root_element_name 'prefixed'
51
+ text_node :attr, '@attr'
52
+ text_node :text, 'text()'
53
+ end
54
+
55
+ class Innermost
56
+ include Namespaced
57
+ namespace PREFIXED_SL3
58
+ object_node :unprefixed, 'unprefixed', class: UnprefixedSL, default_value: nil
59
+ text_node :text, 'text()'
60
+
61
+ def initialize(text_val, unprefixed = nil)
62
+ self.text = text_val
63
+ self.unprefixed = unprefixed
64
+ end
65
+ end
66
+
67
+ class Inner
68
+ include Namespaced
69
+ namespace PREFIXED_SL2
70
+ text_node :attr, '@attr'
71
+ text_node :subnested, 'subnested'
72
+ object_node :innermost, 'innermost', class: Innermost
73
+
74
+ def initialize(attr_val, subnested_val, innermost)
75
+ self.attr = attr_val
76
+ self.subnested = subnested_val
77
+ self.innermost = innermost
78
+ end
79
+ end
80
+
81
+ class Outer
82
+ include Namespaced
83
+ namespace PREFIXED_SL1
84
+ root_element_name 'outer'
85
+ text_node :nested, 'nested'
86
+ array_node :inners, 'inner', class: Inner, default_value: []
87
+
88
+ def initialize(nested, inners)
89
+ self.nested = nested
90
+ self.inners = inners
91
+ end
92
+ end
93
+
29
94
  describe Namespaced do
30
- describe '#save_to_xml' do
31
- it 'writes XML with a default namespace' do
32
- obj = Unprefixed.new
33
- obj.attr = 'attr value'
34
- obj.text = 'text value'
35
- expected = '<unprefixed xmlns="http://example.org/nse" attr="attr value">text value</unprefixed>'
95
+
96
+ describe 'nested' do
97
+ it 'writes XML' do
98
+ obj = Outer.new('nested',
99
+ [Inner.new('1', 'a', Innermost.new('1')),
100
+ Inner.new('2', 'b', Innermost.new('2', UnprefixedSL.new('attr', 'text')))])
101
+ expected = '
102
+ <px1:outer
103
+ xmlns:px1="http://example.org/nse1"
104
+ xmlns:px2="http://example.org/nse2"
105
+ xmlns:px3="http://example.org/nse3"
106
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
107
+ xsi:schemaLocation="http://example.org/nse1 http://example.org/nse1.xsd http://example.org/nse2 http://example.org/nse2.xsd http://example.org/nse3 http://example.org/nse3.xsd">
108
+ <px1:nested>nested</px1:nested>
109
+ <px2:inner attr="1">
110
+ <px2:subnested>a</px2:subnested>
111
+ <px3:innermost>1</px3:innermost>
112
+ </px2:inner>
113
+ <px2:inner attr="2">
114
+ <px2:subnested>b</px2:subnested>
115
+ <px3:innermost>
116
+ <unprefixed xmlns="http://example.org/nse" xsi:schemaLocation="http://example.org/nse http://example.org/nse.xsd" attr="attr">text</unprefixed>
117
+ 2
118
+ </px3:innermost>
119
+ </px2:inner>
120
+ </px1:outer>'
36
121
  expect(obj.write_xml).to be_xml(expected)
37
122
  end
38
- it 'writes XML with a prefixed namespace' do
39
- obj = Prefixed.new
40
- obj.attr = 'attr value'
41
- obj.text = 'text value'
42
- expected = '<px:prefixed xmlns:px="http://example.org/nse" attr="attr value">text value</unprefixed>'
43
- expect(obj.write_xml).to be_xml(expected)
123
+
124
+ it 'parses XML' do
125
+ xml = '<px1:outer
126
+ xmlns:px1="http://example.org/nse1"
127
+ xmlns:px2="http://example.org/nse2"
128
+ xmlns:px3="http://example.org/nse3"
129
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
130
+ xsi:schemaLocation="http://example.org/nse1 http://example.org/nse1.xsd http://example.org/nse2 http://example.org/nse2.xsd http://example.org/nse3 http://example.org/nse3.xsd">
131
+ <px1:nested>nested</px1:nested>
132
+ <px2:inner attr="1">
133
+ <px2:subnested>a</px2:subnested>
134
+ <px3:innermost>1</px3:innermost>
135
+ </px2:inner>
136
+ <px2:inner attr="2">
137
+ <px2:subnested>b</px2:subnested>
138
+ <px3:innermost>2</px3:innermost>
139
+ </px2:inner>
140
+ </px1:outer>'
141
+ result = Outer.parse_xml(xml)
142
+ expect(result).to be_an(Outer)
143
+ expect(result.nested).to eq('nested')
144
+ inners = result.inners
145
+ expect(inners).to be_an(Array)
146
+ expect(inners.size).to eq(2)
147
+ expect(inners[0].attr).to eq('1')
148
+ expect(inners[1].attr).to eq('2')
149
+ expect(inners[0].subnested).to eq('a')
150
+ expect(inners[1].subnested).to eq('b')
151
+ innermost1 = inners[0].innermost
152
+ expect(innermost1).to be_an(Innermost)
153
+ expect(innermost1.text).to eq('1')
154
+ innermost2 = inners[1].innermost
155
+ expect(innermost2).to be_an(Innermost)
156
+ expect(innermost2.text).to eq('2')
157
+ end
158
+ end
159
+
160
+ describe 'without schema location' do
161
+ describe '#save_to_xml' do
162
+ it 'writes XML with a default namespace' do
163
+ obj = Unprefixed.new
164
+ obj.attr = 'attr value'
165
+ obj.text = 'text value'
166
+ expected = '<unprefixed xmlns="http://example.org/nse" attr="attr value">text value</unprefixed>'
167
+ expect(obj.write_xml).to be_xml(expected)
168
+ end
169
+ it 'writes XML with a prefixed namespace' do
170
+ obj = Prefixed.new
171
+ obj.attr = 'attr value'
172
+ obj.text = 'text value'
173
+ expected = '<px:prefixed xmlns:px="http://example.org/nse" attr="attr value">text value</px:prefixed>'
174
+ expect(obj.write_xml).to be_xml(expected)
175
+ end
176
+ end
177
+ describe '#parse_xml' do
178
+ it 'parses XML with a default namespace' do
179
+ parsed = Unprefixed.parse_xml('<unprefixed xmlns="http://example.org/nse" attr="attr value">text value</unprefixed>')
180
+ expect(parsed).to be_an(Unprefixed)
181
+ expect(parsed.attr).to eq('attr value')
182
+ expect(parsed.text).to eq('text value')
183
+ end
184
+ it 'parses XML with a prefixed namespace' do
185
+ parsed = Prefixed.parse_xml('<px:prefixed xmlns:px="http://example.org/nse" attr="attr value">text value</px:prefixed>')
186
+ expect(parsed).to be_an(Prefixed)
187
+ expect(parsed.attr).to eq('attr value')
188
+ expect(parsed.text).to eq('text value')
189
+ end
44
190
  end
45
191
  end
46
192
 
47
- describe '#parse_xml' do
48
- it 'parses XML with a default namespace'
49
- it 'parses XML with a prefixed namespace'
193
+ describe 'with schema location' do
194
+ describe '#save_to_xml' do
195
+ it 'writes XML with a default namespace' do
196
+ obj = UnprefixedSL.new
197
+ obj.attr = 'attr value'
198
+ obj.text = 'text value'
199
+ expected = '<unprefixed
200
+ xmlns="http://example.org/nse"
201
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
202
+ xsi:schemaLocation="http://example.org/nse http://example.org/nse.xsd"
203
+ attr="attr value"
204
+ >text value</unprefixed>'
205
+ expect(obj.write_xml).to be_xml(expected)
206
+ end
207
+ it 'writes XML with a prefixed namespace' do
208
+ obj = PrefixedSL.new
209
+ obj.attr = 'attr value'
210
+ obj.text = 'text value'
211
+ expected = '<px:prefixed
212
+ xmlns:px="http://example.org/nse"
213
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
214
+ xsi:schemaLocation="http://example.org/nse http://example.org/nse.xsd"
215
+ attr="attr value"
216
+ >text value</px:prefixed>'
217
+ expect(obj.write_xml).to be_xml(expected)
218
+ end
219
+ end
220
+ describe '#parse_xml' do
221
+ it 'parses XML with a default namespace' do
222
+ parsed = UnprefixedSL.parse_xml('<unprefixed
223
+ xmlns="http://example.org/nse"
224
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
225
+ xsi:schemaLocation="http://example.org/nse http://example.org/nse.xsd"
226
+ attr="attr value"
227
+ >text value</unprefixed>')
228
+ expect(parsed).to be_an(UnprefixedSL)
229
+ expect(parsed.attr).to eq('attr value')
230
+ expect(parsed.text).to eq('text value')
231
+ end
232
+ it 'parses XML with a prefixed namespace' do
233
+ parsed = PrefixedSL.parse_xml('<px:prefixed
234
+ xmlns:px="http://example.org/nse"
235
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
236
+ xsi:schemaLocation="http://example.org/nse http://example.org/nse.xsd"
237
+ attr="attr value"
238
+ >text value</px:prefixed>')
239
+ expect(parsed).to be_an(PrefixedSL)
240
+ expect(parsed.attr).to eq('attr value')
241
+ expect(parsed.text).to eq('text value')
242
+ end
243
+ end
50
244
  end
51
245
  end
52
246
  end
247
+
53
248
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xml-mapping_extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Moles
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-16 00:00:00.000000000 Z
11
+ date: 2016-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mime-types
@@ -231,7 +231,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
231
231
  version: '0'
232
232
  requirements: []
233
233
  rubyforge_project:
234
- rubygems_version: 2.4.6
234
+ rubygems_version: 2.4.5.1
235
235
  signing_key:
236
236
  specification_version: 4
237
237
  summary: Some extensions for for XML::Mapping