File: inspect.rb

package info (click to toggle)
ruby-mocha 3.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,652 kB
  • sloc: ruby: 12,324; javascript: 499; makefile: 14
file content (73 lines) | stat: -rwxr-xr-x 1,421 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# frozen_string_literal: true

require 'date'

module Mocha
  module Inspect
    module ObjectMethods
      def mocha_inspect
        address = __id__ * 2
        address += 0x100000000 if address < 0
        inspect =~ /#</ ? "#<#{self.class}:0x#{Kernel.format('%<address>x', address: address)}>" : inspect
      end
    end

    module ArrayMethods
      def mocha_inspect(wrapped: true)
        unwrapped = collect(&:mocha_inspect).join(', ')
        wrapped ? "[#{unwrapped}]" : unwrapped
      end
    end

    module HashMethods
      def mocha_inspect
        unwrapped = collect do |key, value|
          case key
          when Symbol
            "#{key}: #{value.mocha_inspect}"
          else
            "#{key.mocha_inspect} => #{value.mocha_inspect}"
          end
        end.join(', ')

        if Hash.ruby2_keywords_hash?(self)
          empty? ? '**{}' : unwrapped
        else
          "{#{unwrapped}}"
        end
      end
    end

    module TimeMethods
      def mocha_inspect
        "#{inspect} (#{to_f} secs)"
      end
    end

    module DateMethods
      def mocha_inspect
        to_s
      end
    end
  end
end

class Object
  include Mocha::Inspect::ObjectMethods
end

class Array
  include Mocha::Inspect::ArrayMethods
end

class Hash
  include Mocha::Inspect::HashMethods
end

class Time
  include Mocha::Inspect::TimeMethods
end

class Date
  include Mocha::Inspect::DateMethods
end