debian-mirror-gitlab/spec/rubocop/cop/api/grape_array_missing_coerce_spec.rb

60 lines
1.7 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
require 'fast_spec_helper'
require_relative '../../../../rubocop/cop/api/grape_array_missing_coerce'
RSpec.describe RuboCop::Cop::API::GrapeArrayMissingCoerce do
2021-03-11 19:13:27 +05:30
let(:msg) do
"This Grape parameter defines an Array but is missing a coerce_with definition. " \
"For more details, see " \
"https://github.com/ruby-grape/grape/blob/master/UPGRADING.md#ensure-that-array-types-have-explicit-coercions"
end
2020-07-28 23:09:34 +05:30
subject(:cop) { described_class.new }
it 'adds an offense with a required parameter' do
2021-03-11 19:13:27 +05:30
expect_offense(<<~TYPE)
2020-07-28 23:09:34 +05:30
class SomeAPI < Grape::API::Instance
params do
requires :values, type: Array[String]
2021-03-11 19:13:27 +05:30
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
2020-07-28 23:09:34 +05:30
end
end
2021-03-11 19:13:27 +05:30
TYPE
2020-07-28 23:09:34 +05:30
end
it 'adds an offense with an optional parameter' do
2021-03-11 19:13:27 +05:30
expect_offense(<<~TYPE)
2020-07-28 23:09:34 +05:30
class SomeAPI < Grape::API::Instance
params do
optional :values, type: Array[String]
2021-03-11 19:13:27 +05:30
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
2020-07-28 23:09:34 +05:30
end
end
2021-03-11 19:13:27 +05:30
TYPE
2020-07-28 23:09:34 +05:30
end
it 'does not add an offense' do
2021-03-11 19:13:27 +05:30
expect_no_offenses(<<~CODE)
2020-07-28 23:09:34 +05:30
class SomeAPI < Grape::API::Instance
params do
requires :values, type: Array[String], coerce_with: ->(val) { val.split(',').map(&:strip) }
requires :milestone, type: String, desc: 'Milestone title'
optional :assignee_id, types: [Integer, String], integer_none_any: true,
desc: 'Return issues which are assigned to the user with the given ID'
end
end
CODE
end
it 'does not add an offense for unrelated classes' do
2021-03-11 19:13:27 +05:30
expect_no_offenses(<<~CODE)
2020-07-28 23:09:34 +05:30
class SomeClass
params do
requires :values, type: Array[String]
end
end
CODE
end
end