Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add conversion methods to Twitter::NullObject

  • Loading branch information...
commit 4900fee474feaa1514c06d459a9da6d52c45a60e 1 parent 940e57f
@sferik authored
Showing with 77 additions and 11 deletions.
  1. +7 −4 lib/twitter/null_object.rb
  2. +70 −7 spec/twitter/null_object_spec.rb
View
11 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)
View
77 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
Please sign in to comment.
Something went wrong with that request. Please try again.