require 'test/unit' require 'stringio' require 'eymiha/util/easylog' include Eymiha class TC_log < Test::Unit::TestCase def teardown finish_logging end def test_debug log = StringIO.new start_logging log log_debug "logging" assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* DEBUG logging \(TC_log\)/ =~ log.string) end def test_level_debug log = StringIO.new start_logging log log_level_fatal log_debug "logging" assert("" == log.string) log_level_error log_debug "logging" assert("" == log.string) log_level_warn log_debug "logging" assert("" == log.string) log_level_info log_debug "logging" assert("" == log.string) log_level_debug log_debug "logging" assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* DEBUG logging \(TC_log\)/ =~ log.string) end def test_level_debug? start_logging StringIO.new log_level_debug assert(log_fatal? == true) assert(log_error? == true) assert(log_warn? == true) assert(log_info? == true) assert(log_debug? == true) end def test_info log = StringIO.new start_logging log log_info "logging" assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* INFO logging \(TC_log\)/ =~ log.string) end def test_level_info log = StringIO.new start_logging log log_level_fatal log_info "logging" assert("" == log.string) log_level_error log_info "logging" assert("" == log.string) log_level_warn log_info "logging" assert("" == log.string) log_level_info log_info "logging" assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* INFO logging \(TC_log\)/ =~ log.string) debug_log = StringIO.new start_logging debug_log log_level_debug log_info "logging" assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* INFO logging \(TC_log\)/ =~ debug_log.string) end def test_level_info? start_logging StringIO.new log_level_info assert(log_fatal? == true) assert(log_error? == true) assert(log_warn? == true) assert(log_info? == true) assert(log_debug? == false) end def test_warn log = StringIO.new start_logging log log_warn "logging" assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* WARN logging \(TC_log\)/ =~ log.string) end def test_level_warn log = StringIO.new start_logging log log_level_fatal log_warn "logging" assert("" == log.string) log_level_error log_warn "logging" assert("" == log.string) log_level_warn log_warn "logging" assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* WARN logging \(TC_log\)/ =~ log.string) info_log = StringIO.new start_logging info_log log_level_info log_warn "logging" assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* WARN logging \(TC_log\)/ =~ info_log.string) debug_log = StringIO.new start_logging debug_log log_level_debug log_warn "logging" assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* WARN logging \(TC_log\)/ =~ debug_log.string) end def test_level_warn? start_logging StringIO.new log_level_warn assert(log_fatal? == true) assert(log_error? == true) assert(log_warn? == true) assert(log_info? == false) assert(log_debug? == false) end def test_error log = StringIO.new start_logging log log_error "logging" assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* ERROR logging \(TC_log\)/ =~ log.string) end def test_level_error log = StringIO.new start_logging log log_level_fatal log_error "logging" assert("" == log.string) log_level_error log_error "logging" assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* ERROR logging \(TC_log\)/ =~ log.string) warn_log = StringIO.new start_logging warn_log log_level_warn log_error "logging" assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* ERROR logging \(TC_log\)/ =~ warn_log.string) info_log = StringIO.new start_logging info_log log_level_info log_error "logging" assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* ERROR logging \(TC_log\)/ =~ info_log.string) debug_log = StringIO.new start_logging debug_log log_level_debug log_error "logging" assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* ERROR logging \(TC_log\)/ =~ debug_log.string) end def test_level_error? start_logging StringIO.new log_level_error assert(log_fatal? == true) assert(log_error? == true) assert(log_warn? == false) assert(log_info? == false) assert(log_debug? == false) end def test_fatal log = StringIO.new start_logging log log_fatal "logging" assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* FATAL logging \(TC_log\)/ =~ log.string) end def test_level_fatal log = StringIO.new start_logging log log_level_fatal log_fatal "logging" assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* FATAL logging \(TC_log\)/ =~ log.string) error_log = StringIO.new start_logging error_log log_level_error log_fatal "logging" assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* FATAL logging \(TC_log\)/ =~ error_log.string) warn_log = StringIO.new start_logging warn_log log_level_warn log_fatal "logging" assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* FATAL logging \(TC_log\)/ =~ warn_log.string) info_log = StringIO.new start_logging info_log log_level_info log_fatal "logging" assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* FATAL logging \(TC_log\)/ =~ info_log.string) debug_log = StringIO.new start_logging debug_log log_level_debug log_fatal "logging" assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* FATAL logging \(TC_log\)/ =~ debug_log.string) end def test_level_fatal? start_logging StringIO.new log_level_fatal assert(log_fatal? == true) assert(log_error? == false) assert(log_warn? == false) assert(log_info? == false) assert(log_debug? == false) end def test_empty_message log = StringIO.new start_logging log log_info assert("" == log.string.chomp) end def test_nearly_empty_message log = StringIO.new start_logging log log_info(" ") assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* INFO / =~ log.string.chomp) end def test_easylogger_instance log1 = StringIO.new logger1 = EasyLogger.new(log1) log2 = StringIO.new logger2 = EasyLogger.new(log2) logger1.info("once") logger2.info("twice") assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* INFO once/ =~ log1.string) assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* INFO twice/ =~ log2.string) end def test_change_log log1 = StringIO.new log2 = StringIO.new start_logging log1 logger2 = EasyLogger.new(log2) log_info "once" change_easylogger logger2 log_info "twice" assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* INFO once \(TC_log\)/ =~ log1.string) assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* INFO twice \(TC_log\)/ =~ log2.string) end def test_multi_log log1 = StringIO.new log2 = StringIO.new start_logging log1 logger2 = EasyLogger.new(log2) log_info "once" log_info "twice",logger2 assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* INFO once \(TC_log\)/ =~ log1.string) assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* INFO twice \(TC_log\)/ =~ log2.string) end def test_change_classify log = StringIO.new start_logging log easylogger.classify = false log_info "logging" assert(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* INFO logging/ =~ log.string) assert(!(/\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d.\d* INFO logging \(/ =~ log.string)) end end