Skip to content

Commit

Permalink
unifying recorder interface
Browse files Browse the repository at this point in the history
git-svn-id: http://expectations.rubyforge.org/svn/trunk@51 6f265639-962f-4d85-a13b-3090ac0aa348
  • Loading branch information
jaycfields committed Mar 4, 2008
1 parent 98fa721 commit 3136f04
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 53 deletions.
5 changes: 3 additions & 2 deletions lib/expectations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ def Expectations(&block)
require 'erb'
require 'fileutils'
require File.expand_path(File.dirname(__FILE__) + '/expectations/object')
require File.expand_path(File.dirname(__FILE__) + '/expectations/recorder')
require File.expand_path(File.dirname(__FILE__) + '/expectations/delegate_expectation')
require File.expand_path(File.dirname(__FILE__) + '/expectations/delegate_recorder')
require File.expand_path(File.dirname(__FILE__) + '/expectations/recorded_state_based_expectation')
require File.expand_path(File.dirname(__FILE__) + '/expectations/recorded_expectation')
require File.expand_path(File.dirname(__FILE__) + '/expectations/state_based_recorder')
require File.expand_path(File.dirname(__FILE__) + '/expectations/positive_state_based_recorder')
require File.expand_path(File.dirname(__FILE__) + '/expectations/negative_state_based_recorder')
require File.expand_path(File.dirname(__FILE__) + '/expectations/reverse_result')
require File.expand_path(File.dirname(__FILE__) + '/expectations/xml_string')
require File.expand_path(File.dirname(__FILE__) + '/expectations/regexp')
require File.expand_path(File.dirname(__FILE__) + '/expectations/range')
Expand Down
19 changes: 9 additions & 10 deletions lib/expectations/delegate_recorder.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
class Expectations::DelegateRecorder
attr_reader :klass, :subject, :meth
def initialize(klass, meth)
@klass, @subject_mock, @meth = klass, Mocha::Mock.new, meth
@subject_mock.expects(meth).returns(:the_subjects_response)
module Expectations::DelegateRecorder
def delegate!(meth)
@subject_mock = Mocha::Mock.new
@meth = meth
@subject_mock.expects(meth)
end

def to(subject)
@subject = subject
def to(receiver)
@receiver = receiver
self
end

def mock
@klass.stubs(@subject).returns(@subject_mock)
@klass
subject.stubs(@receiver).returns(@subject_mock)
subject
end

def verify(actual)
@subject_mock.verify
actual == :the_subjects_response
end
end
3 changes: 1 addition & 2 deletions lib/expectations/expectation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ def initialize(expected, &block)
self.file, self.line = eval "[__FILE__, __LINE__]", block.binding
case
when expected.is_a?(Expectations::DelegateRecorder) then extend(Expectations::DelegateExpectation)
when expected.is_a?(Expectations::PositiveStateBasedRecorder) then extend(Expectations::RecordedStateBasedExpectation)
when expected.is_a?(Expectations::NegativeStateBasedRecorder) then extend(Expectations::RecordedStateBasedExpectation)
when expected.is_a?(Expectations::PositiveStateBasedRecorder) then extend(Expectations::RecordedExpectation)
when expected.is_a?(Expectations::MockRecorder) then extend(Expectations::BehaviorBasedExpectation)
else extend(Expectations::StateBasedExpectation)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/expectations/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ def to_delegate(method)
end

def to
Expectations::StateBasedRecorder.new(self).extend Expectations::PositiveStateBasedRecorder
Expectations::Recorder.new(self)
end

def not
Not.new(self)
end

def __negate__
def not!
!self
end

Expand All @@ -27,7 +27,7 @@ def initialize(subject)
end

def method_missing(sym, *args, &blk)
@subject.send(sym,*args,&blk).__negate__
@subject.send(sym,*args,&blk).not!
end
end

Expand Down
8 changes: 2 additions & 6 deletions lib/expectations/positive_state_based_recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@ module Expectations::PositiveStateBasedRecorder
def verify
method_stack.inject(subject) { |result, element| result.send element.first, *element.last }
end

def __negate__
extend Expectations::NegativeStateBasedRecorder
end


def message
"expected #{subject} to #{@message_parts.join(" ")}"
"expected #{subject} #{@message_parts.join(" ")}"
end

end
29 changes: 0 additions & 29 deletions lib/expectations/state_based_recorder.rb
Original file line number Diff line number Diff line change
@@ -1,29 +0,0 @@
class Expectations::StateBasedRecorder
attr_reader :subject
def initialize(subject)
@subject = subject
@message_parts = []
end

def have
@message_parts << "have"
self
end

def be
@message_parts << "be"
self
end

def method_stack
@method_stack ||= []
end

def method_missing(sym, *args)
@message_parts << "#{sym}"
args.each { |arg| @message_parts << arg.inspect }
method_stack << [sym, args]
self
end

end
3 changes: 2 additions & 1 deletion test/successes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ def save(arg)
record.save(arg)
end
end

# State based delegation test
expect klass.new.to_delegate(:save).to(:record) do |instance|
expect klass.new.to.delegate(:save).to(:record) do |instance|
instance.save(1)
end

Expand Down

0 comments on commit 3136f04

Please sign in to comment.