rubocop-rspec 1.5.2 → 1.5.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,32 +2,31 @@ describe RuboCop::Cop::RSpec::DescribeMethod do
2
2
  subject(:cop) { described_class.new }
3
3
 
4
4
  it 'ignores describes with only a class' do
5
- inspect_source(cop, 'describe Some::Class do; end')
6
- expect(cop.offenses.empty?).to be(true)
5
+ expect_no_violations('describe Some::Class do; end')
7
6
  end
8
7
 
9
8
  it 'enforces non-method names' do
10
- inspect_source(
11
- cop,
12
- "describe Some::Class, 'nope', '.incorrect_usage' do; end"
13
- )
14
-
15
- expect(cop.offenses.size).to eq(1)
16
- expect(cop.offenses.map(&:line).sort).to eq([1])
17
- expect(cop.messages)
18
- .to eq(['The second argument to describe should be the method being ' \
19
- "tested. '#instance' or '.class'"])
9
+ expect_violation(<<-RUBY)
10
+ describe Some::Class, 'nope', '.incorrect_usage' do
11
+ ^^^^^^ The second argument to describe should be the method being tested. '#instance' or '.class'
12
+ end
13
+ RUBY
20
14
  end
21
15
 
22
16
  it 'skips methods starting with a . or #' do
23
- inspect_source(cop, ["describe Some::Class, '.asdf' do; end",
24
- "describe Some::Class, '#fdsa' do; end"])
25
- expect(cop.offenses).to be_empty
17
+ expect_no_violations(<<-RUBY)
18
+ describe Some::Class, '.asdf' do
19
+ end
20
+
21
+ describe Some::Class, '#fdsa' do
22
+ end
23
+ RUBY
26
24
  end
27
25
 
28
26
  it 'skips specs not having a string second argument' do
29
- inspect_source(cop, 'describe Some::Class, :config do; end')
30
-
31
- expect(cop.offenses).to be_empty
27
+ expect_no_violations(<<-RUBY)
28
+ describe Some::Class, :config do
29
+ end
30
+ RUBY
32
31
  end
33
32
  end
@@ -2,176 +2,126 @@ describe RuboCop::Cop::RSpec::DescribedClass do
2
2
  subject(:cop) { described_class.new }
3
3
 
4
4
  it 'checks for the use of the described class' do
5
- inspect_source(
6
- cop,
7
- [
8
- 'describe MyClass do',
9
- ' include MyClass',
10
- ' subject { MyClass.do_something }',
11
- ' before { MyClass.do_something }',
12
- 'end'
13
- ]
14
- )
15
- expect(cop.offenses.size).to eq(3)
16
- expect(cop.offenses.map(&:line).sort).to eq([2, 3, 4])
17
- expect(cop.messages)
18
- .to eq(['Use `described_class` instead of `MyClass`'] * 3)
19
- expect(cop.highlights).to eq(['MyClass'] * 3)
5
+ expect_violation(<<-RUBY)
6
+ describe MyClass do
7
+ include MyClass
8
+ ^^^^^^^ Use `described_class` instead of `MyClass`
9
+
10
+ subject { MyClass.do_something }
11
+ ^^^^^^^ Use `described_class` instead of `MyClass`
12
+
13
+ before { MyClass.do_something }
14
+ ^^^^^^^ Use `described_class` instead of `MyClass`
15
+ end
16
+ RUBY
20
17
  end
21
18
 
22
19
  it 'ignores described class as string' do
23
- inspect_source(
24
- cop,
25
- [
26
- 'describe MyClass do',
27
- ' subject { "MyClass" }',
28
- 'end'
29
- ]
30
- )
31
- expect(cop.offenses).to be_empty
20
+ expect_no_violations(<<-RUBY)
21
+ describe MyClass do
22
+ subject { "MyClass" }
23
+ end
24
+ RUBY
32
25
  end
33
26
 
34
27
  it 'ignores describe that do not referece to a class' do
35
- inspect_source(
36
- cop,
37
- [
38
- 'describe "MyClass" do',
39
- ' subject { "MyClass" }',
40
- 'end'
41
- ]
42
- )
43
- expect(cop.offenses).to be_empty
28
+ expect_no_violations(<<-RUBY)
29
+ describe "MyClass" do
30
+ subject { "MyClass" }
31
+ end
32
+ RUBY
44
33
  end
45
34
 
