rubocop-rspec 1.15.1 → 1.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +20 -1
  3. data/Gemfile +0 -1
  4. data/config/default.yml +105 -0
  5. data/lib/rubocop-rspec.rb +27 -0
  6. data/lib/rubocop/cop/rspec/align_left_let_brace.rb +51 -0
  7. data/lib/rubocop/cop/rspec/align_right_let_brace.rb +51 -0
  8. data/lib/rubocop/cop/rspec/any_instance.rb +1 -1
  9. data/lib/rubocop/cop/rspec/cop.rb +4 -2
  10. data/lib/rubocop/cop/rspec/expect_in_hook.rb +61 -0
  11. data/lib/rubocop/cop/rspec/factory_girl/dynamic_attribute_defined_statically.rb +84 -0
  12. data/lib/rubocop/cop/rspec/hook_argument.rb +11 -5
  13. data/lib/rubocop/cop/rspec/invalid_predicate_matcher.rb +42 -0
  14. data/lib/rubocop/cop/rspec/let_before_examples.rb +66 -0
  15. data/lib/rubocop/cop/rspec/multiple_expectations.rb +3 -5
  16. data/lib/rubocop/cop/rspec/multiple_subjects.rb +80 -0
  17. data/lib/rubocop/cop/rspec/named_subject.rb +5 -6
  18. data/lib/rubocop/cop/rspec/overwriting_setup.rb +1 -1
  19. data/lib/rubocop/cop/rspec/predicate_matcher.rb +337 -0
  20. data/lib/rubocop/cop/rspec/return_from_stub.rb +83 -0
  21. data/lib/rubocop/cop/rspec/void_expect.rb +52 -0
  22. data/lib/rubocop/rspec/align_let_brace.rb +64 -0
  23. data/lib/rubocop/rspec/config_formatter.rb +7 -4
  24. data/lib/rubocop/rspec/description_extractor.rb +2 -2
  25. data/lib/rubocop/rspec/example_group.rb +25 -2
  26. data/lib/rubocop/rspec/factory_girl.rb +7 -0
  27. data/lib/rubocop/rspec/language.rb +6 -1
  28. data/lib/rubocop/rspec/version.rb +1 -1
  29. data/rubocop-rspec.gemspec +1 -4
  30. data/spec/project/default_config_spec.rb +8 -4
  31. data/spec/rubocop/cop/rspec/align_left_let_brace_spec.rb +62 -0
  32. data/spec/rubocop/cop/rspec/align_right_let_brace_spec.rb +62 -0
  33. data/spec/rubocop/cop/rspec/any_instance_spec.rb +3 -3
  34. data/spec/rubocop/cop/rspec/around_block_spec.rb +11 -11
  35. data/spec/rubocop/cop/rspec/be_eql_spec.rb +7 -7
  36. data/spec/rubocop/cop/rspec/before_after_all_spec.rb +4 -4
  37. data/spec/rubocop/cop/rspec/cop_spec.rb +7 -7
  38. data/spec/rubocop/cop/rspec/describe_class_spec.rb +18 -18
  39. data/spec/rubocop/cop/rspec/describe_method_spec.rb +4 -4
  40. data/spec/rubocop/cop/rspec/describe_symbol_spec.rb +6 -6
  41. data/spec/rubocop/cop/rspec/described_class_spec.rb +18 -18
  42. data/spec/rubocop/cop/rspec/empty_example_group_spec.rb +5 -5
  43. data/spec/rubocop/cop/rspec/empty_line_after_final_let_spec.rb +9 -9
  44. data/spec/rubocop/cop/rspec/empty_line_after_subject_spec.rb +5 -5
  45. data/spec/rubocop/cop/rspec/example_length_spec.rb +6 -6
  46. data/spec/rubocop/cop/rspec/example_wording_spec.rb +10 -10
  47. data/spec/rubocop/cop/rspec/expect_actual_spec.rb +10 -10
  48. data/spec/rubocop/cop/rspec/expect_in_hook_spec.rb +79 -0
  49. data/spec/rubocop/cop/rspec/expect_output_spec.rb +7 -7
  50. data/spec/rubocop/cop/rspec/factory_girl/dynamic_attribute_defined_statically_spec.rb +87 -0
  51. data/spec/rubocop/cop/rspec/file_path_spec.rb +29 -29
  52. data/spec/rubocop/cop/rspec/focus_spec.rb +6 -6
  53. data/spec/rubocop/cop/rspec/hook_argument_spec.rb +35 -23
  54. data/spec/rubocop/cop/rspec/implicit_expect_spec.rb +10 -10
  55. data/spec/rubocop/cop/rspec/instance_spy_spec.rb +4 -4
  56. data/spec/rubocop/cop/rspec/instance_variable_spec.rb +7 -7
  57. data/spec/rubocop/cop/rspec/invalid_predicate_matcher_spec.rb +37 -0
  58. data/spec/rubocop/cop/rspec/it_behaves_like_spec.rb +4 -4
  59. data/spec/rubocop/cop/rspec/iterated_expectation_spec.rb +8 -8
  60. data/spec/rubocop/cop/rspec/leading_subject_spec.rb +5 -5
  61. data/spec/rubocop/cop/rspec/let_before_examples_spec.rb +83 -0
  62. data/spec/rubocop/cop/rspec/let_setup_spec.rb +4 -4
  63. data/spec/rubocop/cop/rspec/message_chain_spec.rb +2 -2
  64. data/spec/rubocop/cop/rspec/message_expectation_spec.rb +4 -4
  65. data/spec/rubocop/cop/rspec/message_spies_spec.rb +18 -18
  66. data/spec/rubocop/cop/rspec/multiple_describes_spec.rb +3 -3
  67. data/spec/rubocop/cop/rspec/multiple_expectations_spec.rb +45 -9
  68. data/spec/rubocop/cop/rspec/multiple_subjects_spec.rb +96 -0
  69. data/spec/rubocop/cop/rspec/named_subject_spec.rb +4 -4
  70. data/spec/rubocop/cop/rspec/nested_groups_spec.rb +3 -3
  71. data/spec/rubocop/cop/rspec/not_to_not_spec.rb +4 -4
  72. data/spec/rubocop/cop/rspec/overwriting_setup_spec.rb +4 -4
  73. data/spec/rubocop/cop/rspec/predicate_matcher_spec.rb +335 -0
  74. data/spec/rubocop/cop/rspec/repeated_description_spec.rb +5 -5
  75. data/spec/rubocop/cop/rspec/repeated_example_spec.rb +5 -5
  76. data/spec/rubocop/cop/rspec/return_from_stub_spec.rb +85 -0
  77. data/spec/rubocop/cop/rspec/scattered_let_spec.rb +2 -2
  78. data/spec/rubocop/cop/rspec/scattered_setup_spec.rb +8 -8
  79. data/spec/rubocop/cop/rspec/shared_context_spec.rb +10 -10
  80. data/spec/rubocop/cop/rspec/single_argument_message_chain_spec.rb +10 -10
  81. data/spec/rubocop/cop/rspec/subject_stub_spec.rb +9 -9
  82. data/spec/rubocop/cop/rspec/verified_doubles_spec.rb +7 -7
  83. data/spec/rubocop/cop/rspec/void_expect_spec.rb +47 -0
  84. data/spec/rubocop/rspec/config_formatter_spec.rb +2 -0
  85. data/spec/spec_helper.rb +1 -1
  86. data/spec/support/expect_offense.rb +17 -0
  87. metadata +39 -51
  88. data/spec/expect_violation/expectation_spec.rb +0 -85
  89. data/spec/support/expect_violation.rb +0 -170
