Best Rr_ruby code snippet using RR.Injections.bind
double_injection.rb
Source:double_injection.rb
1module RR2 module Injections3 # RR::DoubleInjection is the binding of an subject and a method.4 # A double_injection has 0 to many Double objects. Each Double5 # has Argument Expectations and Times called Expectations.6 class DoubleInjection < Injection7 extend(Module.new do8 def find_or_create(subject_class, method_name)9 instances[subject_class][method_name.to_sym] ||= begin10 new(subject_class, method_name.to_sym).bind11 end12 end13 def find_or_create_by_subject(subject, method_name)14 find_or_create(class << subject; self; end, method_name)15 end16 def find(subject_class, method_name)17 instances[subject_class] && instances[subject_class][method_name.to_sym]18 end19 def find_by_subject(subject, method_name)20 find(class << subject; self; end, method_name)21 end22 def exists?(subject_class, method_name)23 !!find(subject_class, method_name)24 end25 def exists_by_subject?(subject, method_name)26 exists?((class << subject; self; end), method_name)27 end28 def dispatch_method(subject,29 subject_class,30 method_name,31 arguments,32 keyword_arguments,33 block)34 subject_eigenclass = (class << subject; self; end)35 if (36 exists?(subject_class, method_name) &&37 ((subject_class == subject_eigenclass) || !subject.is_a?(Class))38 )39 find(subject_class, method_name.to_sym).dispatch_method(subject, arguments, keyword_arguments, block)40 else41 new(subject_class, method_name.to_sym).dispatch_original_method(subject, arguments, keyword_arguments, block)42 end43 end44 def reset45 instances.each do |subject_class, method_double_map|46 SingletonMethodAddedInjection.find(subject_class) && SingletonMethodAddedInjection.find(subject_class).reset47 method_double_map.keys.each do |method_name|48 reset_double(subject_class, method_name)49 end50 Injections::DoubleInjection.instances.delete(subject_class) if Injections::DoubleInjection.instances.has_key?(subject_class)51 end52 end53 def verify(*subjects)54 subject_classes = subjects.empty? ?55 Injections::DoubleInjection.instances.keys :56 subjects.map {|subject| class << subject; self; end}57 subject_classes.each do |subject_class|58 instances.include?(subject_class) &&59 instances[subject_class].keys.each do |method_name|60 verify_double(subject_class, method_name)61 end &&62 instances.delete(subject_class)63 end64 end65 # Verifies the DoubleInjection for the passed in subject and method_name.66 def verify_double(subject_class, method_name)67 Injections::DoubleInjection.find(subject_class, method_name).verify68 ensure69 reset_double subject_class, method_name70 end71 # Resets the DoubleInjection for the passed in subject and method_name.72 def reset_double(subject_class, method_name)73 double_injection = Injections::DoubleInjection.instances[subject_class].delete(method_name)74 double_injection.reset75 Injections::DoubleInjection.instances.delete(subject_class) if Injections::DoubleInjection.instances[subject_class].empty?76 end77 def instances78 @instances ||= HashWithObjectIdKey.new do |hash, subject_class|79 hash.set_with_object_id(subject_class, {})80 end81 end82 end)83 include ClassInstanceMethodDefined84 attr_reader :subject_class, :method_name, :doubles85 MethodArguments = Struct.new(:arguments,86 :keyword_arguments,87 :block)88 def initialize(subject_class, method_name)89 @subject_class = subject_class90 @method_name = method_name.to_sym91 @doubles = []92 @dispatch_method_delegates_to_dispatch_original_method = nil93 end94 # RR::DoubleInjection#register_double adds the passed in Double95 # into this DoubleInjection's list of Double objects.96 def register_double(double)97 @doubles << double98 end99 # RR::DoubleInjection#bind injects a method that acts as a dispatcher100 # that dispatches to the matching Double when the method101 # is called.102 def bind103 if subject_has_method_defined?(method_name)104 if subject_has_original_method?105 bind_method106 else107 bind_method_with_alias108 end109 else110 Injections::MethodMissingInjection.find_or_create(subject_class)111 Injections::SingletonMethodAddedInjection.find_or_create(subject_class)112 bind_method_that_self_destructs_and_delegates_to_method_missing113 end114 self115 end116 BoundObjects = {}117 def bind_method_that_self_destructs_and_delegates_to_method_missing118 id = BoundObjects.size119 BoundObjects[id] = subject_class120 subject_class.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)121 def #{method_name}(*args, &block)122 ::RR::Injections::DoubleInjection::BoundObjects[#{id}].class_eval do123 remove_method(:#{method_name})124 end125 method_missing(:#{method_name}, *args, &block)126 end127 ruby2_keywords(:#{method_name}) if respond_to?(:ruby2_keywords, true)128 RUBY129 self130 end131 def bind_method132 id = BoundObjects.size133 BoundObjects[id] = subject_class134 if KeywordArguments.fully_supported?135 subject_class.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)136 def #{method_name}(*args, **kwargs, &block)137 arguments = MethodArguments.new(args, kwargs, block)138 obj = ::RR::Injections::DoubleInjection::BoundObjects[#{id}]139 ::RR::Injections::DoubleInjection.dispatch_method(140 self,141 obj,142 :#{method_name},143 arguments.arguments,144 arguments.keyword_arguments,145 arguments.block146 )147 end148 RUBY149 else150 subject_class.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)151 def #{method_name}(*args, &block)152 arguments = MethodArguments.new(args, {}, block)153 obj = ::RR::Injections::DoubleInjection::BoundObjects[#{id}]154 ::RR::Injections::DoubleInjection.dispatch_method(155 self,156 obj,157 :#{method_name},158 arguments.arguments,159 arguments.keyword_arguments,160 arguments.block161 )162 end163 ruby2_keywords(:#{method_name}) if respond_to?(:ruby2_keywords, true)164 RUBY165 end166 self167 end168 # RR::DoubleInjection#verify verifies each Double169 # TimesCalledExpectation are met.170 def verify171 @doubles.each do |double|172 double.verify173 end174 end175 # RR::DoubleInjection#reset removes the injected dispatcher method.176 # It binds the original method implementation on the subject177 # if one exists.178 def reset179 if subject_has_original_method?180 subject_class.__send__(:remove_method, method_name)181 subject_class.__send__(:alias_method, method_name, original_method_alias_name)182 subject_class.__send__(:remove_method, original_method_alias_name)183 else184 if subject_has_method_defined?(method_name)185 subject_class.__send__(:remove_method, method_name)186 end187 end188 end189 def dispatch_method(subject, args, kwargs, block)190 if @dispatch_method_delegates_to_dispatch_original_method191 dispatch_original_method(subject, args, kwargs, block)192 else193 dispatch = MethodDispatches::MethodDispatch.new(194 self,195 subject,196 args,197 kwargs,198 block199 )200 dispatch.call201 end202 end203 def dispatch_original_method(subject, args, kwargs, block)204 dispatch = MethodDispatches::MethodDispatch.new(205 self,206 subject,207 args,208 kwargs,209 block210 )211 dispatch.call_original_method212 end213 def subject_has_original_method_missing?214 class_instance_method_defined(subject_class, MethodDispatches::MethodMissingDispatch.original_method_missing_alias_name)215 end216 def original_method_alias_name217 "__rr__original_#{@method_name}"218 end219 def dispatch_method_delegates_to_dispatch_original_method220 @dispatch_method_delegates_to_dispatch_original_method = true221 yield222 ensure223 @dispatch_method_delegates_to_dispatch_original_method = nil224 end225 protected226 def deferred_bind_method227 if respond_to?(method_name) and228 not subject_has_method_defined?(original_method_alias_name)229 bind_method_with_alias230 end231 @performed_deferred_bind = true232 end233 def bind_method_with_alias234 subject_class.__send__(:alias_method, original_method_alias_name, method_name)235 bind_method236 end237 end238 end239end...
bind
Using AI Code Generation
1RR.injections.bind(Foo, :new) do2RR.injections.bind(Foo, :new) do3RR.injections.bind(Foo, :new) do4RR.injections.bind(Bar, :new) do5RR.injections.bind(Foo, :new) do
bind
Using AI Code Generation
1 RR::Injections.bind(1, :to_s, 'one')2 RR::Injections::Double.bind(1, :to_s, 'one')3 RR::Injections::Double.bind(1, :to_s, 'one')4 RR::Injections::Double.bind(1, :to_s, 'one')5 RR::Injections::Double.bind(1, :to_s, 'one')6 RR::Injections::Double.bind(1, :to_s, 'one')
bind
Using AI Code Generation
1RR::Injections.bind(C, :foo, 5)2 bind(C, :foo, 5)3 bind(M, :foo, 5)4 bind(M, :foo, 5)5 bind(M, :foo, 5)
bind
Using AI Code Generation
1RR.injections.bind(Foo, :new) do2RR.injections.bind(Foo, :new) do3RR.injections.bind(Foo, :new) do4RR.injections.bind(Bar, :new) do5RR.injections.bind(Foo, :new) do
bind
Using AI Code Generation
1 RR::Injections.bind(1, :to_s, 'one')2 RR::Injections::Double.bind(1, :to_s, 'one')3 RR::Injections::Double.bind(1, :to_s, 'one')4 RR::Injections::Double.bind(1, :to_s, 'one')5 RR::Injections::Double.bind(1, :to_s, 'one')6 RR::Injections::Double.bind(1, :to_s, 'one')
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!