-
Notifications
You must be signed in to change notification settings - Fork 101
/
test_appender.rb
166 lines (125 loc) · 4.34 KB
/
test_appender.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
require File.join(File.dirname(__FILE__), %w[setup])
module TestLogging
class TestAppender < Test::Unit::TestCase
include LoggingTestCase
def setup
super
::Logging.init
@levels = ::Logging::LEVELS
@event = ::Logging::LogEvent.new('logger', @levels['debug'],
'message', false)
@appender = ::Logging::Appender.new 'test_appender'
end
def test_append
ary = []
@appender.instance_variable_set :@ary, ary
def @appender.write( event )
str = event.instance_of?(::Logging::LogEvent) ?
@layout.format(event) : event.to_s
@ary << str
end
assert_nothing_raised {@appender.append @event}
assert_equal "DEBUG logger : message\n", ary.pop
@appender.level = :info
@appender.append @event
assert_nil ary.pop
@event.level = @levels['info']
@appender.append @event
assert_equal " INFO logger : message\n", ary.pop
@appender.close
assert_raise(RuntimeError) {@appender.append @event}
end
def test_class_stderr
stderr = ::Logging::Appender.stderr
assert_instance_of ::Logging::Appenders::Stderr, stderr
assert_equal 'stderr', stderr.name
assert_same stderr, ::Logging::Appender.stderr
end
def test_class_stdout
stdout = ::Logging::Appender.stdout
assert_instance_of ::Logging::Appenders::Stdout, stdout
assert_equal 'stdout', stdout.name
assert_same stdout, ::Logging::Appender.stdout
end
def test_close
assert_equal false, @appender.closed?
@appender.close
assert_equal true, @appender.closed?
end
def test_closed_eh
assert_equal false, @appender.closed?
@appender.close
assert_equal true, @appender.closed?
end
def test_concat
ary = []
@appender.instance_variable_set :@ary, ary
def @appender.write( event )
str = event.instance_of?(::Logging::LogEvent) ?
@layout.format(event) : event.to_s
@ary << str
end
assert_nothing_raised {@appender << 'log message'}
assert_equal 'log message', ary.pop
@appender.level = :off
@appender << 'another log message'
assert_nil ary.pop
layout = @appender.layout
def layout.footer() 'this is the footer' end
@appender.close
assert_raise(RuntimeError) {@appender << 'log message'}
assert_equal 'this is the footer', ary.pop
end
def test_flush
assert_same @appender, @appender.flush
end
def test_initialize
assert_raise(TypeError) {::Logging::Appender.new 'test', :layout => []}
layout = ::Logging::Layouts::Basic.new
@appender = ::Logging::Appender.new 'test', :layout => layout
assert_same layout, @appender.instance_variable_get(:@layout)
end
def test_layout
assert_instance_of ::Logging::Layouts::Basic, @appender.layout
end
def test_layout_eq
layout = ::Logging::Layouts::Basic.new
assert_not_equal layout, @appender.layout
assert_raise(TypeError) {@appender.layout = Object.new}
assert_raise(TypeError) {@appender.layout = 'not a layout'}
@appender.layout = layout
assert_same layout, @appender.layout
end
def test_level
assert_equal 0, @appender.level
end
def test_level_eq
assert_equal 0, @appender.level
assert_raise(ArgumentError) {@appender.level = -1}
assert_raise(ArgumentError) {@appender.level = 6}
assert_raise(ArgumentError) {@appender.level = Object}
assert_raise(ArgumentError) {@appender.level = 'bob'}
assert_raise(ArgumentError) {@appender.level = :wtf}
@appender.level = 'INFO'
assert_equal 1, @appender.level
@appender.level = :warn
assert_equal 2, @appender.level
@appender.level = 'error'
assert_equal 3, @appender.level
@appender.level = 4
assert_equal 4, @appender.level
@appender.level = 'off'
assert_equal 5, @appender.level
@appender.level = :all
assert_equal 0, @appender.level
end
def test_name
assert_equal 'test_appender', @appender.name
end
def test_inspect
expected = "<Appender:0x%x name=\"test_appender\">" % @appender.object_id
assert_equal expected, @appender.inspect
end
end # class TestAppender
end # module TestLogging
# EOF