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
|
# frozen_string_literal: true
RSpec.describe Morpher::Transform::Sequence do
subject { described_class.new(steps) }
let(:steps) do
[
hash_transform,
hash_symbolize
]
end
let(:hash_transform) do
Morpher::Transform::Hash.new(optional: [], required: [key_transform])
end
let(:key_transform) do
Morpher::Transform::Hash::Key.new('foo', Morpher::Transform::Boolean.new)
end
let(:boolean_transform) do
Morpher::Transform::Boolean.new
end
let(:hash_symbolize) do
Morpher::Transform::Hash::Symbolize.new
end
describe '#call' do
def apply
subject.call(input)
end
context 'on valid input' do
let(:input) { { 'foo' => true } }
it 'returns success' do
expect(apply).to eql(right(foo: true))
end
end
context 'on invalid input' do
let(:input) { { 'foo' => 1 } }
let(:boolean_error) do
Morpher::Transform::Error.new(
cause: nil,
input: 1,
message: 'Expected: boolean but got: 1',
transform: boolean_transform
)
end
let(:key_error) do
Morpher::Transform::Error.new(
cause: boolean_error,
input: 1,
message: nil,
transform: key_transform
)
end
let(:hash_error) do
Morpher::Transform::Error.new(
cause: key_error,
input: input,
message: nil,
transform: hash_transform
)
end
let(:index_error) do
Morpher::Transform::Error.new(
cause: hash_error,
input: input,
message: nil,
transform: Morpher::Transform::Index.new(
index: 0,
transform: hash_transform
)
)
end
let(:error) do
Morpher::Transform::Error.new(
cause: index_error,
input: input,
message: nil,
transform: subject
)
end
it 'returns failure' do
expect(apply).to eql(left(error))
end
end
end
describe '#seq' do
subject do
described_class.new([Morpher::Transform::STRING])
end
let(:other) do
Morpher::Transform::Success.new(:upcase.to_proc)
end
it 'build flat sequence' do
expect(subject.seq(other)).to eql(
described_class.new(
[
Morpher::Transform::STRING,
other
]
)
)
end
end
end
|