-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
286 additions
and
238 deletions.
There are no files selected for viewing
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.