~ubuntu-branches/ubuntu/oneiric/puppet/oneiric-security

« back to all changes in this revision

Viewing changes to vendor/gems/mocha-0.5.6/lib/mocha/class_method.rb

  • Committer: Bazaar Package Importer
  • Author(s): Micah Anderson
  • Date: 2008-07-26 15:43:45 UTC
  • mfrom: (1.1.8 upstream) (3.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080726154345-c03m49twzxewdwjn
Tags: 0.24.5-2
* Fix puppetlast to work with 0.24.5
* Adjust logcheck to match against new log messages in 0.24.5
* Update standards version to 3.8.0 (no changes)
* Update changelog to reduce length of line to make lintian happy

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'mocha/metaclass'
 
2
 
 
3
module Mocha
 
4
 
 
5
  class ClassMethod
 
6
  
 
7
    attr_reader :stubbee, :method
 
8
   
 
9
    def initialize(stubbee, method)
 
10
      @stubbee, @method = stubbee, method
 
11
    end
 
12
  
 
13
    def stub
 
14
      hide_original_method
 
15
      define_new_method
 
16
    end
 
17
  
 
18
    def unstub
 
19
      remove_new_method
 
20
      restore_original_method
 
21
      stubbee.reset_mocha
 
22
    end
 
23
    
 
24
    def mock
 
25
      stubbee.mocha
 
26
    end
 
27
  
 
28
    def hide_original_method
 
29
      stubbee.__metaclass__.class_eval "alias_method :#{hidden_method}, :#{method}" if stubbee.__metaclass__.method_defined?(method)
 
30
    end
 
31
  
 
32
    def define_new_method
 
33
      stubbee.__metaclass__.class_eval "def #{method}(*args, &block); mocha.method_missing(:#{method}, *args, &block); end"
 
34
    end
 
35
  
 
36
    def remove_new_method
 
37
      stubbee.__metaclass__.class_eval "remove_method :#{method}"
 
38
    end
 
39
  
 
40
    def restore_original_method
 
41
      stubbee.__metaclass__.class_eval "alias_method :#{method}, :#{hidden_method}; remove_method :#{hidden_method}" if stubbee.__metaclass__.method_defined?(hidden_method)
 
42
    end
 
43
  
 
44
    def hidden_method
 
45
      if RUBY_VERSION < '1.9'
 
46
        method_name = method.to_s.gsub(/\W/) { |s| "_substituted_character_#{s[0]}_" }
 
47
      else
 
48
        method_name = method.to_s.gsub(/\W/) { |s| "_substituted_character_#{s.ord}_" }
 
49
      end
 
50
      "__stubba__#{method_name}__stubba__"
 
51
    end  
 
52
  
 
53
    def eql?(other)
 
54
      return false unless (other.class == self.class)
 
55
      (stubbee == other.stubbee) and (method == other.method)
 
56
    end
 
57
  
 
58
    alias_method :==, :eql?
 
59
  
 
60
    def to_s
 
61
      "#{stubbee}.#{method}"
 
62
    end
 
63
 
 
64
  end
 
65
  
 
66
end
 
 
b'\\ No newline at end of file'