Skip to content

Commit

Permalink
Merge d3496c9 into a82d1d1
Browse files Browse the repository at this point in the history
  • Loading branch information
charlierudolph committed Feb 27, 2014
2 parents a82d1d1 + d3496c9 commit a4ac7f9
Show file tree
Hide file tree
Showing 14 changed files with 286 additions and 238 deletions.
File renamed without changes.
35 changes: 0 additions & 35 deletions spec/basic_null_object_spec.rb

This file was deleted.

6 changes: 6 additions & 0 deletions spec/blackhole_spec.rb → spec/black_hole_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@
expect(null.foobaz).to be(null)
expect(null << 'bar').to be(null)
end

it 'supports chaining of method calls' do
expect(null.foo).to be(null)
expect(null.foo.bar.baz).to be(null)
expect(null << 'hello' << 'world').to be(null)
end
end
16 changes: 16 additions & 0 deletions spec/custom_methods_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'spec_helper'

describe 'null object with custom methods' do
subject(:null) { null_class.new }
let(:null_class) do
Naught.build do |b|
def to_path
'/dev/null'
end
end
end

it 'responds to defined methods ' do
expect(null.to_path).to eq('/dev/null')
end
end
29 changes: 24 additions & 5 deletions spec/explicit_conversions_spec.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
require 'spec_helper.rb'

describe 'explicitly convertable null object' do
subject(:null) { null_class.new }
let(:null_class) do
Naught.build do |b|
b.define_explicit_conversions
end
end
subject(:null) { null_class.new }

it 'defines common explicit conversions to return zero values' do
it 'returns empty string for to_s' do
expect(null.to_s).to eq('')
end

it 'returns empty array for to_a' do
expect(null.to_a).to eq([])
end

it 'returns 0 for to_i' do
expect(null.to_i).to eq(0)
end

it 'returns 0.0 for to_f' do
expect(null.to_f).to eq(0.0)
if RUBY_VERSION >= '2.0'
expect(null.to_h).to eq({})
elsif RUBY_VERSION >= '1.9'
end

if RUBY_VERSION >= '1.9'
it 'returns Complex(0) for to_c' do
expect(null.to_c).to eq(Complex(0))
end

it 'returns Rational(0) for to_r' do
expect(null.to_r).to eq(Rational(0))
end
end

if RUBY_VERSION >= '2.0'
it 'returns empty hash for to_h' do
expect(null.to_h).to eq({})
end
end
end
33 changes: 33 additions & 0 deletions spec/impersonate_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'spec_helper'

describe 'null object impersonating another type' do
class Point
def x
23
end

def y
42
end
end

subject(:null) { impersonation_class.new }
let(:impersonation_class) do
Naught.build do |b|
b.impersonate Point
end
end

it 'matches the impersonated type' do
expect(null).to be_a Point
end

it 'responds to methods from the impersonated type' do
expect(null.x).to be_nil
expect(null.y).to be_nil
end

it 'does not respond to unknown methods' do
expect { null.foo }.to raise_error(NoMethodError)
end
end
33 changes: 20 additions & 13 deletions spec/implicit_conversions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,26 @@
b.define_implicit_conversions
end
end
it 'implicitly splats the same way an empty array does' do
a, b = null
expect(a).to be_nil
expect(b).to be_nil
end
it 'is implicitly convertable to String' do
expect(instance_eval(null)).to be_nil
end
it 'implicitly converts to an empty array' do
expect(null.to_ary).to eq([])
end
it 'implicitly converts to an empty string' do
expect(null.to_str).to eq('')

context 'to_ary' do
it 'returns empty array' do
expect(null.to_ary).to eq([])
end

it 'makes splats possible' do
a, b = null
expect(a).to be_nil
expect(b).to be_nil
end
end

context 'to_str' do
it 'returns empty string for to_str' do
expect(null.to_str).to eq('')
end

it 'makes instance_eval possible' do
expect(instance_eval(null)).to be_nil
end
end
end
38 changes: 2 additions & 36 deletions spec/mimic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def notify_of_overdue_books(titles)
b.mimic LibraryPatron
end
end

it 'responds to all methods defined on the target class' do
expect(null.member?).to be_nil
expect(null.name).to be_nil
Expand All @@ -52,6 +53,7 @@ def notify_of_overdue_books(titles)
expect(null).to respond_to(:notify_of_overdue_books)
expect(null).not_to respond_to(:foobar)
end

