|
| 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 | + |
| 10 | + expect_correction(<<~RUBY) |
| 11 | +
|
| 12 | + RUBY |
| 13 | + end |
| 14 | + |
| 15 | + it 'registers an offense for using `puts` method without a receiver' do |
| 16 | + expect_offense(<<~RUBY) |
| 17 | + puts "sinbad" |
| 18 | + ^^^^^^^^^^^^^ Do not write to stdout in specs. |
| 19 | + RUBY |
| 20 | + |
| 21 | + expect_correction(<<~RUBY) |
| 22 | +
|
| 23 | + RUBY |
| 24 | + end |
| 25 | + |
| 26 | + it 'registers an offense for using `print` method without a receiver' do |
| 27 | + expect_offense(<<~RUBY) |
| 28 | + print "abbe busoni" |
| 29 | + ^^^^^^^^^^^^^^^^^^^ Do not write to stdout in specs. |
| 30 | + RUBY |
| 31 | + |
| 32 | + expect_correction(<<~RUBY) |
| 33 | +
|
| 34 | + RUBY |
| 35 | + end |
| 36 | + |
| 37 | + it 'registers an offense for using `pp` method without a receiver' do |
| 38 | + expect_offense(<<~RUBY) |
| 39 | + pp "monte cristo" |
| 40 | + ^^^^^^^^^^^^^^^^^ Do not write to stdout in specs. |
| 41 | + RUBY |
| 42 | + |
| 43 | + expect_correction(<<~RUBY) |
| 44 | +
|
| 45 | + RUBY |
| 46 | + end |
| 47 | + |
| 48 | + it 'registers an offense with `$stdout.write`' do |
| 49 | + expect_offense(<<~RUBY) |
| 50 | + $stdout.write "lord wilmore" |
| 51 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not write to stdout in specs. |
| 52 | + RUBY |
| 53 | + |
| 54 | + expect_correction(<<~RUBY) |
| 55 | +
|
| 56 | + RUBY |
| 57 | + end |
| 58 | + |
| 59 | + it 'registers an offense with `$stderr.syswrite`' do |
| 60 | + expect_offense(<<~RUBY) |
| 61 | + $stderr.syswrite "faria" |
| 62 | + ^^^^^^^^^^^^^^^^^^^^^^^^ Do not write to stdout in specs. |
| 63 | + RUBY |
| 64 | + |
| 65 | + expect_correction(<<~RUBY) |
| 66 | +
|
| 67 | + RUBY |
| 68 | + end |
| 69 | + |
| 70 | + it 'registers an offense with `STDOUT.write`' do |
| 71 | + expect_offense(<<~RUBY) |
| 72 | + STDOUT.write "bertuccio" |
| 73 | + ^^^^^^^^^^^^^^^^^^^^^^^^ Do not write to stdout in specs. |
| 74 | + RUBY |
| 75 | + |
| 76 | + expect_correction(<<~RUBY) |
| 77 | +
|
| 78 | + RUBY |
| 79 | + end |
| 80 | + |
| 81 | + it 'registers an offense with `::STDOUT.write`' do |
| 82 | + expect_offense(<<~RUBY) |
| 83 | + ::STDOUT.write "bertuccio" |
| 84 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not write to stdout in specs. |
| 85 | + RUBY |
| 86 | + |
| 87 | + expect_correction(<<~RUBY) |
| 88 | +
|
| 89 | + RUBY |
| 90 | + end |
| 91 | + |
| 92 | + it 'registers an offense with `STDERR.write`' do |
| 93 | + expect_offense(<<~RUBY) |
| 94 | + STDERR.write "bertuccio" |
| 95 | + ^^^^^^^^^^^^^^^^^^^^^^^^ Do not write to stdout in specs. |
| 96 | + RUBY |
| 97 | + |
| 98 | + expect_correction(<<~RUBY) |
| 99 | +
|
| 100 | + RUBY |
| 101 | + end |
| 102 | + |
| 103 | + it 'registers an offense with `::STDERR.write`' do |
| 104 | + expect_offense(<<~RUBY) |
| 105 | + ::STDERR.write "bertuccio" |
| 106 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not write to stdout in specs. |
| 107 | + RUBY |
| 108 | + |
| 109 | + expect_correction(<<~RUBY) |
| 110 | +
|
| 111 | + RUBY |
| 112 | + end |
| 113 | + |
| 114 | + it 'does not record an offense for methods with a receiver' do |
| 115 | + expect_no_offenses(<<~RUBY) |
| 116 | + obj.print |
| 117 | + something.p |
| 118 | + nothing.pp |
| 119 | + RUBY |
| 120 | + end |
| 121 | + |
| 122 | + it 'registers an offense for methods without arguments' do |
| 123 | + expect_offense(<<~RUBY) |
| 124 | + print |
| 125 | + ^^^^^ Do not write to stdout in specs. |
| 126 | + pp |
| 127 | + ^^ Do not write to stdout in specs. |
| 128 | + puts |
| 129 | + ^^^^ Do not write to stdout in specs. |
| 130 | + $stdout.write |
| 131 | + ^^^^^^^^^^^^^ Do not write to stdout in specs. |
| 132 | + STDERR.write |
| 133 | + ^^^^^^^^^^^^ Do not write to stdout in specs. |
| 134 | + RUBY |
| 135 | + |
| 136 | + expect_correction(<<~RUBY) |
| 137 | +
|
| 138 | +
|
| 139 | +
|
| 140 | +
|
| 141 | +
|
| 142 | + RUBY |
| 143 | + end |
| 144 | + |
| 145 | + it 'registers an offense when `p` method with positional argument' do |
| 146 | + expect_offense(<<~RUBY) |
| 147 | + p(do_something) |
| 148 | + ^^^^^^^^^^^^^^^ Do not write to stdout in specs. |
| 149 | + RUBY |
| 150 | + |
| 151 | + expect_correction(<<~RUBY) |
| 152 | +
|
| 153 | + RUBY |
| 154 | + end |
| 155 | + |
| 156 | + it 'does not register an offense when a method is called ' \ |
| 157 | + 'to a local variable with the same name as a print method' do |
| 158 | + expect_no_offenses(<<~RUBY) |
| 159 | + p.do_something |
| 160 | + RUBY |
| 161 | + end |
| 162 | + |
| 163 | + it 'does not register an offense when `p` method with keyword argument' do |
| 164 | + expect_no_offenses(<<~RUBY) |
| 165 | + p(class: 'this `p` method is a DSL') |
| 166 | + RUBY |
| 167 | + end |
| 168 | + |
| 169 | + it 'does not register an offense when `p` method with symbol proc' do |
| 170 | + expect_no_offenses(<<~RUBY) |
| 171 | + p(&:this_p_method_is_a_dsl) |
| 172 | + RUBY |
| 173 | + end |
| 174 | + |
| 175 | + it 'does not register an offense when the `p` method is called ' \ |
| 176 | + 'with block argument' do |
| 177 | + expect_no_offenses(<<~RUBY) |
| 178 | + # phlex-rails gem. |
| 179 | + div do |
| 180 | + p { 'Some text' } |
| 181 | + end |
| 182 | + RUBY |
| 183 | + end |
| 184 | + |
| 185 | + it 'does not register an offense when io method is called ' \ |
| 186 | + 'with block argument' do |
| 187 | + expect_no_offenses(<<~RUBY) |
| 188 | + obj.write { do_somethig } |
| 189 | + RUBY |
| 190 | + end |
| 191 | + |
| 192 | + it 'does not register an offense when io method is called ' \ |
| 193 | + 'with numbered block argument' do |
| 194 | + expect_no_offenses(<<~RUBY) |
| 195 | + obj.write { do_something(_1) } |
| 196 | + RUBY |
| 197 | + end |
| 198 | + |
| 199 | + it 'does not register an offense when io method is called ' \ |
| 200 | + 'with `it` parameter', :ruby34, unsupported_on: :parser do |
| 201 | + expect_no_offenses(<<~RUBY) |
| 202 | + obj.write { do_something(it) } |
| 203 | + RUBY |
| 204 | + end |
| 205 | + |
| 206 | + it 'does not register an offense when a method is safe navigation called ' \ |
| 207 | + 'to a local variable with the same name as a print method' do |
| 208 | + expect_no_offenses(<<~RUBY) |
| 209 | + p&.do_something |
| 210 | + RUBY |
| 211 | + end |
| 212 | + |
| 213 | + it 'does not record an offense for comments' do |
| 214 | + expect_no_offenses(<<~RUBY) |
| 215 | + # print "test" |
| 216 | + # p |
| 217 | + # $stdout.write |
| 218 | + # STDERR.binwrite |
| 219 | + RUBY |
| 220 | + end |
| 221 | +end |
0 commit comments