-
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
19 changed files
with
312 additions
and
297 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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 |
29 changes: 24 additions & 5 deletions
29
spec/explicit_conversions_spec.rb → ...nfigurations/explicit_conversions_spec.rb
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
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 |
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
File renamed without changes.
Oops, something went wrong.