Module: MiniTest::Assertions
- Defined in:
- lib/minitest/pretty_diff.rb
Instance Method Summary collapse
-
#mu_pp(obj) ⇒ Object
Returns a pretty-printed version of
obj
. -
#sort_keys(obj) ⇒ Object
Recursively sort the keys of
obj
, which is typically a hash.
Instance Method Details
#mu_pp(obj) ⇒ Object
Returns a pretty-printed version of obj
.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/minitest/pretty_diff.rb', line 9 def mu_pp(obj) s = if obj.is_a?(Hash) || obj.is_a?(Array) JSON.pretty_generate(sort_keys(obj)) else obj.inspect end s = s.encode(Encoding.default_external) if defined? Encoding s end |
#sort_keys(obj) ⇒ Object
Recursively sort the keys of obj
, which is typically a hash. Recurses into arrays (to allow nested structures), but does not sort them.
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/minitest/pretty_diff.rb', line 24 def sort_keys(obj) if obj.is_a?(Hash) obj.keys.sort.reduce(Hash.new) do |memo, key| memo.merge({ key => sort_keys(obj[key]) }) end elsif obj.is_a?(Array) obj.map do |item| sort_keys(item) end else obj end end |