Skip to content

Commit

Permalink
Utils#deprecate only prints each warning once, and has working specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
arsduo committed Oct 5, 2011
1 parent b42d2df commit eab2baf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
11 changes: 7 additions & 4 deletions lib/koala/utils.rb
@@ -1,10 +1,13 @@
module Koala
module Utils

DEPRECATION_PREFIX = "KOALA: Deprecation warning: "
def self.deprecate(message)
begin
send(:warn, "KOALA: Deprecation warning: #{message}")
rescue Exception => err
puts "Unable to issue Koala deprecation warning! #{err.message}"
@posted_deprecations ||= []
unless @posted_deprecations.include?(message)
# only include each message once
Kernel.warn("#{DEPRECATION_PREFIX}#{message}")
@posted_deprecations << message
end
end
end
Expand Down
34 changes: 29 additions & 5 deletions spec/cases/utils_spec.rb
@@ -1,10 +1,34 @@
require 'spec_helper'

describe Koala::Utils do
it "has a deprecate method" do
Koala::Utils.should respond_to(:deprecate)
end
describe "#deprecate" do
before :each do
# unstub deprecate so we can test it
Koala::Utils.unstub(:deprecate)
end

it "has a deprecation prefix that includes the words Koala and deprecation" do
Koala::Utils::DEPRECATION_PREFIX.should =~ /koala/i
Koala::Utils::DEPRECATION_PREFIX.should =~ /deprecation/i
end

it "prints a warning with Kernel.warn" do
message = Time.now.to_s + rand.to_s
Kernel.should_receive(:warn)
Koala::Utils.deprecate(message)
end

# AFAIK there's no way to test that (Kernel.)warn receives the text
# Kernel.should_receive(:warn) doesn't seem to work, even though the text gets printed
it "prints the deprecation prefix and the warning" do
message = Time.now.to_s + rand.to_s
Kernel.should_receive(:warn).with(Koala::Utils::DEPRECATION_PREFIX + message)
Koala::Utils.deprecate(message)
end

it "only prints each unique message once" do
message = Time.now.to_s + rand.to_s
Kernel.should_receive(:warn).once
Koala::Utils.deprecate(message)
Koala::Utils.deprecate(message)
end
end
end

0 comments on commit eab2baf

Please sign in to comment.