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, &block) ⇒ Stub

Returns a new instance of Stub.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/assert/stub.rb', line 37

def initialize(object, method_name, &block)
  @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)

  @do = block || Proc.new do |*args, &block|
    err_msg = "#{inspect_call(args)} not stubbed."
    inspect_lookup_stubs.tap do |stubs|
      err_msg += "\nStubs:\n#{stubs}" if !stubs.empty?
    end
    raise NotStubbedError, err_msg
  end
  @lookup = Hash.new{ |hash, key| self.do }
end

Instance Attribute Details

#doObject

Returns the value of attribute do.



35
36
37
# File 'lib/assert/stub.rb', line 35

def do
  @do
end

#ivar_nameObject (readonly)

Returns the value of attribute ivar_name.



35
36
37
# File 'lib/assert/stub.rb', line 35

def ivar_name
  @ivar_name
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



35
36
37
# File 'lib/assert/stub.rb', line 35

def method_name
  @method_name
end

#nameObject (readonly)

Returns the value of attribute name.



35
36
37
# File 'lib/assert/stub.rb', line 35

def name
  @name
end

Class Method Details

.key(object, method_name) ⇒ Object



31
32
33
# File 'lib/assert/stub.rb', line 31

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

Instance Method Details

#call(*args, &block) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/assert/stub.rb', line 56

def call(*args, &block)
  unless arity_matches?(args)
    message = "arity mismatch on `#{@method_name}`: " \
              "expected #{number_of_args(@method.arity)}, " \
              "called with #{args.size}"
    raise StubArityError, message
  end
  @lookup[args].call(*args, &block)
rescue NotStubbedError => exception
  @lookup.rehash
  @lookup[args].call(*args, &block)
end

#inspectObject



90
91
92
93
94
# File 'lib/assert/stub.rb', line 90

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

#teardownObject



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

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



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

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