mirror of
https://github.com/meineerde/rackstash.git
synced 2025-12-19 15:01:12 +00:00
There can be only one... UNDEFINED
By ensuring that we only ever create a single UNDEFINED object from the UndefinedClass, it behaves more like nil
This commit is contained in:
parent
32e755396b
commit
74242caf0b
@ -84,7 +84,12 @@ module Rackstash
|
|||||||
alias inspect to_s
|
alias inspect to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
UNDEFINED = UndefinedClass.new
|
UNDEFINED = UndefinedClass.new.tap do |undefined|
|
||||||
|
class << undefined.class
|
||||||
|
undef_method :allocate
|
||||||
|
undef_method :new
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
EMPTY_STRING = ''.freeze
|
EMPTY_STRING = ''.freeze
|
||||||
EMPTY_SET = Set.new.freeze
|
EMPTY_SET = Set.new.freeze
|
||||||
|
|||||||
@ -51,16 +51,26 @@ describe Rackstash do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'defines UNDEFINED' do
|
describe 'UNDEFINED' do
|
||||||
expect(Rackstash::UNDEFINED).to be_instance_of Rackstash::UndefinedClass
|
it 'defines the UndefinedClass' do
|
||||||
expect(Rackstash::UNDEFINED.to_s).to eql 'undefined'
|
expect(Rackstash::UndefinedClass).to be_a Class
|
||||||
|
|
||||||
expect(Rackstash::UNDEFINED).to equal Rackstash::UNDEFINED
|
# No (further) ibjects can be created of this class
|
||||||
|
expect { Rackstash::UndefinedClass.new }.to raise_error NoMethodError
|
||||||
|
expect { Rackstash::UndefinedClass.allocate }.to raise_error NoMethodError
|
||||||
|
end
|
||||||
|
|
||||||
expect(Rackstash::UNDEFINED).not_to eql nil
|
it 'defines the singleton object' do
|
||||||
expect(Rackstash::UNDEFINED).not_to eql false
|
expect(Rackstash::UNDEFINED).to be_instance_of Rackstash::UndefinedClass
|
||||||
expect(Rackstash::UNDEFINED).not_to eql true
|
expect(Rackstash::UNDEFINED.to_s).to eql 'undefined'
|
||||||
expect(Rackstash::UNDEFINED).not_to eql 42
|
|
||||||
|
expect(Rackstash::UNDEFINED).to equal Rackstash::UNDEFINED
|
||||||
|
|
||||||
|
expect(Rackstash::UNDEFINED).not_to eql nil
|
||||||
|
expect(Rackstash::UNDEFINED).not_to eql false
|
||||||
|
expect(Rackstash::UNDEFINED).not_to eql true
|
||||||
|
expect(Rackstash::UNDEFINED).not_to eql 42
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '.severity_label' do
|
describe '.severity_label' do
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user