46
35
  it 'ignores class if the scope is changing' do
47
- inspect_source(
48
- cop,
49
- [
50
- 'describe MyClass do',
51
- ' def method',
52
- ' include MyClass',
53
- ' end',
54
- ' class OtherClass',
55
- ' include MyClass',
56
- ' end',
57
- ' module MyModle',
58
- ' include MyClass',
59
- ' end',
60
- 'end'
61
- ]
62
- )
63
- expect(cop.offenses).to be_empty
36
+ expect_no_violations(<<-RUBY)
37
+ describe MyClass do
38
+ def method
39
+ include MyClass
40
+ end
41
+
42
+ class OtherClass
43
+ include MyClass
44
+ end
45
+
46
+ module MyModle
47
+ include MyClass
48
+ end
49
+ end
50
+ RUBY
64
51
  end
65
52
 
66
53
  it 'only takes class from top level describes' do
67
- inspect_source(
68
- cop,
69
- [
70
- 'describe MyClass do',
71
- ' describe MyClass::Foo do',
72
- ' subject { MyClass::Foo }',
73
- ' let(:foo) { MyClass }',
74
- ' end',
75
- 'end'
76
- ]
77
- )
78
- expect(cop.offenses.size).to eq(1)
79
- expect(cop.offenses.map(&:line).sort).to eq([4])
80
- expect(cop.messages)
81
- .to eq(['Use `described_class` instead of `MyClass`'])
82
- expect(cop.highlights).to eq(['MyClass'])
54
+ expect_violation(<<-RUBY)
55
+ describe MyClass do
56
+ describe MyClass::Foo do
57
+ subject { MyClass::Foo }
58
+
59
+ let(:foo) { MyClass }
60
+ ^^^^^^^ Use `described_class` instead of `MyClass`
61
+ end
62
+ end
63
+ RUBY
83
64
  end
84
65
 
85
66
  it 'ignores subclasses' do
86
- inspect_source(
87
- cop,
88
- [
89
- 'describe MyClass do',
90
- ' subject { MyClass::SubClass }',
91
- 'end'
92
- ]
93
- )
94
- expect(cop.offenses).to be_empty
67
+ expect_no_violations(<<-RUBY)
68
+ describe MyClass do
69
+ subject { MyClass::SubClass }
70
+ end
71
+ RUBY
95
72
  end
96
73
 
97
74
  it 'ignores if namespace is not matching' do
98
- inspect_source(
99
- cop,
100
- [
101
- 'describe MyNamespace::MyClass do',
102
- ' subject { ::MyClass }',
103
- ' let(:foo) { MyClass }',
104
- 'end'
105
- ]
106
- )
107
- expect(cop.offenses).to be_empty
75
+ expect_no_violations(<<-RUBY)
76
+ describe MyNamespace::MyClass do
77
+ subject { ::MyClass }
78
+ let(:foo) { MyClass }
79
+ end
80
+ RUBY
108
81
  end
109
82
 
110
83
  it 'checks for the use of described class with namespace' do
111
- inspect_source(
112
- cop,
113
- [
114
- 'describe MyNamespace::MyClass do',
115
- ' subject { MyNamespace::MyClass }',
116
- 'end'
117
- ]
118
- )
119
- expect(cop.offenses.size).to eq(1)
120
- expect(cop.offenses.map(&:line).sort).to eq([2])
121
- expect(cop.messages)
122
- .to eq(['Use `described_class` instead of `MyNamespace::MyClass`'])
123
- expect(cop.highlights).to eq(['MyNamespace::MyClass'])
84
+ expect_violation(<<-RUBY)
85
+ describe MyNamespace::MyClass do
86
+ subject { MyNamespace::MyClass }
87
+ ^^^^^^^^^^^^^^^^^^^^ Use `described_class` instead of `MyNamespace::MyClass`
88
+ end
89
+ RUBY
124
90
  end
125
91
 
126
92
  it 'does not flag violations within a scope change' do
127
- inspect_source(
128
- cop,
129
- [
130
- 'describe MyNamespace::MyClass do',
131
- ' before do',
132
- ' class Foo',
133
- ' thing = MyNamespace::MyClass.new',
134
- ' end',
135
- ' end',
136
- 'end'
137
- ]
138
- )
139
-
140
- expect(cop.offenses).to be_empty
93
+ expect_no_violations(<<-RUBY)
94
+ describe MyNamespace::MyClass do
95
+ before do
96
+ class Foo
97
+ thing = MyNamespace::MyClass.new
98
+ end
99
+ end
100
+ end
101
+ RUBY
141
102
  end