it 'has an informative inspect string' do
expect(null.inspect).to eq('<null:LibraryPatron>')
end
Expand Down Expand Up @@ -79,39 +81,3 @@ def notify_of_overdue_books(titles)
end
end
end

describe 'using mimic with black_hole' do
subject(:null) { mimic_class.new }
let(:mimic_class) do
Naught.build do |b|
b.mimic Logger
b.black_hole
end
end

def self.it_behaves_like_a_black_hole_mimic
it 'returns self from mimicked methods' do
expect(null.info).to equal(null)
expect(null.error).to equal(null)
expect(null << 'test').to equal(null)
end

it 'does not respond to methods not defined on the target class' do
expect { null.foobar }.to raise_error(NoMethodError)
end
end

it_behaves_like_a_black_hole_mimic

describe '(reverse order)' do
let(:mimic_class) do
Naught.build do |b|
b.black_hole
b.mimic Logger
end
end

it_behaves_like_a_black_hole_mimic
end

end
109 changes: 27 additions & 82 deletions spec/naught_spec.rb
Original file line number Diff line number Diff line change
@@ -1,101 +1,46 @@
require 'spec_helper'

describe 'null object impersonating another type' do
class Point
def x
23
end

def y
42
end
end

subject(:null) { impersonation_class.new }
let(:impersonation_class) do
Naught.build do |b|
b.impersonate Point
end
end
describe 'basic null object' do
let(:null_class) { Naught.build }
subject(:null) { null_class.new }

it 'matches the impersonated type' do
expect(null).to be_a Point
it 'responds to arbitrary messages and returns nil' do
expect(null.info).to be_nil
expect(null.foobaz).to be_nil
expect(null.to_s).to be_nil
end

it 'responds to methods from the impersonated type' do
expect(null.x).to be_nil
expect(null.y).to be_nil
it 'accepts any arguments for any messages' do
null.foobaz(1, 2, 3)
end

it 'does not respond to unknown methods' do
expect { null.foo }.to raise_error(NoMethodError)
it 'reports that it responds to any message' do
expect(null).to respond_to(:info)
expect(null).to respond_to(:foobaz)
expect(null).to respond_to(:to_s)
end
end

describe 'traceable null object' do
subject(:trace_null) do
null_object_and_line.first
end
let(:null_object_and_line) do
obj, line = trace_null_class.new, __LINE__
[obj, line]
end
let(:instantiation_line) { null_object_and_line.last }
let(:trace_null_class) do
Naught.build do |b|
b.traceable
end
it 'can be inspected' do
expect(null.inspect).to eq('<null>')
end

it 'remembers the file it was instantiated from' do
expect(trace_null.__file__).to eq(__FILE__)
it 'knows its own class' do
expect(null.class).to eq(null_class)
end

it 'remembers the line it was instantiated from' do
expect(trace_null.__line__).to eq(instantiation_line)
it 'aliases .new to .get' do
expect(null_class.get.class).to be(null_class)
end

def make_null
trace_null_class.get(:caller => caller(1))
end
describe 'a named class' do
TestNull = Naught.build

it 'can accept custom backtrace info' do
obj, line = make_null, __LINE__
expect(obj.__line__).to eq(line)
end
end

describe 'customized null object' do
subject(:custom_null) { custom_null_class.new }
let(:custom_null_class) do
Naught.build do |b|
b.define_explicit_conversions
def to_path
'/dev/null'
end

def to_s
'NOTHING TO SEE HERE'
end
it 'has named ancestor modules', :pending => rubinius? do
expect(TestNull.ancestors[0..2].collect(&:name)).to eq([
'TestNull',
'TestNull::Customizations',
'TestNull::GeneratedMethods'
])
end
end

it 'responds to custom-defined methods' do
expect(custom_null.to_path).to eq('/dev/null')
end

it 'allows generated methods to be overridden' do
expect(custom_null.to_s).to eq('NOTHING TO SEE HERE')
end
end
TestNull = Naught.build

describe 'a named null object class' do
it 'has named ancestor modules', :pending => rubinius? do
expect(TestNull.ancestors[0..2].collect(&:name)).to eq([
'TestNull',
'TestNull::Customizations',
'TestNull::GeneratedMethods'
])
end
end
Loading

0 comments on commit a4ac7f9

Please sign in to comment.