loggability 0.7.0 → 0.8.0.pre.65

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,14 +1,11 @@
1
1
  # -*- rspec -*-
2
+ #encoding: utf-8
2
3
 
3
- BEGIN {
4
- require 'pathname'
5
- basedir = Pathname( __FILE__ ).dirname.parent.parent.parent
6
- $LOAD_PATH.unshift( basedir.to_s ) unless $LOAD_PATH.include?( basedir.to_s )
7
- }
4
+ require_relative '../../helpers'
8
5
 
9
6
  require 'tempfile'
10
7
  require 'rspec'
11
- require 'spec/lib/helpers'
8
+
12
9
  require 'loggability/logger'
13
10
  require 'loggability/formatter'
14
11
  require 'loggability/formatter/color'
@@ -1,14 +1,11 @@
1
1
  # -*- rspec -*-
2
+ #encoding: utf-8
2
3
 
3
- BEGIN {
4
- require 'pathname'
5
- basedir = Pathname( __FILE__ ).dirname.parent.parent.parent
6
- $LOAD_PATH.unshift( basedir.to_s ) unless $LOAD_PATH.include?( basedir.to_s )
7
- }
4
+ require_relative '../../helpers'
8
5
 
9
6
  require 'tempfile'
10
7
  require 'rspec'
11
- require 'spec/lib/helpers'
8
+
12
9
  require 'loggability/logger'
13
10
  require 'loggability/formatter'
14
11
  require 'loggability/formatter/html'
@@ -1,14 +1,10 @@
1
1
  # -*- rspec -*-
2
2
 
3
- BEGIN {
4
- require 'pathname'
5
- basedir = Pathname( __FILE__ ).dirname.parent.parent
6
- $LOAD_PATH.unshift( basedir.to_s ) unless $LOAD_PATH.include?( basedir.to_s )
7
- }
3
+ require_relative '../helpers'
8
4
 
9
5
  require 'tempfile'
10
6
  require 'rspec'
11
- require 'spec/lib/helpers'
7
+
12
8
  require 'loggability/logger'
13
9
  require 'loggability/formatter'
14
10
  require 'loggability/formatter/default'
@@ -18,8 +14,8 @@ describe Loggability::Formatter do
18
14
 
19
15
  it "formats messages with the pattern it's constructed with" do
20
16
  formatter = Loggability::Formatter.new( '[%5$s] %7$s' )
21
- formatter.call( 'INFO', Time.at(1336286481), nil, 'Foom.' ).should =~
22
- /\[INFO\] Foom./i
17
+ result = formatter.call( 'INFO', Time.at(1336286481), nil, 'Foom.' )
18
+ expect( result ).to match(/\[INFO\] Foom./i)
23
19
  end
24
20
 
25
21
  it "formats exceptions into useful messages" do
@@ -32,13 +28,14 @@ describe Loggability::Formatter do
32
28
  msg = formatter.call( 'INFO', Time.at(1336286481), nil, err )
33
29
  end
34
30
 
35
- msg.should =~ /\[INFO\] ArgumentError: invalid argument/i
31
+ expect( msg ).to match(/\[INFO\] ArgumentError: invalid argument/i)
36
32
  end
37
33
 
38
34
  it "formats regular objects into useful messages" do
39
35
  formatter = Loggability::Formatter.new( '[%5$s] %7$s' )
