forked from troessner/reek
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b57e2a1
commit 5230f4e
Showing
14 changed files
with
252 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
$:.unshift File.dirname(__FILE__) | ||
|
||
require 'reek/checker' | ||
require 'reek/smells' | ||
require 'set' | ||
|
||
module Reek | ||
|
||
class ObjectRefs | ||
|
||
def initialize | ||
@refs = Hash.new(0) | ||
end | ||
|
||
def record_reference_to_self | ||
record_ref Sexp.from_array([:lit, :self]) | ||
end | ||
|
||
def record_ref(exp) | ||
@refs[exp] += 1 | ||
end | ||
|
||
def refs_to_self | ||
@refs[Sexp.from_array([:lit, :self])] | ||
end | ||
|
||
# TODO | ||
# Should be moved to Hash; but Hash has 58 methods, and there's currently | ||
# no way to turn off that report; which would therefore make the tests fail | ||
def max_keys | ||
max = @refs.values.max or 0 | ||
@refs.keys.select { |key| @refs[key] == max } | ||
end | ||
|
||
def self_is_max? | ||
max_keys.include?(Sexp.from_array([:lit, :self])) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
require File.dirname(__FILE__) + '/../spec_helper.rb' | ||
|
||
require 'reek/object_refs' | ||
|
||
include Reek | ||
|
||
describe ObjectRefs, 'when empty' do | ||
before(:each) do | ||
@refs = ObjectRefs.new | ||
end | ||
|
||
it 'should report no refs to self' do | ||
@refs.refs_to_self.should == 0 | ||
end | ||
end | ||
|
||
describe ObjectRefs, 'with no refs to self' do | ||
before(:each) do | ||
@refs = ObjectRefs.new | ||
@refs.record_ref('a') | ||
@refs.record_ref('b') | ||
@refs.record_ref('a') | ||
end | ||
|
||
it 'should report no refs to self' do | ||
@refs.refs_to_self.should == 0 | ||
end | ||
|
||
it 'should report :a as the max' do | ||
@refs.max_keys.should == ['a'] | ||
end | ||
|
||
it 'should not report self as the max' do | ||
@refs.self_is_max?.should == false | ||
end | ||
end | ||
|
||
describe ObjectRefs, 'with one ref to self' do | ||
before(:each) do | ||
@refs = ObjectRefs.new | ||
@refs.record_ref('a') | ||
@refs.record_ref('b') | ||
@refs.record_ref('a') | ||
@refs.record_reference_to_self | ||
end | ||
|
||
it 'should report 1 ref to self' do | ||
@refs.refs_to_self.should == 1 | ||
end | ||
|
||
it 'should report :a as the max' do | ||
@refs.max_keys.should == ['a'] | ||
end | ||
|
||
it 'should not report self as the max' do | ||
@refs.self_is_max?.should == false | ||
end | ||
end | ||
|
||
describe ObjectRefs, 'with many refs to self' do | ||
before(:each) do | ||
@refs = ObjectRefs.new | ||
@refs.record_reference_to_self | ||
@refs.record_ref('a') | ||
@refs.record_reference_to_self | ||
@refs.record_ref('b') | ||
@refs.record_ref('a') | ||
@refs.record_reference_to_self | ||
end | ||
|
||
it 'should report all refs to self' do | ||
@refs.refs_to_self.should == 3 | ||
end | ||
|
||
it 'should report self among the max' do | ||
@refs.max_keys.should == [Sexp.from_array([:lit, :self])] | ||
end | ||
|
||
it 'should report self as the max' do | ||
@refs.self_is_max?.should == true | ||
end | ||
end | ||
|
||
describe ObjectRefs, 'when self is not the only max' do | ||
before(:each) do | ||
@refs = ObjectRefs.new | ||
@refs.record_ref('a') | ||
@refs.record_reference_to_self | ||
@refs.record_ref('b') | ||
@refs.record_ref('a') | ||
@refs.record_reference_to_self | ||
end | ||
|
||
it 'should report all refs to self' do | ||
@refs.refs_to_self.should == 2 | ||
end | ||
|
||
it 'should report self among the max' do | ||
@refs.max_keys.should be_include('a') | ||
@refs.max_keys.should be_include(Sexp.from_array([:lit, :self])) | ||
end | ||
|
||
it 'should report self as the max' do | ||
@refs.self_is_max?.should == true | ||
end | ||
end | ||
|
||
describe ObjectRefs, 'when self is not among the max' do | ||
before(:each) do | ||
@refs = ObjectRefs.new | ||
@refs.record_ref('a') | ||
@refs.record_ref('b') | ||
@refs.record_ref('a') | ||
@refs.record_reference_to_self | ||
@refs.record_ref('b') | ||
end | ||
|
||
it 'should report all refs to self' do | ||
@refs.refs_to_self.should == 1 | ||
end | ||
|
||
it 'should not report self among the max' do | ||
@refs.max_keys.should be_include('a') | ||
@refs.max_keys.should be_include('b') | ||
end | ||
|
||
it 'should not report self as the max' do | ||
@refs.self_is_max?.should == false | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.