test/test_logger.rb in logging-0.6.3 vs test/test_logger.rb in logging-0.7.0
- old
+ new
@@ -1,6 +1,6 @@
-# $Id: test_logger.rb 88 2008-02-08 18:47:36Z tim_pease $
+# $Id: test_logger.rb 97 2008-02-13 00:32:18Z tim_pease $
require File.join(File.dirname(__FILE__), %w[setup])
module TestLogging
@@ -15,41 +15,105 @@
assert_raise(ArgumentError) {::Logging::Logger[:test]}
assert_nothing_raised {::Logging::Logger.new(Object)}
end
def test_add
+ root = ::Logging::Logger[:root]
+ root.level = 'info'
+
+ a1 = SioAppender.new 'a1'
+ a2 = SioAppender.new 'a2'
+ log = ::Logging::Logger.new 'A Logger'
+
+ root.add_appenders a1
+ assert_nil a1.readline
+ assert_nil a2.readline
+
+ log.add(0, 'this should NOT be logged')
+ assert_nil a1.readline
+ assert_nil a2.readline
+
+ log.add(1, 'this should be logged')
+ assert_equal " INFO A Logger : this should be logged\n", a1.readline
+ assert_nil a1.readline
+ assert_nil a2.readline
+
+ log.add(2,[1,2,3,4])
+ assert_equal " WARN A Logger : <Array> #{[1,2,3,4]}\n", a1.readline
+ assert_nil a1.readline
+ assert_nil a2.readline
+
+ log.add_appenders a2
+ log.add(3, 'an error has occurred')
+ assert_equal "ERROR A Logger : an error has occurred\n", a1.readline
+ assert_equal "ERROR A Logger : an error has occurred\n", a2.readline
+ assert_nil a1.readline
+ assert_nil a2.readline
+
+ log.additive = false
+ log.add(3, 'another error has occurred')
+ assert_equal "ERROR A Logger : another error has occurred\n", a2.readline
+ assert_nil a1.readline
+ assert_nil a2.readline
+
+ log.add_appenders a1
+ log.add(4, 'fatal exception')
+ assert_equal "FATAL A Logger : fatal exception\n", a1.readline
+ assert_equal "FATAL A Logger : fatal exception\n", a2.readline
+ assert_nil a1.readline
+ assert_nil a2.readline
+
+
+ log.level = :warn
+ log.add(2) do
+ str = 'a string of data'
+ str
+ end
+ assert_equal " WARN A Logger : a string of data\n", a1.readline
+ assert_equal " WARN A Logger : a string of data\n", a2.readline
+ assert_nil a1.readline
+ assert_nil a2.readline
+
+ log.add(1) do
+ rb_raise(RuntimeError, "this block should not be executed")
+ end
+ assert_nil a1.readline
+ assert_nil a2.readline
+ end
+
+ def test_add_appenders
log = ::Logging::Logger.new 'A'
appenders = lambda {log.instance_variable_get :@appenders}
assert_equal [], appenders[]
- assert_raise(TypeError) {log.add Object.new}
- assert_raise(TypeError) {log.add 'not an appender'}
+ assert_raise(ArgumentError) {log.add_appenders Object.new}
+ assert_raise(ArgumentError) {log.add_appenders 'not an appender'}
a = ::Logging::Appender.new 'test_appender_1'
b = ::Logging::Appender.new 'test_appender_2'
c = ::Logging::Appender.new 'test_appender_3'
- log.add a
+ log.add_appenders a
assert_equal [a], appenders[]
- log.add a
+ log.add_appenders a
assert_equal [a], appenders[]
- log.add b
+ log.add_appenders b
assert_equal [a,b], appenders[]
- log.add c
+ log.add_appenders c
assert_equal [a,b,c], appenders[]
- log.add a, c
+ log.add_appenders a, c
assert_equal [a,b,c], appenders[]
- log.clear
+ log.clear_appenders
assert_equal [], appenders[]
- log.add a, c
+ log.add_appenders a, c
assert_equal [a,c], appenders[]
end
def test_additive
root = ::Logging::Logger.new :root
@@ -88,12 +152,12 @@
log = ::Logging::Logger.new '42'
appenders = lambda {log.instance_variable_get :@appenders}
assert_equal [], appenders[]
- assert_raise(TypeError) {log.appenders = Object.new}
- assert_raise(TypeError) {log.appenders = 'not an appender'}
+ assert_raise(ArgumentError) {log.appenders = Object.new}
+ assert_raise(ArgumentError) {log.appenders = 'not an appender'}
a = ::Logging::Appender.new 'test_appender_1'
b = ::Logging::Appender.new 'test_appender_2'
c = ::Logging::Appender.new 'test_appender_3'
@@ -106,10 +170,15 @@
log.appenders = c, a, b
assert_equal [c,a,b], appenders[]
log.appenders = nil
assert_equal [], appenders[]
+
+ log.appenders = %w[test_appender_1 test_appender_3]
+ assert_equal [a,c], appenders[]
+
+ assert_raise(ArgumentError) {log.appenders = 'unknown'}
end
def test_class_aref
root = ::Logging::Logger[:root]
assert_same root, ::Logging::Logger[:root]
@@ -125,33 +194,33 @@
def test_class_root
root = ::Logging::Logger[:root]
assert_same root, ::Logging::Logger.root
end
- def test_clear
+ def test_clear_appenders
log = ::Logging::Logger.new 'Elliott'
appenders = lambda {log.instance_variable_get :@appenders}
assert_equal [], appenders[]
a = ::Logging::Appender.new 'test_appender_1'
b = ::Logging::Appender.new 'test_appender_2'
c = ::Logging::Appender.new 'test_appender_3'
- log.add a, b, c
+ log.add_appenders a, b, c
assert_equal [a,b,c], appenders[]
- log.clear
+ log.clear_appenders
assert_equal [], appenders[]
end
def test_concat
a1 = SioAppender.new 'a1'
a2 = SioAppender.new 'a2'
log = ::Logging::Logger.new 'A'
- ::Logging::Logger[:root].add a1
+ ::Logging::Logger[:root].add_appenders a1
assert_nil a1.readline
assert_nil a2.readline
log << "this is line one of the log file\n"
assert_equal "this is line one of the log file\n", a1.readline
@@ -163,11 +232,11 @@
assert_equal "this is line two of the log file\n", a1.readline
assert_equal "this is line three of the log file\n", a1.readline
assert_nil a1.readline
assert_nil a2.readline
- log.add a2
+ log.add_appenders a2
log << "this is line four of the log file\n"
assert_equal "this is line four of the log file\n", a1.readline
assert_equal "this is line four of the log file\n", a2.readline
assert_nil a1.readline
assert_nil a2.readline
@@ -176,11 +245,11 @@
log << "this is line five of the log file\n"
assert_equal "this is line five of the log file\n", a2.readline
assert_nil a1.readline
assert_nil a2.readline
- log.add a1
+ log.add_appenders a1
log << "this is line six of the log file\n"
assert_equal "this is line six of the log file\n", a1.readline
assert_equal "this is line six of the log file\n", a2.readline
assert_nil a1.readline
assert_nil a2.readline
@@ -270,11 +339,11 @@
a1 = SioAppender.new 'a1'
a2 = SioAppender.new 'a2'
log = ::Logging::Logger.new 'A Logger'
- root.add a1
+ root.add_appenders a1
assert_nil a1.readline
assert_nil a2.readline
log.debug 'this should NOT be logged'
assert_nil a1.readline
@@ -288,11 +357,11 @@
log.warn [1,2,3,4]
assert_equal " WARN A Logger : <Array> #{[1,2,3,4]}\n", a1.readline
assert_nil a1.readline
assert_nil a2.readline
- log.add a2
+ log.add_appenders a2
log.error 'an error has occurred'
assert_equal "ERROR A Logger : an error has occurred\n", a1.readline
assert_equal "ERROR A Logger : an error has occurred\n", a2.readline
assert_nil a1.readline
assert_nil a2.readline
@@ -301,11 +370,11 @@
log.error 'another error has occurred'
assert_equal "ERROR A Logger : another error has occurred\n", a2.readline
assert_nil a1.readline
assert_nil a2.readline
- log.add a1
+ log.add_appenders a1
log.fatal 'fatal exception'
assert_equal "FATAL A Logger : fatal exception\n", a1.readline
assert_equal "FATAL A Logger : fatal exception\n", a2.readline
assert_nil a1.readline
assert_nil a2.readline
@@ -403,41 +472,41 @@
assert_same logger['A::B::C'], logger['A::B::C::F'].parent
assert_same logger['A::B::C::E'], logger['A::B::C::E::G'].parent
end
- def test_remove
+ def test_remove_appenders
log = ::Logging::Logger['X']
appenders = lambda {log.instance_variable_get :@appenders}
assert_equal [], appenders[]
a = ::Logging::Appender.new 'test_appender_1'
b = ::Logging::Appender.new 'test_appender_2'
c = ::Logging::Appender.new 'test_appender_3'
- log.add a, b, c
+ log.add_appenders a, b, c
assert_equal [a,b,c], appenders[]
- assert_raise(TypeError) {log.remove Object.new}
- assert_raise(TypeError) {log.remove 10}
+ assert_raise(ArgumentError) {log.remove_appenders Object.new}
+ assert_raise(ArgumentError) {log.remove_appenders 10}
- log.remove b
+ log.remove_appenders b
assert_equal [a,c], appenders[]
- log.remove 'test_appender_1'
+ log.remove_appenders 'test_appender_1'
assert_equal [c], appenders[]
- log.remove c
+ log.remove_appenders c
assert_equal [], appenders[]
- log.remove a, b, c
+ log.remove_appenders a, b, c
assert_equal [], appenders[]
- log.add a, b, c
+ log.add_appenders a, b, c
assert_equal [a,b,c], appenders[]
- log.remove a, c
+ log.remove_appenders a, c
assert_equal [b], appenders[]
end
def test_spaceship
logs = %w(