debian-mirror-gitlab/spec/rubocop/cop/gitlab/json_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

70 lines
1.7 KiB
Ruby
Raw Permalink Normal View History

2020-05-24 23:13:21 +05:30
# frozen_string_literal: true
2022-10-11 01:57:18 +05:30
require 'rubocop_spec_helper'
2020-05-24 23:13:21 +05:30
require_relative '../../../../rubocop/cop/gitlab/json'
2021-03-08 18:12:59 +05:30
RSpec.describe RuboCop::Cop::Gitlab::Json do
2021-10-27 15:23:28 +05:30
context 'when ::JSON is called' do
2023-01-13 00:05:48 +05:30
it 'registers an offense and autocorrects' do
2021-03-11 19:13:27 +05:30
expect_offense(<<~RUBY)
class Foo
def bar
JSON.parse('{ "foo": "bar" }')
2023-01-13 00:05:48 +05:30
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Gitlab::Json` over calling `JSON` directly. [...]
end
end
RUBY
expect_correction(<<~RUBY)
class Foo
def bar
Gitlab::Json.parse('{ "foo": "bar" }')
end
end
RUBY
end
end
context 'when ::JSON is called in EE' do
it 'registers an offense and autocorrects' do
expect_offense(<<~RUBY, '/path/to/ee/foo.rb')
class Foo
def bar
JSON.parse('{ "foo": "bar" }')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Gitlab::Json` over calling `JSON` directly. [...]
end
end
RUBY
expect_correction(<<~RUBY)
class Foo
def bar
::Gitlab::Json.parse('{ "foo": "bar" }')
2020-05-24 23:13:21 +05:30
end
2021-03-11 19:13:27 +05:30
end
RUBY
2020-05-24 23:13:21 +05:30
end
end
2021-10-27 15:23:28 +05:30
context 'when ActiveSupport::JSON is called' do
2023-01-13 00:05:48 +05:30
it 'registers an offense and autocorrects' do
2021-10-27 15:23:28 +05:30
expect_offense(<<~RUBY)
class Foo
def bar
ActiveSupport::JSON.parse('{ "foo": "bar" }')
2023-01-13 00:05:48 +05:30
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Gitlab::Json` over calling `JSON` directly. [...]
end
end
RUBY
expect_correction(<<~RUBY)
class Foo
def bar
Gitlab::Json.parse('{ "foo": "bar" }')
2021-10-27 15:23:28 +05:30
end
end
RUBY
end
end
2020-05-24 23:13:21 +05:30
end