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

Cop idea: change example length from counting lines to statements #249

Closed
jcoyne opened this issue Dec 7, 2016 · 6 comments
Closed

Cop idea: change example length from counting lines to statements #249

jcoyne opened this issue Dec 7, 2016 · 6 comments

Comments

@jcoyne
Copy link

jcoyne commented Dec 7, 2016

Example:

    it "spawns CreateWorkJobs for each work" do
      expect(CreateWorkJob).to receive(:perform_later).with(user,
                                                            "GenericWork",
                                                            {
                                                              keyword: [],
                                                              title: ['File One'],
                                                              resource_type: ["Article"],
                                                              uploaded_files: ['1']
                                                            },
                                                            child_log).and_return(true)
      expect(CreateWorkJob).to receive(:perform_later).with(user,
                                                            "GenericWork",
                                                            {
                                                              keyword: [],
                                                              title: ['File Two'],
                                                              resource_type: ["Image"],
                                                              uploaded_files: ['2']
                                                            },
                                                            child_log).and_return(true)
      subject
    end

This has 3 statements, which seems like a reasonable number for an example. But due to the way it's formatted (which I believe contributes to good readability *) it has 17 lines, which causes the cop to fail. Would it be possible to count the number of statements instead of lines?

* Kevlin Henney - Seven Ineffective Coding Habits of Many Programmers https://vimeo.com/97329157#t=14:36

@backus
Copy link
Collaborator

backus commented Dec 11, 2016

I think line length and statement complexity are probably both decent heuristics with edge cases that will be obviously wrong in some cases. What do you think abuot a separate cop instead like RSpec/ExampleStatements that might do something like this?

@jfelchner
Copy link

jfelchner commented Feb 13, 2018

Now that I'm looking at this I'm wondering if just using rubocop's built-in PerceivedComplexity, AbcSize, CyclomaticComplexity cops might be good enough here.

@jfelchner
Copy link

FWIW, rubocop's line number cops now allow exceptions for hashes, arrays, and heredocs. Perhaps it's abstracted enough that this cop could take advantage of it as well. See here

@Darhazer
Copy link
Member

Since ExampleLength supports the CountAsOne option for arrays and hashes, perhaps we can close this?

@pirj
Copy link
Member

pirj commented Mar 30, 2022

Makes sense 👍 @bquorning ?

@bquorning
Copy link
Collaborator

bquorning commented Mar 30, 2022

Yes, I agree. As @Darhazer mentioned, the RSpec/ExampleLength cop can be configured with the CountAsOne option (with an array of one or more of array, hash and heredoc). (The documentation at https://docs.rubocop.org/rubocop-rspec/2.9/cops_rspec.html#countasone-array-heredoc doesn’t mention the hash option, which we should probably get fixed.)

Update: the hash is mentioned further up in the docs, at https://docs.rubocop.org/rubocop-rspec/2.9/cops_rspec.html#rspecexamplelength.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants