From 1a64fde18108bf7a3648808ff65b12d1d9f17dc6 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Thu, 29 May 2014 13:36:31 -0500 Subject: [PATCH 1/2] Use YAML.to_json instead of YAML.dump We're not using any features of YAML outside of dictionaries and lists (hashes and arrays), so why not let our data be parseable by YAML _and_ JSON? --- lib/metric_fu/reporting/result.rb | 2 +- spec/metric_fu/formatter/yaml_spec.rb | 14 ++++++++++++-- spec/metric_fu/reporting/result_spec.rb | 14 ++++++-------- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/metric_fu/reporting/result.rb b/lib/metric_fu/reporting/result.rb index 76ae0238e..4eec47690 100644 --- a/lib/metric_fu/reporting/result.rb +++ b/lib/metric_fu/reporting/result.rb @@ -18,7 +18,7 @@ class Result # A YAML object containing the results of the result generation # process def as_yaml - result_hash.to_yaml + YAML.to_json(result_hash) end def per_file_data diff --git a/spec/metric_fu/formatter/yaml_spec.rb b/spec/metric_fu/formatter/yaml_spec.rb index 5a0f7e7dd..62db8a77a 100644 --- a/spec/metric_fu/formatter/yaml_spec.rb +++ b/spec/metric_fu/formatter/yaml_spec.rb @@ -50,8 +50,18 @@ out = MetricFu::Utility.capture_output { MetricFu::Formatter::YAML.new(output: @output).finish } - expect(out).to include ":#{@metric1}:" - expect(out).to include ":#{@metric2}:" + expected_output = YAML.to_json( + "#{@metric1}" => { + "total_violations" => 0, + "violations" => {}, + }, + "#{@metric2}" => { + "files" => [], + "classes" => [], + "methods" => [], + } + ) + expect(out).to eq(expected_output) end end diff --git a/spec/metric_fu/reporting/result_spec.rb b/spec/metric_fu/reporting/result_spec.rb index 5f213df7a..75e303655 100644 --- a/spec/metric_fu/reporting/result_spec.rb +++ b/spec/metric_fu/reporting/result_spec.rb @@ -16,17 +16,15 @@ describe "#as_yaml" do it "should call #result_hash" do - result_hash = double("result_hash") - expect(result_hash).to receive(:to_yaml) - - expect(@result).to receive(:result_hash).and_return(result_hash) - @result.as_yaml + result_hash = { "hi" => "there" } + @result.result_hash.update(result_hash) + yaml = @result.as_yaml + expected_yaml = '{"hi": "there"}' << "\n" + expect(yaml).to eq(expected_yaml) + expect(JSON.load(yaml)).to eq(result_hash) end end - describe "#result_hash" do - end - describe "#add" do it "should add a passed hash to the result_hash instance variable" do result_type = double("result_type") From 34c85bb10bb0595e6476d5e8137fc1ef2779e30c Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Wed, 16 Jul 2014 21:57:04 -0500 Subject: [PATCH 2/2] Account for Syck being default YAML in 1.9.2 --- lib/metric_fu/reporting/result.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/metric_fu/reporting/result.rb b/lib/metric_fu/reporting/result.rb index 4eec47690..610a1ae32 100644 --- a/lib/metric_fu/reporting/result.rb +++ b/lib/metric_fu/reporting/result.rb @@ -11,6 +11,12 @@ def self.result # # It tracks the results generated by each metric used in this test run. class Result + if RUBY_VERSION == "1.9.2" + # Psych is required for YAML.to_json to work + # In 1.9.2, the default is still Syck + YAML::ENGINE.yamler = "psych" + end + # Renders the result of the result_hash into a yaml serialization # ready for writing out to a file. #