File: hash_attributes_coercion_spec.rb

package info (click to toggle)
ruby-virtus 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 660 kB
  • sloc: ruby: 4,378; makefile: 2
file content (54 lines) | stat: -rw-r--r-- 1,305 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
require 'spec_helper'


class Package
  include Virtus

  attribute :dimensions, Hash[Symbol => Float]
  attribute :meta_info , Hash[String => String]
end


describe Package do
  let(:instance) do
    described_class.new(
      :dimensions => { 'width' => "2.2", :height => 2, "length" => 4.5 },
      :meta_info  => { 'from'  => :Me  , :to => 'You' }
    )
  end

  let(:dimensions) { instance.dimensions }
  let(:meta_info)  { instance.meta_info  }

  describe '#dimensions' do
    subject { dimensions }

    it 'has 3 keys' do
      expect(subject.keys.size).to eq(3)
    end
    it { is_expected.to have_key :width  }
    it { is_expected.to have_key :height }
    it { is_expected.to have_key :length }

    it 'should be coerced to [Symbol => Float] format' do
      expect(dimensions[:width]).to  be_eql(2.2)
      expect(dimensions[:height]).to be_eql(2.0)
      expect(dimensions[:length]).to be_eql(4.5)
    end
  end

  describe '#meta_info' do
    subject { meta_info }

    it 'has 2 keys' do
      expect(subject.keys.size).to eq(2)
    end
    it { is_expected.to have_key 'from' }
    it { is_expected.to have_key 'to'   }

    it 'should be coerced to [String => String] format' do
      expect(meta_info['from']).to eq('Me')
      expect(meta_info['to']).to eq('You')
    end
  end
end