Skip to content
Browse files

Improve public specs for logger. Use Mash for log levels.

Squashed commit of the following:

commit 76f51ca9a82fcc8bac46dc28358bc55447914b84
Author: Michael S. Klishin <michael@novemberain.com>
Date:   Sun Oct 5 12:01:09 2008 +0300

    Clean up logger specs by introducing a custom matcher.

commit ee058ece4d4b1bfba9b61f1dee1d207a24937982
Author: Michael S. Klishin <michael@novemberain.com>
Date:   Sun Oct 5 11:40:02 2008 +0300

    Use Mash for log levels. Add more public specs for logging methods.
  • Loading branch information...
1 parent 41472ca commit da532f4339c026970a941b968630b6ca35c7f0a2 @michaelklishin michaelklishin committed
Showing with 168 additions and 83 deletions.
  1. +2 −3 lib/merb-core/logger.rb
  2. +166 −80 spec/public/logger/logger_spec.rb
View
5 lib/merb-core/logger.rb
@@ -61,14 +61,13 @@ class Logger
# :warn:: A warning
# :info:: generic (useful) information about system operation
# :debug:: low-level information for developers
- Levels =
- {
+ Levels = Mash.new({
:fatal => 7,
:error => 6,
:warn => 4,
:info => 3,
:debug => 0
- } unless const_defined?(:Levels)
+ }) unless const_defined?(:Levels)
@@mutex = {}
View
246 spec/public/logger/logger_spec.rb
@@ -4,17 +4,17 @@
describe Merb do
describe "Command Line Options" do
-
+
it "should allow -l / --log_level to set the log_level" do
pending("How do we spec these?")
end
-
+
it "should allow -L / --log_file to set the log_file" do
pending("How do we spec these?")
- # Run an instance of merb from the command line
+ # Run an instance of merb from the command line
# using system and test if the file was created?
end
-
+
end
end
@@ -28,7 +28,7 @@
logger.send(:initialize, 'a partridge', 'a pear tree', 'a time bomb')
end
end
-
+
describe "#set_log" do
before(:each) do
@@ -48,7 +48,7 @@
Merb::BootLoader::Logger.run
Merb.logger.level.should == 0
end
-
+
it "should set the log level to :error (6) when Merb.environment is production" do
Merb.environment = "production"
Merb::Config.delete(:log_level)
@@ -56,19 +56,24 @@
Merb::BootLoader::Logger.run
Merb.logger.level.should == 4
end
-
+
it "should default the delimiter to ' ~ '" do
Merb.logger.delimiter.should eql(" ~ ")
- end
-
+ end
+
+ it 'allows level value be specified as a String' do
+ Merb::Config[:log_level] = 'warn'
+ Merb.logger = nil
+ Merb.logger.level.should == 4
+ end
end
-
- describe "#flush" do
+
+ describe "#flush" do
it "should immediately return if the buffer is empty" do
Merb::Config[:log_stream] = StringIO.new
Merb.logger = nil
-
+
Merb.logger.flush
Merb::Config[:log_stream].string.should == ""
end
@@ -76,122 +81,203 @@
it "should call the write_method with the stringified contents of the buffer if the buffer is non-empty" do
Merb::Config[:log_stream] = StringIO.new
Merb.logger = nil
-
+
Merb.logger << "a message"
Merb.logger << "another message"
Merb.logger.flush
-
+
Merb::Config[:log_stream].string.should == " ~ a message\n ~ another message\n"
end
end
-
+
# There were close specs here, but the logger isn't an IO anymore, and
# shares a stream with other loggers, so it shouldn't be closing the
# stream.
- describe "level methods" do
+ def set_level(level)
+ Merb::Config[:log_level] = level
+ Merb.logger = nil
+ end
- def set_level(level)
- Merb::Config[:log_level] = level
- Merb.logger = nil
+ # Spec examples below all use log_with_method
+ # matcher that is defined right here.
+ Spec::Matchers.create(:log_with_method) do
+ # logger is received, method is matcher argument
+ # So if you call Merb.logger.should log_with_method(:info),
+ # logger has value of Merb.logger and method has value of :info.
+ matches do |logger, method|
+ logger.send(method, "message")
+ logger.flush
+
+ logger.log.string == " ~ message\n"
+ end
+
+ message do |logger, method|
+ "Expected #{logger} NOT to log with method #{method}, but it did."
end
+ failure_message do |logger, method|
+ "Expected #{logger} to log with method #{method}, but it did not."
+ end
+ end
+
+ describe "#debug" do
before(:each) do
@stream = Merb::Config[:log_stream] = StringIO.new
end
- it "should provide a #debug method which adds to the buffer in level :debug" do
+ it "adds to the buffer with debug level" do
set_level(:debug)
- Merb.logger.debug("message")
- Merb.logger.flush
- @stream.string.should == " ~ message\n"
+ Merb.logger.should log_with_method(:debug)
end
- it "should provide a #debug method which does not add to the buffer " \
- "in level :info or higher" do
+ it "does not add to the buffer with info level" do
set_level(:info)
- Merb.logger.debug("message")
- Merb.logger.flush
- @stream.string.should == ""
- end
-
- it "should provide an #info method which adds to the buffer in " \
- "level :info or below" do
- set_level(:info)
- Merb.logger.info("message")
- Merb.logger.flush
- @stream.string.should == " ~ message\n"
+ Merb.logger.should_not log_with_method(:debug)
end
- it "should provide a #info method which does not add to the buffer " \
- "in level :warn or higher" do
+ it "does not add to the buffer with warn level" do
set_level(:warn)
- Merb.logger.info("message")
- Merb.logger.flush
- @stream.string.should == ""
+ Merb.logger.should_not log_with_method(:debug)
end
- it "should provide a #warn method which adds to the buffer in " \
- "level :warn or below" do
+ it "does not add to the buffer with error level" do
+ set_level(:error)
+ Merb.logger.should_not log_with_method(:debug)
+ end
+
+ it "does not add to the buffer with fatal level" do
+ set_level(:fatal)
+ Merb.logger.should_not log_with_method(:debug)
+ end
+ end # #debug
+
+
+ describe "#info" do
+ before(:each) do
+ @stream = Merb::Config[:log_stream] = StringIO.new
+ end
+
+ it "adds to the buffer with debug level" do
+ set_level(:debug)
+ Merb.logger.should log_with_method(:info)
+ end
+
+ it "adds to the buffer with info level" do
+ set_level(:info)
+ Merb.logger.should log_with_method(:info)
+ end
+
+ it "does not add to the buffer with warn level" do
set_level(:warn)
- Merb.logger.warn("message")
- Merb.logger.flush
- @stream.string.should == " ~ message\n"
+ Merb.logger.should_not log_with_method(:info)
end
- it "should provide a #warn method which does not add to the buffer " \
- "in level :error or higher" do
+ it "does not add to the buffer with error level" do
set_level(:error)
- Merb.logger.warn("message")
- Merb.logger.flush
- @stream.string.should == ""
+ Merb.logger.should_not log_with_method(:info)
end
- it "should provide a #error method which adds to the buffer in " \
- "level :error or below" do
+ it "does not add to the buffer with fatal level" do
+ set_level(:fatal)
+ Merb.logger.should_not log_with_method(:info)
+ end
+ end # #info
+
+
+ describe "#warn" do
+ before(:each) do
+ @stream = Merb::Config[:log_stream] = StringIO.new
+ end
+
+ it "adds to the buffer with debug level" do
+ set_level(:debug)
+ Merb.logger.should log_with_method(:warn)
+ end
+
+ it "adds to the buffer with info level" do
+ set_level(:info)
+ Merb.logger.should log_with_method(:warn)
+ end
+
+ it "adds to the buffer with warn level" do
+ set_level(:warn)
+ Merb.logger.should log_with_method(:warn)
+ end
+
+ it "does not add to the buffer with error level" do
set_level(:error)
- Merb.logger.error("message")
- Merb.logger.flush
- @stream.string.should == " ~ message\n"
+ Merb.logger.should_not log_with_method(:warn)
end
- it "should provide a #error method which does not add to the buffer " \
- "in level :fatal or higher" do
+ it "does not add to the buffer with fatal level" do
set_level(:fatal)
- Merb.logger.error("message")
- Merb.logger.flush
- @stream.string.should == ""
+ Merb.logger.should_not log_with_method(:warn)
end
+ end # #warn
- it "should provide a #fatal method which always logs" do
- set_level(:fatal)
- Merb.logger.fatal("message")
- Merb.logger.flush
- @stream.string.should == " ~ message\n"
+
+ describe "#error" do
+ before(:each) do
+ @stream = Merb::Config[:log_stream] = StringIO.new
+ end
+
+ it "adds to the buffer with debug level" do
+ set_level(:debug)
+ Merb.logger.should log_with_method(:error)
end
-
- # TODO: add positive and negative tests for each of the methods below:
- it "should provide a #debug? method" do
- Merb.logger.should respond_to(:debug?)
+
+ it "adds to the buffer with info level" do
+ set_level(:info)
+ Merb.logger.should log_with_method(:error)
end
- it "should provide a #info? method" do
- Merb.logger.should respond_to(:info?)
+ it "adds to the buffer with warn level" do
+ set_level(:warn)
+ Merb.logger.should log_with_method(:error)
end
- it "should provide a #warn? method" do
- Merb.logger.should respond_to(:warn?)
+ it "adds to the buffer with error level" do
+ set_level(:error)
+ Merb.logger.should log_with_method(:error)
end
- it "should provide a #error? method" do
- Merb.logger.should respond_to(:error?)
+ it "does not add to the buffer with fatal level" do
+ set_level(:fatal)
+ Merb.logger.should_not log_with_method(:error)
end
+ end # #error
- it "should provide a #fatal? method" do
- Merb.logger.should respond_to(:fatal?)
+
+ describe "#fatal" do
+ before(:each) do
+ @stream = Merb::Config[:log_stream] = StringIO.new
end
- end
+ it "adds to the buffer with debug level" do
+ set_level(:debug)
+ Merb.logger.should log_with_method(:fatal)
+ end
-end
+ it "adds to the buffer with info level" do
+ set_level(:info)
+ Merb.logger.should log_with_method(:fatal)
+ end
+
+ it "adds to the buffer with warn level" do
+ set_level(:warn)
+ Merb.logger.should log_with_method(:fatal)
+ end
+
+ it "adds to the buffer with error level" do
+ set_level(:error)
+ Merb.logger.should log_with_method(:fatal)
+ end
+
+ it "adds to the buffer with fatal level" do
+ set_level(:fatal)
+ Merb.logger.should log_with_method(:fatal)
+ end
+ end # #fatal
+end # Merb::Logger

0 comments on commit da532f4

Please sign in to comment.
Something went wrong with that request. Please try again.