tilia-xml 1.2.0.2 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,7 +9,7 @@ module Tilia
9
9
  @reader = Reader.new
10
10
  end
11
11
 
12
- def test_should_load_clark
12
+ def test_get_clark
13
13
  input = <<BLA
14
14
  <?xml version="1.0"?>
15
15
  <root xmlns="http://sabredav.org/ns" />
@@ -19,7 +19,7 @@ BLA
19
19
  assert_equal('{http://sabredav.org/ns}root', @reader.clark)
20
20
  end
21
21
 
22
- def test_should_get_clark_without_ns
22
+ def test_get_clark_no_ns
23
23
  input = <<BLA
24
24
  <?xml version="1.0"?>
25
25
  <root />
@@ -29,7 +29,7 @@ BLA
29
29
  assert_equal('{}root', @reader.clark)
30
30
  end
31
31
 
32
- def test_should_load_clark_on_an_element
32
+ def test_get_clark_not_on_an_element
33
33
  input = <<BLA
34
34
  <?xml version="1.0"?>
35
35
  <root />
@@ -38,7 +38,7 @@ BLA
38
38
  assert_nil(@reader.clark)
39
39
  end
40
40
 
41
- def test_should_parse_a_simple_xml_file
41
+ def test_simple
42
42
  input = <<BLA
43
43
  <?xml version="1.0"?>
44
44
  <root xmlns="http://sabredav.org/ns">
@@ -79,7 +79,7 @@ BLA
79
79
  assert_equal(expected, output)
80
80
  end
81
81
 
82
- def test_should_work_with_cdata
82
+ def test_cdata
83
83
  input = <<BLA
84
84
  <?xml version="1.0"?>
85
85
  <root xmlns="http://sabredav.org/ns">
@@ -104,7 +104,7 @@ BLA
104
104
  assert_equal(expected, output)
105
105
  end
106
106
 
107
- def test_should_read_simple_namespaced_attributes
107
+ def test_simple_namespaced_attribute
108
108
  input = <<BLA
109
109
  <?xml version="1.0"?>
110
110
  <root xmlns="http://sabredav.org/ns" xmlns:foo="urn:foo">
@@ -131,7 +131,7 @@ BLA
131
131
  assert_equal(expected, output)
132
132
  end
133
133
 
134
- def test_should_read_mapped_elements
134
+ def test_mapped_element
135
135
  input = <<BLA
136
136
  <?xml version="1.0"?>
137
137
  <root xmlns="http://sabredav.org/ns">
@@ -159,7 +159,26 @@ BLA
159
159
  assert_equal(expected, output)
160
160
  end
161
161
 
162
- def test_should_activate_mapped_element_callbacks
162
+ def test_mapped_element_bad_class
163
+ input = <<BLA
164
+ <?xml version="1.0"?>
165
+ <root xmlns="http://sabredav.org/ns">
166
+ <elem1 />
167
+ </root>
168
+ BLA
169
+
170
+ reader = Reader.new
171
+ reader.element_map = {
172
+ '{http://sabredav.org/ns}elem1' => Class.new
173
+ }
174
+ reader.xml(input)
175
+
176
+ assert_raises(RuntimeError) do
177
+ reader.parse
178
+ end
179
+ end
180
+
181
+ def test_mapped_element_call_back
163
182
  input = <<BLA
164
183
  <?xml version="1.0"?>
165
184
  <root xmlns="http://sabredav.org/ns">
@@ -190,7 +209,7 @@ BLA
190
209
  assert_equal(expected, output)
191
210
  end
192
211
 
193
- def test_should_read_text
212
+ def test_read_text
194
213
  input = <<BLA
195
214
  <?xml version="1.0"?>
196
215
  <root xmlns="http://sabredav.org/ns">
@@ -221,7 +240,7 @@ BLA
221
240
  assert_equal(expected, output)
222
241
  end
223
242
 
224
- def test_should_raise_exceptions
243
+ def test_parse_problem
225
244
  input = <<BLA
