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

[feature]: Using awesome_print for prettier JSON output on failure #30

Open
movstox opened this issue Feb 7, 2015 · 1 comment
Open

Comments

@movstox
Copy link

movstox commented Feb 7, 2015

Hi all and thanks for this beautiful gem,

Do you think it would be useful to enable somekind of mechanism allowing to prettify JSON output in case if matching process fails? If yes, what would be the best way to implement this?
Thanks for your suggestion!

My ugly solution is to include this snippet in test_helper.rb:

module JsonExpressions
  class Matcher
   def json
     @json
   end
  end
end
module MiniTest
  module Assertions
    def mu_pp2(obj, msg = nil)
      ap msg, color: {string: :purpleish} if msg.present?
      ap obj
      ap msg.reverse, color: {string: :purpleish} if msg.present?
    end

    def assert_json_match(exp, act, msg = nil)
      unless JsonExpressions::Matcher === exp
        exp = JsonExpressions::Matcher.new(exp)
      end

      if String === act
        assert act = JSON.parse(act), "Expected #{mu_pp(act)} to be valid JSON"
      end
      assert exp =~ act, ->{ "Expected #{mu_pp2(exp.json, 'Expected---------->')} to match #{mu_pp2(act, 'Actual---------->')}\n" + exp.last_error}

      # Return the matcher
      return exp
    end

    def refute_json_match(exp, act, msg = nil)
      unless JsonExpressions::Matcher === exp
        exp = JsonExpressions::Matcher.new(exp)
      end

      if String === act
        assert act = JSON.parse(act), "Expected #{mu_pp(act)} to be valid JSON"
      end

      refute exp =~ act, ->{ "Expected #{mu_pp2(exp.json, 'Expected')} to match #{mu_pp2(act, 'Actual')}\n" + exp.last_error}
      # Return the matcher
      return exp
    end
  end
end
@movstox movstox changed the title Using awesome_print for prettier JSON output on failure [feature]: Using awesome_print for prettier JSON output on failure Feb 7, 2015
@ethagnawl
Copy link

I'd be happy to take a pass at this and submit a PR if it would be considered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants