Commit 189a96de authored by Robert May's avatar Robert May

Add pretty_generate coverage, skip Oj

Oj's pretty generate capabilities currently can't match the output
of the `json` gem. The `json` gem formats the JSON in a pretty
inconsistent and ugly way, but we have a number of specs that expect
it, and so I've added test coverage to ensure it matches.

Because of this, Oj usage is currently disabled for .pretty_generate.
parent 79045147
......@@ -57,25 +57,14 @@ module Gitlab
# The Oj variant in this looks seriously weird but these are the settings
# needed to emulate the style generated by the JSON gem.
#
# NOTE: This currently ignores Oj, because Oj doesn't generate identical
# formatting, issue: https://github.com/ohler55/oj/issues/608
#
# @param object [Hash, Array, Object] must be hash, array, or an object that responds to .to_h or .to_json
# @param opts [Hash] an options hash with fewer supported settings than .dump
# @return [String]
def pretty_generate(object, opts = {})
if enable_oj?
adapter_generate(
object,
{
indent: " ",
space: " ",
object_nl: "\n",
array_nl: "\n"
}.merge(opts)
).chomp
else
opts = standardize_opts(opts)
::JSON.pretty_generate(object, opts)
end
::JSON.pretty_generate(object, opts)
end
private
......
......@@ -217,7 +217,15 @@ RSpec.describe Gitlab::Json do
describe ".pretty_generate" do
let(:obj) do
{ test: true, "foo.bar" => "baz", is_json: 1, some: [1, 2, 3] }
{
test: true,
"foo.bar" => "baz",
is_json: 1,
some: [1, 2, 3],
more: { test: true },
multi_line_empty_array: [],
multi_line_empty_obj: {}
}
end
it "generates pretty JSON" do
......@@ -230,7 +238,15 @@ RSpec.describe Gitlab::Json do
1,
2,
3
]
],
"more": {
"test": true
},
"multi_line_empty_array": [
],
"multi_line_empty_obj": {
}
}
STR
......@@ -253,7 +269,15 @@ RSpec.describe Gitlab::Json do
1,
2,
3
]
],
"more" : {
"test" : true
},
"multi_line_empty_array" : [
],
"multi_line_empty_obj" : {
}
}
STR
......
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