File: definition_spec.rb

package info (click to toggle)
ruby-roxml 3.3.1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 824 kB
  • ctags: 557
  • sloc: ruby: 4,066; xml: 1,013; makefile: 5
file content (495 lines) | stat: -rw-r--r-- 16,743 bytes parent folder | download
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# encoding: utf-8
require_relative './spec_helper'

describe ROXML::Definition do
  describe "#name_explicit?" do
    it "should indicate whether from option is present" do
      ROXML::Definition.new(:element, :from => 'somewhere').name_explicit?.should be true
      ROXML::Definition.new(:element).name_explicit?.should be false
    end

    it "should not consider name proxies as explicit" do
      ROXML::Definition.new(:element, :from => :attr).name_explicit?.should be false
      ROXML::Definition.new(:element, :from => :content).name_explicit?.should be false
    end
  end

  shared_examples_for "DateTime reference" do
    it "should return nil on empty string" do
      @subject.blocks.first.call("  ").should be_nil
    end

    it "should return a time version of the string" do
      @subject.blocks.first.call("12:05pm, September 3rd, 1970").to_s == "1970-09-03T12:05:00+00:00"
    end

    context "when passed an array of values" do
      it "should timify all of them" do
        @subject.blocks.first.call(["12:05pm, September 3rd, 1970", "3:00pm, May 22, 1700"]).map(&:to_s).should == ["1970-09-03T12:05:00+00:00", "1700-05-22T15:00:00+00:00"]
      end
    end
  end

  shared_examples_for "Date reference" do
    it "should return nil on empty string" do
      @subject.blocks.first.call("  ").should be_nil
    end

    it "should return a time version of the string" do
      @subject.blocks.first.call("September 3rd, 1970").to_s == "1970-09-03"
    end

    context "when passed an array of values" do
      it "should timify all of them" do
        @subject.blocks.first.call(["September 3rd, 1970", "1776-07-04"]).map(&:to_s).should == ["1970-09-03", "1776-07-04"]
      end
    end
  end

  it "should unescape xml entities" do
    ROXML::Definition.new(:questions, :as => []).to_ref(RoxmlObject.new).value_in(%{
      <xml>
        <question>&quot;Wickard &amp; Filburn&quot; &gt;</question>
        <question> &lt; McCulloch &amp; Maryland?</question>
      </xml>
    }).should == ["\"Wickard & Filburn\" >", " < McCulloch & Maryland?"]
  end

  it "should unescape utf characters in xml" do
    ROXML::Definition.new(:questions, :as => []).to_ref(RoxmlObject.new).value_in(%{
      <xml>
        <question>ROXML\342\204\242</question>
      </xml>
    }).should == ["ROXML™"]
  end

  describe "attr name" do
    context "when ending with '_at'" do
      context "and without an :as argument" do
        before(:all) do
          @subject = ROXML::Definition.new(:time_at)
        end
        it_should_behave_like "DateTime reference"
      end
    end

    context "when ending with '_on'" do
      context "and without an :as argument" do
        before(:all) do
          @subject = ROXML::Definition.new(:created_on)
        end
        it_should_behave_like "Date reference"
      end
    end
  end

  describe ":as" do
    describe "=> []" do
      it "should means array of texts" do
        opts = ROXML::Definition.new(:authors, :as => [])
        opts.array?.should be true
        opts.sought_type.should == :text
      end
    end

    describe "=> RoxmlClass" do
      class RoxmlClass
        include ROXML
      end

      it "should store type" do
        opts = ROXML::Definition.new(:name, :as => RoxmlClass)
        opts.sought_type.should == RoxmlClass
      end
    end

    describe "=> NonRoxmlClassWithFromXmlDefined" do
      class OctalInteger
        def self.from_xml(val)
          new(Integer(val.content))
        end
      end

      it "should accept type" do
        opts = ROXML::Definition.new(:name, :as => OctalInteger)
        opts.sought_type.should == OctalInteger
      end
    end

    describe "=> NonRoxmlClass" do
      it "should fail with a warning" do
        proc { ROXML::Definition.new(:authors, :as => Module) }.should raise_error(ArgumentError)
      end
    end

    describe "=> [NonRoxmlClass]" do
      it "should raise" do
        proc { ROXML::Definition.new(:authors, :as => [Module]) }.should raise_error(ArgumentError)
      end
    end

    describe "=> {}" do
      shared_examples_for "hash options declaration" do
        it "should represent a hash" do
          @opts.hash?.should be true
        end

        it "should have hash definition" do
          {@opts.hash.key.sought_type => @opts.hash.key.name}.should == @hash_args[:key]
          {@opts.hash.value.sought_type => @opts.hash.value.name}.should == @hash_args[:value]
        end

        it "should not represent an array" do
          @opts.array?.should be false
        end
      end

      describe "hash with attr key and text val" do
        before do
          @opts = ROXML::Definition.new(:attributes, :as => {:key => '@name',
                                                             :value => 'value'})
          @hash_args = {:key => {:attr => 'name'},
                        :value => {:text => 'value'}}
        end

        it_should_behave_like "hash options declaration"
      end

      describe "hash with String class for type" do
        before do
          @opts = ROXML::Definition.new(:attributes, :as => {:key => 'name',
                                                             :value => 'value'})
          @hash_args = {:key => {:text => 'name'}, :value => {:text => 'value'}}
        end

        it_should_behave_like "hash options declaration"
      end

      describe "hash with attr key and content val" do
        before do
          @opts = ROXML::Definition.new(:attributes, :as => {:key => '@name',
                                                             :value => :content})
          @hash_args = {:key => {:attr => 'name'}, :value => {:text => '.'}}
        end

        it_should_behave_like "hash options declaration"
      end

      describe "hash with names as keys and content vals" do
        before do
          @opts = ROXML::Definition.new(:attributes, :as => {:key => :name,
                                                             :value => :content})
          @hash_args = {:key => {:text => '*'}, :value => {:text => '.'}}
        end

        it_should_behave_like "hash options declaration"
      end
    end

    describe "for block shorthand" do
      describe "in literal array" do
        before do
          @opts = ROXML::Definition.new(:intarray, :as => [Integer])
        end

        it "should be detected as array reference" do
          @opts.array?.should be true
        end

        it "should be normal otherwise" do
          @opts.sought_type.should == :text
          @opts.blocks.size.should == 1
        end
      end

      it "should have no blocks without a shorthand" do
        ROXML::Definition.new(:count).blocks.should be_empty
      end

      it "should raise on unknown :as" do
        proc { ROXML::Definition.new(:count, :as => :bogus) }.should raise_error(ArgumentError)
        proc { ROXML::Definition.new(:count, :as => :foat) }.should raise_error(ArgumentError)
      end

      shared_examples_for "block shorthand type declaration" do
        it "should translate nil to nil" do
          @definition.blocks.first.call(nil).should be_nil
        end

        it "should translate empty strings to nil" do
          @definition.blocks.first.call("").should be_nil
          @definition.blocks.first.call(" ").should be_nil
        end
      end

      describe "Integer" do
        before do
          @definition = ROXML::Definition.new(:intvalue, :as => Integer)
        end

        it_should_behave_like "block shorthand type declaration"

        it "should translate text to integers" do
          @definition.blocks.first['3'].should == 3
          @definition.blocks.first['792'].should == 792
        end

        it "should raise on non-integer values" do
          proc { @definition.blocks.first['08'] }.should raise_error(ArgumentError)
          proc { @definition.blocks.first['793.12'] }.should raise_error(ArgumentError)
          proc { @definition.blocks.first['junk 11'] }.should raise_error(ArgumentError)
          proc { @definition.blocks.first['11sttf'] }.should raise_error(ArgumentError)
        end

        context "when passed an array" do
          it "should translate the array elements to integer" do
            @definition.blocks.first.call(["792", "12", "328"]).should == [792, 12, 328]
          end
        end
      end

      describe "Float" do
        before do
          @definition = ROXML::Definition.new(:floatvalue, :as => Float)
        end

        it_should_behave_like "block shorthand type declaration"

        it "should translate text to float" do
          @definition.blocks.first['3'].should == 3.0
          @definition.blocks.first['12.7'].should == 12.7
        end

        it "should raise on non-float values" do
          proc { @definition.blocks.first['junk 11.3'] }.should raise_error(ArgumentError)
          proc { @definition.blocks.first['11.1sttf'] }.should raise_error(ArgumentError)
        end

        context "when passed an array" do
          it "should translate the array elements to integer" do
            @definition.blocks.first.call(["792.13", "240", "3.14"]).should == [792.13, 240.0, 3.14]
          end
        end
      end

      describe "BigDecimal" do
        before do
          @definition = ROXML::Definition.new(:decimalvalue, :as => BigDecimal)
        end

        it_should_behave_like "block shorthand type declaration"

        it "should translate text to decimal numbers" do
          @definition.blocks.first['3'].should == BigDecimal.new("3.0")
          @definition.blocks.first['0.3'].should == BigDecimal.new("0.3")
        end

        it "should extract what it can, and fall back to 0" do
          @definition.blocks.first['junk 11'].should eql(BigDecimal.new("0"))
          @definition.blocks.first['11sttf'].should eql(BigDecimal.new("11.0"))
        end

        context "when passed an array" do
          it "should translate the array elements to integer" do
            @definition.blocks.first.call(["12.1", "328.2"]).should == [BigDecimal.new("12.1"), BigDecimal.new("328.2")]
          end
        end
      end

      describe "Fixnum" do
        before do
          @definition = ROXML::Definition.new(:fixnumvalue, :as => Fixnum)
        end

        it_should_behave_like "block shorthand type declaration"

        it "should translate text to integers" do
          @definition.blocks.first['3'].should == 3
          @definition.blocks.first['792'].should == 792
          @definition.blocks.first['08'].should == 8
          @definition.blocks.first['279.23'].should == 279
        end

        it "should extract whatever is possible and fall back to 0" do
          @definition.blocks.first['junk 11'].should eql(0)
          @definition.blocks.first['.?sttf'].should eql(0)
          @definition.blocks.first['11sttf'].should eql(11)
        end

        context "when passed an array" do
          it "should translate the array elements to integer" do
            @definition.blocks.first.call(["792", "12", "328"]).should == [792, 12, 328]
          end
        end
      end

      describe ":bool" do
        it "should boolify individual values" do
          ROXML::Definition.new(:floatvalue, :as => :bool).blocks.first.call("1").should be true
          ROXML::Definition.new(:floatvalue, :as => :bool).blocks.first.call("True").should be true
          ROXML::Definition.new(:floatvalue, :as => :bool).blocks.first.call("Yes").should be true
        end

        context "when an array is passed in" do
          it "should boolify arrays of values" do
            ROXML::Definition.new(:floatvalue, :as => :bool).blocks.first.call("0").should be false
            ROXML::Definition.new(:floatvalue, :as => :bool).blocks.first.call("false").should be false
            ROXML::Definition.new(:floatvalue, :as => :bool).blocks.first.call("nO").should be false
          end
        end

        context "when no value is detected" do
          it "should return nil" do
            ROXML::Definition.new(:floatvalue, :as => :bool).blocks.first.call("junk").should be_nil
          end

          context "when a literal block is available" do
            it "should pass the value itself to the block"
          end
        end
      end

      describe "Time" do
        it "should return nil on empty string" do
          ROXML::Definition.new(:floatvalue, :as => Time).blocks.first.call("  ").should be_nil
        end

        it "should return a time version of the string" do
          ROXML::Definition.new(:datevalue, :as => Time).blocks.first.call("12:31am").min.should == 31
        end

        context "when passed an array of values" do
          it "should timify all of them" do
            ROXML::Definition.new(:datevalue, :as => Time).blocks.first.call(["12:31am", "3:00pm", "11:59pm"]).map(&:min).should == [31, 0, 59]
          end
        end
      end

      describe "Date" do
        before do
          @subject = ROXML::Definition.new(:datevalue, :as => Date)
        end
        it_should_behave_like "Date reference"
      end

      describe "DateTime" do
        before do
          @subject = ROXML::Definition.new(:datevalue, :as => DateTime)
        end
        it_should_behave_like "DateTime reference"
      end

      it "should prohibit multiple shorthands" do
        proc { ROXML::Definition.new(:count, :as => [Float, Integer]) }.should raise_error(ArgumentError)
      end

      it "should stack block shorthands with explicit blocks" do
        ROXML::Definition.new(:count, :as => Integer) {|val| val.to_i }.blocks.size.should == 2
        ROXML::Definition.new(:count, :as => Float) {|val| val.object_id }.blocks.size.should == 2
      end
    end
  end

  describe ":from" do
    shared_examples_for "attribute reference" do
      it "should be interpreted as :attr" do
        @opts.sought_type.should == :attr
      end

      it "should strip '@' from name" do
        @opts.name.should == 'attr_name'
      end

      it "should unescape xml entities" do
        @opts.to_ref(RoxmlObject.new).value_in(%{
          <question attr_name="&quot;Wickard &amp; Filburn&quot; &gt; / &lt; McCulloch &amp; Marryland?" />
        }).should == "\"Wickard & Filburn\" > / < McCulloch & Marryland?"
      end
    end

    context ":attr" do
      before do
        @opts = ROXML::Definition.new(:attr_name, :from => :attr)
      end

      it_should_behave_like "attribute reference"
    end

    context "@attribute_name" do
      before do
        @opts = ROXML::Definition.new(:attr_name, :from => '@attr_name')
      end

      it_should_behave_like "attribute reference"
    end

    describe ":content" do
      it "should be recognized" do
        ROXML::Definition.new(:author).content?.should be false
        ROXML::Definition.new(:author, :from => :content).content?.should == true
      end

      it "should be equivalent to :from => '.'" do
        ROXML::Definition.new(:author, :from => '.').content?.should == true
      end
    end
  end

  describe ":in" do
    context "as xpath" do
      it "should pass through as wrapper" do
        ROXML::Definition.new(:manufacturer, :in => './').wrapper.should == './'
      end
    end

    context "as xpath" do
      it "should pass through as wrapper" do
        ROXML::Definition.new(:manufacturer, :in => 'wrapper').wrapper.should == 'wrapper'
      end
    end
  end

  describe "options" do

    shared_examples_for "boolean option" do
      it "should be recognized" do
        ROXML::Definition.new(:author, :from => :content, @option => true).respond_to?(:"#{@option}?")
        ROXML::Definition.new(:author, :from => :content, @option => true).send(:"#{@option}?").should be true
        ROXML::Definition.new(:author, :from => :content, @option => false).send(:"#{@option}?").should be false
      end

      it "should default to false" do
        ROXML::Definition.new(:author, :from => :content).send(:"#{@option}?").should be false
      end
    end

    describe ":required" do
      before do
        @option = :required
      end

      it_should_behave_like "boolean option"

      it "should not be allowed together with :else" do
        proc { ROXML::Definition.new(:author, :from => :content, :required => true, :else => 'Johnny') }.should raise_error(ArgumentError)
        proc { ROXML::Definition.new(:author, :from => :content, :required => false, :else => 'Johnny') }.should_not raise_error
      end
    end

    describe ":frozen" do
      before do
        @option = :frozen
      end

      it_should_behave_like "boolean option"
    end

    describe ":cdata" do
      before do
        @option = :cdata
      end

      it_should_behave_like "boolean option"
    end
  end
end