File: record_spec.rb

package info (click to toggle)
ruby-morpher 0.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 236 kB
  • sloc: ruby: 1,444; makefile: 4
file content (141 lines) | stat: -rw-r--r-- 3,418 bytes parent folder | download
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
# frozen_string_literal: true

RSpec.describe Morpher::Record do
  describe '.new' do
    def apply
      described_class.new(**attributes)
    end

    let(:fields) { { foo: Morpher::Transform::STRING } }

    context 'on absent :optional' do
      let(:attributes) { { required: fields } }

      it 'defaults to empty optional' do
        expect(apply.to_h).to eql(
          optional: {},
          required: fields
        )
      end
    end

    context 'on absent :required' do
      let(:attributes) { { optional: fields } }

      it 'defaults to empty optional' do
        expect(apply.to_h).to eql(
          optional: fields,
          required: {}
        )
      end
    end

    context 'on present :required and :optional' do
      let(:attributes) do
        {
          optional: { foo: Morpher::Transform::STRING },
          required: { bar: Morpher::Transform::STRING }
        }
      end

      it 'does not apply defaults' do
        expect(apply.to_h).to eql(attributes)
      end
    end
  end

  describe '#included' do
    define_method(:apply) do
      instance = instance()

      host.class_eval { include instance }
    end

    let(:attributes)     { { a: 'foo', b: 10, c: nil } }
    let(:host)           { Class.new                   }
    let(:host_instance)  { host.new(attributes)        }

    let(:instance) do
      described_class.new(
        required: required_transform,
        optional: optional_transform
      )
    end

    let(:required_transform) do
      {
        a: Morpher::Transform::Primitive.new(String),
        b: Morpher::Transform::Primitive.new(Integer)
      }
    end

    let(:optional_transform) do
      {
        c: Morpher::Transform::Boolean.new
      }
    end

    let(:expected_required_keys_transform) do
      [
        Morpher::Transform::Hash::Key.new(:a, required_transform.fetch(:a)),
        Morpher::Transform::Hash::Key.new(:b, required_transform.fetch(:b))
      ]
    end

    let(:expected_optional_keys_transform) do
      [
        Morpher::Transform::Hash::Key.new(:c, optional_transform.fetch(:c))
      ]
    end

    it 'creates public #to_h method on host instances' do
      apply

      expect(host_instance.to_h).to eql(attributes)
    end

    it 'includes Adamantium::Flat into the host' do
      apply

      expect(host.ancestors).to include(Adamantium::Flat)

      expect(host_instance.frozen?).to be(true)
    end

    it 'allows to equalize on host instances' do
      apply

      expect(host_instance.eql?(host.new(attributes))).to be(true)
    end

    it 'creates the expected TRANSFORMER constant on the host' do
      apply

      expect(host::TRANSFORM).to eql(
        Morpher::Transform::Sequence.new(
          [
            Morpher::Transform::Primitive.new(Hash),
            Morpher::Transform::Hash::Symbolize.new,
            Morpher::Transform::Hash.new(
              required: expected_required_keys_transform,
              optional: expected_optional_keys_transform
            ),
            Morpher::Transform::Success.new(host.method(:new))
          ]
        )
      )
    end

    it 'does not create the #value method on the instance' do
      apply

      expect(instance.instance_methods).to_not include(:value)
    end

    it 'does not create the TRANSFORMER constant on the instance' do
      apply

      expect(instance.constants).to_not include(:TRANSFORMER)
    end
  end
end