@@ -7,7 +7,7 @@ RSpec.describe RuboCop::Cop::RSpec::MultipleExpectations, :config do
7
7
  let(:cop_config) { {} }
8
8
 
9
9
  it 'flags multiple expectations' do
10
- expect_violation(<<-RUBY)
10
+ expect_offense(<<-RUBY)
11
11
  describe Foo do
12
12
  it 'uses expect twice' do
13
13
  ^^^^^^^^^^^^^^^^^^^^^^ Example has too many expectations [2/1].
@@ -19,7 +19,7 @@ RSpec.describe RuboCop::Cop::RSpec::MultipleExpectations, :config do
19
19
  end
20
20
 
21
21
  it 'approves of one expectation per example' do
22
- expect_no_violations(<<-RUBY)
22
+ expect_no_offenses(<<-RUBY)
23
23
  describe Foo do
24
24
  it 'does something neat' do
25
25
  expect(neat).to be(true)
@@ -32,8 +32,44 @@ RSpec.describe RuboCop::Cop::RSpec::MultipleExpectations, :config do
32
32
  RUBY
33
33
  end
34
34
 
35
+ it 'flags multiple expect_any_instance_of' do
36
+ expect_offense(<<-RUBY)
37
+ describe Foo do
38
+ it 'uses expect_any_instance_of twice' do
39
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Example has too many expectations [2/1].
40
+ expect_any_instance_of(Foo).to receive(:bar)
41
+ expect_any_instance_of(Foo).to receive(:baz)
42
+ end
43
+ end
44
+ RUBY
45
+ end
46
+
47
+ it 'flags multiple is_expected' do
48
+ expect_offense(<<-RUBY)
49
+ describe Foo do
50
+ it 'uses expect_any_instance_of twice' do
51
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Example has too many expectations [2/1].
52
+ is_expected.to receive(:bar)
53
+ is_expected.to receive(:baz)
54
+ end
55
+ end
56
+ RUBY
57
+ end
58
+
59
+ it 'flags multiple expects with blocks' do
60
+ expect_offense(<<-RUBY)
61
+ describe Foo do
62
+ it 'uses expect with block twice' do
63
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Example has too many expectations [2/1].
64
+ expect { something }.to change(Foo.count)
65
+ expect { something }.to change(Bar.count)
66
+ end
67
+ end
68
+ RUBY
69
+ end
70
+
35
71
  it 'counts aggregate_failures as one expectation' do
