Skip to content

Commit

Permalink
more unit tests for coercion
Browse files Browse the repository at this point in the history
  • Loading branch information
dnesteryuk committed Jan 18, 2020
1 parent 348a7c3 commit d3d767c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 53 deletions.
16 changes: 14 additions & 2 deletions spec/grape/validations/types/primitive_coercer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,20 @@
context 'Boolean' do
let(:type) { Grape::API::Boolean }

it 'coerces to Boolean' do
expect(subject.call(0)).to eq(false)
[true, 'true', 1].each do |val|
it "coerces '#{val}' to true" do
expect(subject.call(val)).to eq(true)
end
end

[false, 'false', 0].each do |val|
it "coerces '#{val}' to false" do
expect(subject.call(val)).to eq(false)
end
end

it 'returns an error when the given value cannot be coerced' do
expect(subject.call(123)).to be_instance_of(Grape::Validations::Types::InvalidValue)
end
end

Expand Down
53 changes: 2 additions & 51 deletions spec/grape/validations/validators/coerce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,66 +294,17 @@ def self.parsed?(value)
end
end

it 'Bool' do
subject.params do
requires :bool, coerce: Grape::API::Boolean
end
subject.get '/bool' do
params[:bool].class
end

get '/bool', bool: 1
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('TrueClass')

get '/bool', bool: 0
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('FalseClass')

get '/bool', bool: 'false'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('FalseClass')

get '/bool', bool: 'true'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('TrueClass')
end

it 'Boolean' do
subject.params do
optional :boolean, type: Boolean, default: true
requires :boolean, type: Boolean
end
subject.get '/boolean' do
params[:boolean].class
end

get '/boolean'
get '/boolean', boolean: 1
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('TrueClass')

get '/boolean', boolean: true
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('TrueClass')

get '/boolean', boolean: false
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('FalseClass')

get '/boolean', boolean: 'true'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('TrueClass')

get '/boolean', boolean: 'false'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('FalseClass')

get '/boolean', boolean: 123
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('boolean is invalid')

get '/boolean', boolean: '123'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('boolean is invalid')
end

it 'Rack::Multipart::UploadedFile' do
Expand Down

0 comments on commit d3d767c

Please sign in to comment.