Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use YAML.to_json instead of YAML.dump #233

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/metric_fu/reporting/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ 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.
#
# @return YAML
# A YAML object containing the results of the result generation
# process
def as_yaml
result_hash.to_yaml
YAML.to_json(result_hash)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bf4 This seems a bit confusing, shouldn't we try to rename the method to be as_json instead?

end

def per_file_data
Expand Down
14 changes: 12 additions & 2 deletions spec/metric_fu/formatter/yaml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 6 additions & 8 deletions spec/metric_fu/reporting/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down