36
- expect_no_violations(<<-RUBY)
72
+ expect_no_offenses(<<-RUBY)
37
73
  describe Foo do
38
74
  it 'aggregates failures' do
39
75
  aggregate_failures do
@@ -46,7 +82,7 @@ RSpec.describe RuboCop::Cop::RSpec::MultipleExpectations, :config do
46
82
  end
47
83
 
48
84
  it 'counts every aggregate_failures as an expectation' do
49
- expect_violation(<<-RUBY)
85
+ expect_offense(<<-RUBY)
50
86
  describe Foo do
51
87
  it 'has multiple aggregate_failures calls' do
52
88
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Example has too many expectations [2/1].
@@ -62,7 +98,7 @@ RSpec.describe RuboCop::Cop::RSpec::MultipleExpectations, :config do
62
98
 
63
99
  context 'with meta data' do
64
100
  it 'ignores examples with `:aggregate_failures`' do
65
- expect_no_violations(<<-RUBY)
101
+ expect_no_offenses(<<-RUBY)
66
102
  describe Foo do
67
103
  it 'uses expect twice', :aggregate_failures do
68
104
  expect(foo).to eq(bar)
@@ -73,7 +109,7 @@ RSpec.describe RuboCop::Cop::RSpec::MultipleExpectations, :config do
73
109
  end
74
110
 
75
111
  it 'ignores examples with `aggregate_failures: true`' do
76
- expect_no_violations(<<-RUBY)
112
+ expect_no_offenses(<<-RUBY)
77
113
  describe Foo do
78
114
  it 'uses expect twice', aggregate_failures: true do
79
115
  expect(foo).to eq(bar)
@@ -84,7 +120,7 @@ RSpec.describe RuboCop::Cop::RSpec::MultipleExpectations, :config do
84
120
  end
85
121
 
86
122
  it 'checks examples with `aggregate_failures: false`' do
87
- expect_violation(<<-RUBY)
123
+ expect_offense(<<-RUBY)
88
124
  describe Foo do
89
125
  it 'uses expect twice', aggregate_failures: false do
90
126
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Example has too many expectations [2/1].
@@ -102,7 +138,7 @@ RSpec.describe RuboCop::Cop::RSpec::MultipleExpectations, :config do
102
138
  end
103
139
 
104
140
  it 'permits two expectations' do
105
- expect_no_violations(<<-RUBY)
141
+ expect_no_offenses(<<-RUBY)
106
142
  describe Foo do
107
143
  it 'uses expect twice' do
108
144
  expect(foo).to eq(bar)
@@ -113,7 +149,7 @@ RSpec.describe RuboCop::Cop::RSpec::MultipleExpectations, :config do
113
149
  end
114
150
 
115
151
  it 'flags three expectations' do
116
- expect_violation(<<-RUBY)
152
+ expect_offense(<<-RUBY)
117
153
  describe Foo do
118
154
  it 'uses expect three times' do
119
155
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Example has too many expectations [3/2].
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe RuboCop::Cop::RSpec::MultipleSubjects do
4
+ let(:cop) { described_class.new }
5
+
6
+ it 'registers an offense for every overwritten subject' do
7
+ expect_offense(<<-RUBY)
8
+ describe 'hello there' do
9
+ subject(:foo) { 1 }
10
+ ^^^^^^^^^^^^^^^^^^^ Do not set more than one subject per example group
11
+ subject(:bar) { 2 }
12
+ ^^^^^^^^^^^^^^^^^^^ Do not set more than one subject per example group
13
+ subject { 3 }
14
+ ^^^^^^^^^^^^^ Do not set more than one subject per example group
15
+ subject(:baz) { 4 }
16
+
17
+ describe 'baz' do
18
+ subject(:norf) { 1 }
19
+ end
20
+ end
21
+ RUBY
22
+ end
23
+
24
+ it 'does not try to autocorrect subject!' do
25
+ source = <<-RUBY
26
+ describe Foo do
27
+ subject! { a }
28
+ subject! { b }
29
+ end
30
+ RUBY
31
+
32
+ expect(autocorrect_source(cop, source, 'example_spec.rb')).to eql(source)
33
+ end
34
+
35
+ it 'does not flag shared example groups' do
36
+ expect_no_offenses(<<-RUBY)
37
+ describe Foo do
38
+ it_behaves_like 'user' do
39
+ subject { described_class.new(user, described_class) }
40
+
41
+ it { expect(subject).not_to be_accessible }
42
+ end
43
+
44
+ it_behaves_like 'admin' do
45
+ subject { described_class.new(user, described_class) }
46
+
47
+ it { expect(subject).to be_accessible }
48
+ end
49
+ end
50
+ RUBY
51
+ end
52
+
53
+ include_examples(
54
+ 'autocorrect',
55
+ <<-RUBY,
56
+ describe 'hello there' do
57
+ subject(:foo) { 1 }
58
+ subject(:bar) { 2 }
59
+ subject(:baz) { 3 }
60
+
61
+ describe 'baz' do
62
+ subject(:norf) { 1 }
63
+ end
64
+ end
65
+ RUBY
66
+ <<-RUBY
67
+ describe 'hello there' do
68
+ let(:foo) { 1 }
69
+ let(:bar) { 2 }
70
+ subject(:baz) { 3 }
71
+
72
+ describe 'baz' do
73
+ subject(:norf) { 1 }
74
+ end
75
+ end
76
+ RUBY
77
+ )
78
+
79
+ include_examples(
80
+ 'autocorrect',
81
+ <<-RUBY.strip_indent.chomp,
82
+ describe 'hello there' do
83
+ subject { 1 }
84
+ subject { 2 }
85
+ subject { 3 }
86
+ end
87
+ RUBY
88
+ [
89
+ "describe 'hello there' do",
90
+ ' ',
91
+ ' ',
92
+ ' subject { 3 }',
93
+ 'end'
94
+ ].join("\n")
95
+ )
96
+ end
@@ -4,7 +4,7 @@ RSpec.describe RuboCop::Cop::RSpec::NamedSubject do
4
4
  subject(:cop) { described_class.new }
5
5
 
6
6
  it 'checks `it` and `specify` for explicit subject usage' do
7
- expect_violation(<<-RUBY)
7
+ expect_offense(<<-RUBY)
8
8
  RSpec.describe User do
9
9
  subject { described_class.new }
10
10
 
@@ -22,7 +22,7 @@ RSpec.describe RuboCop::Cop::RSpec::NamedSubject do
22
22
  end
23
23
 
24
24
  it 'checks before and after for explicit subject usage' do
25
- expect_violation(<<-RUBY)
25
+ expect_offense(<<-RUBY)
26
26
  RSpec.describe User do
27
27
  subject { described_class.new }
28
28
 
@@ -40,7 +40,7 @@ RSpec.describe RuboCop::Cop::RSpec::NamedSubject do
40
40
  end
41
41
 
42
42
  it 'checks around(:each) for explicit subject usage' do
43
- expect_violation(<<-RUBY)
43
+ expect_offense(<<-RUBY)
44
44
  RSpec.describe User do
45
45
  subject { described_class.new }
46
46
 
@@ -53,7 +53,7 @@ RSpec.describe RuboCop::Cop::RSpec::NamedSubject do
53
53
  end
54
54
 
55
55
  it 'ignores subject when not wrapped inside a test' do
56
- expect_no_violations(<<-RUBY)
56
+ expect_no_offenses(<<-RUBY)
57
57
  def foo
58
58
  it(subject)
59
59
  end
@@ -4,7 +4,7 @@ RSpec.describe RuboCop::Cop::RSpec::NestedGroups, :config do
4
4
  subject(:cop) { described_class.new(config) }
5
5
 
6
6
  it 'flags nested contexts' do
7
- expect_violation(<<-RUBY)
7
+ expect_offense(<<-RUBY)
8
8
  describe MyClass do
9
9
  context 'when foo' do
10
10
  context 'when bar' do
@@ -23,7 +23,7 @@ RSpec.describe RuboCop::Cop::RSpec::NestedGroups, :config do
23
23
  end
24
24
 
25
25
  it 'ignores non-spec context methods' do
26
- expect_no_violations(<<-RUBY)
26
+ expect_no_offenses(<<-RUBY)
27
27
  class MyThingy
28
28
  context 'this is not rspec' do
29
29
  context 'but it uses contexts' do
@@ -37,7 +37,7 @@ RSpec.describe RuboCop::Cop::RSpec::NestedGroups, :config do
37
37
  let(:cop_config) { { 'Max' => '2' } }
38
38
 
39
39
  it 'flags two levels of nesting' do
40
- expect_violation(<<-RUBY)
40
+ expect_offense(<<-RUBY)
41
41
  describe MyClass do
42
42
  context 'when foo' do
43
43
  context 'when bar' do
@@ -5,14 +5,14 @@ RSpec.describe RuboCop::Cop::RSpec::NotToNot, :config do
5
5
  let(:cop_config) { { 'EnforcedStyle' => 'not_to' } }
6
6
 
7
7
  it 'detects the `to_not` offense' do
8
- expect_violation(<<-RUBY)
8
+ expect_offense(<<-RUBY)
9
9
  it { expect(false).to_not be_true }
10
10
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `not_to` over `to_not`.
11
11
  RUBY
12
12
  end
13
13
 
14
14
  it 'detects no offense when using `not_to`' do
15
- expect_no_violations(<<-RUBY)
15
+ expect_no_offenses(<<-RUBY)
16
16
  it { expect(false).not_to be_true }
17
17
  RUBY
18
18
  end
@@ -26,14 +26,14 @@ RSpec.describe RuboCop::Cop::RSpec::NotToNot, :config do
26
26
  let(:cop_config) { { 'EnforcedStyle' => 'to_not' } }
27
27
 
28
28
  it 'detects the `not_to` offense' do
29
- expect_violation(<<-RUBY)
29
+ expect_offense(<<-RUBY)
30
30
  it { expect(false).not_to be_true }
31
31
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `to_not` over `not_to`.
32
32
  RUBY
33
33
  end
34
34
 
35
35
  it 'detects no offense when using `to_not`' do
36
- expect_no_violations(<<-RUBY)
36
+ expect_no_offenses(<<-RUBY)
37
37
  it { expect(false).to_not be_true }
38
38
  RUBY
39
39
  end
@@ -2,7 +2,7 @@ RSpec.describe RuboCop::Cop::RSpec::OverwritingSetup do
2
2
  subject(:cop) { described_class.new }
3
3
 
4
4
  it 'finds overwriten `let`' do
5
- expect_violation(<<-RUBY)
5
+ expect_offense(<<-RUBY)
6
6
  RSpec.describe User do
7
7
  let(:a) { a }
8
8
  let(:a) { b }
@@ -12,7 +12,7 @@ RSpec.describe RuboCop::Cop::RSpec::OverwritingSetup do
12
12
  end
13
13
 
14
14
  it 'finds overwriten `subject`' do
15
- expect_violation(<<-RUBY)
15
+ expect_offense(<<-RUBY)
16
16
  RSpec.describe User do
17
17
  subject(:a) { a }
18
18
 
@@ -23,7 +23,7 @@ RSpec.describe RuboCop::Cop::RSpec::OverwritingSetup do
23
23
  end
24
24
 
25
25
  it 'finds `let!` overwriting `let`' do
26
- expect_violation(<<-RUBY)
26
+ expect_offense(<<-RUBY)
27
27
  RSpec.describe User do
28
28
  let(:a) { b }
29
29
  let!(:a) { b }
@@ -33,7 +33,7 @@ RSpec.describe RuboCop::Cop::RSpec::OverwritingSetup do
33
33
  end
34
34
 
35
35
  it 'ignores overwriting in different context' do
36
- expect_no_violations(<<-RUBY)
36
+ expect_no_offenses(<<-RUBY)
37
37
  RSpec.describe User do
38
38
  let(:a) { a }
39
39
 
