1
0
mirror of https://github.com/meineerde/rackstash.git synced 2026-03-23 00:29:51 +00:00

Replace specific class-under-test with described_class in all specs

This commit is contained in:
Holger Just 2017-07-19 12:09:21 +02:00
parent b6768c6547
commit ca339a84fa
18 changed files with 165 additions and 155 deletions

View File

@ -10,13 +10,13 @@ require 'spec_helper'
require 'rackstash/adapters/adapter' require 'rackstash/adapters/adapter'
describe Rackstash::Adapters::Adapter do describe Rackstash::Adapters::Adapter do
let(:adapter) { Rackstash::Adapters::Adapter.new } let(:adapter) { described_class.new }
describe '#initialize' do describe '#initialize' do
it 'accepts any arguments' do it 'accepts any arguments' do
Rackstash::Adapters::Adapter.new described_class.new
Rackstash::Adapters::Adapter.new(:foo) described_class.new(:foo)
Rackstash::Adapters::Adapter.new(123, [:foo]) described_class.new(123, [:foo])
end end
end end

View File

@ -11,23 +11,23 @@ require 'rackstash/adapters/callable'
describe Rackstash::Adapters::Callable do describe Rackstash::Adapters::Callable do
let(:callable) { ->(log) { log } } let(:callable) { ->(log) { log } }
let(:adapter) { Rackstash::Adapters::Callable.new(callable) } let(:adapter) { described_class.new(callable) }
describe '#initialize' do describe '#initialize' do
it 'accepts a callable' do it 'accepts a callable' do
expect { Rackstash::Adapters::Callable.new(-> {}) }.not_to raise_error expect { described_class.new(-> {}) }.not_to raise_error
expect { Rackstash::Adapters::Callable.new(proc {}) }.not_to raise_error expect { described_class.new(proc {}) }.not_to raise_error
expect { Rackstash::Adapters::Callable.new(Struct.new(:call).new) }.not_to raise_error expect { described_class.new(Struct.new(:call).new) }.not_to raise_error
expect { Rackstash::Adapters::Callable.new { |log| log } }.not_to raise_error expect { described_class.new { |log| log } }.not_to raise_error
end end
it 'rejects non-IO objects' do it 'rejects non-callable objects' do
expect { Rackstash::Adapters::Callable.new(nil) }.to raise_error TypeError expect { described_class.new(nil) }.to raise_error TypeError
expect { Rackstash::Adapters::Callable.new('hello') }.to raise_error TypeError expect { described_class.new('hello') }.to raise_error TypeError
expect { Rackstash::Adapters::Callable.new(Object.new) }.to raise_error TypeError expect { described_class.new(Object.new) }.to raise_error TypeError
expect { Rackstash::Adapters::Callable.new([]) }.to raise_error TypeError expect { described_class.new([]) }.to raise_error TypeError
expect { Rackstash::Adapters::Callable.new(Struct.new(:foo).new) }.to raise_error TypeError expect { described_class.new(Struct.new(:foo).new) }.to raise_error TypeError
end end
end end

View File

@ -13,19 +13,19 @@ require 'rackstash/adapters/io'
describe Rackstash::Adapters::IO do describe Rackstash::Adapters::IO do
let(:io) { StringIO.new } let(:io) { StringIO.new }
let(:adapter) { Rackstash::Adapters::IO.new(io) } let(:adapter) { described_class.new(io) }
describe '#initialize' do describe '#initialize' do
it 'accepts an IO object' do it 'accepts an IO object' do
expect { Rackstash::Adapters::IO.new($stdout) }.not_to raise_error expect { described_class.new($stdout) }.not_to raise_error
expect { Rackstash::Adapters::IO.new(StringIO.new) }.not_to raise_error expect { described_class.new(StringIO.new) }.not_to raise_error
expect { Rackstash::Adapters::IO.new(Tempfile.new('foo')) }.not_to raise_error expect { described_class.new(Tempfile.new('foo')) }.not_to raise_error
end end
it 'rejects non-IO objects' do it 'rejects non-IO objects' do
expect { Rackstash::Adapters::IO.new(nil) }.to raise_error TypeError expect { described_class.new(nil) }.to raise_error TypeError
expect { Rackstash::Adapters::IO.new('hello') }.to raise_error TypeError expect { described_class.new('hello') }.to raise_error TypeError
expect { Rackstash::Adapters::IO.new(Object.new) }.to raise_error TypeError expect { described_class.new(Object.new) }.to raise_error TypeError
end end
end end

View File

@ -11,16 +11,16 @@ require 'rackstash/adapters'
describe Rackstash::Adapters do describe Rackstash::Adapters do
around(:each) do |example| around(:each) do |example|
types = Rackstash::Adapters.send(:adapter_types) types = described_class.send(:adapter_types)
schemes = Rackstash::Adapters.send(:adapter_schemes) schemes = described_class.send(:adapter_schemes)
Rackstash::Adapters.instance_variable_set(:@adapter_types, nil) described_class.instance_variable_set(:@adapter_types, nil)
Rackstash::Adapters.instance_variable_set(:@adapter_schemes, nil) described_class.instance_variable_set(:@adapter_schemes, nil)
example.run example.run
Rackstash::Adapters.instance_variable_set(:@adapter_types, types) described_class.instance_variable_set(:@adapter_types, types)
Rackstash::Adapters.instance_variable_set(:@adapter_schemes, schemes) described_class.instance_variable_set(:@adapter_schemes, schemes)
end end
let(:adapter) { let(:adapter) {
@ -34,60 +34,60 @@ describe Rackstash::Adapters do
describe '#register' do describe '#register' do
it 'can register a class' do it 'can register a class' do
expect { expect {
Rackstash::Adapters.register adapter, Class.new described_class.register adapter, Class.new
Rackstash::Adapters.register adapter, String described_class.register adapter, String
Rackstash::Adapters.register adapter, Numeric described_class.register adapter, Numeric
Rackstash::Adapters.register adapter, Integer described_class.register adapter, Integer
}.to change { Rackstash::Adapters.send(:adapter_types).size } }.to change { described_class.send(:adapter_types).size }
.from(0).to(4) .from(0).to(4)
expect(Rackstash::Adapters.send(:adapter_schemes).size).to eql 0 expect(described_class.send(:adapter_schemes).size).to eql 0
end end
it 'can register a class name (upper-case String)' do it 'can register a class name (upper-case String)' do
expect { expect {
Rackstash::Adapters.register adapter, '❤' described_class.register adapter, '❤'
Rackstash::Adapters.register adapter, '' described_class.register adapter, ''
Rackstash::Adapters.register adapter, 'Hello::World' described_class.register adapter, 'Hello::World'
}.to change { Rackstash::Adapters.send(:adapter_types).size } }.to change { described_class.send(:adapter_types).size }
.from(0).to(3) .from(0).to(3)
# Registering 'Hello::World' a second time overwrites the first one # Registering 'Hello::World' a second time overwrites the first one
expect { expect {
Rackstash::Adapters.register(adapter, 'Hello::World') described_class.register(adapter, 'Hello::World')
}.not_to change { Rackstash::Adapters.send(:adapter_types).size } }.not_to change { described_class.send(:adapter_types).size }
expect(Rackstash::Adapters.send(:adapter_schemes).size).to eql 0 expect(described_class.send(:adapter_schemes).size).to eql 0
end end
it 'can register a method name (symbol)' do it 'can register a method name (symbol)' do
expect { expect {
Rackstash::Adapters.register adapter, :foo described_class.register adapter, :foo
}.to change { Rackstash::Adapters.send(:adapter_types).size } }.to change { described_class.send(:adapter_types).size }
.from(0).to(1) .from(0).to(1)
expect(Rackstash::Adapters.send(:adapter_schemes).size).to eql 0 expect(described_class.send(:adapter_schemes).size).to eql 0
end end
it 'can register a proc' do it 'can register a proc' do
expect { expect {
Rackstash::Adapters.register adapter, ->(o) { o.respond_to?(:write) } described_class.register adapter, ->(o) { o.respond_to?(:write) }
Rackstash::Adapters.register adapter, -> {} described_class.register adapter, -> {}
}.to change { Rackstash::Adapters.send(:adapter_types).size } }.to change { described_class.send(:adapter_types).size }
.from(0).to(2) .from(0).to(2)
expect(Rackstash::Adapters.send(:adapter_schemes).size).to eql 0 expect(described_class.send(:adapter_schemes).size).to eql 0
end end
it 'can register a scheme (lower-case String)' do it 'can register a scheme (lower-case String)' do
expect { expect {
Rackstash::Adapters.register adapter, 'customscheme' described_class.register adapter, 'customscheme'
}.to change { Rackstash::Adapters.send(:adapter_schemes).size } }.to change { described_class.send(:adapter_schemes).size }
.from(0).to(1) .from(0).to(1)
expect(Rackstash::Adapters.send(:adapter_types).size).to eql 0 expect(described_class.send(:adapter_types).size).to eql 0
end end
it 'rejects invalid adapter classes' do it 'rejects invalid adapter classes' do
expect { Rackstash::Adapters.register nil, :foo } expect { described_class.register nil, :foo }
.to raise_error(TypeError) .to raise_error(TypeError)
expect { Rackstash::Adapters.register Class.new, :foo } expect { described_class.register Class.new, :foo }
.to raise_error(TypeError) .to raise_error(TypeError)
end end
end end
@ -97,7 +97,7 @@ describe Rackstash::Adapters do
let(:device_class) { Class.new } let(:device_class) { Class.new }
before do before do
Rackstash::Adapters.register adapter, device_class described_class.register adapter, device_class
end end
it 'creates an adapter if the class was found' do it 'creates an adapter if the class was found' do
@ -105,7 +105,7 @@ describe Rackstash::Adapters do
expect(device_class).to receive(:===).with(device).and_call_original expect(device_class).to receive(:===).with(device).and_call_original
expect(adapter).to receive(:new).with(device).and_call_original expect(adapter).to receive(:new).with(device).and_call_original
expect(Rackstash::Adapters[device]).to be_an Rackstash::Adapters::Adapter expect(described_class[device]).to be_an Rackstash::Adapters::Adapter
end end
it 'creates an adapter if any parent class was found' do it 'creates an adapter if any parent class was found' do
@ -113,12 +113,12 @@ describe Rackstash::Adapters do
expect(device_class).to receive(:===).with(inherited_device).and_call_original expect(device_class).to receive(:===).with(inherited_device).and_call_original
expect(adapter).to receive(:new).with(inherited_device).and_call_original expect(adapter).to receive(:new).with(inherited_device).and_call_original
expect(Rackstash::Adapters[inherited_device]).to be_an Rackstash::Adapters::Adapter expect(described_class[inherited_device]).to be_an Rackstash::Adapters::Adapter
end end
it 'raises if no class was found' do it 'raises if no class was found' do
expect(adapter).to_not receive(:new) expect(adapter).to_not receive(:new)
expect { Rackstash::Adapters['foo'] }.to raise_error(ArgumentError) expect { described_class['foo'] }.to raise_error(ArgumentError)
end end
end end
@ -127,7 +127,7 @@ describe Rackstash::Adapters do
class SpecDevice; end class SpecDevice; end
class InheritedSpecDevice < SpecDevice; end class InheritedSpecDevice < SpecDevice; end
Rackstash::Adapters.register adapter, 'SpecDevice' described_class.register adapter, 'SpecDevice'
end end
after do after do
@ -139,39 +139,39 @@ describe Rackstash::Adapters do
device = SpecDevice.new device = SpecDevice.new
expect(adapter).to receive(:new).with(device).and_call_original expect(adapter).to receive(:new).with(device).and_call_original
expect(Rackstash::Adapters[device]).to be_an Rackstash::Adapters::Adapter expect(described_class[device]).to be_an Rackstash::Adapters::Adapter
end end
it 'creates an adapter if any parent class was found' do it 'creates an adapter if any parent class was found' do
inherited_device = InheritedSpecDevice.new inherited_device = InheritedSpecDevice.new
expect(adapter).to receive(:new).with(inherited_device).and_call_original expect(adapter).to receive(:new).with(inherited_device).and_call_original
expect(Rackstash::Adapters[inherited_device]).to be_an Rackstash::Adapters::Adapter expect(described_class[inherited_device]).to be_an Rackstash::Adapters::Adapter
end end
it 'raises if no class was found' do it 'raises if no class was found' do
expect(adapter).to_not receive(:new) expect(adapter).to_not receive(:new)
expect { Rackstash::Adapters['foo'] }.to raise_error(ArgumentError) expect { described_class['foo'] }.to raise_error(ArgumentError)
end end
end end
context 'with a registered symbol' do context 'with a registered symbol' do
before do before do
Rackstash::Adapters.register adapter, :foo described_class.register adapter, :foo
end end
it 'creates an adapter if it responds to the registered method' do it 'creates an adapter if it responds to the registered method' do
device = Struct.new(:foo).new('foo') device = Struct.new(:foo).new('foo')
expect(adapter).to receive(:new).with(device).and_call_original expect(adapter).to receive(:new).with(device).and_call_original
expect(Rackstash::Adapters[device]).to be_an Rackstash::Adapters::Adapter expect(described_class[device]).to be_an Rackstash::Adapters::Adapter
end end
it 'raises if it does not respond to the registered method' do it 'raises if it does not respond to the registered method' do
device = Struct.new(:bar).new('bar') device = Struct.new(:bar).new('bar')
expect(adapter).to_not receive(:new) expect(adapter).to_not receive(:new)
expect { Rackstash::Adapters[device] }.to raise_error(ArgumentError) expect { described_class[device] }.to raise_error(ArgumentError)
end end
end end
@ -180,52 +180,62 @@ describe Rackstash::Adapters do
it 'creates an adapter if the proc returns true' do it 'creates an adapter if the proc returns true' do
checker = proc { true } checker = proc { true }
Rackstash::Adapters.register adapter, checker described_class.register adapter, checker
expect(checker).to receive(:===).with(device).and_call_original expect(checker).to receive(:===).with(device).and_call_original
expect(adapter).to receive(:new).with(device).and_call_original expect(adapter).to receive(:new).with(device).and_call_original
expect(Rackstash::Adapters[device]).to be_an Rackstash::Adapters::Adapter expect(described_class[device]).to be_an Rackstash::Adapters::Adapter
end end
it 'does not create an adapter if the proc returns false' do it 'does not create an adapter if the proc returns false' do
checker = proc { false } checker = proc { false }
Rackstash::Adapters.register adapter, checker described_class.register adapter, checker
expect(checker).to receive(:===).with(device).and_call_original expect(checker).to receive(:===).with(device).and_call_original
expect(adapter).to_not receive(:new) expect(adapter).to_not receive(:new)
expect { Rackstash::Adapters[device] }.to raise_error(ArgumentError) expect { described_class[device] }.to raise_error(ArgumentError)
end end
end end
context 'with a registered scheme' do context 'with a registered scheme' do
before do before do
Rackstash::Adapters.register adapter, 'dummy' described_class.register adapter, 'dummy'
end end
it 'creates an adapter from the scheme' do it 'creates an adapter from the scheme' do
raw_uri = 'dummy://example.com' raw_uri = 'dummy://example.com'
expect(adapter).to receive(:from_uri).with(URI(raw_uri)).and_call_original expect(adapter).to receive(:from_uri).with(URI(raw_uri)).and_call_original
expect(Rackstash::Adapters[raw_uri]).to be_an Rackstash::Adapters::Adapter expect(described_class[raw_uri]).to be_an Rackstash::Adapters::Adapter
end
it 'calls adapter.new if adapter.from_uri is not available' do
plain_adapter = Class.new(Rackstash::Adapters::Adapter)
described_class.register plain_adapter, 'dummy'
raw_uri = 'dummy://example.com'
expect(plain_adapter).to receive(:new).with(URI(raw_uri)).and_call_original
expect(described_class[raw_uri]).to be_a plain_adapter
end end
it 'creates an adapter from a URI' do it 'creates an adapter from a URI' do
uri = URI('dummy://example.com') uri = URI('dummy://example.com')
expect(adapter).to receive(:from_uri).with(uri).and_call_original expect(adapter).to receive(:from_uri).with(uri).and_call_original
expect(Rackstash::Adapters[uri]).to be_an Rackstash::Adapters::Adapter expect(described_class[uri]).to be_an Rackstash::Adapters::Adapter
end end
it 'raises if no scheme was found' do it 'raises if no scheme was found' do
expect(adapter).to_not receive(:new) expect(adapter).to_not receive(:new)
expect(adapter).to_not receive(:from_uri) expect(adapter).to_not receive(:from_uri)
expect { Rackstash::Adapters['unknown://example.com'] } expect { described_class['unknown://example.com'] }
.to raise_error(ArgumentError) .to raise_error(ArgumentError)
expect { Rackstash::Adapters[URI('unknown://example.com')] } expect { described_class[URI('unknown://example.com')] }
.to raise_error(ArgumentError) .to raise_error(ArgumentError)
end end
context 'and a registered class' do context 'and a registered class' do
before do before do
Rackstash::Adapters.register adapter, Object described_class.register adapter, Object
end end
it 'falls though on invalid URI' do it 'falls though on invalid URI' do
@ -234,7 +244,7 @@ describe Rackstash::Adapters do
expect(adapter).to_not receive(:from_uri) expect(adapter).to_not receive(:from_uri)
# from the fallback # from the fallback
expect(adapter).to receive(:new).with(invalid_uri).and_call_original expect(adapter).to receive(:new).with(invalid_uri).and_call_original
expect(Rackstash::Adapters[invalid_uri]).to be_an Rackstash::Adapters::Adapter expect(described_class[invalid_uri]).to be_an Rackstash::Adapters::Adapter
end end
it 'falls though if no scheme was found' do it 'falls though if no scheme was found' do
@ -242,7 +252,7 @@ describe Rackstash::Adapters do
expect(adapter).to_not receive(:from_uri) expect(adapter).to_not receive(:from_uri)
expect(adapter).to receive(:new).with(unknown_uri).and_call_original expect(adapter).to receive(:new).with(unknown_uri).and_call_original
expect(Rackstash::Adapters[unknown_uri]).to be_an Rackstash::Adapters::Adapter expect(described_class[unknown_uri]).to be_an Rackstash::Adapters::Adapter
end end
end end
end end
@ -250,10 +260,10 @@ describe Rackstash::Adapters do
context 'with an existing adapter object' do context 'with an existing adapter object' do
it 'just returns the object' do it 'just returns the object' do
adapter_instance = adapter.new adapter_instance = adapter.new
Rackstash::Adapters.register adapter, Object described_class.register adapter, Object
expect(adapter).to_not receive(:new) expect(adapter).to_not receive(:new)
expect(Rackstash::Adapters[adapter_instance]).to equal adapter_instance expect(described_class[adapter_instance]).to equal adapter_instance
end end
end end
end end

View File

@ -12,7 +12,7 @@ require 'rackstash/buffer'
describe Rackstash::Buffer do describe Rackstash::Buffer do
let(:buffer_options) { {} } let(:buffer_options) { {} }
let(:sink) { instance_double(Rackstash::Sink) } let(:sink) { instance_double(Rackstash::Sink) }
let(:buffer) { Rackstash::Buffer.new(sink, **buffer_options) } let(:buffer) { described_class.new(sink, **buffer_options) }
describe '#allow_empty?' do describe '#allow_empty?' do
it 'defaults to false' do it 'defaults to false' do

View File

@ -11,7 +11,7 @@ require 'rackstash/buffer_stack'
describe Rackstash::BufferStack do describe Rackstash::BufferStack do
let(:sink) { instance_double(Rackstash::Sink) } let(:sink) { instance_double(Rackstash::Sink) }
let(:stack) { Rackstash::BufferStack.new(sink) } let(:stack) { described_class.new(sink) }
describe '#current' do describe '#current' do
it 'initializes a buffer' do it 'initializes a buffer' do

View File

@ -10,7 +10,7 @@ require 'spec_helper'
require 'rackstash/encoders/json' require 'rackstash/encoders/json'
describe Rackstash::Encoders::JSON do describe Rackstash::Encoders::JSON do
let(:encoder) { Rackstash::Encoders::JSON.new } let(:encoder) { described_class.new }
describe '#encode' do describe '#encode' do
it 'formats the passed event hash as a JSON string' do it 'formats the passed event hash as a JSON string' do

View File

@ -10,7 +10,7 @@ require 'spec_helper'
require 'rackstash/encoders/message' require 'rackstash/encoders/message'
describe Rackstash::Encoders::Message do describe Rackstash::Encoders::Message do
let(:encoder) { Rackstash::Encoders::Message.new } let(:encoder) { described_class.new }
describe '#encode' do describe '#encode' do
it 'gets the message from the event hash' do it 'gets the message from the event hash' do

View File

@ -10,7 +10,7 @@ require 'spec_helper'
require 'rackstash/encoders/raw' require 'rackstash/encoders/raw'
describe Rackstash::Encoders::Raw do describe Rackstash::Encoders::Raw do
let(:encoder) { Rackstash::Encoders::Raw.new } let(:encoder) { described_class.new }
describe '#encode' do describe '#encode' do
it 'passes the raw event through' do it 'passes the raw event through' do

View File

@ -12,7 +12,7 @@ require 'rackstash/fields/array'
require 'rackstash/fields/hash' require 'rackstash/fields/hash'
describe Rackstash::Fields::AbstractCollection do describe Rackstash::Fields::AbstractCollection do
let(:collection) { Rackstash::Fields::AbstractCollection.new } let(:collection) { described_class.new }
def normalize(*args) def normalize(*args)
collection.send(:normalize, *args) collection.send(:normalize, *args)
@ -42,7 +42,7 @@ describe Rackstash::Fields::AbstractCollection do
expect(collection).to receive(:eql?).twice.and_call_original expect(collection).to receive(:eql?).twice.and_call_original
expect(collection).to receive(:==).twice.and_call_original expect(collection).to receive(:==).twice.and_call_original
other = Rackstash::Fields::AbstractCollection.new other = described_class.new
expect(collection).to eql other expect(collection).to eql other
expect(collection).to eq other expect(collection).to eq other
@ -97,7 +97,7 @@ describe Rackstash::Fields::AbstractCollection do
it 'returns the same hash for the same raw content' do it 'returns the same hash for the same raw content' do
collection.send(:raw=, [123, 'foo']) collection.send(:raw=, [123, 'foo'])
collection2 = Rackstash::Fields::AbstractCollection.new collection2 = described_class.new
collection2.send(:raw=, [123, 'foo']) collection2.send(:raw=, [123, 'foo'])
expect(collection.send(:raw)).not_to equal collection2.send(:raw) expect(collection.send(:raw)).not_to equal collection2.send(:raw)

View File

@ -10,18 +10,18 @@ require 'spec_helper'
require 'rackstash/fields/array' require 'rackstash/fields/array'
describe Rackstash::Fields::Array do describe Rackstash::Fields::Array do
let(:array) { Rackstash::Fields::Array.new } let(:array) { described_class.new }
describe '#+' do describe '#+' do
it 'returns the addition of elements' do it 'returns the addition of elements' do
array[0] = 'existing' array[0] = 'existing'
expect(array + ['existing', -> { 'new' }, [:nested]]) expect(array + ['existing', -> { 'new' }, [:nested]])
.to contain_exactly('existing', 'existing', 'new', ['nested']) .to contain_exactly('existing', 'existing', 'new', ['nested'])
.and be_a(Rackstash::Fields::Array) .and be_a(described_class)
end end
it 'returns a new Array' do it 'returns a new Array' do
expect(array + [:foo]).to be_a(Rackstash::Fields::Array) expect(array + [:foo]).to be_a(described_class)
expect(array + [:foo]).not_to equal array expect(array + [:foo]).not_to equal array
end end
end end
@ -34,7 +34,7 @@ describe Rackstash::Fields::Array do
end end
it 'returns a new Array' do it 'returns a new Array' do
expect(array - [:foo]).to be_a(Rackstash::Fields::Array).and be_empty expect(array - [:foo]).to be_a(described_class).and be_empty
expect(array - [:foo]).not_to equal array expect(array - [:foo]).not_to equal array
end end
end end
@ -47,7 +47,7 @@ describe Rackstash::Fields::Array do
end end
it 'returns a new Array' do it 'returns a new Array' do
expect(array | [:foo]).to be_a(Rackstash::Fields::Array) expect(array | [:foo]).to be_a(described_class)
expect(array | [:foo]).not_to equal array expect(array | [:foo]).not_to equal array
end end
end end
@ -60,7 +60,7 @@ describe Rackstash::Fields::Array do
end end
it 'returns a new Array' do it 'returns a new Array' do
expect(array & [:foo]).to be_a(Rackstash::Fields::Array).and be_empty expect(array & [:foo]).to be_a(described_class).and be_empty
expect(array & [:foo]).not_to equal array expect(array & [:foo]).not_to equal array
end end
end end
@ -78,7 +78,7 @@ describe Rackstash::Fields::Array do
end end
it 'returns an array from start, end' do it 'returns an array from start, end' do
expect(array[1, 3]).to be_a Rackstash::Fields::Array expect(array[1, 3]).to be_a described_class
expect(array[1, 3].to_ary).to eql %w[foo bar baz] expect(array[1, 3].to_ary).to eql %w[foo bar baz]
expect(array[2, 0].to_ary).to eql [] expect(array[2, 0].to_ary).to eql []
@ -87,7 +87,7 @@ describe Rackstash::Fields::Array do
end end
it 'returns an array from a range' do it 'returns an array from a range' do
expect(array[1..3]).to be_a Rackstash::Fields::Array expect(array[1..3]).to be_a described_class
expect(array[1..3].to_ary).to eql %w[foo bar baz] expect(array[1..3].to_ary).to eql %w[foo bar baz]
expect(array[2..4].to_ary).to eql %w[bar baz] expect(array[2..4].to_ary).to eql %w[bar baz]
@ -167,7 +167,7 @@ describe Rackstash::Fields::Array do
end end
it 'returns a nested array' do it 'returns a nested array' do
expect(array[2]).to be_a Rackstash::Fields::Array expect(array[2]).to be_a described_class
expect(array.as_json[2]).to be_instance_of ::Array expect(array.as_json[2]).to be_instance_of ::Array
expect(array.as_json[2]).to eql %w[v1 v2] expect(array.as_json[2]).to eql %w[v1 v2]
@ -283,11 +283,11 @@ describe Rackstash::Fields::Array do
array[0] = 'existing' array[0] = 'existing'
expect(array.merge(['new', :existing, -> { 123 }])) expect(array.merge(['new', :existing, -> { 123 }]))
.to contain_exactly('existing', 'new', 123) .to contain_exactly('existing', 'new', 123)
.and be_a(Rackstash::Fields::Array) .and be_a(described_class)
end end
it 'returns a new Array' do it 'returns a new Array' do
expect(array.merge([:foo])).to be_a(Rackstash::Fields::Array) expect(array.merge([:foo])).to be_a(described_class)
expect(array.merge([:foo])).not_to equal array expect(array.merge([:foo])).not_to equal array
end end
@ -348,7 +348,7 @@ describe Rackstash::Fields::Array do
value = ['hello'] value = ['hello']
array.push value array.push value
expect(array[0]).to be_a Rackstash::Fields::Array expect(array[0]).to be_a described_class
expect(array[0].to_a).to eql value expect(array[0].to_a).to eql value
end end
@ -400,7 +400,7 @@ describe Rackstash::Fields::Array do
raw = [Time.now, 'foo'] raw = [Time.now, 'foo']
array = Rackstash::Fields::Array(raw) array = Rackstash::Fields::Array(raw)
expect(array).to be_a Rackstash::Fields::Array expect(array).to be_a described_class
expect(array[0]).to be_a String expect(array[0]).to be_a String
expect(array[1]).to eql 'foo' expect(array[1]).to eql 'foo'
end end

View File

@ -128,7 +128,7 @@ describe Rackstash::Fields::Hash do
end end
it 'returns a nested hash' do it 'returns a nested hash' do
expect(hash['hash']).to be_instance_of Rackstash::Fields::Hash expect(hash['hash']).to be_instance_of described_class
expect(hash.as_json['hash']).to be_instance_of Hash expect(hash.as_json['hash']).to be_instance_of Hash
expect(hash.as_json['hash']).to eql 'key' => 'nested value', 'number' => 42 expect(hash.as_json['hash']).to eql 'key' => 'nested value', 'number' => 42
@ -200,7 +200,7 @@ describe Rackstash::Fields::Hash do
hash['foo'] = ['bar'] hash['foo'] = ['bar']
new_hash = hash.deep_merge('beep' => :boop, 'foo' => [123]) new_hash = hash.deep_merge('beep' => :boop, 'foo' => [123])
expect(new_hash).to be_a Rackstash::Fields::Hash expect(new_hash).to be_a described_class
expect(hash).not_to have_key 'beep' expect(hash).not_to have_key 'beep'
expect(hash['foo']).to contain_exactly 'bar' expect(hash['foo']).to contain_exactly 'bar'
@ -283,7 +283,7 @@ describe Rackstash::Fields::Hash do
it 'allows to merge forbidden fields in nested hashes' do it 'allows to merge forbidden fields in nested hashes' do
hash.deep_merge!({ top: { 'forbidden' => 'value' } }, force: true) hash.deep_merge!({ top: { 'forbidden' => 'value' } }, force: true)
expect(hash['top']) expect(hash['top'])
.to be_a(Rackstash::Fields::Hash) .to be_a(described_class)
.and have_key 'forbidden' .and have_key 'forbidden'
end end
end end
@ -311,12 +311,12 @@ describe Rackstash::Fields::Hash do
hash.deep_merge!({ 'key' => [:foo, 'baz'] }, force: false) hash.deep_merge!({ 'key' => [:foo, 'baz'] }, force: false)
expect(hash['key']) expect(hash['key'])
.to be_a(Rackstash::Fields::Hash) .to be_a(described_class)
.and have_key 'nested_key' .and have_key 'nested_key'
hash.deep_merge!({ 'key' => 123 }, force: false) hash.deep_merge!({ 'key' => 123 }, force: false)
expect(hash['key']) expect(hash['key'])
.to be_a(Rackstash::Fields::Hash) .to be_a(described_class)
.and have_key 'nested_key' .and have_key 'nested_key'
end end
@ -325,7 +325,7 @@ describe Rackstash::Fields::Hash do
expect(hash).to have_key 'key' expect(hash).to have_key 'key'
hash.deep_merge!({ 'key' => { nested: 'value' } }, force: false) hash.deep_merge!({ 'key' => { nested: 'value' } }, force: false)
expect(hash['key']).to be_a Rackstash::Fields::Hash expect(hash['key']).to be_a described_class
end end
it 'ignores forbidden fields' do it 'ignores forbidden fields' do
@ -339,7 +339,7 @@ describe Rackstash::Fields::Hash do
it 'allows to merge forbidden fields in nested hashes' do it 'allows to merge forbidden fields in nested hashes' do
hash.deep_merge!({ top: { 'forbidden' => 'value' } }, force: false) hash.deep_merge!({ top: { 'forbidden' => 'value' } }, force: false)
expect(hash['top']) expect(hash['top'])
.to be_a(Rackstash::Fields::Hash) .to be_a(described_class)
.and have_key 'forbidden' .and have_key 'forbidden'
end end
end end
@ -473,10 +473,10 @@ describe Rackstash::Fields::Hash do
end end
it 'merges an empty hash with compatible arguments' do it 'merges an empty hash with compatible arguments' do
empty_hash = Rackstash::Fields::Hash.new empty_hash = described_class.new
expect(hash.merge!({})).to eql empty_hash expect(hash.merge!({})).to eql empty_hash
expect(hash.merge!(Rackstash::Fields::Hash.new)).to eql empty_hash expect(hash.merge!(described_class.new)).to eql empty_hash
end end
it 'merges a normalized hash' do it 'merges a normalized hash' do
@ -572,7 +572,7 @@ describe Rackstash::Fields::Hash do
it 'returns a new object' do it 'returns a new object' do
new_hash = hash.merge(foo: :bar) new_hash = hash.merge(foo: :bar)
expect(new_hash).to be_instance_of Rackstash::Fields::Hash expect(new_hash).to be_instance_of described_class
expect(new_hash).not_to equal hash expect(new_hash).not_to equal hash
# The origiginal hash is not changed # The origiginal hash is not changed

View File

@ -10,7 +10,7 @@ require 'spec_helper'
require 'rackstash/fields/tags' require 'rackstash/fields/tags'
describe Rackstash::Fields::Tags do describe Rackstash::Fields::Tags do
let(:tags) { Rackstash::Fields::Tags.new } let(:tags) { described_class.new }
describe '#<<' do describe '#<<' do
it 'adds a single tag' do it 'adds a single tag' do
@ -74,7 +74,7 @@ describe Rackstash::Fields::Tags do
it 'returns a new object' do it 'returns a new object' do
new_tags = tags.merge ['hello'] new_tags = tags.merge ['hello']
expect(new_tags).to be_a Rackstash::Fields::Tags expect(new_tags).to be_a described_class
expect(new_tags.tagged?('hello')).to be true expect(new_tags.tagged?('hello')).to be true
expect(new_tags).not_to equal tags expect(new_tags).not_to equal tags
@ -111,7 +111,7 @@ describe Rackstash::Fields::Tags do
end end
it 'accepts tags' do it 'accepts tags' do
new_tags = Rackstash::Fields::Tags.new new_tags = described_class.new
new_tags << 'foo' new_tags << 'foo'
tags.merge! [new_tags] tags.merge! [new_tags]
@ -158,7 +158,7 @@ describe Rackstash::Fields::Tags do
raw = [Time.now, 'foo'] raw = [Time.now, 'foo']
tags = Rackstash::Fields::Tags(raw) tags = Rackstash::Fields::Tags(raw)
expect(tags).to be_a Rackstash::Fields::Tags expect(tags).to be_a described_class
expect(tags.to_a).to match [String, 'foo'] expect(tags.to_a).to match [String, 'foo']
end end
end end

View File

@ -11,7 +11,7 @@ require 'rackstash/flows'
require 'rackstash/flow' require 'rackstash/flow'
describe Rackstash::Flows do describe Rackstash::Flows do
let(:flows) { Rackstash::Flows.new } let(:flows) { described_class.new }
def a_flow def a_flow
flow = instance_double('Rackstash::Flow') flow = instance_double('Rackstash::Flow')
@ -21,17 +21,17 @@ describe Rackstash::Flows do
describe '#initialize' do describe '#initialize' do
it 'accepts a single flow' do it 'accepts a single flow' do
list = Rackstash::Flows.new(a_flow) list = described_class.new(a_flow)
expect(list.size).to eql 1 expect(list.size).to eql 1
end end
it 'accepts a list of flows' do it 'accepts a list of flows' do
raw_flows = Array.new(3) { a_flow } raw_flows = Array.new(3) { a_flow }
list_with_array = Rackstash::Flows.new(raw_flows) list_with_array = described_class.new(raw_flows)
expect(list_with_array.size).to eql 3 expect(list_with_array.size).to eql 3
list_with_splat = Rackstash::Flows.new(*raw_flows) list_with_splat = described_class.new(*raw_flows)
expect(list_with_splat.size).to eql 3 expect(list_with_splat.size).to eql 3
end end
@ -39,7 +39,7 @@ describe Rackstash::Flows do
flow_class = class_double('Rackstash::Flow').as_stubbed_const flow_class = class_double('Rackstash::Flow').as_stubbed_const
expect(flow_class).to receive(:new).with(:dummy).and_return(a_flow) expect(flow_class).to receive(:new).with(:dummy).and_return(a_flow)
Rackstash::Flows.new(:dummy) described_class.new(:dummy)
end end
end end

View File

@ -11,7 +11,7 @@ require 'time'
require 'rackstash/formatter' require 'rackstash/formatter'
describe Rackstash::Formatter do describe Rackstash::Formatter do
let(:formatter) { Rackstash::Formatter.new } let(:formatter) { described_class.new }
it 'formats plain strings' do it 'formats plain strings' do
expect(formatter.call('ERROR', Time.now, 'progname', 'Hello')).to eql "Hello\n" expect(formatter.call('ERROR', Time.now, 'progname', 'Hello')).to eql "Hello\n"
@ -50,7 +50,7 @@ describe Rackstash::Formatter do
end end
describe Rackstash::RawFormatter do describe Rackstash::RawFormatter do
let(:formatter) { Rackstash::RawFormatter.new } let(:formatter) { described_class.new }
it 'returns the message' do it 'returns the message' do
msg = 'my message' msg = 'my message'

View File

@ -10,8 +10,8 @@ require 'spec_helper'
require 'rackstash/logger' require 'rackstash/logger'
describe Rackstash::Logger do describe Rackstash::Logger do
let(:targets) { double('targets') } let(:target) { StringIO.new }
let(:logger) { Rackstash::Logger.new(targets) } let(:logger) { described_class.new(target) }
describe '#formatter' do describe '#formatter' do
it 'defaults to a Rackstash::Formatter' do it 'defaults to a Rackstash::Formatter' do

View File

@ -23,7 +23,7 @@ describe Rackstash::Message do
] ]
messages.each do |msg, clean| messages.each do |msg, clean|
message = Rackstash::Message.new(msg) message = described_class.new(msg)
expect(message.message).to eql clean expect(message.message).to eql clean
expect(message.message).to be_frozen expect(message.message).to be_frozen
end end
@ -34,25 +34,25 @@ describe Rackstash::Message do
latin_str = utf8_str.encode(Encoding::ISO8859_9) latin_str = utf8_str.encode(Encoding::ISO8859_9)
expect(latin_str.encoding).to eql Encoding::ISO8859_9 expect(latin_str.encoding).to eql Encoding::ISO8859_9
message = Rackstash::Message.new(latin_str) message = described_class.new(latin_str)
expect(message.message).to eql utf8_str expect(message.message).to eql utf8_str
expect(message.message.encoding).to eql Encoding::UTF_8 expect(message.message.encoding).to eql Encoding::UTF_8
end end
it 'does not raise an error on incompatible encodings' do it 'does not raise an error on incompatible encodings' do
binary = Digest::SHA256.digest('string') binary = Digest::SHA256.digest('string')
message = Rackstash::Message.new(binary) message = described_class.new(binary)
expect(message.message).to include '<27>' expect(message.message).to include '<27>'
expect(message.message.encoding).to eql Encoding::UTF_8 expect(message.message.encoding).to eql Encoding::UTF_8
end end
it 'accepts non-string objects' do it 'accepts non-string objects' do
message = Rackstash::Message.new(StandardError.new('An error')) message = described_class.new(StandardError.new('An error'))
expect(message.message).to eql '#<StandardError: An error>' expect(message.message).to eql '#<StandardError: An error>'
expect(message.message).to be_frozen expect(message.message).to be_frozen
message = Rackstash::Message.new(:symbol) message = described_class.new(:symbol)
expect(message.message).to eql ':symbol' expect(message.message).to eql ':symbol'
expect(message.message).to be_frozen expect(message.message).to be_frozen
end end
@ -61,7 +61,7 @@ describe Rackstash::Message do
str = 'hello' str = 'hello'
expect(str.encoding).to eql Encoding::UTF_8 expect(str.encoding).to eql Encoding::UTF_8
message = Rackstash::Message.new(str) message = described_class.new(str)
expect(message.message).to be_frozen expect(message.message).to be_frozen
expect(message.message).not_to equal str expect(message.message).not_to equal str
expect(message.message).to eql str expect(message.message).to eql str
@ -70,49 +70,49 @@ describe Rackstash::Message do
describe '#message' do describe '#message' do
it 'is aliased to to_str' do it 'is aliased to to_str' do
message = Rackstash::Message.new('hello world') message = described_class.new('hello world')
expect(message.to_s).to eql 'hello world' expect(message.to_s).to eql 'hello world'
end end
it 'is aliased to to_str' do it 'is aliased to to_str' do
message = Rackstash::Message.new('hello world') message = described_class.new('hello world')
expect(message.to_str).to eql 'hello world' expect(message.to_str).to eql 'hello world'
end end
it 'is aliased to as_json' do it 'is aliased to as_json' do
message = Rackstash::Message.new('hello world') message = described_class.new('hello world')
expect(message.as_json).to eql 'hello world' expect(message.as_json).to eql 'hello world'
end end
end end
describe '#severity' do describe '#severity' do
it 'defaults to UNKNOWN' do it 'defaults to UNKNOWN' do
expect(Rackstash::Message.new('').severity).to eql 5 expect(described_class.new('').severity).to eql 5
end end
it 'accepts any non-negative integer' do it 'accepts any non-negative integer' do
expect(Rackstash::Message.new('', severity: 0).severity).to eql 0 expect(described_class.new('', severity: 0).severity).to eql 0
expect(Rackstash::Message.new('', severity: 1).severity).to eql 1 expect(described_class.new('', severity: 1).severity).to eql 1
expect(Rackstash::Message.new('', severity: 23).severity).to eql 23 expect(described_class.new('', severity: 23).severity).to eql 23
expect(Rackstash::Message.new('', severity: '3').severity).to eql 3 expect(described_class.new('', severity: '3').severity).to eql 3
end end
it 'uses 0 for negative severities' do it 'uses 0 for negative severities' do
expect(Rackstash::Message.new('', severity: -1).severity).to eql 0 expect(described_class.new('', severity: -1).severity).to eql 0
expect(Rackstash::Message.new('', severity: -42).severity).to eql 0 expect(described_class.new('', severity: -42).severity).to eql 0
end end
it 'does not accept non-integers' do it 'does not accept non-integers' do
expect { Rackstash::Message.new('', severity: nil) }.to raise_error TypeError expect { described_class.new('', severity: nil) }.to raise_error TypeError
expect { Rackstash::Message.new('', severity: [42]) }.to raise_error TypeError expect { described_class.new('', severity: [42]) }.to raise_error TypeError
expect { Rackstash::Message.new('', severity: 'foo') }.to raise_error ArgumentError expect { described_class.new('', severity: 'foo') }.to raise_error ArgumentError
end end
end end
describe '#progname' do describe '#progname' do
it 'dup-freezes a mutable progname' do it 'dup-freezes a mutable progname' do
progname = String.new('a message') progname = String.new('a message')
message = Rackstash::Message.new('', progname: progname) message = described_class.new('', progname: progname)
expect(message.progname).to eql progname expect(message.progname).to eql progname
expect(message.progname).not_to equal progname expect(message.progname).not_to equal progname
@ -120,18 +120,18 @@ describe Rackstash::Message do
end end
it 'defaults to PROGNAME' do it 'defaults to PROGNAME' do
expect(Rackstash::Message.new('').progname).to match %r{\Arackstash/v\d+(\..+)*\z} expect(described_class.new('').progname).to match %r{\Arackstash/v\d+(\..+)*\z}
end end
end end
describe '#length' do describe '#length' do
it 'returns the size if the message' do it 'returns the size if the message' do
message = Rackstash::Message.new('hello world') message = described_class.new('hello world')
expect(message.length).to eql 11 expect(message.length).to eql 11
end end
it 'can use the #size alias' do it 'can use the #size alias' do
message = Rackstash::Message.new('hello world') message = described_class.new('hello world')
expect(message.size).to eql 11 expect(message.size).to eql 11
end end
end end
@ -139,16 +139,16 @@ describe Rackstash::Message do
describe '#severity_label' do describe '#severity_label' do
it 'returns the severity label' do it 'returns the severity label' do
expect(Rackstash).to receive(:severity_label).exactly(3).times.and_call_original expect(Rackstash).to receive(:severity_label).exactly(3).times.and_call_original
expect(Rackstash::Message.new('', severity: 0).severity_label).to eql 'DEBUG' expect(described_class.new('', severity: 0).severity_label).to eql 'DEBUG'
expect(Rackstash::Message.new('', severity: 2).severity_label).to eql 'WARN' expect(described_class.new('', severity: 2).severity_label).to eql 'WARN'
expect(Rackstash::Message.new('', severity: 5).severity_label).to eql 'ANY' expect(described_class.new('', severity: 5).severity_label).to eql 'ANY'
end end
end end
describe '#time' do describe '#time' do
it 'dups the time' do it 'dups the time' do
time = Time.now time = Time.now
message = Rackstash::Message.new('', time: time) message = described_class.new('', time: time)
expect(message.time).to eql time expect(message.time).to eql time
expect(message.time).not_to equal time expect(message.time).not_to equal time
@ -158,15 +158,15 @@ describe Rackstash::Message do
end end
it 'defaults to Time.now.utc' do it 'defaults to Time.now.utc' do
expect(Rackstash::Message.new('').time).to be_within(1).of(Time.now) expect(described_class.new('').time).to be_within(1).of(Time.now)
expect(Rackstash::Message.new('').time).to be_frozen expect(described_class.new('').time).to be_frozen
expect(Rackstash::Message.new('').time).to be_utc expect(described_class.new('').time).to be_utc
end end
end end
describe '#to_json' do describe '#to_json' do
it 'formats the message as JSON' do it 'formats the message as JSON' do
message = Rackstash::Message.new('hello world') message = described_class.new('hello world')
expect(message.to_json).to eql '"hello world"' expect(message.to_json).to eql '"hello world"'
end end
end end

View File

@ -11,7 +11,7 @@ require 'rackstash/sink'
describe Rackstash::Sink do describe Rackstash::Sink do
let(:targets) { [] } let(:targets) { [] }
let(:sink) { Rackstash::Sink.new(targets) } let(:sink) { described_class.new(targets) }
describe '#initialize' do describe '#initialize' do
it 'accepts an array with targets' do it 'accepts an array with targets' do
@ -21,7 +21,7 @@ describe Rackstash::Sink do
it 'wraps a single target into an array' do it 'wraps a single target into an array' do
target = Object.new target = Object.new
expect(Rackstash::Sink.new(target).targets).to eql [target] expect(described_class.new(target).targets).to eql [target]
end end
end end