226
245
  <?xml version="1.0"?>
227
246
  <root xmlns="http://sabredav.org/ns">
@@ -234,7 +253,7 @@ BLA
234
253
  assert_raises(LibXmlException) { @reader.parse }
235
254
  end
236
255
 
237
- def test_should_handle_broken_parser
256
+ def test_broken_parser_class
238
257
  input = <<BLA
239
258
  <?xml version="1.0"?>
240
259
  <root xmlns="http://sabredav.org/ns">
@@ -248,7 +267,7 @@ BLA
248
267
  assert_raises(ParseException) { @reader.parse }
249
268
  end
250
269
 
251
- def test_should_handle_invalid_xml
270
+ def test_broken_xml
252
271
  input = <<BLA
253
272
  <test>
254
273
  <hello>
@@ -259,7 +278,7 @@ BLA
259
278
  assert_raises(LibXmlException) { @reader.parse }
260
279
  end
261
280
 
262
- def test_should_handle_invalid_xml_2
281
+ def test_broken_xml2
263
282
  input = <<BLA
264
283
  <?xml version="1.0" encoding="UTF-8"?>
265
284
  <definitions>
@@ -277,7 +296,7 @@ BLA
277
296
  assert_raises(LibXmlException) { @reader.parse }
278
297
  end
279
298
 
280
- def test_should_parse_an_inner_tree
299
+ def test_parse_inner_tree
281
300
  input = <<BLA
282
301
  <?xml version="1.0"?>
283
302
  <root xmlns="http://sabredav.org/ns">
@@ -321,7 +340,7 @@ BLA
321
340
  assert_equal(expected, output)
322
341
  end
323
342
 
324
- def test_should_use_parse_get_elements
343
+ def test_parse_get_elements
325
344
  input = <<BLA
326
345
  <?xml version="1.0"?>
327
346
  <root xmlns="http://sabredav.org/ns">
@@ -365,7 +384,7 @@ BLA
365
384
  assert_equal(expected, output)
366
385
  end
367
386
 
368
- def test_should_work_with_no_elements
387
+ def test_parse_get_elements_no_elements
369
388
  input = <<BLA
370
389
  <?xml version="1.0"?>
371
390
  <root xmlns="http://sabredav.org/ns">
@@ -0,0 +1,36 @@
1
+ require 'test_helper'
2
+
3
+ module Tilia
4
+ module Xml
5
+ module Serializer
6
+ class EnumTest < Minitest::Test
7
+ def test_serialize
8
+ service = Service.new
9
+ service.namespace_map['urn:test'] = nil
10
+
11
+ xml = service.write(
12
+ '{urn:test}root',
13
+ lambda do |writer|
14
+ Serializer.enum(
15
+ writer,
16
+ [
17
+ '{urn:test}foo1',
18
+ '{urn:test}foo2'
19
+ ]
20
+ )
21
+ end
22
+ )
23
+
24
+ expected = <<XML
25
+ <?xml version="1.0"?>
26
+ <root xmlns="urn:test">
27
+ <foo1/>
28
+ <foo2/>
29
+ </root>
30
+ XML
31
+ assert_xml_equal(expected, xml)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,37 @@
1
+ require 'test_helper'
2
+
3
+ module Tilia
4
+ module Xml
5
+ module Serializer
6
+ class RepeatingElementsTest < Minitest::Test
7
+ def test_serialize
8
+ service = Service.new
9
+ service.namespace_map['urn:test'] = nil
10
+ xml = service.write(
11
+ '{urn:test}collection',
12
+ lambda do |writer|
13
+ Serializer.repeating_elements(
14
+ writer,
15
+ [
16
+ 'foo',
17
+ 'bar'
18
+ ],
19
+ '{urn:test}item'
20
+ )
21
+ end
22
+ )
23
+
24
+ expected = <<XML
25
+ <?xml version="1.0"?>
26
+ <collection xmlns="urn:test">
27
+ <item>foo</item>
28
+ <item>bar</item>
29
+ </collection>
30
+ XML
31
+
32
+ assert_xml_equal(expected, xml)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -142,6 +142,48 @@ XML
142
142
  assert_equal(expected, result)
