/
nested_groups_spec.rb
95 lines (85 loc) · 2.37 KB
/
nested_groups_spec.rb
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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::RSpec::NestedGroups, :config do
it 'flags nested contexts' do
expect_offense(<<-RUBY)
describe MyClass do
context 'when foo' do
context 'when bar' do
context 'when baz' do
^^^^^^^^^^^^^^^^^^ Maximum example group nesting exceeded [4/3].
end
end
end
context 'when qux' do
context 'when norf' do
end
end
end
RUBY
end
it 'support --auto-gen-config' do
inspect_source(<<-RUBY, 'spec/foo_spec.rb')
describe MyClass do
context 'when foo' do
context 'when bar' do
context 'when baz' do
end
end
end
end
RUBY
expect(cop.config_to_allow_offenses[:exclude_limit]).to eq('Max' => 4)
end
it 'ignores non-spec context methods' do
expect_no_offenses(<<-RUBY)
class MyThingy
context 'this is not rspec' do
context 'but it uses contexts' do
end
end
end
RUBY
end
context 'when Max is configured as 2' do
let(:cop_config) { { 'Max' => '2' } }
it 'flags two levels of nesting' do
expect_offense(<<-RUBY)
describe MyClass do
context 'when foo' do
context 'when bar' do
^^^^^^^^^^^^^^^^^^ Maximum example group nesting exceeded [3/2].
context 'when baz' do
^^^^^^^^^^^^^^^^^^ Maximum example group nesting exceeded [4/2].
end
end
end
end
RUBY
end
end
context 'when configured with MaxNesting' do
let(:cop_config) { { 'MaxNesting' => '1' } }
it 'emits a deprecation warning' do
expect { inspect_source('describe(Foo) { }', 'foo_spec.rb') }
.to output(
'Configuration key `MaxNesting` for RSpec/NestedGroups is ' \
"deprecated in favor of `Max`. Please use that instead.\n"
).to_stderr
end
end
it 'counts nesting correctly when non-spec nesting' do
expect_offense(<<-RUBY)
describe MyClass do
context 'when foo' do
context 'when bar' do
[].each do |i|
context 'when baz' do
^^^^^^^^^^^^^^^^^^ Maximum example group nesting exceeded [4/3].
end
end
end
end
end
RUBY
end
end