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

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

90 lines
2 KiB
Ruby
Raw Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
2022-10-11 01:57:18 +05:30
require 'rubocop_spec_helper'
2018-03-17 18:26:18 +05:30
require_relative '../../../../rubocop/cop/gitlab/predicate_memoization'
2021-03-08 18:12:59 +05:30
RSpec.describe RuboCop::Cop::Gitlab::PredicateMemoization do
2018-03-17 18:26:18 +05:30
shared_examples('not registering offense') do
it 'does not register offenses' do
2021-03-11 19:13:27 +05:30
expect_no_offenses(source)
2018-03-17 18:26:18 +05:30
end
end
2021-03-11 19:13:27 +05:30
context 'when source is a predicate method using ivar with assignment' do
it_behaves_like 'not registering offense' do
2018-03-17 18:26:18 +05:30
let(:source) do
<<~RUBY
class C
def really?
2021-03-11 19:13:27 +05:30
@really = true
2018-03-17 18:26:18 +05:30
end
end
RUBY
end
end
2021-03-11 19:13:27 +05:30
end
2018-03-17 18:26:18 +05:30
2021-03-11 19:13:27 +05:30
context 'when source is a predicate method using local with ||=' do
it_behaves_like 'not registering offense' do
2018-03-17 18:26:18 +05:30
let(:source) do
<<~RUBY
class C
def really?
2021-03-11 19:13:27 +05:30
really ||= true
2018-03-17 18:26:18 +05:30
end
end
RUBY
end
end
end
2021-03-11 19:13:27 +05:30
context 'when source is a regular method memoizing via ivar' do
2018-03-17 18:26:18 +05:30
it_behaves_like 'not registering offense' do
let(:source) do
<<~RUBY
class C
2021-03-11 19:13:27 +05:30
def really
@really ||= true
2018-03-17 18:26:18 +05:30
end
end
RUBY
end
end
end
2021-03-11 19:13:27 +05:30
context 'when source is a predicate method memoizing via ivar' do
let(:msg) { "Avoid using `@value ||= query` [...]" }
context 'when assigning to boolean' do
it 'registers an offense' do
node = "@really ||= true"
expect_offense(<<~CODE, node: node, msg: msg)
2018-03-17 18:26:18 +05:30
class C
def really?
2021-03-11 19:13:27 +05:30
%{node}
^{node} %{msg}
2018-03-17 18:26:18 +05:30
end
end
2021-03-11 19:13:27 +05:30
CODE
2018-03-17 18:26:18 +05:30
end
end
2021-03-11 19:13:27 +05:30
context 'when assigning to another variable that is a boolean' do
it 'registers an offense' do
node = "@really ||= value"
expect_offense(<<~CODE, node: node, msg: msg)
2018-03-17 18:26:18 +05:30
class C
2021-03-11 19:13:27 +05:30
def really?
value = true
%{node}
^{node} %{msg}
2018-03-17 18:26:18 +05:30
end
end
2021-03-11 19:13:27 +05:30
CODE
2018-03-17 18:26:18 +05:30
end
end
end
end