/
flay_spec.rb
104 lines (90 loc) · 4.38 KB
/
flay_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
require File.dirname(__FILE__) + '/../spec_helper.rb'
describe Flay do
before :each do
MetricFu::Flay.stub!(:verify_dependencies!).and_return(true)
end
describe "emit method" do
before :each do
MetricFu::Configuration.run {|config| config.flay = { :dirs_to_flay => ['app', 'lib'] } }
File.stub!(:directory?).and_return(true)
@flay = MetricFu::Flay.new('base_dir')
end
it "should look at the dirs" do
Dir.should_receive(:[]).with(File.join("app", "**/*.rb")).and_return("path/to/app")
Dir.should_receive(:[]).with(File.join("lib", "**/*.rb")).and_return("path/to/lib")
@flay.should_receive(:`).with("flay path/to/app path/to/lib")
output = @flay.emit
end
end
describe "analyze method" do
before :each do
lines = <<-HERE
Total score (lower is better) = 246
1) IDENTICAL code found in :or (mass*2 = 68)
app/controllers/link_targets_controller.rb:57
app/controllers/primary_sites_controller.rb:138
2) Similar code found in :if (mass = 64)
app/controllers/primary_sites_controller.rb:75
app/controllers/primary_sites_controller.rb:76
app/controllers/primary_sites_controller.rb:88
app/controllers/primary_sites_controller.rb:89
HERE
MetricFu::Configuration.run {}
File.stub!(:directory?).and_return(true)
@flay = MetricFu::Flay.new('base_dir')
@flay.instance_variable_set(:@output, lines)
end
it "should analyze and return matches" do
@flay.analyze.should == [ ["Total score (lower is better) = 246"],
["\n1) IDENTICAL code found in :or (mass*2 = 68)",
"app/controllers/link_targets_controller.rb:57",
"app/controllers/primary_sites_controller.rb:138"],
["2) Similar code found in :if (mass = 64)",
"app/controllers/primary_sites_controller.rb:75",
"app/controllers/primary_sites_controller.rb:76",
"app/controllers/primary_sites_controller.rb:88",
"app/controllers/primary_sites_controller.rb:89"] ]
end
end
describe "to_h method" do
before :each do
lines = [ ["Total score (lower is better) = 284"],
["\n1) IDENTICAL code found in :or (mass*2 = 68)",
"app/controllers/link_targets_controller.rb:57",
"app/controllers/primary_sites_controller.rb:138"],
["2) Similar code found in :if (mass = 64)",
"app/controllers/primary_sites_controller.rb:75",
"app/controllers/primary_sites_controller.rb:76",
"app/controllers/primary_sites_controller.rb:88",
"app/controllers/primary_sites_controller.rb:89"],
["3) Similar code found in :defn (mass = 40)",
"app/controllers/link_targets_controller.rb:40",
"app/controllers/primary_sites_controller.rb:98"],
["4) Similar code found in :defn (mass = 38)",
"app/controllers/link_targets_controller.rb:13",
"app/controllers/primary_sites_controller.rb:50"],
["5) Similar code found in :defn (mass = 38)",
"app/models/primary_site.rb:104",
"app/models/primary_site.rb:109"],
["6) Similar code found in :call (mass = 36)",
"app/controllers/bookmarklet_integration_controller.rb:6",
"app/controllers/bookmarklet_integration_controller.rb:17"]]
MetricFu::Configuration.run {}
File.stub!(:directory?).and_return(true)
flay = MetricFu::Flay.new('base_dir')
flay.instance_variable_set(:@matches, lines)
@results = flay.to_h
end
it "should find the total_score" do
@results[:flay][:total_score].should == '284'
end
it "should have 6 matches" do
@results[:flay][:matches].size.should == 6
end
it "should capture info for match" do
@results[:flay][:matches].first[:reason].should =~ /IDENTICAL/
@results[:flay][:matches].first[:matches].first[:name].should =~ /link_targets_controller/
@results[:flay][:matches].first[:matches].first[:line].should == "57"
end
end
end