Class: Assert::Stub

Inherits:
Object
  • Object
show all
Defined in:
lib/assert/stub.rb

Defined Under Namespace

Modules: ParameterList

Constant Summary collapse

NullStub =
Class.new do
  def teardown; end # no-op
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, method_name, orig_caller = nil, &block) ⇒ Stub

Returns a new instance of Stub.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/assert/stub.rb', line 47

def initialize(object, method_name, orig_caller = nil, &block)
  orig_caller ||= caller
  @metaclass = class << object; self; end
  @method_name = method_name.to_s
  @name = "__assert_stub__#{object.object_id}_#{@method_name}"
  @ivar_name = "@__assert_stub_#{object.object_id}_" \
               "#{@method_name.to_sym.object_id}"

  setup(object, orig_caller)

  @do     = block
  @lookup = {}
end

Instance Attribute Details

#doObject

Returns the value of attribute do.



45
46
47
# File 'lib/assert/stub.rb', line 45

def do
  @do
end

#ivar_nameObject (readonly)

Returns the value of attribute ivar_name.



45
46
47
# File 'lib/assert/stub.rb', line 45

def ivar_name
  @ivar_name
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



45
46
47
# File 'lib/assert/stub.rb', line 45

def method_name
  @method_name
end

#nameObject (readonly)

Returns the value of attribute name.



45
46
47
# File 'lib/assert/stub.rb', line 45

def name
  @name
end

Class Method Details

.key(object, method_name) ⇒ Object



41
42
43
# File 'lib/assert/stub.rb', line 41

def self.key(object, method_name)
  "--#{object.object_id}--#{method_name}--"
end

Instance Method Details

#call(args, orig_caller = nil, &block) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/assert/stub.rb', line 69

def call(args, orig_caller = nil, &block)
  orig_caller ||= caller
  unless arity_matches?(args)
    msg = "arity mismatch on `#{@method_name}`: " \
          "expected #{number_of_args(@method.arity)}, " \
          "called with #{args.size}"
    raise StubArityError.new(msg).tap{ |e| e.set_backtrace(orig_caller) }
  end
  lookup(args, orig_caller).call(*args, &block)
rescue NotStubbedError => exception
  @lookup.rehash
  lookup(args, orig_caller).call(*args, &block)
end

#call_method(args, &block) ⇒ Object



65
66
67
# File 'lib/assert/stub.rb', line 65

def call_method(args, &block)
  @method.call(*args, &block)
end

#inspectObject



101
102
103
104
105
# File 'lib/assert/stub.rb', line 101

def inspect
  "#<#{self.class}:#{'0x0%x' % (object_id << 1)}" \
  " @method_name=#{@method_name.inspect}" \
  ">"
end

#teardownObject



94
95
96
97
98
99
# File 'lib/assert/stub.rb', line 94

def teardown
  @metaclass.send(:undef_method, @method_name)
  Assert.send(:remove_instance_variable, @ivar_name)
  @metaclass.send(:alias_method, @method_name, @name)
  @metaclass.send(:undef_method, @name)
end

#with(*args, &block) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/assert/stub.rb', line 83

def with(*args, &block)
  orig_caller = caller
  unless arity_matches?(args)
    msg = "arity mismatch on `#{@method_name}`: " \
          "expected #{number_of_args(@method.arity)}, " \
          "stubbed with #{args.size}"
    raise StubArityError.new(msg).tap{ |e| e.set_backtrace(orig_caller) }
  end
  @lookup[args] = block
end