|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RuboCop::Cop::RSpec::Output do |
| 4 | + it 'registers an offense for using `p` method without a receiver' do |
| 5 | + expect_offense(<<~RUBY) |
| 6 | + p "edmond dantes" |
| 7 | + ^ Do not write to stdout in specs. |
| 8 | + RUBY |
| 9 | + end |
| 10 | + |
| 11 | + it 'registers an offense for using `puts` method without a receiver' do |
| 12 | + expect_offense(<<~RUBY) |
| 13 | + puts "sinbad" |
| 14 | + ^^^^ Do not write to stdout in specs. |
| 15 | + RUBY |
| 16 | + end |
| 17 | + |
| 18 | + it 'registers an offense for using `print` method without a receiver' do |
| 19 | + expect_offense(<<~RUBY) |
| 20 | + print "abbe busoni" |
| 21 | + ^^^^^ Do not write to stdout in specs. |
| 22 | + RUBY |
| 23 | + end |
| 24 | + |
| 25 | + it 'registers an offense for using `pp` method without a receiver' do |
| 26 | + expect_offense(<<~RUBY) |
| 27 | + pp "monte cristo" |
| 28 | + ^^ Do not write to stdout in specs. |
| 29 | + RUBY |
| 30 | + end |
| 31 | + |
| 32 | + it 'registers an offense with `$stdout.write`' do |
| 33 | + expect_offense(<<~RUBY) |
| 34 | + $stdout.write "lord wilmore" |
| 35 | + ^^^^^^^^^^^^^ Do not write to stdout in specs. |
| 36 | + RUBY |
| 37 | + end |
| 38 | + |
| 39 | + it 'registers an offense with `$stderr.syswrite`' do |
| 40 | + expect_offense(<<~RUBY) |
| 41 | + $stderr.syswrite "faria" |
| 42 | + ^^^^^^^^^^^^^^^^ Do not write to stdout in specs. |
| 43 | + RUBY |
| 44 | + end |
| 45 | + |
| 46 | + it 'registers an offense with `STDOUT.write`' do |
| 47 | + expect_offense(<<~RUBY) |
| 48 | + STDOUT.write "bertuccio" |
| 49 | + ^^^^^^^^^^^^ Do not write to stdout in specs. |
| 50 | + RUBY |
| 51 | + end |
| 52 | + |
| 53 | + it 'registers an offense with `::STDOUT.write`' do |
| 54 | + expect_offense(<<~RUBY) |
| 55 | + ::STDOUT.write "bertuccio" |
| 56 | + ^^^^^^^^^^^^^^ Do not write to stdout in specs. |
| 57 | + RUBY |
| 58 | + end |
| 59 | + |
| 60 | + it 'registers an offense with `STDERR.write`' do |
| 61 | + expect_offense(<<~RUBY) |
| 62 | + STDERR.write "bertuccio" |
| 63 | + ^^^^^^^^^^^^ Do not write to stdout in specs. |
| 64 | + RUBY |
| 65 | + end |
| 66 | + |
| 67 | + it 'registers an offense with `::STDERR.write`' do |
| 68 | + expect_offense(<<~RUBY) |
| 69 | + ::STDERR.write "bertuccio" |
| 70 | + ^^^^^^^^^^^^^^ Do not write to stdout in specs. |
| 71 | + RUBY |
| 72 | + end |
| 73 | + |
| 74 | + it 'does not record an offense for methods with a receiver' do |
| 75 | + expect_no_offenses(<<~RUBY) |
| 76 | + obj.print |
| 77 | + something.p |
| 78 | + nothing.pp |
| 79 | + RUBY |
| 80 | + end |
| 81 | + |
| 82 | + it 'registers an offense for methods without arguments' do |
| 83 | + expect_offense(<<~RUBY) |
| 84 | + print |
| 85 | + ^^^^^ Do not write to stdout in specs. |
| 86 | + pp |
| 87 | + ^^ Do not write to stdout in specs. |
| 88 | + puts |
| 89 | + ^^^^ Do not write to stdout in specs. |
| 90 | + $stdout.write |
| 91 | + ^^^^^^^^^^^^^ Do not write to stdout in specs. |
| 92 | + STDERR.write |
| 93 | + ^^^^^^^^^^^^ Do not write to stdout in specs. |
| 94 | + RUBY |
| 95 | + end |
| 96 | + |
| 97 | + it 'registers an offense when `p` method with positional argument' do |
| 98 | + expect_offense(<<~RUBY) |
| 99 | + p(do_something) |
| 100 | + ^ Do not write to stdout in specs. |
| 101 | + RUBY |
| 102 | + end |
| 103 | + |
| 104 | + it 'does not register an offense when a method is called ' \ |
| 105 | + 'to a local variable with the same name as a print method' do |
| 106 | + expect_no_offenses(<<~RUBY) |
| 107 | + p.do_something |
| 108 | + RUBY |
| 109 | + end |
| 110 | + |
| 111 | + it 'does not register an offense when `p` method with keyword argument' do |
| 112 | + expect_no_offenses(<<~RUBY) |
| 113 | + p(class: 'this `p` method is a DSL') |
| 114 | + RUBY |
| 115 | + end |
| 116 | + |
| 117 | + it 'does not register an offense when `p` method with symbol proc' do |
| 118 | + expect_no_offenses(<<~RUBY) |
| 119 | + p(&:this_p_method_is_a_dsl) |
| 120 | + RUBY |
| 121 | + end |
| 122 | + |
| 123 | + it 'does not register an offense when the `p` method is called ' \ |
| 124 | + 'with block argument' do |
| 125 | + expect_no_offenses(<<~RUBY) |
| 126 | + # phlex-rails gem. |
| 127 | + div do |
| 128 | + p { 'Some text' } |
| 129 | + end |
| 130 | + RUBY |
| 131 | + end |
| 132 | + |
| 133 | + it 'does not register an offense when io method is called ' \ |
| 134 | + 'with block argument' do |
| 135 | + expect_no_offenses(<<~RUBY) |
| 136 | + obj.write { do_somethig } |
| 137 | + RUBY |
| 138 | + end |
| 139 | + |
| 140 | + it 'does not register an offense when io method is called ' \ |
| 141 | + 'with numbered block argument' do |
| 142 | + expect_no_offenses(<<~RUBY) |
| 143 | + obj.write { do_something(_1) } |
| 144 | + RUBY |
| 145 | + end |
| 146 | + |
| 147 | + it 'does not register an offense when io method is called ' \ |
| 148 | + 'with `it` parameter', :ruby34, unsupported_on: :parser do |
| 149 | + expect_no_offenses(<<~RUBY) |
| 150 | + obj.write { do_something(it) } |
| 151 | + RUBY |
| 152 | + end |
| 153 | + |
| 154 | + it 'does not register an offense when a method is safe navigation called ' \ |
| 155 | + 'to a local variable with the same name as a print method' do |
| 156 | + expect_no_offenses(<<~RUBY) |
| 157 | + p&.do_something |
| 158 | + RUBY |
| 159 | + end |
| 160 | + |
| 161 | + it 'does not record an offense for comments' do |
| 162 | + expect_no_offenses(<<~RUBY) |
| 163 | + # print "test" |
| 164 | + # p |
| 165 | + # $stdout.write |
| 166 | + # STDERR.binwrite |
| 167 | + RUBY |
| 168 | + end |
| 169 | +end |
0 commit comments