142
103
 
143
104
  it 'does not flag violations within a scope change' do
144
- inspect_source(
145
- cop,
146
- [
147
- 'describe do',
148
- ' before do',
149
- ' MyNamespace::MyClass.new',
150
- ' end',
151
- 'end'
152
- ]
153
- )
154
-
155
- expect(cop.offenses).to be_empty
105
+ expect_no_violations(<<-RUBY)
106
+ describe do
107
+ before do
108
+ MyNamespace::MyClass.new
109
+ end
110
+ end
111
+ RUBY
156
112
  end
157
113
 
158
114
  it 'checks for the use of described class with module' do
159
115
  skip
160
- inspect_source(
161
- lcop,
162
- [
163
- 'module MyNamespace',
164
- ' describe MyClass do',
165
- ' subject { MyNamespace::MyClass }',
166
- ' end',
167
- 'end'
168
- ]
169
- )
170
- expect(cop.offenses.size).to eq(1)
171
- expect(cop.offenses.map(&:line).sort).to eq([2])
172
- expect(cop.messages)
173
- .to eq(['Use `described_class` instead of `MyNamespace::MyClass`'])
174
- expect(cop.highlights).to eq(['MyNamespace::MyClass'])
116
+
117
+ expect_violation(<<-RUBY)
118
+ module MyNamespace
119
+ describe MyClass do
120
+ subject { MyNamespace::MyClass }
121
+ ^^^^^^^^^^^^^^^^^^^^ Use `described_class` instead of `MyNamespace::MyClass`
122
+ end
123
+ end
124
+ RUBY
175
125
  end
176
126
 
177
127
  it 'autocorrects an offenses' do
@@ -10,47 +10,38 @@ describe RuboCop::Cop::RSpec::ExampleWording, :config do
10
10
  end
11
11
 
12
12
  it 'ignores non-example blocks' do
13
- inspect_source(cop, 'foo "should do something" do; end')
14
- expect(cop.offenses).to be_empty
13
+ expect_no_violations('foo "should do something" do; end')
15
14
  end
16
15
 
17
16
  it 'finds description with `should` at the beginning' do
18
- inspect_source(cop, ["it 'should do something' do", 'end'])
19
- expect(cop.offenses.size).to eq(1)
20
- expect(cop.offenses.map(&:line).sort).to eq([1])
21
- expect(cop.messages)
22
- .to eq(['Do not use should when describing your tests.'])
23
- expect(cop.highlights).to eq(['should do something'])
17
+ expect_violation(<<-RUBY)
18
+ it 'should do something' do
19
+ ^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
20
+ end
21
+ RUBY
24
22
  end
25
23
 
26
24
  it 'finds description with `Should` at the beginning' do
27
- inspect_source(cop, ["it 'Should do something' do", 'end'])
28
- expect(cop.offenses.size).to eq(1)
29
- expect(cop.offenses.map(&:line).sort).to eq([1])
30
- expect(cop.messages)
31
- .to eq(['Do not use should when describing your tests.'])
32
- expect(cop.highlights).to eq(['Should do something'])
25
+ expect_violation(<<-RUBY)
26
+ it 'Should do something' do
27
+ ^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
28
+ end
29
+ RUBY
33
30
  end
34
31
 
35
32
  it 'finds description with `shouldn\'t` at the beginning' do
36
- inspect_source(cop, ['it "shouldn\'t do something" do', 'end'])
37
- expect(cop.offenses.size).to eq(1)
38
- expect(cop.offenses.map(&:line).sort).to eq([1])
39
- expect(cop.messages)
40
- .to eq(['Do not use should when describing your tests.'])
41
- expect(cop.highlights).to eq(['shouldn\'t do something'])
33
+ expect_violation(<<-RUBY)
34
+ it "shouldn't do something" do
35
+ ^^^^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
36
+ end
37
+ RUBY
42
38
  end
43
39
 
44
40
  it 'skips descriptions without `should` at the beginning' do
45
- inspect_source(
46
- cop,
47
- [
48
- "it 'finds no should ' \\",
49
- " 'here' do",
50
- 'end'
51
- ]
52
- )
53
- expect(cop.offenses).to be_empty
41
+ expect_no_violations(<<-RUBY)
42
+ it 'finds no should here' do
43
+ end
44
+ RUBY
54
45
  end
