File: build_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 (106 lines) | stat: -rw-r--r-- 2,892 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
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
require 'spec_helper'

describe Virtus::Attribute::Hash, '.build' do
  subject { described_class.build(type, options) }

  let(:options) { {} }

  shared_examples_for 'a valid hash attribute instance' do
    it { is_expected.to be_instance_of(Virtus::Attribute::Hash) }

    it { is_expected.to be_frozen }
  end

  context 'when type is Hash' do
    let(:type) { Hash }

    it { is_expected.to be_instance_of(Virtus::Attribute::Hash) }

    it 'sets default key type' do
      expect(subject.type.key_type).to be(Axiom::Types::Object)
    end

    it 'sets default value type' do
      expect(subject.type.value_type).to be(Axiom::Types::Object)
    end
  end

  context 'when type is Hash[String => Integer]' do
    let(:type) { Hash[String => Integer] }

    it { is_expected.to be_instance_of(Virtus::Attribute::Hash) }

    it 'sets key type' do
      expect(subject.type.key_type).to be(Axiom::Types::String)
    end

    it 'sets value type' do
      expect(subject.type.value_type).to be(Axiom::Types::Integer)
    end
  end

  context 'when type is Hash[Virtus::Attribute::Hash => Virtus::Attribute::Boolean]' do
    let(:type) { Hash[Virtus::Attribute::Hash => Virtus::Attribute::Boolean] }

    it { is_expected.to be_instance_of(Virtus::Attribute::Hash) }

    it 'sets key type' do
      expect(subject.type.key_type).to be(Axiom::Types::Hash)
    end

    it 'sets value type' do
      expect(subject.type.value_type).to be(Axiom::Types::Boolean)
    end
  end

  context 'when type is Hash[Struct.new(:id) => Integer]' do
    let(:type)     { Hash[key_type => Integer] }
    let(:key_type) { Struct.new(:id) }

    it { is_expected.to be_instance_of(Virtus::Attribute::Hash) }

    it 'sets key type' do
      expect(subject.type.key_type).to be(key_type)
    end

    it 'sets value type' do
      expect(subject.type.value_type).to be(Axiom::Types::Integer)
    end
  end

  context 'when type is Hash[String => Struct.new(:id)]' do
    let(:type)       { Hash[String => value_type] }
    let(:value_type) { Struct.new(:id) }

    it { is_expected.to be_instance_of(Virtus::Attribute::Hash) }

    it 'sets key type' do
      expect(subject.type.key_type).to be(Axiom::Types::String)
    end

    it 'sets value type' do
      expect(subject.type.value_type).to be(value_type)
    end
  end

  context 'when type is Hash[String => Integer, Integer => String]' do
    let(:type) { Hash[String => Integer, :Integer => :String] }

    specify do
      expect { subject }.to raise_error(
        ArgumentError,
        "more than one [key => value] pair in `#{type}`"
      )
    end
  end

  context 'when strict mode is used' do
    let(:type) { Hash[String => Integer] }
    let(:options) { { :strict => true } }

    it 'sets the strict mode for key/value types' do
      expect(subject.key_type).to be_strict
      expect(subject.value_type).to be_strict
    end
  end
end