Module: RBS::Test::Hook
- Defined in:
- lib/rbs/test/hook.rb
Constant Summary collapse
- OPERATORS =
{ :== => "eqeq", :=== => "eqeqeq", :+ => "plus", :- => "minus", :* => "star", :/ => "slash", :> => "gt", :>= => "gteq", :< => "lt", :<= => "lteq", :<=> => "ufo", :& => "amp", :| => "vbar", :^ => "hat", :! => "not", :<< => "lshift", :>> => "rshift", :~ => "tilda" }
Class Method Summary collapse
- .alias_names(target) ⇒ Object
- .hook_instance_method(klass, method, key:) ⇒ Object
- .hook_method_source(prefix, method_name, key) ⇒ Object
- .hook_singleton_method(klass, method, key:) ⇒ Object
- .setup_alias_method_chain(klass, target) ⇒ Object
Class Method Details
.alias_names(target) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rbs/test/hook.rb', line 27 def self.alias_names(target) case target when *OPERATORS.keys name = OPERATORS[target] [ "#{name}____with__#{Test.suffix}", "#{name}____without__#{Test.suffix}" ] else aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1 [ "#{aliased_target}__with__#{Test.suffix}#{punctuation}", "#{aliased_target}__without__#{Test.suffix}#{punctuation}" ] end end |
.hook_instance_method(klass, method, key:) ⇒ Object
153 154 155 156 157 158 |
# File 'lib/rbs/test/hook.rb', line 153 def self.hook_instance_method(klass, method, key:) line, source = hook_method_source("#{klass}#", method, key) klass.module_eval(source, __FILE__, line) setup_alias_method_chain klass, method end |
.hook_method_source(prefix, method_name, key) ⇒ Object
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 |
# File 'lib/rbs/test/hook.rb', line 65 def self.hook_method_source(prefix, method_name, key) with_name, without_name = alias_names(method_name) full_method_name = "#{prefix}#{method_name}" [__LINE__ + 1, <<RUBY] def #{with_name}(*args) ::RBS.logger.debug { "#{full_method_name} with arguments: [" + args.map(&:inspect).join(", ") + "]" } begin return_from_call = false block_calls = [] if block_given? result = __send__(:"#{without_name}", *args) do |*block_args| return_from_block = false begin block_result = yield(*block_args) return_from_block = true ensure exn = $! case when return_from_block # Returned from yield block_calls << ::RBS::Test::ArgumentsReturn.return( arguments: block_args, value: block_result ) when exn # Exception block_calls << ::RBS::Test::ArgumentsReturn.exception( arguments: block_args, exception: exn ) else # break? block_calls << ::RBS::Test::ArgumentsReturn.break( arguments: block_args ) end end block_result end else result = __send__(:"#{without_name}", *args) end return_from_call = true result ensure exn = $! case when return_from_call ::RBS.logger.debug { "#{full_method_name} return with value: " + result.inspect } method_call = ::RBS::Test::ArgumentsReturn.return( arguments: args, value: result ) when exn ::RBS.logger.debug { "#{full_method_name} exit with exception: " + exn.inspect } method_call = ::RBS::Test::ArgumentsReturn.exception( arguments: args, exception: exn ) else ::RBS.logger.debug { "#{full_method_name} exit with jump" } method_call = ::RBS::Test::ArgumentsReturn.break(arguments: args) end trace = ::RBS::Test::CallTrace.new( method_name: #{method_name.inspect}, method_call: method_call, block_calls: block_calls, block_given: block_given?, ) ::RBS::Test::Observer.notify(#{key.inspect}, self, trace) end result end ruby2_keywords :#{with_name} RUBY end |
.hook_singleton_method(klass, method, key:) ⇒ Object
160 161 162 163 164 165 |
# File 'lib/rbs/test/hook.rb', line 160 def self.hook_singleton_method(klass, method, key:) line, source = hook_method_source("#{klass}.",method, key) klass.singleton_class.module_eval(source, __FILE__, line) setup_alias_method_chain klass.singleton_class, method end |
.setup_alias_method_chain(klass, target) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rbs/test/hook.rb', line 45 def self.setup_alias_method_chain(klass, target) with_method, without_method = alias_names(target) RBS.logger.debug "alias name: #{target}, #{with_method}, #{without_method}" klass.instance_eval do alias_method without_method, target alias_method target, with_method case when public_method_defined?(without_method) public target when protected_method_defined?(without_method) protected target when private_method_defined?(without_method) private target end end end |