aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/plugins/rspec/lib/spec/expectations
diff options
context:
space:
mode:
authorlouise <louise>2007-10-16 19:10:21 +0000
committerlouise <louise>2007-10-16 19:10:21 +0000
commitd350850897a5ee7a994d3c618529cf5beecf71ea (patch)
tree39de7013d0a3377f063fbd53da7c89f207eeedd0 /vendor/plugins/rspec/lib/spec/expectations
parent3b1d8bfdeea68da1ad083a305d0df8f458c362a0 (diff)
Adding rspec plugin
Diffstat (limited to 'vendor/plugins/rspec/lib/spec/expectations')
-rw-r--r--vendor/plugins/rspec/lib/spec/expectations/differs/default.rb61
-rw-r--r--vendor/plugins/rspec/lib/spec/expectations/errors.rb6
-rw-r--r--vendor/plugins/rspec/lib/spec/expectations/extensions.rb2
-rw-r--r--vendor/plugins/rspec/lib/spec/expectations/extensions/object.rb66
-rw-r--r--vendor/plugins/rspec/lib/spec/expectations/extensions/string_and_symbol.rb17
-rw-r--r--vendor/plugins/rspec/lib/spec/expectations/handler.rb43
6 files changed, 195 insertions, 0 deletions
diff --git a/vendor/plugins/rspec/lib/spec/expectations/differs/default.rb b/vendor/plugins/rspec/lib/spec/expectations/differs/default.rb
new file mode 100644
index 000000000..87e59b3a6
--- /dev/null
+++ b/vendor/plugins/rspec/lib/spec/expectations/differs/default.rb
@@ -0,0 +1,61 @@
+begin
+ require 'rubygems'
+ require 'diff/lcs' #necessary due to loading bug on some machines - not sure why - DaC
+ require 'diff/lcs/hunk'
+rescue LoadError ; raise "You must gem install diff-lcs to use diffing" ; end
+
+require 'pp'
+
+module Spec
+ module Expectations
+ module Differs
+
+ # TODO add some rdoc
+ class Default
+ def initialize(format=:unified,context_lines=nil,colour=nil)
+
+ context_lines ||= 3
+ colour ||= false
+
+ @format,@context_lines,@colour = format,context_lines,colour
+ end
+
+ # This is snagged from diff/lcs/ldiff.rb (which is a commandline tool)
+ def diff_as_string(data_old, data_new)
+ data_old = data_old.split(/\n/).map! { |e| e.chomp }
+ data_new = data_new.split(/\n/).map! { |e| e.chomp }
+ output = ""
+ diffs = Diff::LCS.diff(data_old, data_new)
+ return output if diffs.empty?
+ oldhunk = hunk = nil
+ file_length_difference = 0
+ diffs.each do |piece|
+ begin
+ hunk = Diff::LCS::Hunk.new(data_old, data_new, piece, @context_lines,
+ file_length_difference)
+ file_length_difference = hunk.file_length_difference
+ next unless oldhunk
+ # Hunks may overlap, which is why we need to be careful when our
+ # diff includes lines of context. Otherwise, we might print
+ # redundant lines.
+ if (@context_lines > 0) and hunk.overlaps?(oldhunk)
+ hunk.unshift(oldhunk)
+ else
+ output << oldhunk.diff(@format)
+ end
+ ensure
+ oldhunk = hunk
+ output << "\n"
+ end
+ end
+ #Handle the last remaining hunk
+ output << oldhunk.diff(@format) << "\n"
+ end
+
+ def diff_as_object(target,expected)
+ diff_as_string(PP.pp(target,""), PP.pp(expected,""))
+ end
+ end
+ end
+ end
+end
diff --git a/vendor/plugins/rspec/lib/spec/expectations/errors.rb b/vendor/plugins/rspec/lib/spec/expectations/errors.rb
new file mode 100644
index 000000000..03e81a064
--- /dev/null
+++ b/vendor/plugins/rspec/lib/spec/expectations/errors.rb
@@ -0,0 +1,6 @@
+module Spec
+ module Expectations
+ class ExpectationNotMetError < StandardError
+ end
+ end
+end
diff --git a/vendor/plugins/rspec/lib/spec/expectations/extensions.rb b/vendor/plugins/rspec/lib/spec/expectations/extensions.rb
new file mode 100644
index 000000000..60c9b9e7d
--- /dev/null
+++ b/vendor/plugins/rspec/lib/spec/expectations/extensions.rb
@@ -0,0 +1,2 @@
+require 'spec/expectations/extensions/object'
+require 'spec/expectations/extensions/string_and_symbol'
diff --git a/vendor/plugins/rspec/lib/spec/expectations/extensions/object.rb b/vendor/plugins/rspec/lib/spec/expectations/extensions/object.rb
new file mode 100644
index 000000000..f59af722e
--- /dev/null
+++ b/vendor/plugins/rspec/lib/spec/expectations/extensions/object.rb
@@ -0,0 +1,66 @@
+module Spec
+ module Expectations
+ # rspec adds #should and #should_not to every Object (and,
+ # implicitly, every Class).
+ module ObjectExpectations
+
+ # :call-seq:
+ # should(matcher)
+ # should == expected
+ # should === expected
+ # should =~ expected
+ #
+ # receiver.should(matcher)
+ # => Passes if matcher.matches?(receiver)
+ #
+ # receiver.should == expected #any value
+ # => Passes if (receiver == expected)
+ #
+ # receiver.should === expected #any value
+ # => Passes if (receiver === expected)
+ #
+ # receiver.should =~ regexp
+ # => Passes if (receiver =~ regexp)
+ #
+ # See Spec::Matchers for more information about matchers
+ #
+ # == Warning
+ #
+ # NOTE that this does NOT support receiver.should != expected.
+ # Instead, use receiver.should_not == expected
+ def should(matcher=nil, &block)
+ return ExpectationMatcherHandler.handle_matcher(self, matcher, &block) if matcher
+ Spec::Matchers::PositiveOperatorMatcher.new(self)
+ end
+
+ # :call-seq:
+ # should_not(matcher)
+ # should_not == expected
+ # should_not === expected
+ # should_not =~ expected
+ #
+ # receiver.should_not(matcher)
+ # => Passes unless matcher.matches?(receiver)
+ #
+ # receiver.should_not == expected
+ # => Passes unless (receiver == expected)
+ #
+ # receiver.should_not === expected
+ # => Passes unless (receiver === expected)
+ #
+ # receiver.should_not =~ regexp
+ # => Passes unless (receiver =~ regexp)
+ #
+ # See Spec::Matchers for more information about matchers
+ def should_not(matcher=nil, &block)
+ return NegativeExpectationMatcherHandler.handle_matcher(self, matcher, &block) if matcher
+ Spec::Matchers::NegativeOperatorMatcher.new(self)
+ end
+
+ end
+ end
+end
+
+class Object
+ include Spec::Expectations::ObjectExpectations
+end
diff --git a/vendor/plugins/rspec/lib/spec/expectations/extensions/string_and_symbol.rb b/vendor/plugins/rspec/lib/spec/expectations/extensions/string_and_symbol.rb
new file mode 100644
index 000000000..29cfbddfa
--- /dev/null
+++ b/vendor/plugins/rspec/lib/spec/expectations/extensions/string_and_symbol.rb
@@ -0,0 +1,17 @@
+module Spec
+ module Expectations
+ module StringHelpers
+ def starts_with?(prefix)
+ to_s[0..(prefix.to_s.length - 1)] == prefix.to_s
+ end
+ end
+ end
+end
+
+class String
+ include Spec::Expectations::StringHelpers
+end
+
+class Symbol
+ include Spec::Expectations::StringHelpers
+end
diff --git a/vendor/plugins/rspec/lib/spec/expectations/handler.rb b/vendor/plugins/rspec/lib/spec/expectations/handler.rb
new file mode 100644
index 000000000..4caa321e4
--- /dev/null
+++ b/vendor/plugins/rspec/lib/spec/expectations/handler.rb
@@ -0,0 +1,43 @@
+module Spec
+ module Expectations
+
+ module MatcherHandlerHelper
+ def describe(matcher)
+ matcher.respond_to?(:description) ? matcher.description : "[#{matcher.class.name} does not provide a description]"
+ end
+ end
+
+ class ExpectationMatcherHandler
+ class << self
+ include MatcherHandlerHelper
+ def handle_matcher(actual, matcher, &block)
+ match = matcher.matches?(actual, &block)
+ ::Spec::Matchers.generated_description = "should #{describe(matcher)}"
+ Spec::Expectations.fail_with(matcher.failure_message) unless match
+ end
+ end
+ end
+
+ class NegativeExpectationMatcherHandler
+ class << self
+ include MatcherHandlerHelper
+ def handle_matcher(actual, matcher, &block)
+ unless matcher.respond_to?(:negative_failure_message)
+ Spec::Expectations.fail_with(
+<<-EOF
+Matcher does not support should_not.
+See Spec::Matchers for more information
+about matchers.
+EOF
+)
+ end
+ match = matcher.matches?(actual, &block)
+ ::Spec::Matchers.generated_description = "should not #{describe(matcher)}"
+ Spec::Expectations.fail_with(matcher.negative_failure_message) if match
+ end
+ end
+ end
+
+ end
+end
+