File: coerce_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 (129 lines) | stat: -rw-r--r-- 3,632 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
require 'spec_helper'

describe Virtus::Attribute, '#coerce' do
  subject { object.coerce(input) }

  fake(:coercer) { Virtus::Attribute::Coercer }

  let(:object)   {
    described_class.build(String,
      :coercer => coercer, :strict => strict, :required => required, :nullify_blank => nullify_blank)
  }

  let(:required) { true }
  let(:nullify_blank) { false }
  let(:input)    { 1 }
  let(:output)   { '1' }

  context 'when strict mode is turned off' do
    let(:strict) { false }

    it 'uses coercer to coerce the input value' do
      mock(coercer).call(input) { output }

      expect(subject).to be(output)

      expect(coercer).to have_received.call(input)
    end
  end

  context 'when strict mode is turned on' do
    let(:strict) { true }

    it 'uses coercer to coerce the input value' do
      mock(coercer).call(input) { output }
      mock(coercer).success?(String, output) { true }

      expect(subject).to be(output)

      expect(coercer).to have_received.call(input)
      expect(coercer).to have_received.success?(String, output)
    end

    context 'when attribute is not required and input is nil' do
      let(:required) { false }
      let(:input)    { nil }

      it 'returns nil' do
        mock(coercer).call(input) { input }
        mock(coercer).success?(String, input) { false }

        expect(subject).to be(nil)

        expect(coercer).to have_received.call(input)
        expect(coercer).to have_received.success?(String, input)
      end
    end

    context 'when attribute is required and input is nil' do
      let(:input) { nil }

      it 'returns raises error' do
        mock(coercer).call(input) { input }
        mock(coercer).success?(String, input) { false }

        expect { subject }.to raise_error(Virtus::CoercionError)

        expect(coercer).to have_received.call(input)
        expect(coercer).to have_received.success?(String, input)
      end
    end

    it 'raises error when input was not coerced' do
      mock(coercer).call(input) { input }
      mock(coercer).success?(String, input) { false }

      expect { subject }.to raise_error(Virtus::CoercionError)

      expect(coercer).to have_received.call(input)
      expect(coercer).to have_received.success?(String, input)
    end
  end

  context 'when the input is an empty String' do
    let(:input) { '' }
    let(:output) { '' }

    context 'when nullify_blank is turned on' do
      let(:nullify_blank) { true }
      let(:strict) { false }
      let(:require) { false }

      it 'returns nil' do
        mock(coercer).call(input) { input }
        mock(coercer).success?(String, input) { false }

        expect(subject).to be_nil

        expect(coercer).to have_received.call(input)
        expect(coercer).to have_received.success?(String, input)
      end

      it 'returns the ouput if it was coerced' do
        mock(coercer).call(input) { output }
        mock(coercer).success?(String, output) { true }

        expect(subject).to be(output)

        expect(coercer).to have_received.call(input)
        expect(coercer).to have_received.success?(String, output)
      end
    end

    context 'when both nullify_blank and strict are turned on' do
      let(:nullify_blank) { true }
      let(:strict) { true }

      it 'does not raises an coercion error' do
        mock(coercer).call(input) { input }
        mock(coercer).success?(String, input) { false }

        expect { subject }.not_to raise_error
        expect(subject).to be_nil

        expect(coercer).to have_received.call(input)
        expect(coercer).to have_received.success?(String, input)
      end
    end
  end
end