|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RuboCop::Cop::RSpec::EmptyOutput, :config do |
| 4 | + it 'registers an offense when using `#output` with an empty string' do |
| 5 | + expect_offense(<<~RUBY) |
| 6 | + expect { foo }.to output('').to_stderr |
| 7 | + ^^^^^^^^^^ Use `not_to` instead of matching on an empty output. |
| 8 | + expect { foo }.to output('').to_stdout |
| 9 | + ^^^^^^^^^^ Use `not_to` instead of matching on an empty output. |
| 10 | + RUBY |
| 11 | + |
| 12 | + expect_correction(<<~RUBY) |
| 13 | + expect { foo }.not_to output.to_stderr |
| 14 | + expect { foo }.not_to output.to_stdout |
| 15 | + RUBY |
| 16 | + end |
| 17 | + |
| 18 | + it 'registers an offense when negatively matching `#output` with ' \ |
| 19 | + 'an empty string' do |
| 20 | + expect_offense(<<~RUBY) |
| 21 | + expect { foo }.not_to output('').to_stderr |
| 22 | + ^^^^^^^^^^ Use `to` instead of matching on an empty output. |
| 23 | + expect { foo }.to_not output('').to_stdout |
| 24 | + ^^^^^^^^^^ Use `to` instead of matching on an empty output. |
| 25 | + RUBY |
| 26 | + |
| 27 | + expect_correction(<<~RUBY) |
| 28 | + expect { foo }.to output.to_stderr |
| 29 | + expect { foo }.to output.to_stdout |
| 30 | + RUBY |
| 31 | + end |
| 32 | + |
| 33 | + describe 'compound expectations' do |
| 34 | + it 'does not register an offense when matching empty strings' do |
| 35 | + expect_no_offenses(<<~RUBY) |
| 36 | + expect { |
| 37 | + :noop |
| 38 | + }.to output('').to_stdout.and output('').to_stderr |
| 39 | + RUBY |
| 40 | + end |
| 41 | + |
| 42 | + it 'does not register an offense when matching non-empty strings' do |
| 43 | + expect_no_offenses(<<~RUBY) |
| 44 | + expect { |
| 45 | + warn "foo" |
| 46 | + puts "bar" |
| 47 | + }.to output("bar\n").to_stdout.and output(/foo/).to_stderr |
| 48 | + RUBY |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + it 'does not register an offense when using `#output` with ' \ |
| 53 | + 'a non-empty string' do |
| 54 | + expect_no_offenses(<<~RUBY) |
| 55 | + expect { foo }.to output('foo').to_stderr |
| 56 | + expect { foo }.not_to output('foo').to_stderr |
| 57 | + expect { foo }.to_not output('foo').to_stderr |
| 58 | + RUBY |
| 59 | + end |
| 60 | + |
| 61 | + it 'does not register an offense when using `not_to output`' do |
| 62 | + expect_no_offenses(<<~RUBY) |
| 63 | + expect { foo }.not_to output.to_stderr |
| 64 | + expect { foo }.to_not output.to_stderr |
| 65 | + RUBY |
| 66 | + end |
| 67 | +end |
0 commit comments