|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'test/unit' |
| 4 | +require_relative '../../lib/debug/reflection' |
| 5 | + |
| 6 | +module DEBUGGER__ |
| 7 | + class ReflectionTest < Test::Unit::TestCase |
| 8 | + def setup |
| 9 | + @sample_object = SampleClass.new(1, 2) |
| 10 | + end |
| 11 | + |
| 12 | + def test_instance_variables_of |
| 13 | + assert_equal [:@a, :@b], Reflection.instance_variables_of(@sample_object) |
| 14 | + end |
| 15 | + |
| 16 | + def test_instance_variables_get |
| 17 | + assert_equal 1, Reflection.instance_variable_get_from(@sample_object, :@a) |
| 18 | + assert_equal 2, Reflection.instance_variable_get_from(@sample_object, :@b) |
| 19 | + end |
| 20 | + |
| 21 | + def test_class_of |
| 22 | + assert_same SampleClass, Reflection.class_of(@sample_object) |
| 23 | + end |
| 24 | + |
| 25 | + def test_singleton_class_of |
| 26 | + expected = class << SampleClass |
| 27 | + self |
| 28 | + end |
| 29 | + |
| 30 | + assert_same expected, Reflection.singleton_class_of(SampleClass) |
| 31 | + end |
| 32 | + |
| 33 | + def test_is_kind_of?() |
| 34 | + assert_true Reflection.is_kind_of?(@sample_object, SampleClass) |
| 35 | + assert_false Reflection.is_kind_of?(@sample_object, Object) |
| 36 | + end |
| 37 | + |
| 38 | + def test_responds_to? |
| 39 | + assert_true Reflection.responds_to?(@sample_object, :a) |
| 40 | + assert_false Reflection.responds_to?(@sample_object, :doesnt_exist) |
| 41 | + |
| 42 | + assert_false Reflection.responds_to?(@sample_object, :sample_private_method) |
| 43 | + assert_false Reflection.responds_to?(@sample_object, :sample_private_method, include_all: false) |
| 44 | + assert_true Reflection.responds_to?(@sample_object, :sample_private_method, include_all: true) |
| 45 | + end |
| 46 | + |
| 47 | + def test_method_of |
| 48 | + assert_equal 1, Reflection.method_of(@sample_object, :a).call |
| 49 | + end |
| 50 | + |
| 51 | + def test_object_id_of |
| 52 | + assert_equal @sample_object.__id__, Reflection.object_id_of(@sample_object) |
| 53 | + end |
| 54 | + |
| 55 | + def test_name_of |
| 56 | + assert_equal "DEBUGGER__::ReflectionTest::SampleClass", Reflection.name_of(SampleClass) |
| 57 | + end |
| 58 | + |
| 59 | + def test_bind_call_backport |
| 60 | + omit_if( |
| 61 | + UnboundMethod.instance_method(:bind_call).source_location.nil?, |
| 62 | + "This Ruby version (#{RUBY_VERSION}) has a native #bind_call implementation, so it doesn't need the backport.", |
| 63 | + ) |
| 64 | + |
| 65 | + puts caller_locations |
| 66 | + original_object = SampleTarget.new("original") |
| 67 | + new_target = SampleTarget.new("new") |
| 68 | + |
| 69 | + m = original_object.method(:sample_method).unbind |
| 70 | + |
| 71 | + rest_args = ["a1", "a2"] |
| 72 | + kwargs = { k1: 1, k2: 2 } |
| 73 | + proc = Proc.new { |x| x } |
| 74 | + |
| 75 | + result = m.bind_call(new_target, "parg1", "parg2", *rest_args, **kwargs, &proc) |
| 76 | + |
| 77 | + assert_equal "new", result.fetch(:self).id |
| 78 | + assert_equal "parg1", result.fetch(:parg1) |
| 79 | + assert_equal "parg2", result.fetch(:parg2) |
| 80 | + assert_equal rest_args, result.fetch(:rest_args) |
| 81 | + assert_equal kwargs, result.fetch(:kwargs) |
| 82 | + assert_same proc, result.fetch(:block) |
| 83 | + end |
| 84 | + |
| 85 | + private |
| 86 | + |
| 87 | + # A class for testing reflection, which doesn't implement all the usual reflection methods being tested. |
| 88 | + class SampleClass < BasicObject |
| 89 | + attr_reader :a, :b |
| 90 | + |
| 91 | + def initialize(a, b) |
| 92 | + @a = a |
| 93 | + @b = b |
| 94 | + end |
| 95 | + |
| 96 | + private |
| 97 | + |
| 98 | + def sample_private_method; end |
| 99 | + |
| 100 | + class << self |
| 101 | + undef_method :method |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + class SampleTarget |
| 106 | + attr_reader :id |
| 107 | + |
| 108 | + def initialize(id) |
| 109 | + @id = id |
| 110 | + end |
| 111 | + |
| 112 | + def sample_method(parg1, parg2, *rest_args, **kwargs, &block) |
| 113 | + { self: self, parg1: parg1, parg2: parg2, rest_args: rest_args, kwargs: kwargs, block: block } |
| 114 | + end |
| 115 | + end |
| 116 | + end |
| 117 | +end |
0 commit comments