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

line_break_after_guard_clauses_spec.rb « cop « rubocop « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8899dc8538455cdd2daa557cff36f30b5d1e868d (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
require 'spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../rubocop/cop/line_break_after_guard_clauses'

describe RuboCop::Cop::LineBreakAfterGuardClauses do
  include CopHelper

  subject(:cop) { described_class.new }

  shared_examples 'examples with guard clause' do |title|
    %w[if unless].each do |conditional|
      it "flags violation for #{title} #{conditional} without line breaks" do
        source = <<~RUBY
          #{title} #{conditional} condition
          do_stuff
        RUBY
        inspect_source(cop, source)

        expect(cop.offenses.size).to eq(1)
        offense = cop.offenses.first

        expect(offense.line).to eq(1)
        expect(cop.highlights).to eq(["#{title} #{conditional} condition"])
        expect(offense.message).to eq('Add a line break after guard clauses')
      end

      it "doesn't flag violation for #{title} #{conditional} with line break" do
        source = <<~RUBY
          #{title} #{conditional} condition

          do_stuff
        RUBY
        inspect_source(cop, source)

        expect(cop.offenses).to be_empty
      end

      it "doesn't flag violation for #{title} #{conditional} on multiple lines without line break" do
        source = <<~RUBY
          #{conditional} condition
            #{title}
          end
          do_stuff
        RUBY
        inspect_source(cop, source)

        expect(cop.offenses).to be_empty
      end

      it "doesn't flag violation for #{title} #{conditional} without line breaks when followed by end keyword" do
        source = <<~RUBY
          def test
            #{title} #{conditional} condition
          end
        RUBY
        inspect_source(cop, source)

        expect(cop.offenses).to be_empty
      end

      it "doesn't flag violation for #{title} #{conditional} without line breaks when followed by elsif keyword" do
        source = <<~RUBY
          if model
            #{title} #{conditional} condition
          elsif
            do_something
          end
        RUBY
        inspect_source(cop, source)

        expect(cop.offenses).to be_empty
      end

      it "doesn't flag violation for #{title} #{conditional} without line breaks when followed by else keyword" do
        source = <<~RUBY
          if model
            #{title} #{conditional} condition
          else
            do_something
          end
        RUBY
        inspect_source(cop, source)

        expect(cop.offenses).to be_empty
      end

      it "doesn't flag violation for #{title} #{conditional} without line breaks when followed by when keyword" do
        source = <<~RUBY
          case model
            when condition_a
              #{title} #{conditional} condition
            when condition_b
              do_something
            end
        RUBY
        inspect_source(cop, source)

        expect(cop.offenses).to be_empty
      end

      it "doesn't flag violation for #{title} #{conditional} without line breaks when followed by rescue keyword" do
        source = <<~RUBY
          begin
            #{title} #{conditional} condition
          rescue StandardError
            do_something
          end
        RUBY
        inspect_source(cop, source)

        expect(cop.offenses).to be_empty
      end

      it "doesn't flag violation for #{title} #{conditional} without line breaks when followed by ensure keyword" do
        source = <<~RUBY
          def foo
            #{title} #{conditional} condition
          ensure
            do_something
          end
        RUBY
        inspect_source(cop, source)

        expect(cop.offenses).to be_empty
      end

      it "doesn't flag violation for #{title} #{conditional} without line breaks when followed by another guard clause" do
        source = <<~RUBY
          #{title} #{conditional} condition
          #{title} #{conditional} condition

          do_stuff
        RUBY
        inspect_source(cop, source)

        expect(cop.offenses).to be_empty
      end

      it "autocorrects #{title} #{conditional} guard clauses without line break" do
        source = <<~RUBY
          #{title} #{conditional} condition
          do_stuff
        RUBY
        autocorrected = autocorrect_source(cop, source)

        expected_source = <<~RUBY
          #{title} #{conditional} condition

          do_stuff
        RUBY
        expect(autocorrected).to eql(expected_source)
      end
    end
  end

  %w[return fail raise next break throw].each do |example|
    it_behaves_like 'examples with guard clause', example
  end
end