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 (105 lines) | stat: -rw-r--r-- 2,640 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
require 'spec_helper'

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

  let(:options) { {} }

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

    it { is_expected.to be_frozen }
  end

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

    it_behaves_like 'a valid collection attribute instance'

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

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

    it_behaves_like 'a valid collection attribute instance'

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

  context 'when type is Array[Float]' do
    let(:type) { Array[Float] }

    it_behaves_like 'a valid collection attribute instance'

    it 'sets member type' do
      expect(subject.type.member_type).to be(Axiom::Types::Float)
    end
  end

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

    specify do
      expect { subject }.to raise_error(
        NotImplementedError,
        "build SumType from list of types (#{type.inspect})"
      )
    end
  end

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

    it_behaves_like 'a valid collection attribute instance'

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

  context 'when type is Set[Float]' do
    let(:type) { Set[Float] }

    it_behaves_like 'a valid collection attribute instance'

    it 'sets member type' do
      expect(subject.type.member_type).to be(Axiom::Types::Float)
    end
  end

  context 'when type is an Enumerable' do
    let(:type) { Class.new { include Enumerable } }

    it_behaves_like 'a valid collection attribute instance'
  end

  context 'when type is Array subclass' do
    let(:type) { Class.new(Array) }

    it_behaves_like 'a valid collection attribute instance'
  end

  context 'when type is a custom collection instance' do
    let(:type) { Class.new(Array)[String] }

    it_behaves_like 'a valid collection attribute instance'

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

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

    it 'sets strict mode for member type' do
      expect(subject.member_type).to be_strict
    end
  end
end