File: module_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 (174 lines) | stat: -rw-r--r-- 5,028 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
require 'spec_helper'

describe Virtus, '.module' do
  shared_examples_for 'a valid virtus object' do
    it 'reads and writes attribute' do
      instance.name = 'John'
      expect(instance.name).to eql('John')
    end
  end

  shared_examples_for 'an object extended with virtus module' do
    context 'with default configuration' do
      subject { Virtus.module }

      it_behaves_like 'a valid virtus object' do
        let(:instance) { model.new }
      end

      it 'sets defaults' do
        expect(instance.name).to eql('Jane')
      end
    end

    context 'with constructor turned off' do
      subject { Virtus.module(:constructor => false) }

      it_behaves_like 'a valid virtus object' do
        let(:instance) { model.new }
      end

      it 'skips including constructor' do
        expect { model.new({}) }.to raise_error(ArgumentError)
      end
    end

    context 'with mass assignment is turned off' do
      subject { Virtus.module(:mass_assignment => false) }

      it_behaves_like 'a valid virtus object'

      it 'skips including mass assignment' do
        expect(instance).not_to respond_to(:attributes)
        expect(instance).not_to respond_to(:attributes=)
      end
    end

    context 'with coercion turned off' do
      subject { Virtus.module(:coerce => false) }

      it_behaves_like 'a valid virtus object'

      it 'builds non-coercible attributes' do
        expect(object.send(:attribute_set)[:name]).not_to be_coercible
      end
    end
  end

  let(:mod)      { Module.new }
  let(:model)    { Class.new }
  let(:instance) { model.new }

  before do
    mod.send(:include, subject)
    mod.attribute :name, String, :default => 'Jane'
    mod.attribute :something
  end

  context 'with a class' do
    let(:object) { model }

    before do
      model.send(:include, mod)
    end

    it 'provides attributes for the model' do
      expect(model.attribute_set[:name]).to be_kind_of(Virtus::Attribute)
    end

    it 'defaults to Object for attribute type' do
      expect(model.attribute_set[:something].type).to be(Axiom::Types::Object)
    end

    it_behaves_like 'an object extended with virtus module'
  end

  context 'with a model instance' do
    let(:object) { instance }

    before do
      instance.extend(mod)
    end

    it 'provides attributes for the instance' do
      expect(instance.send(:attribute_set)[:name]).to be_kind_of(Virtus::Attribute)
    end

    it_behaves_like 'an object extended with virtus module'
  end

  context 'with another module' do
    let(:other)  { Module.new }

    let(:object) { instance }

    before do
      other.send(:include, mod)
      model.send(:include, other)
    end

    it_behaves_like 'an object extended with virtus module'

    it 'provides attributes for the model' do
      expect(model.attribute_set[:name]).to be_kind_of(Virtus::Attribute)
    end
  end

  context 'as a peer to another module within a class' do
    subject { Virtus.module }
    let(:other)  { Module.new }

    before do
      other.send(:include, Virtus.module)
      other.attribute :last_name, String, :default => 'Doe'
      other.attribute :something_else
      model.send(:include, mod)
      model.send(:include, other)
    end

    it 'provides attributes for the model from both modules' do
      expect(model.attribute_set[:name]).to be_kind_of(Virtus::Attribute)
      expect(model.attribute_set[:something]).to be_kind_of(Virtus::Attribute)
      expect(model.attribute_set[:last_name]).to be_kind_of(Virtus::Attribute)
      expect(model.attribute_set[:something_else]).to be_kind_of(Virtus::Attribute)
    end

    it 'includes the attributes from both modules' do
      expect(model.new.attributes.keys).to eq(
        [:name, :something, :last_name, :something_else]
      )
    end
  end

  context 'with multiple other modules mixed into it' do
    subject { Virtus.module }
    let(:other)  { Module.new }
    let(:yet_another)  { Module.new }

    before do
      other.send(:include, Virtus.module)
      other.attribute :last_name, String, :default => 'Doe'
      other.attribute :something_else
      yet_another.send(:include, Virtus.module)
      yet_another.send(:include, mod)
      yet_another.send(:include, other)
      yet_another.attribute :middle_name, String, :default => 'Foobar'
      model.send(:include, yet_another)
    end

    it 'provides attributes for the model from all modules' do
      expect(model.attribute_set[:name]).to be_kind_of(Virtus::Attribute)
      expect(model.attribute_set[:something]).to be_kind_of(Virtus::Attribute)
      expect(model.attribute_set[:last_name]).to be_kind_of(Virtus::Attribute)
      expect(model.attribute_set[:something_else]).to be_kind_of(Virtus::Attribute)
      expect(model.attribute_set[:middle_name]).to be_kind_of(Virtus::Attribute)
    end

    it 'includes the attributes from all modules' do
      expect(model.new.attributes.keys).to eq(
        [:name, :something, :last_name, :something_else, :middle_name]
      )
    end
  end

end