Skip to content
Browse files

ST: Added further adapter specs

  • Loading branch information...
1 parent 953c33d commit 117832dc1813b789c6f06a816ac0791ac7b1212a @rudionrails committed
View
5 Rakefile
@@ -3,6 +3,11 @@ $LOAD_PATH.unshift( 'lib' )
require 'bundler'
Bundler::GemHelper.install_tasks
+task :examples do
+ puts "**** Running Examples"
+ Dir[ File.dirname(__FILE__) + '/examples/*.rb' ].each { |file| require file }
+end
+
# === RSpec
begin
require 'rspec/core/rake_task'
View
14 lib/yell/adapters/base.rb
@@ -9,9 +9,7 @@ module Adapters #:nodoc:
# Other adapters should inherit from it for the methods used
# by the {Yell::Logger}.
class Base
-
- # Accessor to the level
- attr_reader :level
+ include Yell::Level::Helpers
# Accessor to the options
attr_reader :options
@@ -40,16 +38,6 @@ def close
raise 'Not implemented'
end
- # Set the log level.
- #
- # @example Set minimum level to :info
- # level :info
- #
- # For more examples, refer to {Yell::Level}.
- def level=( severity )
- @level = Yell::Level.new( severity )
- end
-
private
View
8 lib/yell/adapters/io.rb
@@ -73,11 +73,11 @@ def write!( event )
stream.print( message )
stream.flush
- # rescue Exception => e
- # close
+ rescue Exception => e
+ close
- # # re-raise the exception
- # raise( e, caller )
+ # re-raise the exception
+ raise( e, caller )
end
# Determie whether to colorize the log output or nor
View
1 lib/yell/formatter.rb
@@ -74,6 +74,7 @@ def initialize( pattern = nil, date_pattern = nil )
define!
end
+
private
# defines the format method
View
18 lib/yell/level.rb
@@ -23,6 +23,23 @@ module Yell #:nodoc:
# @example Set at :info only
# Yell::Level.new.at(:info)
class Level
+ module Helpers
+ # Accessor to the log level
+ attr_reader :level
+
+ # Set the minimum log level.
+ #
+ # @example Set the level to :warn
+ # level = :warn
+ #
+ # @param [String, Symbol, Integer] val The minimum log level
+ def level=( severity )
+ @level = case severity
+ when Yell::Level then severity
+ else Yell::Level.new( severity )
+ end
+ end
+ end
attr_reader :severities
@@ -45,7 +62,6 @@ def initialize( severity = nil )
@severities = Yell::Severities.map { true } # all levels allowed by default
case severity
- when Yell::Level then @severities = severity.severities
when Array then at( *severity )
when Range then gte(severity.first).lte(severity.last)
when Integer, String, Symbol then gte(severity)
View
14 lib/yell/logger.rb
@@ -29,9 +29,7 @@ module Yell #:nodoc:
# l.level = :info
# end
class Logger
-
- # Accessor to the log level
- attr_reader :level
+ include Yell::Level::Helpers
def initialize( *args, &block )
@adapters = []
@@ -87,16 +85,6 @@ def adapter( type = :file, *args, &block )
@adapters << Yell::Adapters.new( type, options, &block )
end
- # Set the minimum log level.
- #
- # @example Set the level to :warn
- # level = :warn
- #
- # @param [String, Symbol, Integer] val The minimum log level
- def level=( severity )
- @level = Yell::Level.new( severity )
- end
-
# Deprecated: Use attr_reader in future
def level( val = nil )
if val.nil?
View
2 spec/spec_helper.rb
@@ -1,6 +1,8 @@
$:.unshift File.expand_path('..', __FILE__)
$:.unshift File.expand_path('../../lib', __FILE__)
+require 'ostruct'
+
require 'yell'
require 'rspec'
View
75 spec/yell/adapters/io_spec.rb
@@ -0,0 +1,75 @@
+require 'spec_helper'
+
+describe Yell::Adapters::Io do
+
+ it { should be_kind_of Yell::Adapters::Base }
+
+ context "initialize" do
+ it "should set default :format" do
+ adapter = Yell::Adapters::Io.new
+
+ adapter.format.should be_kind_of Yell::Formatter
+ end
+
+ context :level do
+ let( :level ) { Yell::Level.new(:warn) }
+
+ it "should set the level" do
+ adapter = Yell::Adapters::Io.new :level => level
+ adapter.level.should == level
+ end
+
+ it "should set the level when block was given" do
+ adapter = Yell::Adapters::Io.new { |a| a.level = level }
+ adapter.level.should == level
+ end
+ end
+
+ context :format do
+ let( :format ) { Yell::Formatter.new }
+
+ it "should set the level" do
+ adapter = Yell::Adapters::Io.new :format => format
+ adapter.format.should == format
+ end
+
+ it "should set the level when block was given" do
+ adapter = Yell::Adapters::Io.new { |a| a.format = format }
+ adapter.format.should == format
+ end
+ end
+ end
+
+ context :stream do
+ it "should raise" do
+ lambda { Yell::Adapters::Io.new.stream }.should raise_error("Not implemented" )
+ end
+ end
+
+ context :write do
+ let( :event ) { Yell::Event.new("INFO", "Hello World") }
+ let( :adapter ) { Yell::Adapters::Io.new }
+ let( :stream ) { File.new('/dev/null', 'w') }
+
+ before do
+ stub( adapter ).stream { stream }
+ end
+
+ it "should format the message" do
+ mock.proxy( adapter.format ).format( event )
+
+ adapter.write( event )
+ end
+
+ it "should print formatted message to stream" do
+ formatted = Yell::Formatter.new.format( event )
+
+ mock( stream ).print( formatted << "\n" )
+ mock( stream ).flush
+
+ adapter.write( event )
+ end
+ end
+
+end
+
View
26 spec/yell/adapters/streams_spec.rb
@@ -0,0 +1,26 @@
+require 'spec_helper'
+
+describe Yell::Adapters::Stdout do
+
+ it { should be_kind_of Yell::Adapters::Io }
+
+ context :stream do
+ subject { Yell::Adapters::Stdout.new.stream }
+
+ it { should be_kind_of IO }
+ end
+
+end
+
+describe Yell::Adapters::Stderr do
+
+ it { should be_kind_of Yell::Adapters::Io }
+
+ context :stream do
+ subject { Yell::Adapters::Stderr.new.stream }
+
+ it { should be_kind_of IO }
+ end
+
+end
+
View
6 spec/yell/logger_spec.rb
@@ -75,15 +75,15 @@
let( :level ) { Yell::Level.new :error }
let( :adapter ) { Yell::Adapters::Stdout.new }
- let( :logger ) do
- Yell::Logger.new do |l|
+ let( :logger ) do
+ Yell::Logger.new do |l|
l.level = level
l.adapter adapter
end
end
it "should set the level" do
- logger.level.severities.should == level.severities
+ logger.level.should == level
end
it "should define adapter" do

0 comments on commit 117832d

Please sign in to comment.
Something went wrong with that request. Please try again.