55
46
 
56
47
  it 'corrects `it "should only have"` to it "only has"' do
@@ -1,78 +1,130 @@
1
1
  describe RuboCop::Cop::RSpec::Focus do
2
2
  subject(:cop) { described_class.new }
3
3
 
4
- [
5
- :example_group, :describe, :context, :xdescribe, :xcontext,
6
- :it, :example, :specify, :xit, :xexample, :xspecify,
7
- :feature, :scenario, :xfeature, :xscenario
8
- ].each do |block_type|
9
- it "finds `#{block_type}` blocks with `focus: true`" do
10
- inspect_source(
11
- cop,
12
- [
13
- "#{block_type} 'test', meta: true, focus: true do",
14
- 'end'
15
- ]
16
- )
17
- expect(cop.offenses.size).to eq(1)
18
- expect(cop.offenses.map(&:line).sort).to eq([1])
19
- expect(cop.messages).to eq(['Focused spec found.'])
20
- expect(cop.highlights).to eq(['focus: true'])
21
- end
4
+ # rubocop:disable RSpec/ExampleLength
5
+ it 'flags all rspec example blocks with that include `focus: true`' do
6
+ expect_violation(<<-RUBY)
7
+ example 'test', meta: true, focus: true do; end
8
+ ^^^^^^^^^^^ Focused spec found.
9
+ xit 'test', meta: true, focus: true do; end
10
+ ^^^^^^^^^^^ Focused spec found.
11
+ describe 'test', meta: true, focus: true do; end
12
+ ^^^^^^^^^^^ Focused spec found.
13
+ it 'test', meta: true, focus: true do; end
14
+ ^^^^^^^^^^^ Focused spec found.
15
+ xspecify 'test', meta: true, focus: true do; end
16
+ ^^^^^^^^^^^ Focused spec found.
17
+ specify 'test', meta: true, focus: true do; end
18
+ ^^^^^^^^^^^ Focused spec found.
19
+ example_group 'test', meta: true, focus: true do; end
20
+ ^^^^^^^^^^^ Focused spec found.
21
+ scenario 'test', meta: true, focus: true do; end
22
+ ^^^^^^^^^^^ Focused spec found.
23
+ xexample 'test', meta: true, focus: true do; end
24
+ ^^^^^^^^^^^ Focused spec found.
25
+ xdescribe 'test', meta: true, focus: true do; end
26
+ ^^^^^^^^^^^ Focused spec found.
27
+ context 'test', meta: true, focus: true do; end
28
+ ^^^^^^^^^^^ Focused spec found.
29
+ xcontext 'test', meta: true, focus: true do; end
30
+ ^^^^^^^^^^^ Focused spec found.
31
+ feature 'test', meta: true, focus: true do; end
32
+ ^^^^^^^^^^^ Focused spec found.
33
+ xfeature 'test', meta: true, focus: true do; end
34
+ ^^^^^^^^^^^ Focused spec found.
35
+ xscenario 'test', meta: true, focus: true do; end
36
+ ^^^^^^^^^^^ Focused spec found.
37
+ RUBY
38
+ end
22
39
 
23
- it "finds `#{block_type}` blocks with `:focus`" do
24
- inspect_source(
25
- cop,
26
- [
27
- "#{block_type} 'test', :focus do",
28
- 'end'
29
- ]
30
- )
31
- expect(cop.offenses.size).to eq(1)
32
- expect(cop.offenses.map(&:line).sort).to eq([1])
33
- expect(cop.messages).to eq(['Focused spec found.'])
34
- expect(cop.highlights).to eq([':focus'])
35
- end
40
+ it 'flags all repec example blocks that include `:focus`' do
41
+ expect_violation(<<-RUBY)
42
+ example_group 'test', :focus do; end
43
+ ^^^^^^ Focused spec found.
44
+ feature 'test', :focus do; end
45
+ ^^^^^^ Focused spec found.
46
+ xexample 'test', :focus do; end
47
+ ^^^^^^ Focused spec found.
48
+ xdescribe 'test', :focus do; end
49
+ ^^^^^^ Focused spec found.
50
+ xscenario 'test', :focus do; end
51
+ ^^^^^^ Focused spec found.
52
+ specify 'test', :focus do; end
53
+ ^^^^^^ Focused spec found.
54
+ example 'test', :focus do; end
55
+ ^^^^^^ Focused spec found.
56
+ xfeature 'test', :focus do; end
57
+ ^^^^^^ Focused spec found.
58
+ xspecify 'test', :focus do; end
59
+ ^^^^^^ Focused spec found.
60
+ scenario 'test', :focus do; end
61
+ ^^^^^^ Focused spec found.
62
+ describe 'test', :focus do; end
63
+ ^^^^^^ Focused spec found.
64
+ xit 'test', :focus do; end
65
+ ^^^^^^ Focused spec found.
66
+ context 'test', :focus do; end
67
+ ^^^^^^ Focused spec found.
68
+ xcontext 'test', :focus do; end
69
+ ^^^^^^ Focused spec found.
70
+ it 'test', :focus do; end
71
+ ^^^^^^ Focused spec found.
72
+ RUBY
73
+ end
36
74
 
