Rubocop: Is strip_indent a bad practice in tests?

Created on 7 Jan 2018  ยท  6Comments  ยท  Source: rubocop-hq/rubocop

I have fixed 2 issues about indent #5410 and #5292, all tests did not figure out the issues because they use strip_indent to ignore indent.

So I open a question here: is strip_indent a bad practice in tests? If so, I'll try to create a PR to remove strip_indent.

question

All 6 comments

No, I think it's actually great. It shows you better the real indentation of the code - imagine how ugly everything would be if the indentation had to start from the beginning of the line. strip_indent doesn't ignore the indent, just the leading indent. If the code snippet is well-formed, that's never really an issue.

@bbatsov I didn't mean to write code starting from the beginning fo the line. e.g.

I want to change

    expect(corrected).to eq(<<-RUBY.strip_indent)
      foo(
        a: 1,
        b: 2
      )
    RUBY

to

    expect(corrected).to eq(<<-RUBY)
      foo(
        a: 1,
        b: 2
      )
    RUBY

so it won't make code ugly, but it makes tests code taking care of code indent in the real world.

The following code shows different results.

<<-RUBY.strip_indent
  hi
RUBY # => "hi\n"

<<-RUBY
  hi
RUBY # => "  hi\n"
````

IMO, currently it seems preferable to use `strip_indent`.

When RuboCop drops supporting Ruby 2.2 or lower, I think it is an opportunity to replace `strip_indent` with squiggly heredoc (`<<~`) .

```ruby
<<~RUBY
  hi
RUBY # => "hi\n"

Until then, it seems that confusion will not occur if strip_indent is maintained.

I completely agree with @koic.

The way the question is asked, the answer is โ€œnoโ€, but things are rarely that simple. ๐Ÿ™‚ Our exceptions would be the cops that explicitly deal with leading indentation. Stripping the leading indent in those test cases would lead to pretty convoluted tests. In those cases, #strip_margin works better. ๐Ÿ™‚

Agreed.

On Mon, 25 Jun 2018 at 14:21, Ted Johansson notifications@github.com
wrote:

The way the question is asked, the answer is โ€œnoโ€, but things are rarely
that simple. ๐Ÿ™‚ Our exceptions would be the cops that explicitly deal
with leading indentation. Stripping the leading indent in those test cases
would lead to pretty convoluted tests. In those cases, #strip_margin
works better. ๐Ÿ™‚

โ€”
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/rubocop-hq/rubocop/issues/5415#issuecomment-399918252,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAGVykKJvmLwPH7E8_OPmXrfWahcX4Aeks5uAMe6gaJpZM4RVo99
.

>

Best Regards,
Bozhidar Batsov

http://www.batsov.com

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bbatsov picture bbatsov  ยท  3Comments

david942j picture david942j  ยท  3Comments

ecbrodie picture ecbrodie  ยท  3Comments

AndreiMotinga picture AndreiMotinga  ยท  3Comments

cabello picture cabello  ยท  3Comments