Changeset 188

Show
Ignore:
Timestamp:
02/16/08 07:18:55 (6 months ago)
Author:
stu
Message:

optionally send warnings to a log

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • incubator/reopen/lib/reopen.rb

    r185 r188  
    2323   
    2424  class <<self 
     25    attr_accessor :log 
     26     
    2527    def check_for_collisions(clazz, module_to_include) 
    2628      new_methods = Set.new(module_to_include.instance_methods(false)) 
     
    3234    end 
    3335 
    34     def collision(clazz, module_to_include, method) 
    35       puts "Warning: #{module_to_include} is attempting to redefine #{method}" 
    36       puts "         Originally defined in #{clazz}" 
     36    def warning(clazz, module_to_include, method) 
     37      "Warning: #{module_to_include} is attempting to redefine #{method} originally defined in #{clazz}" 
     38    end 
     39     
     40    def collision(*args) 
     41      log ? log.warn(warning(*args)) : puts(warning(*args)) 
    3742    end 
    3843  end 
  • incubator/reopen/test/test_reopen.rb

    r185 r188  
    4646  end 
    4747 
    48   it "should report collisions to the console (by default)" do 
    49     Reopen.expects(:puts).times(2) 
     48   
     49end 
     50 
     51describe "reopen warnings" do 
     52  before do 
     53    @class = Class.new 
     54  end 
     55   
     56  after do 
     57    Reopen.log = nil 
     58  end 
     59   
     60  it "should format a warning message" do 
     61    Reopen.warning(:a, :b, :c).should == "Warning: b is attempting to redefine c originally defined in a" 
     62  end 
     63   
     64  it "should delegate to a log if specified" do 
     65    log = stub 
     66    log.expects(:warn).returns(:warning) 
     67    Reopen.log = log 
     68    Reopen.expects(:warning).returns(:warning) 
     69    Reopen.expects(:puts).never 
     70    @class.reopen do 
     71      def hello; "hello"; end 
     72    end 
     73    @class.reopen do 
     74      def hello; "hello again"; end 
     75    end 
     76     
     77  end 
     78   
     79  it "should report collision warnings to the console (by default)" do 
     80    Reopen.expects(:warning).returns(:stub_warning) 
     81    Reopen.expects(:puts).with(:stub_warning).times(1) 
    5082    @class.reopen do 
    5183      def hello; "hello"; end 
     
    5789   
    5890end 
     91