diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-11-21 18:12:22 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-11-21 18:12:22 +0300 |
commit | 5dc70663c4ff1feb215428ce50673b5b646f9809 (patch) | |
tree | 944bde8a8350ac8ee64335cc5b98d1c60ba9c3c5 /spec/rubocop | |
parent | fbc1f4ffc29ae99f438b07872c3c00fbe7651caa (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/rubocop')
-rw-r--r-- | spec/rubocop/cop/gitlab/strong_memoize_attr_spec.rb | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/spec/rubocop/cop/gitlab/strong_memoize_attr_spec.rb b/spec/rubocop/cop/gitlab/strong_memoize_attr_spec.rb new file mode 100644 index 00000000000..20dd0f89f53 --- /dev/null +++ b/spec/rubocop/cop/gitlab/strong_memoize_attr_spec.rb @@ -0,0 +1,72 @@ +# 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 + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `strong_memoize_attr`, instead of using `strong_memoize` directly + '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 + ^^^^^^^^^^^^^^^^^^^^^^^^ Use `strong_memoize_attr`, instead of using `strong_memoize` directly + true + end + end + end + RUBY + + expect_correction(<<~RUBY) + class Foo + def enabled? + true + end + strong_memoize_attr :enabled?, :enabled + end + RUBY + end + end + end + + context 'when strong_memoize() is not the entire body of the method' do + it 'does not register an offense or autocorrect' do + expect_no_offenses(<<~RUBY) + class Foo + def memoized_method + msg = 'This is a memoized method' + + strong_memoize(:memoized_method) do + msg + end + end + end + RUBY + end + end +end |