2023-03-04 22:38:38 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rubocop_spec_helper'
|
|
|
|
require_relative '../../../../rubocop/cop/gitlab/strong_memoize_attr'
|
|
|
|
|
|
|
|
RSpec.describe RuboCop::Cop::Gitlab::StrongMemoizeAttr do
|
|
|
|
context 'when strong_memoize() is the entire body of a method' do
|
|
|
|
context 'when the memoization name is the same as the method name' do
|
|
|
|
it 'registers an offense and autocorrects' do
|
|
|
|
expect_offense(<<~RUBY)
|
|
|
|
class Foo
|
|
|
|
def memoized_method
|
|
|
|
strong_memoize(:memoized_method) do
|
2023-03-17 16:20:25 +05:30
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `strong_memoize_attr`, instead of using `strong_memoize` directly.
|
2023-03-04 22:38:38 +05:30
|
|
|
'This is a memoized method'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
expect_correction(<<~RUBY)
|
|
|
|
class Foo
|
|
|
|
def memoized_method
|
|
|
|
'This is a memoized method'
|
|
|
|
end
|
|
|
|
strong_memoize_attr :memoized_method
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the memoization name is different from the method name' do
|
|
|
|
it 'registers an offense and autocorrects' do
|
|
|
|
expect_offense(<<~RUBY)
|
|
|
|
class Foo
|
|
|
|
def enabled?
|
|
|
|
strong_memoize(:enabled) do
|
2023-03-17 16:20:25 +05:30
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^ Use `strong_memoize_attr`, instead of using `strong_memoize` directly.
|
2023-03-04 22:38:38 +05:30
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
expect_correction(<<~RUBY)
|
|
|
|
class Foo
|
|
|
|
def enabled?
|
|
|
|
true
|
|
|
|
end
|
2023-03-17 16:20:25 +05:30
|
|
|
strong_memoize_attr :enabled?
|
2023-03-04 22:38:38 +05:30
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when strong_memoize() is not the entire body of the method' do
|
|
|
|
it 'registers an offense and does not autocorrect' do
|
|
|
|
expect_offense(<<~RUBY)
|
|
|
|
class Foo
|
|
|
|
def memoized_method
|
|
|
|
msg = 'This is a memoized method'
|
|
|
|
|
|
|
|
strong_memoize(:memoized_method) do
|
2023-03-17 16:20:25 +05:30
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `strong_memoize_attr`, instead of using `strong_memoize` directly.
|
2023-03-04 22:38:38 +05:30
|
|
|
msg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
expect_no_corrections
|
|
|
|
end
|
|
|
|
end
|
2023-03-17 16:20:25 +05:30
|
|
|
|
|
|
|
context 'when strong_memoize() is used in a method with parameters' do
|
|
|
|
it 'does not register an offense' do
|
|
|
|
expect_no_offenses(<<~RUBY)
|
|
|
|
class Foo
|
|
|
|
def memoized_method(param)
|
|
|
|
strong_memoize(:memoized_method) do
|
|
|
|
param.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when strong_memoize() is used in a singleton method' do
|
|
|
|
it 'does not register an offense' do
|
|
|
|
expect_no_offenses(<<~RUBY)
|
|
|
|
class Foo
|
|
|
|
def self.memoized_method
|
|
|
|
strong_memoize(:memoized_method) do
|
|
|
|
'this is a memoized method'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
2023-03-04 22:38:38 +05:30
|
|
|
end
|