Permalink
Browse files

including tests, version 1.1 bump

  • Loading branch information...
1 parent e64d244 commit 9e1213c65ee10c83cbc3192a41b85fa5be2f0ef7 Rob Hurring committed Mar 10, 2011
Showing with 81 additions and 2 deletions.
  1. +3 −2 lib/class_logger.rb
  2. +78 −0 test/test_class_logger.rb
View
@@ -1,7 +1,7 @@
require 'logger'
module ClassLogger
- VERSION = '1.0'
+ VERSION = '1.1'
def self.included(base)
base.extend DSL
@@ -27,7 +27,7 @@ def has_logger(options = {})
module ClassMethods
def loggers
- @@loggers ||= {}
+ @loggers ||= {}
end
def setup_logger(options)
@@ -42,6 +42,7 @@ def setup_logger(options)
end
file_path = File.join(options[:path], options[:file]).to_s % interpolations
+
if (rotate = options[:rotate])
_logger = ::Logger.new(file_path, rotate)
else
View
@@ -0,0 +1,78 @@
+require File.expand_path('../../lib/class_logger', __FILE__)
+require 'fileutils'
+require 'test/unit'
+
+$logpath = File.expand_path('../logs', __FILE__)
+Dir[File.join($logpath, '*')].each{ |f| File.delete f }
+
+def log_includes(filename, regexp)
+ IO.read(filename) =~ regexp
+end
+
+module TestModule
+ include ClassLogger
+ has_logger :path => $logpath
+ has_logger :in => File.join($logpath, 'module_named.log'), :as => :named
+ has_logger :path => $logpath, :file => "module_file.log", :as => :file
+ has_logger :in => "#{$logpath}/module_formatted.log", :as => :formatted,
+ :formatter => proc{ |s,t,p,m| "FORMATTED-%s: %s" % [s,m] }
+end
+
+class TestClass
+ include ClassLogger
+ has_logger :path => $logpath
+ has_logger :in => "#{$logpath}/class_named.log", :as => :named
+ has_logger :in => "#{$logpath}/level.log", :as => :level, :level => Logger::ERROR
+end
+
+class ModuleLoggerTest < Test::Unit::TestCase
+ def test_log_creation
+ assert File.exists?(File.join($logpath, TestModule.to_s.downcase+'.log')), '[Default] log wasnt created in the proper place'
+ assert File.exists?(File.join($logpath, 'module_named.log')), '[Named] log wasnt created in the proper place'
+ assert File.exists?(File.join($logpath, 'module_file.log')), '[File1] log wasnt created in the proper place'
+ assert File.exists?(File.join($logpath, 'module_formatted.log')), '[Formatted] log wasnt created in the proper place'
+ end
+
+ def test_default_logger
+ assert TestModule.logger.info("Hello")
+ assert log_includes(File.join($logpath,TestModule.to_s.downcase+'.log'), /Hello/)
+ end
+
+ def test_named_logger
+ assert TestModule.named.info("Named!")
+ assert log_includes(File.join($logpath,'module_named.log'), /Named/)
+ end
+
+ def test_formatted_logger
+ assert TestModule.formatted.info("Am i formatted?")
+ assert log_includes(File.join($logpath,'module_formatted.log'), /FORMATTED-INFO: Am i formatted\?/)
+ end
+end
+
+class ClassLoggerTest < Test::Unit::TestCase
+ def setup
+ @tc = TestClass.new
+ end
+
+ def test_log_creation
+ assert File.exists?(File.join($logpath, TestClass.to_s.downcase+'.log')), '[Default] log wasnt created in the proper place'
+ end
+
+ def test_default_logger
+ assert TestClass.logger.info("Hello")
+ assert @tc.logger.info("Instanced!")
+ assert log_includes(File.join($logpath,TestClass.to_s.downcase+'.log'), /Hello/)
+ end
+
+ def test_named_logger
+ assert @tc.named.info("Named")
+ assert TestClass.named.info("Named")
+ end
+
+ def test_level
+ @tc.level.info "Info"
+ @tc.level.error "Error"
+ assert !log_includes(File.join($logpath, 'level.log'), /Info/)
+ assert log_includes(File.join($logpath, 'level.log'), /Error/)
+ end
+end

0 comments on commit 9e1213c

Please sign in to comment.