143
143
  end
144
144
 
145
+ def test_map_value_object
146
+ input = <<XML
147
+ <?xml version="1.0"?>
148
+ <order xmlns="http://sabredav.org/ns">
149
+ <id>1234</id>
150
+ <amount>99.99</amount>
151
+ <description>black friday deal</description>
152
+ <status>
153
+ <id>5</id>
154
+ <label>processed</label>
155
+ </status>
156
+ </order>
157
+ XML
158
+
159
+ ns = 'http://sabredav.org/ns'
160
+ order_service = Service.new
161
+ order_service.map_value_object("{#{ns}}order", Xml::Order)
162
+ order_service.map_value_object("{#{ns}}status", Xml::OrderStatus)
163
+ order_service.namespace_map[ns] = nil
164
+
165
+ order = order_service.parse(input)
166
+ expected = Order.new
167
+ expected.id = '1234'
168
+ expected.amount = '99.99'
169
+ expected.description = 'black friday deal'
170
+ expected.status = OrderStatus.new
171
+ expected.status.id = '5'
172
+ expected.status.label = 'processed'
173
+
174
+ assert_instance_equal(expected, order)
175
+
176
+ written_xml = order_service.write_value_object(order)
177
+ assert_equal(input, written_xml)
178
+ end
179
+
180
+ def test_write_vo_not_found
181
+ service = Service.new
182
+ assert_raises(ArgumentError) do
183
+ service.write_value_object(Class.new)
184
+ end
185
+ end
186
+
145
187
  def test_parse_clark_notation
146
188
  expected = ['http://sabredav.org/ns', 'elem']
147
189
  result = Service.parse_clark_notation('{http://sabredav.org/ns}elem')
@@ -152,5 +194,31 @@ XML
152
194
  assert_raises(ArgumentError) { Service.parse_clark_notation('http://sabredav.org/ns}elem') }
153
195
  end
154
196
  end
197
+
198
+ # asset for test_map_value_object
199
+ class Order
200
+ attr_accessor :id
201
+ attr_accessor :amount
202
+ attr_accessor :description
203
+ attr_accessor :status
204
+
205
+ def initialize
206
+ @id = nil
207
+ @amount = nil
208
+ @description = nil
209
+ @status = nil
210
+ end
211
+ end
212
+
213
+ # asset for test_map_value_object
214
+ class OrderStatus
215
+ attr_accessor :id
216
+ attr_accessor :label
217
+
218
+ def initialize
219
+ @id = nil
220
+ @label = nil
221
+ end
222
+ end
155
223
  end
156
224
  end
@@ -17,9 +17,6 @@ module Tilia
17
17
  assert_equal(output, @writer.output_memory)
18
18
  end
19
19
 
20
- # rubocop:disable Style/ClosingParenthesisIndentation
21
- # Rubocop has a problem with the closing parenthesis of the long compare()
22
- # statements, but IMO that's the best way.
23
20
  def test_simple
24
21
  compare(
25
22
  { '{http://sabredav.org/ns}root' => 'text' },
@@ -31,7 +28,9 @@ HI
31
28
  end
32
29
 
33
30
  def test_simple_quotes
34
- compare({ '{http://sabredav.org/ns}root' => '"text"' }, <<HI
31
+ compare(
32
+ { '{http://sabredav.org/ns}root' => '"text"' },
33
+ <<HI
35
34
  <?xml version="1.0"?>
36
35
  <s:root xmlns:s="http://sabredav.org/ns">&quot;text&quot;</s:root>
37
36
  HI
@@ -47,7 +46,8 @@ HI
47
46
  'attr1' => 'attribute value'
48
47
  }
49
48
  }
50
- }, <<HI
49
+ },
50
+ <<HI
51
51
  <?xml version="1.0"?>
52
52
  <s:root xmlns:s="http://sabredav.org/ns" attr1="attribute value">text</s:root>
