Skip to content

Commit

Permalink
Merge 241fac3 into a82d1d1
Browse files Browse the repository at this point in the history
  • Loading branch information
charlierudolph committed Feb 27, 2014
2 parents a82d1d1 + 241fac3 commit 65953b8
Show file tree
Hide file tree
Showing 19 changed files with 312 additions and 297 deletions.
35 changes: 0 additions & 35 deletions spec/basic_null_object_spec.rb

This file was deleted.

25 changes: 0 additions & 25 deletions spec/implicit_conversions_spec.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
require 'spec_helper'

describe 'null object with a custom base class' do

subject(:null) { custom_base_null_class.new }

let(:custom_base_null_class) do
subject(:null) { null_class.new }
let(:null_class) do
Naught.build do |b|
b.base_class = Object
end
Expand All @@ -25,23 +23,4 @@
end
expect(default_base_class).to eq(Naught::BasicObject)
end

describe 'singleton null object' do
subject(:null_instance) { custom_base_singleton_null_class.instance }

let(:custom_base_singleton_null_class) do
Naught.build do |b|
b.singleton
b.base_class = Object
end
end

it 'can be cloned' do
expect(null_instance.clone).to be(null_instance)
end

it 'can be duplicated' do
expect(null_instance.dup).to be(null_instance)
end
end
end
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 methodcalls' 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/naught/configurations/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
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/naught/configurations/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
32 changes: 32 additions & 0 deletions spec/naught/configurations/implicit_conversions_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'spec_helper'

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

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 → spec/naught/configurations/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
File renamed without changes.
Loading

0 comments on commit 65953b8

Please sign in to comment.