37
- it 'detects no offense when spec is not focused' do
38
- inspect_source(
39
- cop,
40
- [
41
- "#{block_type} 'test' do",
42
- 'end'
43
- ]
44
- )
45
- expect(subject.messages).to be_empty
46
- end
75
+ it 'does not flag unfocused specs' do
76
+ expect_no_violations(<<-RUBY)
77
+ xcontext 'test' do; end
78
+ xscenario 'test' do; end
79
+ xspecify 'test' do; end
80
+ describe 'test' do; end
81
+ example 'test' do; end
82
+ xexample 'test' do; end
83
+ scenario 'test' do; end
84
+ specify 'test' do; end
85
+ xit 'test' do; end
86
+ feature 'test' do; end
87
+ xfeature 'test' do; end
88
+ context 'test' do; end
89
+ it 'test' do; end
90
+ example_group 'test' do; end
91
+ xdescribe 'test' do; end
92
+ RUBY
47
93
  end
48
94
 
49
95
  it 'does not flag a method that is focused twice' do
50
- inspect_source(cop, 'fit "foo", :focus do; end')
51
- expect(cop.offenses.size).to be(1)
96
+ expect_violation(<<-RUBY)
97
+ fit "foo", :focus do
98
+ ^^^^^^^^^^^^^^^^^ Focused spec found.
99
+ end
100
+ RUBY
52
101
  end
53
102
 
54
103
  it 'ignores non-rspec code with :focus blocks' do
55
- inspect_source(cop, 'some_method "foo", focus: true do; end')
56
- expect(cop.offenses).to be_empty
104
+ expect_no_violations(<<-RUBY)
105
+ some_method "foo", focus: true do
106
+ end
107
+ RUBY
57
108
  end
58
109
 
59
- [
60
- :fdescribe, :fcontext,
61
- :focus, :fexample, :fit, :fspecify,
62
- :ffeature, :fscenario
63
- ].each do |block_type|
64
- it "finds `#{block_type}` blocks" do
65
- inspect_source(
66
- cop,
67
- [
68
- "#{block_type} 'test' do",
69
- 'end'
70
- ]
71
- )
72
- expect(cop.offenses.size).to eq(1)
73
- expect(cop.offenses.map(&:line).sort).to eq([1])
74
- expect(cop.messages).to eq(['Focused spec found.'])
75
- expect(cop.highlights).to eq(["#{block_type} 'test'"])
76
- end
110
+ it 'flags focused block types' do
111
+ expect_violation(<<-RUBY)
112
+ fdescribe 'test' do; end
113
+ ^^^^^^^^^^^^^^^^ Focused spec found.
114
+ ffeature 'test' do; end
115
+ ^^^^^^^^^^^^^^^ Focused spec found.
116
+ fcontext 'test' do; end
117
+ ^^^^^^^^^^^^^^^ Focused spec found.
118
+ fit 'test' do; end
119
+ ^^^^^^^^^^ Focused spec found.
120
+ fscenario 'test' do; end
121
+ ^^^^^^^^^^^^^^^^ Focused spec found.
122
+ fexample 'test' do; end
123
+ ^^^^^^^^^^^^^^^ Focused spec found.
124
+ fspecify 'test' do; end
125
+ ^^^^^^^^^^^^^^^ Focused spec found.
126
+ focus 'test' do; end
127
+ ^^^^^^^^^^^^ Focused spec found.
128
+ RUBY
77
129
  end
78
130
  end