2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JSON でdiff をとった

Last updated at Posted at 2020-09-08

JSON matcher を使って rspec を書いてたけど JSON の要素が多くてどこが違うかわからなかった

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

みたいに書いてみたけど,結局見つけたのは部分部分ケズってみて原因を特定したりした.

記事を書いて供養しておこう.

追記

結局こうなった

$ 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"}'
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?