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 (180 lines) | stat: -rw-r--r-- 4,529 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
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
require 'spec_helper'

describe Virtus::Attribute, '.build' do
  subject { described_class.build(type, options.merge(:name => name)) }

  let(:name)    { :test }
  let(:type)    { String }
  let(:options) { {} }

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

    it { is_expected.to be_frozen }
  end

  context 'without options' do
    it_behaves_like 'a valid attribute instance'

    it { is_expected.to be_coercible }
    it { is_expected.to be_public_reader }
    it { is_expected.to be_public_writer }
    it { is_expected.not_to be_lazy }

    it 'sets up a coercer' do
      expect(subject.options[:coerce]).to be(true)
      expect(subject.coercer).to be_instance_of(Virtus::Attribute::Coercer)
    end
  end

  context 'when name is passed as a string' do
    let(:name) { 'something' }

    describe '#name' do
      subject { super().name }
      it { is_expected.to be(:something) }
    end
  end

  context 'when coercion is turned off in options' do
    let(:options) { { :coerce => false } }

    it_behaves_like 'a valid attribute instance'

    it { is_expected.not_to be_coercible }
  end

  context 'when options specify reader visibility' do
    let(:options) { { :reader => :private } }

    it_behaves_like 'a valid attribute instance'

    it { is_expected.not_to be_public_reader }
    it { is_expected.to be_public_writer }
  end

  context 'when options specify writer visibility' do
    let(:options) { { :writer => :private } }

    it_behaves_like 'a valid attribute instance'

    it { is_expected.to be_public_reader }
    it { is_expected.not_to be_public_writer }
  end

  context 'when options specify lazy accessor' do
    let(:options) { { :lazy => true } }

    it_behaves_like 'a valid attribute instance'

    it { is_expected.to be_lazy }
  end

  context 'when options specify strict mode' do
    let(:options) { { :strict => true } }

    it_behaves_like 'a valid attribute instance'

    it { is_expected.to be_strict }
  end

  context 'when options specify nullify blank mode' do
    let(:options) { { :nullify_blank => true } }

    it_behaves_like 'a valid attribute instance'

    it { is_expected.to be_nullify_blank }
  end

  context 'when type is a string' do
    let(:type) { 'Integer' }

    it_behaves_like 'a valid attribute instance'

    describe '#type' do
      subject { super().type }
      it { is_expected.to be(Axiom::Types::Integer) }
    end
  end

  context 'when type is a range' do
    let(:type) { 0..10 }

    it_behaves_like 'a valid attribute instance'

    describe '#type' do
      subject { super().type }
      it { is_expected.to be(Axiom::Types.infer(Range)) }
    end
  end

  context 'when type is a symbol of an existing class constant' do
    let(:type) { :String }

    it_behaves_like 'a valid attribute instance'

    describe '#type' do
      subject { super().type }
      it { is_expected.to be(Axiom::Types::String) }
    end
  end

  context 'when type is an axiom type' do
    let(:type) { Axiom::Types::Integer }

    it_behaves_like 'a valid attribute instance'

    describe '#type' do
      subject { super().type }
      it { is_expected.to be(type) }
    end
  end

  context 'when custom attribute class exists for a given primitive' do
    let(:type)      { Class.new }
    let(:attribute) { Class.new(Virtus::Attribute) }

    before do
      attribute.primitive(type)
    end

    it { is_expected.to be_instance_of(attribute) }

    describe '#type' do
      subject { super().type }
      it { is_expected.to be(Axiom::Types::Object) }
    end
  end

  context 'when custom attribute class exists for a given array with member coercion defined' do
    let(:type)      { Class.new(Array)[String] }
    let(:attribute) { Class.new(Virtus::Attribute) }

    before do
      attribute.primitive(type.class)
    end

    it { is_expected.to be_instance_of(attribute) }

    describe '#type' do
      subject { super().type }
      it { is_expected.to be < Axiom::Types::Collection }
    end
  end

  context 'when custom collection-like attribute class exists for a given enumerable primitive' do
    let(:type)      { Class.new { include Enumerable } }
    let(:attribute) { Class.new(Virtus::Attribute::Collection) }

    before do
      attribute.primitive(type)
    end

    it { is_expected.to be_instance_of(attribute) }

    describe '#type' do
      subject { super().type }
      it { is_expected.to be < Axiom::Types::Collection }
    end
  end
end