Skip to content

Commit

Permalink
Ensure JSON support for e.g. jruby, mri
Browse files Browse the repository at this point in the history
  • Loading branch information
bf4 committed May 22, 2013
1 parent b468572 commit 454b70d
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/metric_fu/initial_requires.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

require 'yaml'
require 'redcard'
require 'multi_json'

MetricFu.configure
MetricFu.logging_require { 'mf_debugger' }
Expand Down
2 changes: 1 addition & 1 deletion lib/metric_fu/metrics/cane/cane_bluff_grapher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def graph!
#{BLUFF_DEFAULT_OPTIONS}
g.title = 'Cane: code quality threshold violations';
g.data('cane', [#{@cane_violations.join(',')}]);
g.labels = #{@labels.to_json};
g.labels = #{MultiJson.dump(@labels)};
g.draw();
EOS
File.open(File.join(MetricFu.output_directory, 'cane.js'), 'w') {|f| f << content }
Expand Down
2 changes: 1 addition & 1 deletion lib/metric_fu/metrics/flay/flay_bluff_grapher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def graph!
#{BLUFF_DEFAULT_OPTIONS}
g.title = 'Flay: duplication';
g.data('flay', [#{@flay_score.join(',')}]);
g.labels = #{@labels.to_json};
g.labels = #{MultiJson.dump(@labels)};
g.draw();
EOS
File.open(File.join(MetricFu.output_directory, 'flay.js'), 'w') {|f| f << content }
Expand Down
2 changes: 1 addition & 1 deletion lib/metric_fu/metrics/flog/flog_bluff_grapher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def graph!
g.title = 'Flog: code complexity';
g.data('average', [#{@flog_average.join(',')}]);
g.data('top 5% average', [#{@top_five_percent_average.join(',')}])
g.labels = #{@labels.to_json};
g.labels = #{MultiJson.dump(@labels)};
g.draw();
EOS
File.open(File.join(MetricFu.output_directory, 'flog.js'), 'w') {|f| f << content }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def graph!
#{BLUFF_DEFAULT_OPTIONS}
g.title = 'Rails Best Practices: design problems';
g.data('rails_best_practices', [#{@rails_best_practices_count.join(',')}]);
g.labels = #{@labels.to_json};
g.labels = #{MultiJson.dump(@labels)};
g.draw();
EOS
File.open(File.join(MetricFu.output_directory, 'rails_best_practices.js'), 'w') {|f| f << content }
Expand Down
2 changes: 1 addition & 1 deletion lib/metric_fu/metrics/rcov/rcov_bluff_grapher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def graph!
#{BLUFF_DEFAULT_OPTIONS}
g.title = 'Rcov: code coverage';
g.data('rcov', [#{@rcov_percent.join(',')}]);
g.labels = #{@labels.to_json};
g.labels = #{MultiJson.dump(@labels)};
g.draw();
EOS
File.open(File.join(MetricFu.output_directory, 'rcov.js'), 'w') {|f| f << content }
Expand Down
2 changes: 1 addition & 1 deletion lib/metric_fu/metrics/reek/reek_bluff_grapher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def graph!
#{BLUFF_DEFAULT_OPTIONS}
g.title = 'Reek: code smells';
#{data}
g.labels = #{@labels.to_json};
g.labels = #{MultiJson.dump(@labels)};
g.draw();
EOS
File.open(File.join(MetricFu.output_directory, 'reek.js'), 'w') {|f| f << content }
Expand Down
2 changes: 1 addition & 1 deletion lib/metric_fu/metrics/roodi/roodi_bluff_grapher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def graph!
#{BLUFF_DEFAULT_OPTIONS}
g.title = 'Roodi: design problems';
g.data('roodi', [#{@roodi_count.join(',')}]);
g.labels = #{@labels.to_json};
g.labels = #{MultiJson.dump(@labels)};
g.draw();
EOS
File.open(File.join(MetricFu.output_directory, 'roodi.js'), 'w') {|f| f << content }
Expand Down
2 changes: 1 addition & 1 deletion lib/metric_fu/metrics/stats/stats_bluff_grapher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def graph!
g.title = 'Stats: LOC & LOT';
g.data('LOC', [#{@loc_counts.join(',')}]);
g.data('LOT', [#{@lot_counts.join(',')}])
g.labels = #{@labels.to_json};
g.labels = #{MultiJson.dump(@labels)};
g.draw();
EOS
File.open(File.join(MetricFu.output_directory, 'stats.js'), 'w') {|f| f << content }
Expand Down
2 changes: 2 additions & 0 deletions metric_fu.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ Gem::Specification.new do |s|
"coderay" => [],
# default graphing libraries
"bluff" => [],
# to_json support
'multi_json' => [],
}.each do |gem, version|
if version == []
s.add_runtime_dependency(gem)
Expand Down
9 changes: 5 additions & 4 deletions spec/metric_fu/metrics/hotspots/hotspots_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'multi_json'
require "spec_helper"

describe Hotspots do
Expand Down Expand Up @@ -69,17 +70,17 @@
analyzer = HotspotAnalyzer.new(@yaml)
hotspots.instance_variable_set(:@analyzer, analyzer)
result = hotspots.analyze
expected = JSON.parse("{\"methods\":[{\"location\":{\"class_name\":\"Client\",\"method_name\":\"Client#client_requested_sync\",\"file_path\":\"lib/client/client.rb\",\"hash\":7919384682,\"simple_method_name\":\"#client_requested_sync\"},\"details\":{\"reek\":\"found 1 code smells\",\"flog\":\"complexity is 37.9\"}}],\"classes\":[{\"location\":{\"class_name\":\"Client\",\"method_name\":null,\"file_path\":\"lib/client/client.rb\",\"hash\":7995629750},\"details\":{\"reek\":\"found 2 code smells\",\"flog\":\"complexity is 37.9\"}}],\"files\":[{\"location\":{\"class_name\":null,\"method_name\":null,\"file_path\":\"lib/client/client.rb\",\"hash\":-5738801681},\"details\":{\"reek\":\"found 2 code smells\",\"flog\":\"complexity is 37.9\",\"churn\":\"detected high level of churn (changed 54 times)\"}},{\"location\":{\"class_name\":null,\"method_name\":null,\"file_path\":\"lib/client/foo.rb\",\"hash\":-7081271905},\"details\":{\"churn\":\"detected high level of churn (changed 52 times)\"}}]}")
compare_hashes(JSON.parse(hotspots.to_h[:hotspots].to_json), expected)
expected = MultiJson.load("{\"methods\":[{\"location\":{\"class_name\":\"Client\",\"method_name\":\"Client#client_requested_sync\",\"file_path\":\"lib/client/client.rb\",\"hash\":7919384682,\"simple_method_name\":\"#client_requested_sync\"},\"details\":{\"reek\":\"found 1 code smells\",\"flog\":\"complexity is 37.9\"}}],\"classes\":[{\"location\":{\"class_name\":\"Client\",\"method_name\":null,\"file_path\":\"lib/client/client.rb\",\"hash\":7995629750},\"details\":{\"reek\":\"found 2 code smells\",\"flog\":\"complexity is 37.9\"}}],\"files\":[{\"location\":{\"class_name\":null,\"method_name\":null,\"file_path\":\"lib/client/client.rb\",\"hash\":-5738801681},\"details\":{\"reek\":\"found 2 code smells\",\"flog\":\"complexity is 37.9\",\"churn\":\"detected high level of churn (changed 54 times)\"}},{\"location\":{\"class_name\":null,\"method_name\":null,\"file_path\":\"lib/client/foo.rb\",\"hash\":-7081271905},\"details\":{\"churn\":\"detected high level of churn (changed 52 times)\"}}]}")
compare_hashes(MultiJson.load(hotspots.to_h[:hotspots].to_json), expected)
end

it "should put the changes into a hash" do
hotspots = MetricFu::Hotspots.new
analyzer = HotspotAnalyzer.new(@yaml)
hotspots.instance_variable_set(:@analyzer, analyzer)
hotspots.analyze
expected = JSON.parse("{\"methods\":[{\"location\":{\"class_name\":\"Client\",\"method_name\":\"Client#client_requested_sync\",\"file_path\":\"lib/client/client.rb\",\"hash\":7919384682,\"simple_method_name\":\"#client_requested_sync\"},\"details\":{\"reek\":\"found 1 code smells\",\"flog\":\"complexity is 37.9\"}}],\"classes\":[{\"location\":{\"class_name\":\"Client\",\"method_name\":null,\"file_path\":\"lib/client/client.rb\",\"hash\":7995629750},\"details\":{\"reek\":\"found 2 code smells\",\"flog\":\"complexity is 37.9\"}}],\"files\":[{\"location\":{\"class_name\":null,\"method_name\":null,\"file_path\":\"lib/client/client.rb\",\"hash\":-5738801681},\"details\":{\"reek\":\"found 2 code smells\",\"flog\":\"complexity is 37.9\",\"churn\":\"detected high level of churn (changed 54 times)\"}},{\"location\":{\"class_name\":null,\"method_name\":null,\"file_path\":\"lib/client/foo.rb\",\"hash\":-7081271905},\"details\":{\"churn\":\"detected high level of churn (changed 52 times)\"}}]}")
compare_hashes(JSON.parse(hotspots.to_h[:hotspots].to_json), expected)
expected = MultiJson.load("{\"methods\":[{\"location\":{\"class_name\":\"Client\",\"method_name\":\"Client#client_requested_sync\",\"file_path\":\"lib/client/client.rb\",\"hash\":7919384682,\"simple_method_name\":\"#client_requested_sync\"},\"details\":{\"reek\":\"found 1 code smells\",\"flog\":\"complexity is 37.9\"}}],\"classes\":[{\"location\":{\"class_name\":\"Client\",\"method_name\":null,\"file_path\":\"lib/client/client.rb\",\"hash\":7995629750},\"details\":{\"reek\":\"found 2 code smells\",\"flog\":\"complexity is 37.9\"}}],\"files\":[{\"location\":{\"class_name\":null,\"method_name\":null,\"file_path\":\"lib/client/client.rb\",\"hash\":-5738801681},\"details\":{\"reek\":\"found 2 code smells\",\"flog\":\"complexity is 37.9\",\"churn\":\"detected high level of churn (changed 54 times)\"}},{\"location\":{\"class_name\":null,\"method_name\":null,\"file_path\":\"lib/client/foo.rb\",\"hash\":-7081271905},\"details\":{\"churn\":\"detected high level of churn (changed 52 times)\"}}]}")
compare_hashes(MultiJson.load(hotspots.to_h[:hotspots].to_json), expected)
end
# really testing the output of analyzed_problems#worst_items
it "should return the worst item granularities: files, classes, methods" do
Expand Down

0 comments on commit 454b70d

Please sign in to comment.