53
53
  HI
@@ -83,7 +83,8 @@ HI
83
83
  }
84
84
  }
85
85
  }
86
- }, <<HI
86
+ },
87
+ <<HI
87
88
  <?xml version="1.0"?>
88
89
  <s:root xmlns:s="http://sabredav.org/ns">
89
90
  <single>value</single>
@@ -95,15 +96,17 @@ HI
95
96
  <verbose foo="bar">syntax</verbose>
96
97
  </s:root>
97
98
  HI
98
- )
99
+ )
99
100
  end
100
101
 
101
102
  def test_null
102
- compare({ '{http://sabredav.org/ns}root' => nil }, <<HI
103
+ compare(
104
+ { '{http://sabredav.org/ns}root' => nil },
105
+ <<HI
103
106
  <?xml version="1.0"?>
104
107
  <s:root xmlns:s="http://sabredav.org/ns"/>
105
108
  HI
106
- )
109
+ )
107
110
  end
108
111
 
109
112
  def test_array_format2
@@ -118,7 +121,8 @@ HI
118
121
  }
119
122
  }
120
123
  ]
121
- }, <<HI
124
+ },
125
+ <<HI
122
126
  <?xml version="1.0"?>
123
127
  <s:root xmlns:s="http://sabredav.org/ns">
124
128
  <s:elem1 attr1="attribute value">text</s:elem1>
@@ -127,13 +131,35 @@ HI
127
131
  )
128
132
  end
129
133
 
134
+ def test_array_format2_no_value
135
+ compare(
136
+ {
137
+ '{http://sabredav.org/ns}root' => [
138
+ {
139
+ 'name' => '{http://sabredav.org/ns}elem1',
140
+ 'attributes' => {
141
+ 'attr1' => 'attribute value'
142
+ }
143
+ }
144
+ ]
145
+ },
146
+ <<HI
147
+ <?xml version="1.0"?>
148
+ <s:root xmlns:s="http://sabredav.org/ns">
149
+ <s:elem1 attr1="attribute value"/>
150
+ </s:root>
151
+ HI
152
+ )
153
+ end
154
+
130
155
  def test_custom_namespace
131
156
  compare(
132
157
  {
133
158
  '{http://sabredav.org/ns}root' => {
134
159
  '{urn:foo}elem1' => 'bar'
135
160
  }
136
- }, <<HI
161
+ },
162
+ <<HI
137
163
  <?xml version="1.0"?>
138
164
  <s:root xmlns:s="http://sabredav.org/ns">
139
165
  <x1:elem1 xmlns:x1="urn:foo">bar</x1:elem1>
@@ -144,7 +170,9 @@ HI
144
170
 
145
171
  def test_empty_namespace
146
172
  # Empty namespaces are allowed, so we should support this.
147
- compare({ '{http://sabredav.org/ns}root' => { '{}elem1' => 'bar' } }, <<HI
173
+ compare(
174
+ { '{http://sabredav.org/ns}root' => { '{}elem1' => 'bar' } },
175
+ <<HI
148
176
  <?xml version="1.0"?>
149
177
  <s:root xmlns:s="http://sabredav.org/ns">
150
178
  <elem1 xmlns="">bar</elem1>
@@ -167,7 +195,8 @@ HI
167
195
  }
168
196
  }
169
197
  ]
170
- }, <<HI
198
+ },
199
+ <<HI
171
200
  <?xml version="1.0"?>
172
201
  <s:root xmlns:s="http://sabredav.org/ns">
173
202
  <s:elem1 attr1="val1" s:attr2="val2" x1:attr3="val3" xmlns:x1="urn:foo">text</s:elem1>
@@ -190,7 +219,9 @@ HI
190
219
  end
191
220
 
192
221
  def test_base_element
