Skip to content

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

@movstox

Description

@movstox

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions