Skip to content

Commit

Permalink
Fix regression detecting label for 'odd' methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviershay committed Mar 31, 2012
1 parent c019e7c commit ef4e094
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/cane/abc_check.rb
Expand Up @@ -76,7 +76,9 @@ def calculate_abc(method_node)
def label_for(node)
# A default case is deliberately omitted since I know of no way this
# could fail and want it to fail fast.
node.detect {|x| [:@ident, :@op].include?(x[0]) }[1]
node.detect {|x|
[:@ident, :@op, :@kw, :@const, :@backtick].include?(x[0])
}[1]
end

def count_nodes(node, types)
Expand Down
54 changes: 54 additions & 0 deletions spec/abc_check_spec.rb
Expand Up @@ -70,4 +70,58 @@ def self.complex_method(a)
violations[0].should be_instance_of(Cane::AbcMaxViolation)
violations[0].columns.should == [file_name, "Harness > complex_method", 2]
end

it 'creates an AbcMaxViolation for methods named after keywords' do
# Seen in the wild in actionpack:
# lib/action_controller/vendor/html-scanner/html/tokenizer.rb
file_name = make_file(<<-RUBY)
class Harness
def next(a)
b = a
return b if b > 3
end
end
RUBY

violations = described_class.new(files: file_name, max: 1).violations
violations.length.should == 1
violations[0].should be_instance_of(Cane::AbcMaxViolation)
violations[0].columns.should == [file_name, "Harness > next", 2]
end

it 'creates an AbcMaxViolation for methods named after constants' do
# Seen in the wild in actionpack:
# lib/action_dispatch/http/request.rb
file_name = make_file(<<-RUBY)
class Harness
def GET(a)
b = a
return b if b > 3
end
end
RUBY

violations = described_class.new(files: file_name, max: 1).violations
violations.length.should == 1
violations[0].should be_instance_of(Cane::AbcMaxViolation)
violations[0].columns.should == [file_name, "Harness > GET", 2]
end

it 'creates an AbcMaxViolation for backtick override' do
# Seen in the wild in actionpack:
# lib/active_support/core_ext/kernel/agnostics.rb
file_name = make_file(<<-RUBY)
class Harness
def `(a)
b = a
return b if b > 3
end
end
RUBY

violations = described_class.new(files: file_name, max: 1).violations
violations.length.should == 1
violations[0].should be_instance_of(Cane::AbcMaxViolation)
violations[0].columns.should == [file_name, "Harness > `", 2]
end
end

0 comments on commit ef4e094

Please sign in to comment.