@@ -0,0 +1,335 @@
1
+ RSpec.describe RuboCop::Cop::RSpec::PredicateMatcher, :config do
2
+ subject(:cop) { described_class.new(config) }
3
+
4
+ let(:cop_config) do
5
+ { 'EnforcedStyle' => enforced_style,
6
+ 'Strict' => strict }
7
+ end
8
+
9
+ context 'when enforced style is `inflected`' do
10
+ let(:enforced_style) { 'inflected' }
11
+
12
+ shared_examples :inflected_common do
13
+ it 'registers an offense for a predicate method in actual' do
14
+ expect_offense(<<-RUBY)
15
+ expect(foo.empty?).to be_truthy
16
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_empty` matcher over `empty?`.
17
+ expect(foo.empty?).not_to be_truthy
18
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_empty` matcher over `empty?`.
19
+ expect(foo.empty?).to_not be_truthy
20
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_empty` matcher over `empty?`.
21
+ expect(foo.empty?).to be_falsey
22
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_empty` matcher over `empty?`.
23
+ expect(foo.has_something?).to be_truthy
24
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `have_something` matcher over `has_something?`.
25
+ expect(foo.include?(something)).to be_truthy
26
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `include` matcher over `include?`.
27
+ expect(foo.respond_to?(:bar)).to be_truthy
28
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `respond_to` matcher over `respond_to?`.
29
+ RUBY
30
+ end
31
+
32
+ it 'registers an offense for a predicate method with argument' do
33
+ expect_offense(<<-RUBY)
34
+ expect(foo.something?('foo')).to be_truthy
35
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_something` matcher over `something?`.
36
+ expect(foo.something?('foo', 'bar')).to be_truthy
37
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_something` matcher over `something?`.
38
+ expect(foo.has_key?('foo')).to be_truthy
39
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `have_key` matcher over `has_key?`.
40
+ RUBY
41
+ end
42
+
43
+ it 'registers an offense for a predicate method with a block' do
44
+ expect_offense(<<-RUBY)
45
+ expect(foo.all?(&:present?)).to be_truthy
46
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_all` matcher over `all?`.
47
+ expect(foo.all? { |x| x.present? }).to be_truthy
48
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_all` matcher over `all?`.
49
+ expect(foo.all? { present }).to be_truthy
50
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_all` matcher over `all?`.
51
+ expect(foo.something?(x) { |y| y.present? }).to be_truthy
52
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_something` matcher over `something?`.
53
+ RUBY
54
+ end
55
+
56
+ it 'accepts a predicate method that is not ckeced true/false' do
57
+ expect_no_offenses(<<-RUBY)
58
+ expect(foo.something?).to eq "something"
59
+ expect(foo.has_something?).to eq "something"
60
+ RUBY
61
+ end
62
+
63
+ it 'accepts non-predicate method' do
64
+ expect_no_offenses(<<-RUBY)
65
+ expect(foo.something).to be(true)
66
+ expect(foo.has_something).to be(true)
67
+ RUBY
68
+ end
69
+
70
+ include_examples 'autocorrect',
71
+ 'expect(foo.empty?).to be_truthy',
72
+ 'expect(foo).to be_empty'
73
+ include_examples 'autocorrect',
74
+ 'expect(foo.empty?).not_to be_truthy',
75
+ 'expect(foo).not_to be_empty'
76
+ include_examples 'autocorrect',
77
+ 'expect(foo.empty?).to_not be_truthy',
78
+ 'expect(foo).not_to be_empty'
79
+ include_examples 'autocorrect',
80
+ 'expect(foo.empty?).to be_falsey',
81
+ 'expect(foo).not_to be_empty'
82
+ include_examples 'autocorrect',
83
+ 'expect(foo.empty?).not_to be_falsey',
84
+ 'expect(foo).to be_empty'
85
+ include_examples 'autocorrect',
86
+ 'expect(foo.empty?).not_to a_truthy_value',
87
+ 'expect(foo).not_to be_empty'
88
+
89
+ include_examples 'autocorrect',
90
+ 'expect(foo.is_a?(Array)).to be_truthy',
91
+ 'expect(foo).to be_a(Array)'
92
+
93
+ include_examples 'autocorrect',
94
+ 'expect(foo.has_something?).to be_truthy',
95
+ 'expect(foo).to have_something'
96
+ include_examples 'autocorrect',
97
+ 'expect(foo.has_something?).not_to be_truthy',
98
+ 'expect(foo).not_to have_something'
99
+ include_examples 'autocorrect',
100
+ 'expect(foo.include?(something)).to be_truthy',
101
+ 'expect(foo).to include(something)'
102
+ include_examples 'autocorrect',
103
+ 'expect(foo.respond_to?(:bar)).to be_truthy',
104
+ 'expect(foo).to respond_to(:bar)'
105
+
106
+ include_examples 'autocorrect',
107
+ 'expect(foo.something?()).to be_truthy',
108
+ 'expect(foo).to be_something()'
109
+ include_examples 'autocorrect',
110
+ 'expect(foo.something? 1, 2).to be_truthy',
111
+ 'expect(foo).to be_something 1, 2'
112
+ include_examples 'autocorrect',
113
+ 'expect(foo.has_key?("foo")).to be_truthy',
114
+ 'expect(foo).to have_key("foo")'
115
+ include_examples 'autocorrect',
116
+ 'expect(foo.something?(1, 2)).to be_truthy',
117
+ 'expect(foo).to be_something(1, 2)'
118
+
119
+ include_examples 'autocorrect',
120
+ 'expect(foo.all? { |x| x.present? }).to be_truthy',
121
+ 'expect(foo).to be_all { |x| x.present? }'
122
+ include_examples 'autocorrect',
123
+ 'expect(foo.all?(n) { |x| x.present? }).to be_truthy',
124
+ 'expect(foo).to be_all(n) { |x| x.present? }'
125
+ include_examples 'autocorrect',
126
+ 'expect(foo.all? { present }).to be_truthy',
127
+ 'expect(foo).to be_all { present }'
128
+ include_examples 'autocorrect',
129
+ 'expect(foo.all? { }).to be_truthy',
130
+ 'expect(foo).to be_all { }'
131
+ include_examples 'autocorrect',
132
+ <<-BEFORE, <<-AFTER
133
+ expect(foo.all? do |x|
134
+ x + 1
135
+ x >= 2
136
+ end).to be_truthy
137
+ BEFORE
138
+ expect(foo).to be_all do |x|
139
+ x + 1
140
+ x >= 2
141
+ end
142
+ AFTER
143
+ include_examples 'autocorrect',
144
+ 'expect(foo.all? do; end).to be_truthy',
145
+ 'expect(foo).to be_all do; end'
146
+ end
147
+
148
+ context 'when strict is true' do
149
+ let(:strict) { true }
150
+
151
+ include_examples :inflected_common
152
+
153
+ it 'accepts strict checking boolean matcher' do
154
+ expect_no_offenses(<<-RUBY)
155
+ expect(foo.empty?).to eq(true)
156
+ expect(foo.empty?).to be(true)
157
+ expect(foo.empty?).to be(false)
158
+ expect(foo.empty?).not_to be true
159
+ expect(foo.empty?).not_to be false
160
+ RUBY
161
+ end
162
+ end
163
+
164
+ context 'when strict is false' do
165
+ let(:strict) { false }
166
+
167
+ include_examples :inflected_common
168
+
169
+ it 'registers an offense for a predicate method in actual' do
170
+ expect_offense(<<-RUBY)
171
+ expect(foo.empty?).to eq(true)
172
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_empty` matcher over `empty?`.
173
+ expect(foo.empty?).to be(true)
174
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_empty` matcher over `empty?`.
175
+ expect(foo.empty?).to be(false)
176
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_empty` matcher over `empty?`.
177
+ expect(foo.empty?).not_to be true
178
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_empty` matcher over `empty?`.
179
+ expect(foo.empty?).not_to be false
180
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `be_empty` matcher over `empty?`.
181
+ RUBY
182
+ end
183
+
184
+ include_examples 'autocorrect',
185
+ 'expect(foo.empty?).to eq(true)',
186
+ 'expect(foo).to be_empty'
187
+ include_examples 'autocorrect',
188
+ 'expect(foo.empty?).to eq(false)',
189
+ 'expect(foo).not_to be_empty'
190
+ include_examples 'autocorrect',
191
+ 'expect(foo.empty?).to be(true)',
192
+ 'expect(foo).to be_empty'
193
+ include_examples 'autocorrect',
194
+ 'expect(foo.empty?).to be(false)',
195
+ 'expect(foo).not_to be_empty'
196
+ include_examples 'autocorrect',
197
+ 'expect(foo.empty?).not_to be(true)',
198
+ 'expect(foo).not_to be_empty'
199
+ include_examples 'autocorrect',
200
+ 'expect(foo.empty?).not_to be(false)',
201
+ 'expect(foo).to be_empty'
202
+ end
203
+ end
204
+
205
+ context 'when enforced style is `explicit`' do
206
+ let(:enforced_style) { 'explicit' }
207
+
208
+ shared_examples :explicit_common do
209
+ it 'registers an offense for a predicate mather' do
210
+ expect_offense(<<-RUBY)
211
+ expect(foo).to be_empty
212
+ ^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `empty?` over `be_empty` matcher.
213
+ expect(foo).not_to be_empty
214
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `empty?` over `be_empty` matcher.
215
+ expect(foo).to have_something
216
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `has_something?` over `have_something` matcher.
217
+ RUBY
218
+ end
219
+
220
+ it 'registers an offense for a predicate mather with argument' do
221
+ expect_offense(<<-RUBY)
222
+ expect(foo).to be_something(1, 2)
223
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `something?` over `be_something` matcher.
224
+ expect(foo).to have_key(1)
225
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `has_key?` over `have_key` matcher.
226
+ RUBY
227
+ end
228
+
229
+ it 'registers an offense for a predicate matcher with a block' do
230
+ expect_offense(<<-RUBY)
231
+ expect(foo).to be_all(&:present?)
232
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `all?` over `be_all` matcher.
233
+ expect(foo).to be_all { |x| x.present? }
234
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `all?` over `be_all` matcher.
235
+ expect(foo).to be_all { present }
236
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `all?` over `be_all` matcher.
237
+ expect(foo).to be_something(x) { |y| y.present? }
238
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `something?` over `be_something` matcher.
239
+ RUBY
240
+ end
241
+
242
+ it 'accepts built in matchers' do
243
+ expect_no_offenses(<<-RUBY)
244
+ expect(foo).to be_truthy
245
+ expect(foo).to be_falsey
246
+ expect(foo).to be_falsy
247
+ expect(foo).to have_attributes(name: 'foo')
248
+ expect(foo).to have_received(:foo)
249
+ expect(foo).to be_between(1, 10)
250
+ expect(foo).to be_within(0.1).of(10.0)
251
+ RUBY
252
+ end
253
+
254
+ it 'accepts non-predicate matcher' do
255
+ expect_no_offenses(<<-RUBY)
256
+ expect(foo).to be(true)
257
+ RUBY
258
+ end
259
+ end
260
+
261
+ shared_examples :explicit_autocorrect do |matcher_true, matcher_false|
262
+ include_examples 'autocorrect',
263
+ 'expect(foo).to be_something',
264
+ "expect(foo.something?).to #{matcher_true}"
265
+ include_examples 'autocorrect',
266
+ 'expect(foo).not_to be_something',
267
+ "expect(foo.something?).to #{matcher_false}"
268
+ include_examples 'autocorrect',
269
+ 'expect(foo).to have_something',
270
+ "expect(foo.has_something?).to #{matcher_true}"
271
+
272
+ include_examples 'autocorrect',
273
+ 'expect(foo).to be_a(Array)',
274
+ "expect(foo.is_a?(Array)).to #{matcher_true}"
275
+ include_examples 'autocorrect',
276
+ 'expect(foo).to be_instance_of(Array)',
277
+ "expect(foo.instance_of?(Array)).to #{matcher_true}"
278
+
279
+ include_examples 'autocorrect',
280
+ 'expect(foo).to be_something()',
281
+ "expect(foo.something?()).to #{matcher_true}"
282
+ include_examples 'autocorrect',
283
+ 'expect(foo).to be_something(1)',
284
+ "expect(foo.something?(1)).to #{matcher_true}"
285
+ include_examples 'autocorrect',
286
+ 'expect(foo).to be_something(1, 2)',
287
+ "expect(foo.something?(1, 2)).to #{matcher_true}"
288
+ include_examples 'autocorrect',
289
+ 'expect(foo).to be_something 1, 2',
290
+ "expect(foo.something? 1, 2).to #{matcher_true}"
291
+
292
+ include_examples 'autocorrect',
293
+ 'expect(foo).to be_all { |x| x.present? }',
294
+ "expect(foo.all? { |x| x.present? }).to #{matcher_true}"
295
+ include_examples 'autocorrect',
296
+ 'expect(foo).to be_all(n) { |x| x.ok? }',
297
+ "expect(foo.all?(n) { |x| x.ok? }).to #{matcher_true}"
298
+ include_examples 'autocorrect',
299
+ 'expect(foo).to be_all { present }',
300
+ "expect(foo.all? { present }).to #{matcher_true}"
301
+ include_examples 'autocorrect',
302
+ 'expect(foo).to be_all { }',
303
+ "expect(foo.all? { }).to #{matcher_true}"
304
+ include_examples 'autocorrect',
305
+ <<-BEFORE, <<-AFTER
306
+ expect(foo).to be_all do |x|
307
+ x + 1
308
+ x >= 2
309
+ end
310
+ BEFORE
311
+ expect(foo.all? do |x|
312
+ x + 1
313
+ x >= 2
314
+ end).to #{matcher_true}
315
+ AFTER
316
+ include_examples 'autocorrect',
317
+ 'expect(foo).to be_all do; end',
318
+ "expect(foo.all? do; end).to #{matcher_true}"
319
+ end
320
+
321
+ context 'when strict is true' do
322
+ let(:strict) { true }
323
+
324
+ include_examples :explicit_common
325
+ include_examples :explicit_autocorrect, 'be(true)', 'be(false)'
326
+ end
327
+
328
+ context 'when strict is false' do
329
+ let(:strict) { false }
330
+
331
+ include_examples :explicit_common
332
+ include_examples :explicit_autocorrect, 'be_truthy', 'be_falsey'
333
+ end
334
+ end
335
+ end