Skip to content

Commit

Permalink
Add conversion methods to Twitter::NullObject
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jan 6, 2014
1 parent 940e57f commit 4900fee
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 11 deletions.
11 changes: 7 additions & 4 deletions lib/twitter/null_object.rb
@@ -1,9 +1,12 @@
require 'forwardable'

module Twitter
class NullObject
# @return [TrueClass] This method always returns true.
def nil?
true
end
extend Forwardable
def_instance_delegators :nil, :nil?, :to_a, :to_c, :to_c, :to_f, :to_h,
:to_i, :to_r, :to_s
alias_method :to_ary, :to_a
alias_method :to_str, :to_s

# @return [Twitter::NullObject] This method always returns self.
def method_missing(*args, &block)
Expand Down
77 changes: 70 additions & 7 deletions spec/twitter/null_object_spec.rb
Expand Up @@ -2,25 +2,88 @@

describe Twitter::NullObject do

before do
@null_object = Twitter::NullObject.new
end

describe '#nil?' do
it 'returns true' do
expect(@null_object.nil?).to be true
expect(subject.nil?).to be true
end
end

describe '#to_a' do
it 'returns an empty array' do
expect(subject.to_a).to be_an Array
expect(subject.to_a).to be_empty
end
end

describe '#to_ary' do
it 'returns an empty array' do
expect(subject.to_ary).to be_an Array
expect(subject.to_ary).to be_empty
end
end

if RUBY_VERSION >= '1.9'
describe '#to_c' do
it 'returns zero as a complex number' do
expect(subject.to_c).to be_a Complex
expect(subject.to_c).to be_zero
end
end

describe '#to_r' do
it 'returns zero as a rational number' do
expect(subject.to_r).to be_a Rational
expect(subject.to_r).to be_zero
end
end
end

if RUBY_VERSION >= '2.0'
describe '#to_h' do
it 'returns an empty hash' do
expect(subject.to_h).to be_a Hash
expect(subject.to_h).to be_empty
end
end
end

describe '#to_f' do
it 'returns zero as a floating point number' do
expect(subject.to_f).to be_a Float
expect(subject.to_f).to be_zero
end
end

describe '#to_i' do
it 'returns zero' do
expect(subject.to_i).to be_an Integer
expect(subject.to_i).to be_zero
end
end

describe '#to_s' do
it 'returns an empty string' do
expect(subject.to_s).to be_a String
expect(subject.to_s).to be_empty
end
end

describe '#to_str' do
it 'returns an empty string' do
expect(subject.to_str).to be_a String
expect(subject.to_str).to be_empty
end
end

describe 'calling any method' do
it 'returns self' do
expect(@null_object.any).to equal @null_object
expect(subject.any).to equal subject
end
end

describe '#respond_to?' do
it 'returns true' do
expect(@null_object.respond_to?(:any)).to be true
expect(subject.respond_to?(:any)).to be true
end
end

Expand Down

0 comments on commit 4900fee

Please sign in to comment.