Commit 6fbb3cee authored by Coraline Ehmke's avatar Coraline Ehmke

Increment of love

parent 8d901aa8
module Snuffle
class Snuffle::LatentObject
class LatentObject
# def suspicious_methods
# @suspicious_methods ||=
# methods_owned_by_klass.select{ |m| (words_in_method(m.to_s) & potential_objects).size > 0 }
# end
include PoroPlus
DUPLICATE_THRESHOLD = 1
attr_accessor :object_candidate, :source_methods
def self.methods_from(nodes)
Snuffle::Element::MethodDefinition.materialize(nodes.methods)
end
def self.suspicious_methods(nodes, threshold=DUPLICATE_THRESHOLD)
# results = Snuffle::Util::Histogram::from(arrays)
# nodes.methods.map(&:name)
# select{ |m| (words_in_method(m.to_s) & potential_objects).size > 0 }
names = methods_from(nodes).map(&:method_name)
words = names.map.inject({}) do |words, method_name|
name.split('_').each do |word|
words[word] ||= []
words[word] << method_name
end
words
end
method_words = words.keys
dupes = Snuffle::Util::Histogram::from(method_words)
dupes.select{|k,v| v > threshold }
end
def self.potential_objects
@potential_objects ||=
begin
common_words = find_common_words_in(hash_of_words_used_in_methods)
words_used_more_than_twice(common_words)
end
end
private
DUPLICATE_THRESHOLD = 1
def words_used_more_than_twice(hash_of_words = {})
hash_of_words.select{ |k| k.size > 2 }
end
def hash_of_words_used_in_methods
@hash_of_words_used_in_methods ||=
methods_owned_by_klass.inject({}) do |hash, method|
hash[method] = words_in_method(method)
hash
end
end
def find_common_words_in(hash_of_words = hash_of_words_used_in_methods)
count_word_frequency(hash_of_words).select{ |k,v| v > 1 }.keys
def self.from(nodes)
potential_objects_with_methods(nodes).map do |k,v|
new(object_candidate: k, source_methods: v)
end
end
def count_word_frequency(hash_of_words = {})
hash_of_words.values.flatten.inject({}) do |hash, word|
hash[word] ||= 0
hash[word] += 1
hash
end
end
def self.potential_objects_with_methods(nodes, threshold=DUPLICATE_THRESHOLD)
method_candidates = Snuffle::Element::MethodDefinition.materialize(nodes.methods)
extract_candidates(method_candidates).select{|k,v| v.count > threshold }
end
def words_in_method(name)
name.to_s.gsub(/[^a-z\_]/i,'').split('_')
def self.extract_candidates(methods)
methods.map(&:method_name).inject({}) do |words, method_name|
atoms = method_name.split('_')
atoms.each{ |word| words[word] ||= []; words[word] << method_name }
words
end
end
end
\ No newline at end of file
end
......@@ -4,9 +4,35 @@ describe Snuffle::LatentObject do
let(:program_2) { Snuffle::SourceFile.new(path_to_file: "spec/fixtures/latent_object_fixture.rb") }
describe ".suspicious_methods" do
describe ".from" do
it "detects namespaced methods"
let(:results) { Snuffle::LatentObject.from(program_2.nodes) }
it "returns an array of LatentObject instances" do
expect(results.first.class.name).to eq "Snuffle::LatentObject"
end
it "returns instances with object candidates" do
expect(results.first.object_candidate).to eq "user"
end
it "returns instances with source methods" do
expect(results.first.source_methods).to eq ["user_name", "user_address", "user_email"]
end
end
describe ".potential_objects_with_methods" do
let(:results) { Snuffle::LatentObject.potential_objects_with_methods(program_2.nodes) }
it "finds repeated words " do
expect(results.keys).to eq(["user"])
end
it "finds methods that repeated words appear in" do
expect(results['user']).to eq(["user_name", "user_address", "user_email"])
end
end
......
require "spec_helper"
describe Snuffle::Node do
let(:source) { Snuffle::SourceFile.new }
let(:parent) { Snuffle::Node.new(id: 1, parent_id: :root, type: :hash) }
let(:child_1) { Snuffle::Node.new(id: 2, parent_id: 1, type: :hash) }
let(:child_2) { Snuffle::Node.new(id: 3, parent_id: 1, type: :hash) }
describe ".nil" do
it "returns a default object" do
expect(Snuffle::Node.nil.type).to eq(:nil)
end
end
describe "#parent" do
it "returns a node's parent" do
allow(child_2).to receive(:id) { 3 }
expect(Snuffle::Node.by_id(3).first.parent).to eq parent
end
end
describe "#siblings" do
before do
allow(parent).to receive(:id) { 1 }
allow(child_1).to receive(:id) { 2 }
allow(child_2).to receive(:id) { 3 }
end
it "returns other nodes of a node's type" do
expect(Snuffle::Node.by_id(3).first.siblings).to eq [parent, child_1]
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment