mirror of
https://github.com/meineerde/rackstash.git
synced 2025-10-17 14:01:01 +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
|
||||
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_SET = Set.new.freeze
|
||||
|
||||
@ -51,16 +51,26 @@ describe Rackstash do
|
||||
end
|
||||
end
|
||||
|
||||
it 'defines UNDEFINED' do
|
||||
expect(Rackstash::UNDEFINED).to be_instance_of Rackstash::UndefinedClass
|
||||
expect(Rackstash::UNDEFINED.to_s).to eql 'undefined'
|
||||
describe 'UNDEFINED' do
|
||||
it 'defines the UndefinedClass' do
|
||||
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
|
||||
expect(Rackstash::UNDEFINED).not_to eql false
|
||||
expect(Rackstash::UNDEFINED).not_to eql true
|
||||
expect(Rackstash::UNDEFINED).not_to eql 42
|
||||
it 'defines the singleton object' do
|
||||
expect(Rackstash::UNDEFINED).to be_instance_of Rackstash::UndefinedClass
|
||||
expect(Rackstash::UNDEFINED.to_s).to eql 'undefined'
|
||||
|
||||
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
|
||||
|
||||
describe '.severity_label' do
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user