summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/gitlab/json_spec.rb
blob: d64f60c8583be63100fefc141e6bb479a709c092 (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
# frozen_string_literal: true

require 'spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../../rubocop/cop/gitlab/json'

describe RuboCop::Cop::Gitlab::Json do
  include CopHelper

  subject(:cop) { described_class.new }

  shared_examples('registering call offense') do |options|
    let(:offending_lines) { options[:offending_lines] }

    it 'registers an offense when the class calls JSON' do
      inspect_source(source)

      aggregate_failures do
        expect(cop.offenses.size).to eq(offending_lines.size)
        expect(cop.offenses.map(&:line)).to eq(offending_lines)
      end
    end
  end

  context 'when JSON is called' do
    it_behaves_like 'registering call offense', offending_lines: [3] do
      let(:source) do
        <<~RUBY
          class Foo
            def bar
              JSON.parse('{ "foo": "bar" }')
            end
          end
        RUBY
      end
    end
  end
end