Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add expect_issue and expect_no_issues spec helpers #245

Merged
merged 14 commits into from
Oct 22, 2021

Conversation

FnControlOption
Copy link
Contributor

@FnControlOption FnControlOption commented Oct 19, 2021

These are based on RuboCop's expect_offense and expect_no_offenses.

Todo:

  • Convert some existing specs
  • Add spec for AnnotatedSource
  • Add documentation
  • Further clean up RuboCop vestiges

Future PRs:

Old:

it "reports multiple shared variables in spawn" do
  s = Source.new %(
    foo, bar, baz = 0, 0, 0
    while foo < 10
      baz += 1
      spawn do
        puts foo
        puts foo + bar + baz
      end
      foo += 1
    end
  )
  subject.catch(s).should_not be_valid
  s.issues.size.should eq 3
  s.issues[0].location.to_s.should eq ":5:10"
  s.issues[0].end_location.to_s.should eq ":5:12"
  s.issues[0].message.should eq "Shared variable `foo` is used in fiber"

  s.issues[1].location.to_s.should eq ":6:10"
  s.issues[1].end_location.to_s.should eq ":6:12"
  s.issues[1].message.should eq "Shared variable `foo` is used in fiber"

  s.issues[2].location.to_s.should eq ":6:22"
  s.issues[2].end_location.to_s.should eq ":6:24"
  s.issues[2].message.should eq "Shared variable `baz` is used in fiber"
end

New:

it "reports multiple shared variables in spawn" do
  expect_issue subject, %(
    foo, bar, baz = 0, 0, 0
    while foo < 10
      baz += 1
      spawn do
        puts foo
            # ^^^ error: Shared variable `foo` is used in fiber
        puts foo + bar + baz
            # ^^^ error: Shared variable `foo` is used in fiber
                        # ^^^ error: Shared variable `baz` is used in fiber
      end
      foo += 1
    end
  )
end

@veelenga
Copy link
Member

Thanks for the suggestion.

  1. I've seen the rubocop's dsl and was not sure if we should introduce it into Ameba. It looks much more concise, yes, however, it also looks a bit strange to me and I was curious if there are more idiomatic and declarative ideas/ways.

  2. I would remove the diff from the scope and add it as a separate feature.

@FnControlOption
Copy link
Contributor Author

FnControlOption commented Oct 19, 2021

however, it also looks a bit strange to me and I was curious if there are more idiomatic and declarative ideas/ways.

Perhaps Sorbet-style errors would be an improvement? For example:

# ameba:foo Lint/BadDirective # error: Bad action in comment directive: 'foo'. [...]
[1, 2, 3].reject { |e| e > 2 }.any?
        # ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `any? {...}` instead of `reject {...}.any?`

it can happen that there are multiple issues reported by the same (or some other) rules. Would it be possible to catch that as well?

This is possible, yes.

@FnControlOption FnControlOption changed the title Add report_issue and report_no_issues spec helpers Add expect_issue and expect_no_issues spec helpers Oct 20, 2021
@FnControlOption FnControlOption marked this pull request as ready for review October 20, 2021 03:42
@veelenga
Copy link
Member

Yeah, sorbet-style errors look great to me.

@FnControlOption
Copy link
Contributor Author

I might start working on some form of autocorrect now. How does that sound?

@veelenga
Copy link
Member

veelenga commented Oct 20, 2021

That would be very helpful.

Would you mind to do the following as part of this PR:

  1. cover AnnotatedSource by specs
  2. convert those two files to new matchers, so the specs are consistent at least in the same file
  • spec/ameba/rule/lint/shared_var_in_fiber_spec.cr
  • spec/ameba/rule/performance/any_after_filter_spec.cr

I can convert rest of the specs on my own

@FnControlOption
Copy link
Contributor Author

FnControlOption commented Oct 21, 2021

I already converted most uses of should be_valid so you can apply this patch when you start converting specs: https://gist.github.com/FnControlOption/b10358b166061d8ff3d6f3283bb3c02c

Or if it's easier, I've pushed this to my fork in the tmp-report_issue2 branch: FnControlOption@4ec325c

@FnControlOption
Copy link
Contributor Author

FnControlOption commented Oct 21, 2021

Also, let me know if I should add more tests for AnnotatedSource because I kinda lost track of what I was doing while in the middle of adding them, haha 😅

Copy link
Member

@Sija Sija left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left just a couple of minor comments, but overall it's great! These helpers will make testing much more pleasant ❤️

src/ameba/spec_support/expect_issue.cr Outdated Show resolved Hide resolved
src/ameba/spec_support.cr Outdated Show resolved Hide resolved
Copy link
Member

@veelenga veelenga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 💟

@veelenga veelenga requested a review from Sija October 22, 2021 12:50
Copy link
Member

@Sija Sija left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I've added a few style-related comments.

src/ameba/spec/util.cr Outdated Show resolved Hide resolved
spec/ameba/spec/annotated_source_spec.cr Outdated Show resolved Hide resolved
spec/ameba/spec/annotated_source_spec.cr Outdated Show resolved Hide resolved
src/ameba/spec/expect_issue.cr Outdated Show resolved Hide resolved
src/ameba/spec/expect_issue.cr Outdated Show resolved Hide resolved
src/ameba/spec/util.cr Outdated Show resolved Hide resolved
@Sija Sija merged commit 3d432fd into crystal-ameba:master Oct 22, 2021
@FnControlOption FnControlOption deleted the report_issue branch November 16, 2021 23:07
@Sija Sija changed the title Add expect_issue and expect_no_issues spec helpers Add expect_issue and expect_no_issues spec helpers Jan 10, 2022
@Sija Sija added this to the 0.15.0 milestone Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants