File: definition_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 (476 lines) | stat: -rw-r--r-- 16,268 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
# encoding: utf-8
require_relative './spec_helper'

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

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

  shared_examples_for "DateTime reference" do
    it "should return nil on empty string" do
      expect(@subject.blocks.first.call("  ")).to 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
        expect(@subject.blocks.first.call(["12:05pm, September 3rd, 1970", "3:00pm, May 22, 1700"]).map(&:to_s)).to eq(["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
      expect(@subject.blocks.first.call("  ")).to 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
        expect(@subject.blocks.first.call(["September 3rd, 1970", "1776-07-04"]).map(&:to_s)).to eq(["1970-09-03", "1776-07-04"])
      end
    end
  end

  it "should unescape xml entities" do
    expect(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>
    })).to eq(["\"Wickard & Filburn\" >", " < McCulloch & Maryland?"])
  end

  it "should unescape utf characters in xml" do
    expect(ROXML::Definition.new(:questions, :as => []).to_ref(RoxmlObject.new).value_in(%{
      <xml>
        <question>ROXML\342\204\242</question>
      </xml>
    })).to eq(["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 => [])
        expect(opts.array?).to be_truthy
        expect(opts.sought_type).to eq(:text)
      end
    end

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

      it "should store type" do
        opts = ROXML::Definition.new(:name, :as => RoxmlClass)
        expect(opts.sought_type).to eq(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)
        expect(opts.sought_type).to eq(OctalInteger)
      end
    end

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

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

    describe "=> {}" do
      shared_examples_for "hash options declaration" do
        it "should represent a hash" do
          expect(@opts.hash?).to be_truthy
        end

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

        it "should not represent an array" do
          expect(@opts.array?).to be_falsey
        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
          expect(@opts.array?).to be_truthy
        end

        it "should be normal otherwise" do
          expect(@opts.sought_type).to eq(:text)
          expect(@opts.blocks.size).to eq(1)
        end
      end

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

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

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

        it "should translate empty strings to nil" do
          expect(@definition.blocks.first.call("")).to be_nil
          expect(@definition.blocks.first.call(" ")).to 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
          expect(@definition.blocks.first['3']).to eq(3)
          expect(@definition.blocks.first['792']).to eq(792)
          expect(@definition.blocks.first['08']).to eq(8)
          expect(@definition.blocks.first['279.23']).to eq(279)
        end

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

        context "when passed an array" do
          it "should translate the array elements to integer" do
            expect(@definition.blocks.first.call(["792", "12", "328"])).to eq([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
          expect(@definition.blocks.first['3']).to eq(3.0)
          expect(@definition.blocks.first['12.7']).to eq(12.7)
        end

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

        context "when passed an array" do
          it "should translate the array elements to integer" do
            expect(@definition.blocks.first.call(["792.13", "240", "3.14"])).to eq([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
          expect(@definition.blocks.first['3']).to eq(BigDecimal("3.0"))
          expect(@definition.blocks.first['0.3']).to eq(BigDecimal("0.3"))
        end

        # Ruby behavior of BigDecimal changed in 2.4, this test is not valid on older rubies
        if RUBY_VERSION >= "2.4"
          it "should raise on non-decimal values" do
            expect { @definition.blocks.first['junk 11'] }.to raise_error(ArgumentError)
          end
        end

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

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

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

        context "when no value is detected" do
          it "should return nil" do
            expect(ROXML::Definition.new(:floatvalue, :as => :bool).blocks.first.call("junk")).to 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
          expect(ROXML::Definition.new(:floatvalue, :as => Time).blocks.first.call("  ")).to be_nil
        end

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

        context "when passed an array of values" do
          it "should timify all of them" do
            expect(ROXML::Definition.new(:datevalue, :as => Time).blocks.first.call(["12:31am", "3:00pm", "11:59pm"]).map(&:min)).to eq([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
        expect { ROXML::Definition.new(:count, :as => [Float, Integer]) }.to raise_error(ArgumentError)
      end

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

  describe ":from" do
    shared_examples_for "attribute reference" do
      it "should be interpreted as :attr" do
        expect(@opts.sought_type).to eq(:attr)
      end

      it "should strip '@' from name" do
        expect(@opts.name).to eq('attr_name')
      end

      it "should unescape xml entities" do
        expect(@opts.to_ref(RoxmlObject.new).value_in(%{
          <question attr_name="&quot;Wickard &amp; Filburn&quot; &gt; / &lt; McCulloch &amp; Marryland?" />
        })).to eq("\"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
        expect(ROXML::Definition.new(:author).content?).to be_falsey
        expect(ROXML::Definition.new(:author, :from => :content).content?).to eq(true)
      end

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

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

    context "as xpath" do
      it "should pass through as wrapper" do
        expect(ROXML::Definition.new(:manufacturer, :in => 'wrapper').wrapper).to eq('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}?")
        expect(ROXML::Definition.new(:author, :from => :content, @option => true).send(:"#{@option}?")).to be_truthy
        expect(ROXML::Definition.new(:author, :from => :content, @option => false).send(:"#{@option}?")).to be_falsey
      end

      it "should default to false" do
        expect(ROXML::Definition.new(:author, :from => :content).send(:"#{@option}?")).to be_falsey
      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
        expect { ROXML::Definition.new(:author, :from => :content, :required => true, :else => 'Johnny') }.to raise_error(ArgumentError)
        expect { ROXML::Definition.new(:author, :from => :content, :required => false, :else => 'Johnny') }.to_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

  describe 'frozen_string_literal behavior' do
    it 'should not raise error' do
      expect { ROXML::Definition.new(:element, :from => '@somewhere'.freeze) }.not_to raise_error(FrozenError)
    end
  end
end