|
18 | 18 | end |
19 | 19 | end |
20 | 20 | RUBY |
| 21 | + |
| 22 | + expect_correction(<<-RUBY) |
| 23 | + describe Foo do |
| 24 | + it 'uses expect incorrectly' do |
| 25 | + expect(bar).to eq(123) |
| 26 | + expect(bar).to eq(12.3) |
| 27 | + expect(bar).to eq(1i) |
| 28 | + expect(bar).to eq(1r) |
| 29 | + end |
| 30 | + end |
| 31 | + RUBY |
21 | 32 | end |
22 | 33 |
|
23 | 34 | it 'flags boolean literal values within expect(...)' do |
|
31 | 42 | end |
32 | 43 | end |
33 | 44 | RUBY |
| 45 | + |
| 46 | + expect_correction(<<-RUBY) |
| 47 | + describe Foo do |
| 48 | + it 'uses expect incorrectly' do |
| 49 | + expect(bar).to eq(true) |
| 50 | + expect(bar).to eq(false) |
| 51 | + end |
| 52 | + end |
| 53 | + RUBY |
34 | 54 | end |
35 | 55 |
|
36 | 56 | it 'flags string and symbol literal values within expect(...)' do |
|
44 | 64 | end |
45 | 65 | end |
46 | 66 | RUBY |
| 67 | + |
| 68 | + expect_correction(<<-RUBY) |
| 69 | + describe Foo do |
| 70 | + it 'uses expect incorrectly' do |
| 71 | + expect(bar).to eq("foo") |
| 72 | + expect(bar).to eq(:foo) |
| 73 | + end |
| 74 | + end |
| 75 | + RUBY |
47 | 76 | end |
48 | 77 |
|
49 | 78 | it 'flags literal nil value within expect(...)' do |
|
55 | 84 | end |
56 | 85 | end |
57 | 86 | RUBY |
| 87 | + |
| 88 | + expect_correction(<<-RUBY) |
| 89 | + describe Foo do |
| 90 | + it 'uses expect incorrectly' do |
| 91 | + expect(bar).to eq(nil) |
| 92 | + end |
| 93 | + end |
| 94 | + RUBY |
58 | 95 | end |
59 | 96 |
|
60 | 97 | it 'does not flag dynamic values within expect(...)' do |
|
80 | 117 | end |
81 | 118 | end |
82 | 119 | RUBY |
| 120 | + |
| 121 | + expect_correction(<<-RUBY) |
| 122 | + describe Foo do |
| 123 | + it 'uses expect incorrectly' do |
| 124 | + expect(bar).to eq([123]) |
| 125 | + expect(bar).to eq([[123]]) |
| 126 | + end |
| 127 | + end |
| 128 | + RUBY |
83 | 129 | end |
84 | 130 |
|
85 | 131 | it 'flags hashes containing only literal values within expect(...)' do |
|
93 | 139 | end |
94 | 140 | end |
95 | 141 | RUBY |
| 142 | + |
| 143 | + expect_correction(<<-RUBY) |
| 144 | + describe Foo do |
| 145 | + it 'uses expect incorrectly' do |
| 146 | + expect(bar).to eq(foo: 1, bar: 2) |
| 147 | + expect(bar).to eq(foo: 1, bar: [{}]) |
| 148 | + end |
| 149 | + end |
| 150 | + RUBY |
96 | 151 | end |
97 | 152 |
|
98 | 153 | it 'flags ranges containing only literal values within expect(...)' do |
|
106 | 161 | end |
107 | 162 | end |
108 | 163 | RUBY |
| 164 | + |
| 165 | + expect_correction(<<-RUBY) |
| 166 | + describe Foo do |
| 167 | + it 'uses expect incorrectly' do |
| 168 | + expect(bar).to eq(1..2) |
| 169 | + expect(bar).to eq(1...2) |
| 170 | + end |
| 171 | + end |
| 172 | + RUBY |
109 | 173 | end |
110 | 174 |
|
111 | 175 | it 'flags regexps containing only literal values within expect(...)' do |
|
117 | 181 | end |
118 | 182 | end |
119 | 183 | RUBY |
| 184 | + |
| 185 | + expect_correction(<<-RUBY) |
| 186 | + describe Foo do |
| 187 | + it 'uses expect incorrectly' do |
| 188 | + expect(bar).to eq(/foo|bar/) |
| 189 | + end |
| 190 | + end |
| 191 | + RUBY |
120 | 192 | end |
121 | 193 |
|
122 | 194 | it 'does not flag complex values with dynamic parts within expect(...)' do |
|
135 | 207 | RUBY |
136 | 208 | end |
137 | 209 |
|
| 210 | + it 'ignores `be` with no argument' do |
| 211 | + expect_no_offenses(<<~RUBY) |
| 212 | + describe Foo do |
| 213 | + it 'uses expect legitimately' do |
| 214 | + expect(1).to be |
| 215 | + end |
| 216 | + end |
| 217 | + RUBY |
| 218 | + end |
| 219 | + |
| 220 | + it 'flags `be` with an argument' do |
| 221 | + expect_offense(<<~RUBY) |
| 222 | + describe Foo do |
| 223 | + it 'uses expect incorrectly' do |
| 224 | + expect(true).to be(a) |
| 225 | + ^^^^ Provide the actual you are testing to `expect(...)`. |
| 226 | + end |
| 227 | + end |
| 228 | + RUBY |
| 229 | + |
| 230 | + expect_correction(<<~RUBY) |
| 231 | + describe Foo do |
| 232 | + it 'uses expect incorrectly' do |
| 233 | + expect(a).to be(true) |
| 234 | + end |
| 235 | + end |
| 236 | + RUBY |
| 237 | + end |
| 238 | + |
| 239 | + it 'flags `be ==`' do |
| 240 | + expect_offense(<<~RUBY) |
| 241 | + describe Foo do |
| 242 | + it 'uses expect incorrectly' do |
| 243 | + expect(1).to be == a |
| 244 | + ^ Provide the actual you are testing to `expect(...)`. |
| 245 | + end |
| 246 | + end |
| 247 | + RUBY |
| 248 | + |
| 249 | + expect_correction(<<~RUBY) |
| 250 | + describe Foo do |
| 251 | + it 'uses expect incorrectly' do |
| 252 | + expect(a).to be == 1 |
| 253 | + end |
| 254 | + end |
| 255 | + RUBY |
| 256 | + end |
| 257 | + |
| 258 | + it 'flags with `eql` matcher' do |
| 259 | + expect_offense(<<-RUBY) |
| 260 | + describe Foo do |
| 261 | + it 'uses expect incorrectly' do |
| 262 | + expect(1).to eql(bar) |
| 263 | + ^ Provide the actual you are testing to `expect(...)`. |
| 264 | + end |
| 265 | + end |
| 266 | + RUBY |
| 267 | + |
| 268 | + expect_correction(<<-RUBY) |
| 269 | + describe Foo do |
| 270 | + it 'uses expect incorrectly' do |
| 271 | + expect(bar).to eql(1) |
| 272 | + end |
| 273 | + end |
| 274 | + RUBY |
| 275 | + end |
| 276 | + |
| 277 | + it 'flags with `equal` matcher' do |
| 278 | + expect_offense(<<-RUBY) |
| 279 | + describe Foo do |
| 280 | + it 'uses expect incorrectly' do |
| 281 | + expect(1).to equal(bar) |
| 282 | + ^ Provide the actual you are testing to `expect(...)`. |
| 283 | + end |
| 284 | + end |
| 285 | + RUBY |
| 286 | + |
| 287 | + expect_correction(<<-RUBY) |
| 288 | + describe Foo do |
| 289 | + it 'uses expect incorrectly' do |
| 290 | + expect(bar).to equal(1) |
| 291 | + end |
| 292 | + end |
| 293 | + RUBY |
| 294 | + end |
| 295 | + |
| 296 | + it 'flags but does not autocorrect violations for other matchers' do |
| 297 | + expect_offense(<<-RUBY) |
| 298 | + describe Foo do |
| 299 | + it 'uses expect incorrectly' do |
| 300 | + expect([1,2,3]).to include(a) |
| 301 | + ^^^^^^^ Provide the actual you are testing to `expect(...)`. |
| 302 | + end |
| 303 | + end |
| 304 | + RUBY |
| 305 | + |
| 306 | + expect_no_corrections |
| 307 | + end |
| 308 | + |
138 | 309 | context 'when inspecting rspec-rails routing specs' do |
139 | 310 | let(:cop_config) { {} } |
140 | 311 |
|
|
0 commit comments