Welcome to mirror list, hosted at ThFree Co, Russian Federation.

grape_array_missing_coerce_spec.rb « api « cop « rubocop « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8252e07837dbcc63d2045aa130c196898f80b3f9 (plain)
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
# frozen_string_literal: true

require 'fast_spec_helper'
require 'rubocop'
require_relative '../../../support/helpers/expect_offense'
require_relative '../../../../rubocop/cop/api/grape_array_missing_coerce'

describe RuboCop::Cop::API::GrapeArrayMissingCoerce do
  include CopHelper
  include ExpectOffense

  subject(:cop) { described_class.new }

  it 'adds an offense with a required parameter' do
    inspect_source(<<~CODE.strip_indent)
      class SomeAPI < Grape::API::Instance
        params do
          requires :values, type: Array[String]
        end
      end
    CODE

    expect(cop.offenses.size).to eq(1)
  end

  it 'adds an offense with an optional parameter' do
    inspect_source(<<~CODE.strip_indent)
      class SomeAPI < Grape::API::Instance
        params do
          optional :values, type: Array[String]
        end
      end
    CODE

    expect(cop.offenses.size).to eq(1)
  end

  it 'does not add an offense' do
    inspect_source(<<~CODE.strip_indent)
      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

    expect(cop.offenses.size).to be_zero
  end

  it 'does not add an offense for unrelated classes' do
    inspect_source(<<~CODE.strip_indent)
      class SomeClass
        params do
          requires :values, type: Array[String]
        end
      end
    CODE

    expect(cop.offenses.size).to be_zero
  end
end