Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

unifying recorder interface

git-svn-id: http://expectations.rubyforge.org/svn/trunk@51 6f265639-962f-4d85-a13b-3090ac0aa348
  • Loading branch information...
commit 3136f0406c84fc4c8f8d7b339205493c7851cfc0 1 parent 98fa721
jaycfields authored
5 lib/expectations.rb
View
@@ -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')
19 lib/expectations/delegate_recorder.rb
View
@@ -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  lib/expectations/expectation.rb
View
@@ -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
6 lib/expectations/object.rb
View
@@ -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
@@ -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
8 lib/expectations/positive_state_based_recorder.rb
View
@@ -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 lib/expectations/state_based_recorder.rb
View
@@ -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  test/successes_test.rb
View
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.