40
- formatter.call( 'INFO', Time.at(1336286481), nil, Object.new ).should =~
41
- /\[INFO\] #<Object:0x[[:xdigit:]]+>/i
36
+ result = formatter.call( 'INFO', Time.at(1336286481), nil, Object.new )
37
+
38
+ expect( result ).to match(/\[INFO\] #<Object:0x[[:xdigit:]]+>/i)
42
39
  end
43
40
 
44
41
  end
@@ -1,14 +1,10 @@
1
1
  # -*- rspec -*-
2
2
 
3
- BEGIN {
4
- require 'pathname'
5
- basedir = Pathname( __FILE__ ).dirname.parent.parent
6
- $LOAD_PATH.unshift( basedir.to_s ) unless $LOAD_PATH.include?( basedir.to_s )
7
- }
3
+ require_relative '../helpers'
8
4
 
9
5
  require 'tempfile'
10
6
  require 'rspec'
11
- require 'spec/lib/helpers'
7
+
12
8
  require 'loggability/logger'
13
9
  require 'loggability/formatter'
14
10
  require 'loggability/formatter/default'
@@ -32,17 +28,17 @@ describe Loggability::Logger do
32
28
 
33
29
 
34
30
  it "has a less-verbose inspection format than that of its parent" do
35
- @logger.inspect.should =~ /severity: \S+ formatter: \S+ outputting to: \S+/
31
+ expect( @logger.inspect ).to match( /severity: \S+ formatter: \S+ outputting to: \S+/ )
36
32
  end
37
33
 
38
34
 
39
35
  it "provides an upgrade constructor for regular Logger objects" do
40
36
  logger = ::Logger.new( $stderr )
41
37
  newlogger = described_class.from_std_logger( logger )
42
- newlogger.should be_a( Loggability::Logger )
43
- newlogger.logdev.dev.should be( logger.instance_variable_get(:@logdev).dev )
44
- Loggability::LOG_LEVELS[ newlogger.level ].should == logger.level
45
- newlogger.formatter.should be_a( Loggability::Formatter::Default )
38
+ expect( newlogger ).to be_a( Loggability::Logger )
39
+ expect( newlogger.logdev.dev ).to be( logger.instance_variable_get(:@logdev).dev )
40
+ expect( Loggability::LOG_LEVELS[newlogger.level] ).to eq( logger.level )
41
+ expect( newlogger.formatter ).to be_a( Loggability::Formatter::Default )
46
42
  end
47
43
 
48
44
 
@@ -52,20 +48,60 @@ describe Loggability::Logger do
52
48
  @logger.output_to( results )
53
49
  @logger << "This is an appended message."
54
50
 
55
- results.first.should =~ /debug.*this is an appended message/i
51
+ expect( results.first ).to match( /debug.*this is an appended message/i )
52
+ end
53
+
54
+
55
+ it "can return a Hash of its current settings" do
56
+ expect( @logger.settings ).to be_a( Hash )
57
+ expect( @logger.settings ).to include( :level, :formatter, :logdev )
58
+ expect( @logger.settings[:level] ).to eq( @logger.level )
59
+ expect( @logger.settings[:formatter] ).to eq( @logger.formatter )
60
+ expect( @logger.settings[:logdev] ).to eq( @logger.logdev )
61
+ end
62
+
63
+
64
+ it "can restore its settings from a Hash" do
65
+ settings = @logger.settings
66
+
67
+ @logger.level = :fatal
68
+ @logger.formatter = Loggability::Formatter.create( :html )
69
+ @logger.output_to( [] )
70
+
71
+ @logger.restore_settings( settings )
72
+
73
+ expect( @logger.level ).to be( settings[:level] )
74
+ expect( @logger.formatter ).to be( settings[:formatter] )
75
+ expect( @logger.logdev ).to be( settings[:logdev] )
76
+ end
77
+
78
+
79
+ it "ignores missing keys when restoring its settings from a Hash" do
80
+ settings = @logger.settings
81
+ settings.delete( :level )
82
+
83
+ @logger.level = :fatal
84
+ @logger.formatter = Loggability::Formatter.create( :html )
85
+ @logger.output_to( [] )
86
+
87
+ @logger.restore_settings( settings )
88
+
89
+ expect( @logger.level ).to be( :fatal )
90
+ expect( @logger.formatter ).to be( settings[:formatter] )
91
+ expect( @logger.logdev ).to be( settings[:logdev] )
56
92
  end
57
93
 
58
94
 
59
95
  describe "severity level API" do
60
96
 
61
97
  it "defaults to :warn level" do
62
- @logger.level.should == :warn
98
+ expect( @logger.level ).to eq( :warn )
63
99
  end
64
100
 
65
101
  it "defaults to :debug level when $DEBUG is true" do
66
102
  begin
67
103
  $DEBUG = true
68
- described_class.new.level.should == :debug
104
+ expect( described_class.new.level ).to eq( :debug )
69
105
  ensure
70
106
  $DEBUG = false
71
107
  end
@@ -75,17 +111,17 @@ describe Loggability::Logger do
75
111
  newlevel = Logger::DEBUG
76
112
  $stderr.puts "Setting newlevel to %p" % [ newlevel ]
77
113
  @logger.level = newlevel
78
- @logger.level.should == :debug
114
+ expect( @logger.level ).to eq( :debug )
79
115
  end
80
116
 
81
117
  it "allows its levels to be set with Symbolic level names" do
82
118
  @logger.level = :info
83
- @logger.level.should == :info
119
+ expect( @logger.level ).to eq( :info )
84
120
  end
85
121
 
86
122
  it "allows its levels to be set with Stringish level names" do
87
123
  @logger.level = 'fatal'
88
- @logger.level.should == :fatal
124
+ expect( @logger.level ).to eq( :fatal )
89
125
  end
90
126
 
91
127
  end
@@ -94,32 +130,50 @@ describe Loggability::Logger do
94
130
  describe "log device API" do
95
131
 
96
132
  it "logs to STDERR by default" do
97
- @logger.logdev.dev.should be( $stderr )
133
+ expect( @logger.logdev.dev ).to be( $stderr )
98
134
  end
99
135
 
100
136
  it "can be told to log to a file" do
101
137
  tmpfile = Tempfile.new( 'loggability-device-spec' )
102
138
  @logger.output_to( tmpfile.path )
103
- @logger.logdev.dev.should be_a( File )
139
+ expect( @logger.logdev.dev ).to be_a( File )
104
140
  end
105
141
 
106
142
  it "supports log-rotation arguments for logfiles" do
107
143
  tmpfile = Tempfile.new( 'loggability-device-spec' )
108
144
  @logger.output_to( tmpfile.path, 5, 125000 )
109
- @logger.logdev.dev.should be_a( File )
110
- @logger.logdev.filename.should == tmpfile.path
111
- @logger.logdev.instance_variable_get( :@shift_age ).should == 5
112
- @logger.logdev.instance_variable_get( :@shift_size ).should == 125000
145
+ expect( @logger.logdev.dev ).to be_a( File )
146
+ expect( @logger.logdev.filename ).to eq( tmpfile.path )
147
+ expect( @logger.logdev.instance_variable_get(:@shift_age) ).to eq( 5 )
148
+ expect( @logger.logdev.instance_variable_get(:@shift_size) ).to eq( 125000 )
113
149
  end
114
150
 
115
151
  it "can be told to log to an Array" do
116
152
  logmessages = []
117
153
  @logger.output_to( logmessages )
118
- @logger.logdev.should be_a( Loggability::Logger::AppendingLogDevice )
154
+ expect( @logger.logdev ).to be_a( Loggability::Logger::AppendingLogDevice )
119
155
  @logger.level = :debug
120
156
  @logger.info( "Something happened." )
121
- logmessages.should have( 1 ).member
122
- logmessages.first.should =~ /something happened/i
157
+ expect( logmessages ).to have( 1 ).member
158
+ expect( logmessages.first ).to match( /something happened/i )
159
+ end
160
+
161
+ it "doesn't re-wrap a Logger::LogDevice" do
162
+ tmpfile = Tempfile.new( 'loggability-device-spec' )
163
+ @logger.output_to( tmpfile.path, 5, 125000 )
164
+
165
+ original_logdev = @logger.logdev
166
+ @logger.output_to( original_logdev )
167
+
168
+ expect( @logger.logdev ).to be( original_logdev )
169
+ end
170
+
171
+ it "doesn't re-wrap an AppendingLogDevice" do
172
+ log_array = []
173
+ @logger.output_to( log_array )
174
+ @logger.output_to( @logger.logdev )
175
+
176
+ expect( @logger.logdev.target ).to be( log_array )
123
177
  end
124
178
 
125
179
  end
@@ -128,12 +182,12 @@ describe Loggability::Logger do
128
182
  describe "formatter API" do
129
183
 
130
184
  it "logs with the default formatter by default" do
131
- @logger.formatter.should be_a( Loggability::Formatter::Default )
185
+ expect( @logger.formatter ).to be_a( Loggability::Formatter::Default )
132
186
  end
133
187
 
134
188
  it "can be told to use the default formatter explicitly" do
135
189
  @logger.format_as( :default )
136
- @logger.formatter.should be_a( Loggability::Formatter::Default )
190
+ expect( @logger.formatter ).to be_a( Loggability::Formatter::Default )
137
191
  end
138
192
 
139
193
  it "can be told to use a block as a formatter" do
@@ -141,12 +195,12 @@ describe Loggability::Logger do
141
195
  original_formatter.call(severity, datetime, progname, msg.dump)
142
196
  end
143
197
 
144
- @logger.formatter.should be_a( Proc )
198
+ expect( @logger.formatter ).to be_a( Proc )
145
199
  end
146
200
 
147
201
  it "can be told to use the HTML formatter" do
148
202
  @logger.format_as( :html )
149
- @logger.formatter.should be_a( Loggability::Formatter::HTML )
203
+ expect( @logger.formatter ).to be_a( Loggability::Formatter::HTML )
150
204
  end
151
205
 
152
206
  it "supports formatting with ::Logger::Formatter, too" do
@@ -155,7 +209,9 @@ describe Loggability::Logger do
155
209
  @logger.level = :debug
156
210
  @logger.formatter = ::Logger::Formatter.new
157
211
  @logger.debug "This should work."
158
- output.first.should =~ /D, \[\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d.\d+ #\d+\] DEBUG -- : This should work.\n/
212
+
213
+ expect( output.first ).
214
+ to match(/D, \[\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d.\d+ #\d+\] DEBUG -- : This should work.\n/)
159
215
  end
160
216
 
161
217
  end
@@ -176,7 +232,7 @@ describe Loggability::Logger do
176
232
  proxy.error( "An error message." )
177
233
  proxy.fatal( "A fatal message." )
178
234
 
179
- messages.first.should =~ /DEBUG \{Object:0x[[:xdigit:]]+\} -- A debug message.\n/i
235
+ expect( messages.first ).to match(/DEBUG \{Object:0x[[:xdigit:]]+\} -- A debug message.\n/i)
180
236
  end
181
237
 
182
238
  it "has a terse inspection format" do
@@ -0,0 +1,134 @@
1
+ # -*- rspec -*-
2
+
3
+ require_relative '../helpers'
4
+
5
+ require 'tempfile'
6
+ require 'rspec'
7
+
8
+ require 'loggability/logger'
9
+ require 'loggability/override'
10
+
11
+
12
+ describe Loggability::Override do
13
+
14
+ after( :each ) do
15
+ Loggability.clear_loghosts
16
+ end
17
+
18
+
19
+ let!( :override ) { described_class.new }
20
+ let!( :loghost ) do
21
+ Class.new do
22
+ extend Loggability
23
+ log_as :testing
24
+ end
25
+ end
26
+
27
+
28
+ it "doesn't override anything by default" do
29
+ previous_level = Loggability[ loghost ].level
30
+ override.call do
31
+ expect( Loggability[ loghost ].level ).to be( previous_level )
32
+ end
33
+ end
34
+
35
+
36
+ it "raises an error when #called re-entrantly" do
37
+ override.call do
38
+ expect { override.call {} }.to raise_error( LocalJumpError )
39
+ end
40
+ end
41
+
42
+
43
+ it "can mutate itself into a variant that modifies the logging level" do
44
+ log = []
45
+
46
+ Loggability.level = :fatal
47
+ Loggability.output_to( log )
48
+ Loggability.format_with( :default )
49
+
50
+ level_override = override.with_level( :debug )
51
+
52
+ loghost.logger.debug "This shouldn't show up."
53
+ level_override.call do
54
+ loghost.logger.debug "But this should."
55
+ end
56
+ loghost.logger.debug "This shouldn't either."
57
+
58
+ expect( log ).to have( 1 ).log_entry
59
+ end
60
+
61
+
62
+ it "has a constructor delegator for its level mutator" do
63
+ override = described_class.with_level( :debug )
64
+ expect( override.settings ).to eq({ level: :debug })
65
+ end
66
+
67
+
68
+ it "can mutate itself into a variant that modifies where output goes" do
69
+ original_destination = []
70
+ new_destination = []
71
+ output_override = override.outputting_to( new_destination )
72
+
73
+ Loggability.level = :debug
74
+ Loggability.output_to( original_destination )
75
+ Loggability.format_with( :default )
76
+
77
+ loghost.logger.debug "This should be output to the original destination"
78
+ output_override.call do
79
+ loghost.logger.debug "This should be output to the overridden destination"
80
+ end
81
+ loghost.logger.debug "This should be output to the original destination"
82
+
83
+ expect( original_destination ).to have( 2 ).log_entries
84
+ expect( new_destination ).to have( 1 ).log_entry
85
+ end
86
+
87
+
88
+ it "has a constructor delegator for its output mutator" do
89
+ log = []
90
+ override = described_class.outputting_to( log )
91
+ expect( override.settings ).to eq({ logdev: log })
92
+ end
93
+
94
+
95
+ it "can mutate itself into a variant that formats output differently" do
96
+ log = []
97
+ Loggability.level = :debug
98
+ Loggability.output_to( log )
99
+ Loggability.format_with( :default )
100
+
101
+ format_override = override.formatted_with( :html )
102
+
103
+ loghost.logger.debug "This should be in the default format"
104
+ format_override.call do
105
+ loghost.logger.debug "This should be in the color format"
106
+ end
107
+ loghost.logger.debug "This should be in the default format again"
108
+
109
+ html_log = log.grep( /<div/ )
110
+
111
+ expect( log ).to have( 3 ).entries
112
+ expect( html_log ).to have( 1 ).entry
113
+ end
114
+
115
+
116
+ it "has a constructor delegator for its format mutator" do
117
+ override = described_class.formatted_with( :color )
118
+ expect( override.settings ).to eq({ formatter: :color })
119
+ end
120
+
121
+
122
+ it "has additive mutators" do
123
+ override = described_class.
124
+ formatted_with( :color ).
125
+ with_level( :debug ).
126
+ outputting_to( $stdout )
127
+
128
+ expect( override.settings[:formatter] ).to be( :color )
129
+ expect( override.settings[:logdev] ).to be( $stdout )
130
+ expect( override.settings[:level] ).to be( :debug )
131
+ end
132
+
133
+ end
134
+