-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1974 from ruby-grape/chore/primitive-coercer-spec
more tests for Grape::Validations::Types::PrimitiveCoercer
- Loading branch information
Showing
2 changed files
with
66 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe Grape::Validations::Types::PrimitiveCoercer do | ||
let(:strict) { false } | ||
|
||
subject { described_class.new(type, strict) } | ||
|
||
describe '.call' do | ||
context 'Boolean' do | ||
let(:type) { Grape::API::Boolean } | ||
|
||
it 'coerces to Boolean' do | ||
expect(subject.call(0)).to eq(false) | ||
end | ||
end | ||
|
||
context 'String' do | ||
let(:type) { String } | ||
|
||
it 'coerces to String' do | ||
expect(subject.call(10)).to eq('10') | ||
end | ||
end | ||
|
||
context 'BigDecimal' do | ||
let(:type) { BigDecimal } | ||
|
||
it 'coerces to BigDecimal' do | ||
expect(subject.call(5)).to eq(BigDecimal(5)) | ||
end | ||
end | ||
|
||
context 'the strict mode' do | ||
let(:strict) { true } | ||
|
||
context 'Boolean' do | ||
let(:type) { Grape::API::Boolean } | ||
|
||
it 'returns an error when the given value is not Boolean' do | ||
expect(subject.call(1)).to be_instance_of(Grape::Validations::Types::InvalidValue) | ||
end | ||
|
||
it 'returns a value as it is when the given value is Boolean' do | ||
expect(subject.call(true)).to eq(true) | ||
end | ||
end | ||
|
||
context 'BigDecimal' do | ||
let(:type) { BigDecimal } | ||
|
||
it 'returns an error when the given value is not BigDecimal' do | ||
expect(subject.call(1)).to be_instance_of(Grape::Validations::Types::InvalidValue) | ||
end | ||
|
||
it 'returns a value as it is when the given value is BigDecimal' do | ||
expect(subject.call(BigDecimal(0))).to eq(BigDecimal(0)) | ||
end | ||
end | ||
end | ||
end | ||
end |