193
- compare({ '{http://sabredav.org/ns}root' => Tilia::Xml::Element::Base.new('hello') }, <<HI
222
+ compare(
223
+ { '{http://sabredav.org/ns}root' => Tilia::Xml::Element::Base.new('hello') },
224
+ <<HI
194
225
  <?xml version="1.0"?>
195
226
  <s:root xmlns:s="http://sabredav.org/ns">hello</s:root>
196
227
  HI
@@ -198,7 +229,9 @@ HI
198
229
  end
199
230
 
200
231
  def test_element_obj
201
- compare({ '{http://sabredav.org/ns}root' => Tilia::Xml::Element::Mock.new }, <<HI
232
+ compare(
233
+ { '{http://sabredav.org/ns}root' => Tilia::Xml::Element::Mock.new },
234
+ <<HI
202
235
  <?xml version="1.0"?>
203
236
  <s:root xmlns:s="http://sabredav.org/ns">
204
237
  <s:elem1>hiiii!</s:elem1>
@@ -209,7 +242,22 @@ HI
209
242
 
210
243
  def test_empty_namespace_prefix
211
244
  @writer.namespace_map['http://sabredav.org/ns'] = nil
212
- compare({ '{http://sabredav.org/ns}root' => Tilia::Xml::Element::Mock.new }, <<HI
245
+ compare(
246
+ { '{http://sabredav.org/ns}root' => Tilia::Xml::Element::Mock.new },
247
+ <<HI
248
+ <?xml version="1.0"?>
249
+ <root xmlns="http://sabredav.org/ns">
250
+ <elem1>hiiii!</elem1>
251
+ </root>
252
+ HI
253
+ )
254
+ end
255
+
256
+ def test_empty_namespace_prefix_empty_string
257
+ @writer.namespace_map['http://sabredav.org/ns'] = ''
258
+ compare(
259
+ { '{http://sabredav.org/ns}root' => Element::Mock.new },
260
+ <<HI
213
261
  <?xml version="1.0"?>
214
262
  <root xmlns="http://sabredav.org/ns">
215
263
  <elem1>hiiii!</elem1>
@@ -254,7 +302,62 @@ HI
254
302
  HI
255
303
  assert_equal(output, @writer.output_memory)
256
304
  end
257
- # rubocop:enable Style/ClosingParenthesisIndentation
305
+
306
+ def test_callback
307
+ compare(
308
+ {
309
+ '{http://sabredav.org/ns}root' => lambda do |writer|
310
+ writer.write_string('deferred writer')
311
+ end
312
+ },
313
+ <<HI
314
+ <?xml version="1.0"?>
315
+ <s:root xmlns:s="http://sabredav.org/ns">deferred writer</s:root>
316
+ HI
317
+ )
318
+ end
319
+
320
+ def test_resource
321
+ assert_raises(ArgumentError) do
322
+ compare(
323
+ { '{http://sabredav.org/ns}root' => StringIO.new },
324
+ <<HI
325
+ <?xml version="1.0"?>
326
+ <s:root xmlns:s="http://sabredav.org/ns">deferred writer</s:root>
327
+ HI
328
+ )
329
+ end
330
+ end
331
+
332
+ def test_class_map
333
+ obj = TestClass.new('value1', 'value2')
334
+
335
+ @writer.class_map[TestClass] = lambda do |writer, value|
336
+ [:@key1, :@key2].each do |key|
337
+ val = value.instance_variable_get(key)
338
+ key = key.to_s[1..-1]
339
+ writer.write_element("{http://sabredav.org/ns}#{key}", val)
340
+ end
341
+ end
342
+
343
+ compare(
344
+ { '{http://sabredav.org/ns}root' => obj },
345
+ <<HI
346
+ <?xml version="1.0"?>
347
+ <s:root xmlns:s="http://sabredav.org/ns">
348
+ <s:key1>value1</s:key1>
349
+ <s:key2>value2</s:key2>
350
+ </s:root>
351
+ HI
352
+ )
353
+ end
354
+ end
355
+
356
+ class TestClass
357
+ def initialize(a, b)
358
+ @key1 = a
359
+ @key2 = b
360
+ end
258
361
  end
259
362
  end
260
363
  end