Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source :rubygems
source 'https://rubygems.org'
gemspec

gem 'rspec'
gem 'rspec'
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ PATH
simple-decorator (0.2.0)

GEM
remote: http://rubygems.org/
remote: https://rubygems.org/
specs:
diff-lcs (1.1.3)
rspec (2.10.0)
Expand Down
52 changes: 28 additions & 24 deletions spec/simple_decorator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,53 @@
require 'spec_helper'

describe SimpleDecorator do
# User mock object
let(:user) { mock('user') }

# Interface
let(:decorator) { SimpleDecorator.new(user) }

# An other decorator
let(:other_decorator) { SimpleDecorator.new(decorator) }

before do
# User mock object
@user = mock('user')
@user.stub('first_name') { 'Foo' }
@user.stub('last_name') { 'Bar' }
@user.stub('count') { 1 }

# Interface
@decorator = SimpleDecorator.new(@user)
def @decorator.first_name; 'Geronimo' end
def @decorator.count; @component.count + 1 end

# An other decorator
@other_decorator = SimpleDecorator.new(@decorator)
def @other_decorator.count; @component.count + 2 end
user.stub('first_name') { 'Foo' }
user.stub('last_name') { 'Bar' }
user.stub('count') { 1 }

def decorator.first_name; 'Geronimo' end
def decorator.count; @component.count + 1 end

def other_decorator.count; @component.count + 2 end
end

describe "is a true decorator" do
describe 'Object behavior' do
it 'should take an object as an argument for instanciation' do
SimpleDecorator.new(@user).should_not raise_error(ArgumentError)
SimpleDecorator.new(user).should_not raise_error(ArgumentError)
end

describe 'should be fully transparent' do
it { @decorator.should be_an_instance_of(@user.class) }
it { @decorator.should == @user }
it { decorator.should be_an_instance_of(user.class) }
it { decorator.should == user }
end
end # Object behavior

describe 'Delegation behavior' do
it 'should receive any known method an not pass them to the component' do
@decorator.first_name.should == 'Geronimo'
@decorator.first_name.should_not == 'Foo'
decorator.first_name.should == 'Geronimo'
decorator.first_name.should_not == 'Foo'
end

it 'should delegate unknown methods to the underlying component' do
@decorator.last_name.should == 'Bar'
decorator.last_name.should == 'Bar'
end

it 'should be able to decorate a decorator' do
@user.count.should == 1
@decorator.count.should == 2
@other_decorator.count.should == 4
user.count.should == 1
decorator.count.should == 2
other_decorator.count.should == 4
end
end # Delegation behavior
end # is a true decorator
end
end