/usr/lib/ruby/vendor_ruby/rspec/support/object_formatter.rb is in ruby-rspec-support 3.5.0c3e0m0s0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | RSpec::Support.require_rspec_support 'matcher_definition'
module RSpec
module Support
# Provide additional output details beyond what `inspect` provides when
# printing Time, DateTime, or BigDecimal
# @api private
class ObjectFormatter # rubocop:disable Style/ClassLength
ELLIPSIS = "..."
attr_accessor :max_formatted_output_length
# Methods are deferred to a default instance of the class to maintain the interface
# For example, calling ObjectFormatter.format is still possible
def self.default_instance
@default_instance ||= new
end
def self.format(object)
default_instance.format(object)
end
def self.prepare_for_inspection(object)
default_instance.prepare_for_inspection(object)
end
def initialize(max_formatted_output_length=200)
@max_formatted_output_length = max_formatted_output_length
@current_structure_stack = []
end
def format(object)
if max_formatted_output_length.nil?
return prepare_for_inspection(object).inspect
else
formatted_object = prepare_for_inspection(object).inspect
if formatted_object.length < max_formatted_output_length
return formatted_object
else
beginning = formatted_object[0 .. max_formatted_output_length / 2]
ending = formatted_object[-max_formatted_output_length / 2 ..-1]
return beginning + ELLIPSIS + ending
end
end
end
# Prepares the provided object to be formatted by wrapping it as needed
# in something that, when `inspect` is called on it, will produce the
# desired output.
#
# This allows us to apply the desired formatting to hash/array data structures
# at any level of nesting, simply by walking that structure and replacing items
# with custom items that have `inspect` defined to return the desired output
# for that item. Then we can just use `Array#inspect` or `Hash#inspect` to
# format the entire thing.
def prepare_for_inspection(object)
case object
when Array
prepare_array(object)
when Hash
prepare_hash(object)
else
inspector_class = INSPECTOR_CLASSES.find { |inspector| inspector.can_inspect?(object) }
inspector_class.new(object, self)
end
end
def prepare_array(array)
with_entering_structure(array) do
array.map { |element| prepare_element(element) }
end
end
def prepare_hash(input_hash)
with_entering_structure(input_hash) do
input_hash.inject({}) do |output_hash, key_and_value|
key, value = key_and_value.map { |element| prepare_element(element) }
output_hash[key] = value
output_hash
end
end
end
def prepare_element(element)
if recursive_structure?(element)
case element
when Array then InspectableItem.new('[...]')
when Hash then InspectableItem.new('{...}')
else raise # This won't happen
end
else
prepare_for_inspection(element)
end
end
def with_entering_structure(structure)
@current_structure_stack.push(structure)
return_value = yield
@current_structure_stack.pop
return_value
end
def recursive_structure?(object)
@current_structure_stack.any? { |seen_structure| seen_structure.equal?(object) }
end
InspectableItem = Struct.new(:text) do
def inspect
text
end
def pretty_print(pp)
pp.text(text)
end
end
BaseInspector = Struct.new(:object, :formatter) do
def self.can_inspect?(_object)
raise NotImplementedError
end
def inspect
raise NotImplementedError
end
def pretty_print(pp)
pp.text(inspect)
end
end
class TimeInspector < BaseInspector
FORMAT = "%Y-%m-%d %H:%M:%S"
def self.can_inspect?(object)
Time === object
end
if Time.method_defined?(:nsec)
def inspect
object.strftime("#{FORMAT}.#{"%09d" % object.nsec} %z")
end
else # for 1.8.7
def inspect
object.strftime("#{FORMAT}.#{"%06d" % object.usec} %z")
end
end
end
class DateTimeInspector < BaseInspector
FORMAT = "%a, %d %b %Y %H:%M:%S.%N %z"
def self.can_inspect?(object)
defined?(DateTime) && DateTime === object
end
# ActiveSupport sometimes overrides inspect. If `ActiveSupport` is
# defined use a custom format string that includes more time precision.
def inspect
if defined?(ActiveSupport)
object.strftime(FORMAT)
else
object.inspect
end
end
end
class BigDecimalInspector < BaseInspector
def self.can_inspect?(object)
defined?(BigDecimal) && BigDecimal === object
end
def inspect
"#{object.to_s('F')} (#{object.inspect})"
end
end
class DescribableMatcherInspector < BaseInspector
def self.can_inspect?(object)
Support.is_a_matcher?(object) && object.respond_to?(:description)
end
def inspect
object.description
end
end
class UninspectableObjectInspector < BaseInspector
OBJECT_ID_FORMAT = '%#016x'
def self.can_inspect?(object)
object.inspect
false
rescue NoMethodError
true
end
def inspect
"#<#{klass}:#{native_object_id}>"
end
def klass
singleton_class = class << object; self; end
singleton_class.ancestors.find { |ancestor| !ancestor.equal?(singleton_class) }
end
# http://stackoverflow.com/a/2818916
def native_object_id
OBJECT_ID_FORMAT % (object.__id__ << 1)
rescue NoMethodError
# In Ruby 1.9.2, BasicObject responds to none of #__id__, #object_id, #id...
'-'
end
end
class DelegatorInspector < BaseInspector
def self.can_inspect?(object)
defined?(Delegator) && Delegator === object
end
def inspect
"#<#{object.class}(#{formatter.format(object.__getobj__)})>"
end
end
class InspectableObjectInspector < BaseInspector
def self.can_inspect?(object)
object.inspect
true
rescue NoMethodError
false
end
def inspect
object.inspect
end
end
INSPECTOR_CLASSES = [
TimeInspector,
DateTimeInspector,
BigDecimalInspector,
UninspectableObjectInspector,
DescribableMatcherInspector,
DelegatorInspector,
InspectableObjectInspector
]
end
end
end
|