Skip to content

Commit

Permalink
chore: format empty arrays over new lines
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed Nov 29, 2024
1 parent 66910bd commit 3c2ae45
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/pact/matchers/unix_diff_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def generate_string diff, target
# Can't think of an elegant way to check if we can pretty generate other than to try it and maybe fail
json = fix_blank_lines_in_empty_hashes JSON.pretty_generate(comparable)
json = add_blank_lines_in_empty_hashes json
json = add_blank_lines_in_empty_arrays json
add_comma_to_end_of_arrays json
rescue JSON::GeneratorError
comparable.to_s
Expand Down
9 changes: 6 additions & 3 deletions lib/pact/shared/jruby_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ def fix_blank_lines_in_empty_hashes json
json.gsub(/({\n)\n(\s*})/,'\1\2')
end

# formatting changes when using json 2.8.x
# in ruby 3.x +
# brought in by faraday 2.12.0
# preserve pre json 2.8.x behaviour
# https://github.com/ruby/json/pull/626
def add_blank_lines_in_empty_hashes(json)
json.gsub(/({\s*})/, "{\n }")
end

def add_blank_lines_in_empty_arrays(json)
json.gsub(/\[\s*\]/, "[\n ]")
end
end
end
1 change: 1 addition & 0 deletions spec/lib/pact/matchers/unix_diff_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ module Matchers
end

it "generates the right number of lines, even with ActiveSupport loaded" do
puts subject
expect(line_count).to eq 7 + key_lines_count
end
end
Expand Down

0 comments on commit 3c2ae45

Please sign in to comment.