@@ -196,6 +196,31 @@ def test_named_members_of_other_objects
196
196
assert_equal expected , @inspector . named_members_of ( point )
197
197
end
198
198
199
+ def test_debug_representation_hook
200
+ object_with_simple_repr = ClassWithCustomDebugRepresentation . new ( { a : 1 , b : 2 } )
201
+
202
+ expected = [
203
+ # We should always show the `#class` when using this hook, even if the
204
+ # debug_representation is a simple value.
205
+ Variable . internal ( name : '#class' , value : ClassWithCustomDebugRepresentation ) ,
206
+ Variable . new ( name : ':a' , value : 1 ) ,
207
+ Variable . new ( name : ':b' , value : 2 ) ,
208
+ ]
209
+
210
+ assert_equal expected , @inspector . named_members_of ( object_with_simple_repr )
211
+
212
+ object_with_complex_repr = ClassWithCustomDebugRepresentation . new ( Point . new ( x : 1 , y : 2 ) )
213
+
214
+ expected = [
215
+ # Make sure we don't add the '#class' twice for non-simple debug representations
216
+ Variable . internal ( name : '#class' , value : ClassWithCustomDebugRepresentation ) ,
217
+ Variable . new ( name : :@x , value : 1 ) ,
218
+ Variable . new ( name : :@y , value : 2 ) ,
219
+ ]
220
+
221
+ assert_equal expected , @inspector . named_members_of ( object_with_complex_repr )
222
+ end
223
+
199
224
private
200
225
201
226
class PointStruct < Struct . new ( :x , :y , keyword_init : true )
@@ -211,5 +236,15 @@ def initialize(x:, y:)
211
236
@y = y
212
237
end
213
238
end
239
+
240
+ class ClassWithCustomDebugRepresentation
241
+ def initialize ( debug_representation )
242
+ @debug_representation = debug_representation
243
+ end
244
+
245
+ def debug_representation
246
+ @debug_representation
247
+ end
248
+ end
214
249
end
215
250
end
0 commit comments