From 4900fee474feaa1514c06d459a9da6d52c45a60e Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Mon, 6 Jan 2014 02:33:39 +0100 Subject: [PATCH] Add conversion methods to Twitter::NullObject --- lib/twitter/null_object.rb | 11 +++-- spec/twitter/null_object_spec.rb | 77 +++++++++++++++++++++++++++++--- 2 files changed, 77 insertions(+), 11 deletions(-) diff --git a/lib/twitter/null_object.rb b/lib/twitter/null_object.rb index 7fe5f15ca..ebf2d50e1 100644 --- a/lib/twitter/null_object.rb +++ b/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) diff --git a/spec/twitter/null_object_spec.rb b/spec/twitter/null_object_spec.rb index c0d2e86d0..96a2b9b8f 100644 --- a/spec/twitter/null_object_spec.rb +++ b/spec/twitter/null_object_spec.rb @@ -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