<commit_before>require 'mocha/metaclass'

module Mocha

  class ClassMethod
  
    attr_reader :stubbee, :method
   
    def initialize(stubbee, method)
      @stubbee, @method = stubbee, method
    end
  
    def stub
      hide_original_method
      define_new_method
    end
  
    def unstub
      remove_new_method
      restore_original_method
      stubbee.reset_mocha
    end
    
    def mock
      stubbee.mocha
    end
  
    def hide_original_method
      stubbee.__metaclass__.class_eval "alias_method :#{hidden_method}, :#{method}" if stubbee.__metaclass__.method_defined?(method)
    end
  
    def define_new_method
      stubbee.__metaclass__.class_eval "def #{method}(*args, &block); mocha.method_missing(:#{method}, *args, &block); end"
    end
  
    def remove_new_method
      stubbee.__metaclass__.class_eval "remove_method :#{method}"
    end
  
    def restore_original_method
      stubbee.__metaclass__.class_eval "alias_method :#{method}, :#{hidden_method}; remove_method :#{hidden_method}" if stubbee.__metaclass__.method_defined?(hidden_method)
    end
  
    def hidden_method
      method_name = method.to_s.gsub(/\W/) { |s| "_substituted_character_#{s.respond_to?(:ord) ? s[0].ord : s[0]}_" }
      "__stubba__#{method_name}__stubba__"
    end  
  
    def eql?(other)
      return false unless (other.class == self.class)
      (stubbee == other.stubbee) and (method == other.method)
    end
  
    alias_method :==, :eql?<commit_msg>Fixed mistake in Ruby 1.9 compatibility changeset:206 which led to errors like this one reported by <NAME>... NoMethodError: undefined method `ord\' for 63:Fixnum. Would probably be better to use explicit version checking rather than respond_to? calls, but I'll sort that out later.<commit_after>require 'mocha/metaclass'

module Mocha

  class ClassMethod
  
    attr_reader :stubbee, :method
   
    def initialize(stubbee, method)
      @stubbee, @method = stubbee, method
    end
  
    def stub
      hide_original_method
      define_new_method
    end
  
    def unstub
      remove_new_method
      restore_original_method
      stubbee.reset_mocha
    end
    
    def mock
      stubbee.mocha
    end
  
    def hide_original_method
      stubbee.__metaclass__.class_eval "alias_method :#{hidden_method}, :#{method}" if stubbee.__metaclass__.method_defined?(method)
    end
  
    def define_new_method
      stubbee.__metaclass__.class_eval "def #{method}(*args, &block); mocha.method_missing(:#{method}, *args, &block); end"
    end
  
    def remove_new_method
      stubbee.__metaclass__.class_eval "remove_method :#{method}"
    end
  
    def restore_original_method
      stubbee.__metaclass__.class_eval "alias_method :#{method}, :#{hidden_method}; remove_method :#{hidden_method}" if stubbee.__metaclass__.method_defined?(hidden_method)
    end
  
    def hidden_method
      method_name = method.to_s.gsub(/\W/) { |s| "_substituted_character_#{s[0].respond_to?(:ord) ? s[0].ord : s[0]}_" }
      "__stubba__#{method_name}__stubba__"
    end  
  
    def eql?(other)
      return false unless (other.class == self.class)
      (stubbee == other.stubbee) and (method == other.method)
    end
  
    alias_method :==, :eql?
{'fraction_non_alphanumeric': 0.1006857855361596, 'fraction_numerical': 0.003740648379052369, 'mean_word_length': 3.2503311258278145, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3917749', 'n_tokens_mistral': 1052, 'n_tokens_neox': 1085, 'n_words': 228}