Commit 5be3427b authored by Coraline Ehmke's avatar Coraline Ehmke

SourceFile specs

parent 8f24dd78
......@@ -6,7 +6,8 @@ may be extracted from a given class.
## TODO
Ignore data clumps called in "loose" class methods (e.g. attr_accessor)
* Ignore data clumps called in "loose" class methods (e.g. attr_accessor)
* Consider weighting based on match type
## Installation
......@@ -24,18 +25,13 @@ Or install it yourself as:
## Usage
> sp = Snuffle::SourceParser.new("./spec/fixtures/program_2.rb")
> sp.report
=> [
{
:source_file=>"./spec/fixtures/program_2.rb",
:object_candidates=> [
[:city, :postal_code, :state],
[:city, :state],
[:company_name, :customer_name]
]
}
]
$ snuffle check example.rb
+----------------------------+------------+-----------------------------+
| Filename | Host Class | Candidate Object Attributes |
+----------------------------+------------+-----------------------------+
| example.rb | Customer | company_name, customer_name |
+----------------------------+------------+-----------------------------+
## Contributing
......
......@@ -10,8 +10,9 @@ module Snuffle
attr_accessor :path_to_file, :source
def source
@source ||= File.read(self.path_to_file)
def class_name
return @class_name if @class_name
@class_name = find_class(ast) || "?"
end
def nodes
......@@ -22,6 +23,10 @@ module Snuffle
@object_candidates ||= Cohort.from(self.nodes).map(&:values)
end
def source
@source ||= File.read(self.path_to_file)
end
def summary
Summary.new(
source_file: self,
......@@ -32,11 +37,6 @@ module Snuffle
)
end
def class_name
return @class_name if @class_name
@class_name = find_class(ast) || "?"
end
private
def text_at(start_pos, end_pos)
......@@ -49,11 +49,10 @@ module Snuffle
if node.type == :module || node.type == :class
concat << text_at(node.loc.name.begin_pos, node.loc.name.end_pos)
end
if node.type == :class
return concat.flatten.select(&:present?).join('::')
else
unless node.type == :class
concat << node.children.map{|child| find_class(child)}.compact
end
concat.flatten.select(&:present?).join('::')
end
def ast
......
......@@ -6,55 +6,45 @@ describe Snuffle::SourceFile do
let(:program_2) {Snuffle::SourceFile.new(path_to_file: "spec/fixtures/program_2.rb") }
let(:program_3) {Snuffle::SourceFile.new(path_to_file: "spec/fixtures/program_3.rb") }
describe "weighting" do
# attributes are used together as values in a hash
# E.g. [city, state]
context "a hash cohort" do
# Example Case: hash hit with attr_accessor match
# Fixture file contains two instances of city, postal_code, state
# One is args to attr_accessor, one is args hash to method
it "does not match hash values with non-hash values" do
args = ['city', 'postal_code', 'state']
values = program_2.summary.object_candidates
expect(values.include?(args)).to be_falsey
end
it "matches elements with different parents" do
args = ['company_name', 'customer_name']
values = program_3.summary.object_candidates
expect(values.include?(args)).to be_truthy
end
xit "is detected" do
expect(detector.cohorts.map(&:values)).to eq(
[
[:city, :postal_code, :state],
[:city, :state],
[:company_name, :customer_name]
]
)
end
it "is assigned a weight of X"
describe "#object_candidates" do
it "does not match hash values with non-hash values" do
attr_accessor_args = ['city', 'postal_code', 'state']
values = program_2.summary.object_candidates
expect(values.include?(attr_accessor_args)).to be_falsey
end
# attributes accessed together in one or more methods
# E.g. [city, state], [city, state, postal_code]
context "method cohorts" do
it "is detected"
it "is assigned a weight of X"
it "matches elements with the same type" do
args = ['company_name', 'customer_name']
values = program_3.summary.object_candidates
expect(values.include?(args)).to be_truthy
end
# attributes used together in string interpolation
# E.g. [city, city, postal_code]
context "string cohorts" do
it "is detected"
it "is assigned a weight of X"
end
describe "#class_name" do
let(:top_level) { "require 'something'; class Foo; def bar; puts 'hi'; end; end"}
let(:namespaced_1) { "require 'something'; class Foo::Bar; def bar; puts 'hi'; end; end"}
let(:namespaced_2) { "require 'something'; module Foo; module Bar; class Baz; def bar; puts 'hi'; end; end; end; end"}
let(:source_file) { Snuffle::SourceFile.new }
it "picks up a non-nested class name" do
source_file.stub(:source) { top_level }
expect(source_file.class_name).to eq("Foo")
end
it "picks up a namespaced class name" do
source_file.stub(:source) { namespaced_1 }
expect(source_file.class_name).to eq("Foo::Bar")
end
it "picks up a class name inside nested modules" do
source_file.stub(:source) { namespaced_2 }
expect(source_file.class_name).to eq("Foo::Bar::Baz")
end
end
end
\ No newline at end of file
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