a.rb
#!/usr/bin/env ruby
def main
ARGF.each do |line|
line.match(/expected (.*) to be JSON matching (.*)/) do |m|
e, r = m[1, 2]
r.gsub!(/(At=>)([^"](?:[^,]+|,[^]|,[^:])+)(,:)/) do
"#{$1}\"#{$2}\"#{$3}"
end
r.gsub!(/(\(an instance of String\))/, "\"\\1\"")
r = eval(r)
puts e
puts r.to_json
end
end
end
main
$ rspec aaa_spec.rb:xxx >! out
$ ./a.rb out | jq -s '.[0]' >! b
$ ./a.rb out | jq -s '.[1]' >! c
$ vimdiff b c
I wrote it like this, but in the end I found a partial scratch to identify the cause.
Let's write an article and make a memorial service.
It ended up like this
$ perl -lne 'm/^\s+[+-]/ and do { s/^\s+-/e = {/; s/^\s+\+/r = {/; s/,$/}/; s/(At=>)([^"][^,]+)(, :)/\1"\2"\3/g; s/(=>)(\([^)]+\))(,)/\1"\2"\3/g; s/\(contain exactly /[/g and do { s/([}\]"\d])\)/\1]/g; s/ and /, /g;}; print};BEGIN{print"#!/usr/bin/env ruby\nrequire '"'"'json'"'"'\n"}END{print "puts e.to_json\nputs r.to_json"}'
Recommended Posts