Welcome to mirror list, hosted at ThFree Co, Russian Federation.

strong_memoize_attr_spec.rb « gitlab « cop « rubocop « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fde53f8f98c4bfadf945d9d18a73cb6250c287b0 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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?
          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
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `strong_memoize_attr`, instead of using `strong_memoize` directly.
              msg
            end
          end
        end
      RUBY

      expect_no_corrections
    end
  end

  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
end