Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Standard formatter tests

Started putting minitest specs in place, starting with the StandardFormatter.
  • Loading branch information...
commit 04e09795896ebe617b910f5731c3a2f9ba726557 1 parent efbdb44
@gshutler authored
View
1  .rvmrc
@@ -0,0 +1 @@
+rvm use 1.9.3
View
6 Rakefile
@@ -1,2 +1,8 @@
#!/usr/bin/env rake
+
require "bundler/gem_tasks"
+require 'rake/testtask'
+
+Rake::TestTask.new do |test|
+ test.pattern = "spec/*_spec.rb"
+end
View
4 hatchet.gemspec
@@ -4,8 +4,8 @@ require File.expand_path('../lib/hatchet/version', __FILE__)
Gem::Specification.new do |gem|
gem.authors = ["Garry Shutler"]
gem.email = ["garry@robustsoftware.co.uk"]
- gem.description = %q{Ruby logging library that provides the ability to add class/module specific filters}
- gem.summary = %q{Ruby logging library that provides the ability to add class/module specific filters}
+ gem.description = %q{Logging library that provides the ability to add class/module specific filters}
+ gem.summary = %q{Logging library that provides the ability to add class/module specific filters}
gem.homepage = "https://github.com/gshutler/hatchet"
gem.files = Dir['{lib,test}/**/*'] + %w{LICENSE}
View
8 lib/hatchet/standard_formatter.rb
@@ -15,7 +15,7 @@ class StandardFormatter
# Returns the context and message separated by a hypen.
#
def format(level, context, message)
- "#{timestamp} [#{thread_name}] #{level.to_s.upcase.ljust 5} #{message}"
+ "#{timestamp} [#{thread_name}] #{format_level level} #{context} - #{message}"
end
private
@@ -37,6 +37,12 @@ def thread_name
end
end
+ # Private: Returns the level formatted for log output as a String.
+ #
+ def format_level(level)
+ level.to_s.upcase.ljust(5)
+ end
+
end
end
View
9 spec/spec_helper.rb
@@ -0,0 +1,9 @@
+# -*- encoding: utf-8 -*-
+
+require 'minitest/autorun'
+require 'minitest/spec'
+require_relative '../lib/hatchet'
+
+# Avoid having to refer to everything absolutely all the time.
+include Hatchet
+
View
71 spec/standard_formatter_spec.rb
@@ -0,0 +1,71 @@
+# -*- encoding: utf-8 -*-
+
+require_relative 'spec_helper'
+require 'date'
+
+describe StandardFormatter do
+ TIME_FORMAT = '%Y-%m-%d %H:%M:%S.%L'
+
+ let(:subject) { StandardFormatter.new }
+
+ describe 'when formatting a message' do
+ before do
+ @message = 'Hello, World'
+ @context = 'Custom::Context'
+ @level = :info
+ @formatted_message = subject.format(@level, @context, @message)
+ end
+
+ it 'outputs the timestamp in %Y-%m-%d %H:%M:%S.%L format' do
+ # Dear god parsing times in Ruby is a pain so create a correctly formatted
+ # string then parse them both to compare them, otherwise timezones get
+ # crazy.
+ expected = to_time format_as_string(Time.now)
+ actual = to_time @formatted_message
+ diff = expected - actual
+
+ assert diff >= 0, 'must be no later expected'
+ assert diff <= 50, 'must be very recent'
+ end
+
+ it 'outputs the pid when run in the main thread' do
+ assert_equal Process.pid.to_s, thread_name
+ end
+
+ it 'converts the level to uppercase' do
+ assert_includes @formatted_message, @level.to_s.upcase
+ end
+
+ it 'formats the message after the time as expected' do
+ expected = "[#{Process.pid}] #{@level.to_s.upcase.ljust 5} #{@context} - #{@message}"
+ actual_without_time = @formatted_message[24..-1]
+
+ assert_equal expected, actual_without_time
+ end
+
+ describe 'when running in a thread' do
+ before do
+ @thread = Thread.new { subject.format(:info, @context, @message) }
+ @formatted_message = @thread.value
+ end
+
+ it 'outputs the pid and the thread object_id' do
+ assert_equal "#{Process.pid}##{@thread.object_id}", thread_name
+ end
+ end
+
+ def format_as_string(time)
+ time.strftime TIME_FORMAT
+ end
+
+ def to_time(string)
+ DateTime.strptime string[0..23], TIME_FORMAT
+ end
+
+ def thread_name
+ match = /\[([^\]]+)\]/.match(@formatted_message)
+ match[1]
+ end
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.