зеркало из https://github.com/github/ruby.git
[ruby/logger] Test cases
https://github.com/ruby/logger/commit/40adb645e2
This commit is contained in:
Родитель
a8b11b5cdd
Коммит
4375546810
|
@ -0,0 +1,35 @@
|
|||
# coding: US-ASCII
|
||||
# frozen_string_literal: false
|
||||
require_relative 'helper'
|
||||
|
||||
class TestFormatter < Test::Unit::TestCase
|
||||
def test_call
|
||||
severity = 'INFO'
|
||||
time = Time.now
|
||||
progname = 'ruby'
|
||||
msg = 'This is a test'
|
||||
formatter = Logger::Formatter.new
|
||||
|
||||
result = formatter.call(severity, time, progname, msg)
|
||||
time_matcher = /\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}/
|
||||
matcher = /#{severity[0..0]}, \[#{time_matcher} #\d+\] #{severity} -- #{progname}: #{msg}\n/
|
||||
|
||||
assert_match(matcher, result)
|
||||
end
|
||||
|
||||
class CustomFormatter < Logger::Formatter
|
||||
def call(time)
|
||||
format_datetime(time)
|
||||
end
|
||||
end
|
||||
|
||||
def test_format_datetime
|
||||
time = Time.now
|
||||
formatter = CustomFormatter.new
|
||||
|
||||
result = formatter.call(time)
|
||||
matcher = /^\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}$/
|
||||
|
||||
assert_match(matcher, result)
|
||||
end
|
||||
end
|
|
@ -13,7 +13,7 @@ class TestLogger < Test::Unit::TestCase
|
|||
class Log
|
||||
attr_reader :label, :datetime, :pid, :severity, :progname, :msg
|
||||
def initialize(line)
|
||||
/\A(\w+), \[([^#]*)#(\d+)\]\s+(\w+) -- (\w*): ([\x0-\xff]*)/ =~ line
|
||||
/\A(\w+), \[([^#]*) #(\d+)\]\s+(\w+) -- (\w*): ([\x0-\xff]*)/ =~ line
|
||||
@label, @datetime, @pid, @severity, @progname, @msg = $1, $2, $3, $4, $5, $6
|
||||
end
|
||||
end
|
||||
|
@ -124,7 +124,7 @@ class TestLogger < Test::Unit::TestCase
|
|||
dummy = STDERR
|
||||
logger = Logger.new(dummy)
|
||||
log = log_add(logger, INFO, "foo")
|
||||
assert_match(/^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\s*\d+ $/, log.datetime)
|
||||
assert_match(/^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\s*\d+$/, log.datetime)
|
||||
logger.datetime_format = "%d%b%Y@%H:%M:%S"
|
||||
log = log_add(logger, INFO, "foo")
|
||||
assert_match(/^\d\d\w\w\w\d\d\d\d@\d\d:\d\d:\d\d$/, log.datetime)
|
||||
|
@ -203,7 +203,7 @@ class TestLogger < Test::Unit::TestCase
|
|||
# default
|
||||
logger = Logger.new(STDERR)
|
||||
log = log_add(logger, INFO, "foo")
|
||||
assert_match(/^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\s*\d+ $/, log.datetime)
|
||||
assert_match(/^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\s*\d+$/, log.datetime)
|
||||
# config
|
||||
logger = Logger.new(STDERR, datetime_format: "%d%b%Y@%H:%M:%S")
|
||||
log = log_add(logger, INFO, "foo")
|
||||
|
|
Загрузка…
Ссылка в новой задаче