mirror of
https://github.com/meineerde/rackstash.git
synced 2026-02-01 01:37:12 +00:00
We only format and convert it after passing it through the formatter. That way, we can ensure that the actually interesting formatted value is valid.
66 lines
1.7 KiB
Ruby
66 lines
1.7 KiB
Ruby
# Copyright 2017 Holger Just
|
|
#
|
|
# This software may be modified and distributed under the terms
|
|
# of the MIT license. See the LICENSE.txt file for details.
|
|
|
|
require 'spec_helper'
|
|
|
|
require 'time'
|
|
require 'rackstash/formatter'
|
|
|
|
describe Rackstash::Formatter do
|
|
let(:formatter) { Rackstash::Formatter.new }
|
|
|
|
it 'formats plain strings' do
|
|
expect(formatter.call('ERROR', Time.now, 'progname', 'Hello')).to eql "Hello\n"
|
|
end
|
|
|
|
it 'formats stringifiable objects' do
|
|
expect(formatter.call('ERROR', Time.now, 'progname', 123)).to eql "123\n"
|
|
end
|
|
|
|
it 'formats Hashes' do
|
|
expect(formatter.call('ERROR', Time.now, 'progname', { k: 'v' })).to eql "{:k=>\"v\"}\n"
|
|
end
|
|
|
|
it 'formats exceptions' do
|
|
exception = nil
|
|
begin
|
|
raise StandardError, 'An Error'
|
|
rescue => e
|
|
exception = e
|
|
end
|
|
|
|
checker = Regexp.new <<-EOF.gsub(/^\s+/, '').rstrip, Regexp::MULTILINE
|
|
\\AAn Error \\(StandardError\\)
|
|
#{Regexp.escape __FILE__}:#{__LINE__ - 7}:in `block .*`
|
|
EOF
|
|
expect(formatter.call('ERROR', Time.now, 'progname', exception)).to match checker
|
|
end
|
|
|
|
it 'inspects unknown objects' do
|
|
object = Object.new
|
|
inspected = object.inspect
|
|
|
|
expect(object).to receive(:inspect).once.and_call_original
|
|
expect(formatter.call('ERROR', Time.now, 'progname', object)).to eql "#{inspected}\n"
|
|
end
|
|
end
|
|
|
|
describe Rackstash::RawFormatter do
|
|
let(:formatter) { Rackstash::RawFormatter.new }
|
|
|
|
it 'returns the message' do
|
|
msg = 'my message'
|
|
expect(formatter.call('ERROR', Time.now, 'progname', msg)).to equal msg
|
|
end
|
|
|
|
it 'inspects non-string messages' do
|
|
obj = Object.new
|
|
|
|
expect(obj).to receive(:inspect).and_return('object')
|
|
expect(formatter.call('ERROR', Time.now, 'progname', obj)).to eql 'object'
|
|
end
|
|
|
|
end
|