added RSpec and RSpec on Rails

This commit is contained in:
Xin Zheng 2008-01-22 16:39:09 +00:00
parent ddd5b4cf19
commit 3f607d565b
316 changed files with 23828 additions and 0 deletions

View file

@ -0,0 +1,224 @@
module Spec
module Matchers
class Be #:nodoc:
def initialize(*args)
if args.empty?
@expected = :satisfy_if
else
@expected = parse_expected(args.shift)
end
@args = args
@comparison = ""
end
def matches?(actual)
@actual = actual
if handling_predicate?
begin
return @result = actual.__send__(predicate, *@args)
rescue => predicate_error
# This clause should be empty, but rcov will not report it as covered
# unless something (anything) is executed within the clause
rcov_error_report = "http://eigenclass.org/hiki.rb?rcov-0.8.0"
end
# This supports should_exist > target.exists? in the old world.
# We should consider deprecating that ability as in the new world
# you can't write "should exist" unless you have your own custom matcher.
begin
return @result = actual.__send__(present_tense_predicate, *@args)
rescue
raise predicate_error
end
else
return match_or_compare
end
end
def failure_message
return "expected #{@comparison}#{expected}, got #{@actual.inspect}" unless handling_predicate?
return "expected #{predicate}#{args_to_s} to return true, got #{@result.inspect}"
end
def negative_failure_message
return "expected not #{expected}, got #{@actual.inspect}" unless handling_predicate?
return "expected #{predicate}#{args_to_s} to return false, got #{@result.inspect}"
end
def expected
return "if to be satisfied" if @expected == :satisfy_if
return true if @expected == :true
return false if @expected == :false
return "nil" if @expected == :nil
return @expected.inspect
end
def match_or_compare
return @actual ? true : false if @expected == :satisfy_if
return @actual == true if @expected == :true
return @actual == false if @expected == :false
return @actual.nil? if @expected == :nil
return @actual < @expected if @less_than
return @actual <= @expected if @less_than_or_equal
return @actual >= @expected if @greater_than_or_equal
return @actual > @expected if @greater_than
return @actual == @expected if @double_equal
return @actual === @expected if @triple_equal
return @actual.equal?(@expected)
end
def ==(expected)
@prefix = "be "
@double_equal = true
@comparison = "== "
@expected = expected
self
end
def ===(expected)
@prefix = "be "
@triple_equal = true
@comparison = "=== "
@expected = expected
self
end
def <(expected)
@prefix = "be "
@less_than = true
@comparison = "< "
@expected = expected
self
end
def <=(expected)
@prefix = "be "
@less_than_or_equal = true
@comparison = "<= "
@expected = expected
self
end
def >=(expected)
@prefix = "be "
@greater_than_or_equal = true
@comparison = ">= "
@expected = expected
self
end
def >(expected)
@prefix = "be "
@greater_than = true
@comparison = "> "
@expected = expected
self
end
def description
"#{prefix_to_sentence}#{comparison}#{expected_to_sentence}#{args_to_sentence}"
end
private
def parse_expected(expected)
if Symbol === expected
@handling_predicate = true
["be_an_","be_a_","be_"].each do |prefix|
if expected.starts_with?(prefix)
@prefix = prefix
return "#{expected.to_s.sub(@prefix,"")}".to_sym
end
end
end
@prefix = ""
return expected
end
def handling_predicate?
return false if [:true, :false, :nil].include?(@expected)
return @handling_predicate
end
def predicate
"#{@expected.to_s}?".to_sym
end
def present_tense_predicate
"#{@expected.to_s}s?".to_sym
end
def args_to_s
return "" if @args.empty?
inspected_args = @args.collect{|a| a.inspect}
return "(#{inspected_args.join(', ')})"
end
def comparison
@comparison
end
def expected_to_sentence
split_words(@expected)
end
def prefix_to_sentence
split_words(@prefix)
end
def split_words(sym)
sym.to_s.gsub(/_/,' ')
end
def args_to_sentence
case @args.length
when 0
""
when 1
" #{@args[0]}"
else
" #{@args[0...-1].join(', ')} and #{@args[-1]}"
end
end
end
# :call-seq:
# should be
# should be_true
# should be_false
# should be_nil
# should be_arbitrary_predicate(*args)
# should_not be_nil
# should_not be_arbitrary_predicate(*args)
#
# Given true, false, or nil, will pass if actual is
# true, false or nil (respectively). Given no args means
# the caller should satisfy an if condition (to be or not to be).
#
# Predicates are any Ruby method that ends in a "?" and returns true or false.
# Given be_ followed by arbitrary_predicate (without the "?"), RSpec will match
# convert that into a query against the target object.
#
# The arbitrary_predicate feature will handle any predicate
# prefixed with "be_an_" (e.g. be_an_instance_of), "be_a_" (e.g. be_a_kind_of)
# or "be_" (e.g. be_empty), letting you choose the prefix that best suits the predicate.
#
# == Examples
#
# target.should be
# target.should be_true
# target.should be_false
# target.should be_nil
# target.should_not be_nil
#
# collection.should be_empty #passes if target.empty?
# "this string".should be_an_intance_of(String)
#
# target.should_not be_empty #passes unless target.empty?
# target.should_not be_old_enough(16) #passes unless target.old_enough?(16)
def be(*args)
Matchers::Be.new(*args)
end
end
end

View file

@ -0,0 +1,37 @@
module Spec
module Matchers
class BeClose #:nodoc:
def initialize(expected, delta)
@expected = expected
@delta = delta
end
def matches?(actual)
@actual = actual
(@actual - @expected).abs < @delta
end
def failure_message
"expected #{@expected} +/- (< #{@delta}), got #{@actual}"
end
def description
"be close to #{@expected} (within +- #{@delta})"
end
end
# :call-seq:
# should be_close(expected, delta)
# should_not be_close(expected, delta)
#
# Passes if actual == expected +/- delta
#
# == Example
#
# result.should be_close(3.0, 0.5)
def be_close(expected, delta)
Matchers::BeClose.new(expected, delta)
end
end
end

View file

@ -0,0 +1,144 @@
module Spec
module Matchers
#Based on patch from Wilson Bilkovich
class Change #:nodoc:
def initialize(receiver=nil, message=nil, &block)
@receiver = receiver
@message = message
@block = block
end
def matches?(target, &block)
if block
raise MatcherError.new(<<-EOF
block passed to should or should_not change must use {} instead of do/end
EOF
)
end
@target = target
execute_change
return false if @from && (@from != @before)
return false if @to && (@to != @after)
return (@before + @amount == @after) if @amount
return ((@after - @before) >= @minimum) if @minimum
return ((@after - @before) <= @maximum) if @maximum
return @before != @after
end
def execute_change
@before = @block.nil? ? @receiver.send(@message) : @block.call
@target.call
@after = @block.nil? ? @receiver.send(@message) : @block.call
end
def failure_message
if @to
"#{result} should have been changed to #{@to.inspect}, but is now #{@after.inspect}"
elsif @from
"#{result} should have initially been #{@from.inspect}, but was #{@before.inspect}"
elsif @amount
"#{result} should have been changed by #{@amount.inspect}, but was changed by #{actual_delta.inspect}"
elsif @minimum
"#{result} should have been changed by at least #{@minimum.inspect}, but was changed by #{actual_delta.inspect}"
elsif @maximum
"#{result} should have been changed by at most #{@maximum.inspect}, but was changed by #{actual_delta.inspect}"
else
"#{result} should have changed, but is still #{@before.inspect}"
end
end
def result
@message || "result"
end
def actual_delta
@after - @before
end
def negative_failure_message
"#{result} should not have changed, but did change from #{@before.inspect} to #{@after.inspect}"
end
def by(amount)
@amount = amount
self
end
def by_at_least(minimum)
@minimum = minimum
self
end
def by_at_most(maximum)
@maximum = maximum
self
end
def to(to)
@to = to
self
end
def from (from)
@from = from
self
end
end
# :call-seq:
# should change(receiver, message, &block)
# should change(receiver, message, &block).by(value)
# should change(receiver, message, &block).from(old).to(new)
# should_not change(receiver, message, &block)
#
# Allows you to specify that a Proc will cause some value to change.
#
# == Examples
#
# lambda {
# team.add_player(player)
# }.should change(roster, :count)
#
# lambda {
# team.add_player(player)
# }.should change(roster, :count).by(1)
#
# lambda {
# team.add_player(player)
# }.should change(roster, :count).by_at_least(1)
#
# lambda {
# team.add_player(player)
# }.should change(roster, :count).by_at_most(1)
#
# string = "string"
# lambda {
# string.reverse
# }.should change { string }.from("string").to("gnirts")
#
# lambda {
# person.happy_birthday
# }.should change(person, :birthday).from(32).to(33)
#
# lambda {
# employee.develop_great_new_social_networking_app
# }.should change(employee, :title).from("Mail Clerk").to("CEO")
#
# Evaluates +receiver.message+ or +block+ before and
# after it evaluates the c object (generated by the lambdas in the examples above).
#
# Then compares the values before and after the +receiver.message+ and
# evaluates the difference compared to the expected difference.
#
# == Warning
# +should_not+ +change+ only supports the form with no subsequent calls to
# +by+, +by_at_least+, +by_at_most+, +to+ or +from+.
#
# blocks passed to +should+ +change+ and +should_not+ +change+
# must use the <tt>{}</tt> form (<tt>do/end</tt> is not supported)
def change(target=nil, message=nil, &block)
Matchers::Change.new(target, message, &block)
end
end
end

View file

@ -0,0 +1,43 @@
module Spec
module Matchers
class Eql #:nodoc:
def initialize(expected)
@expected = expected
end
def matches?(actual)
@actual = actual
@actual.eql?(@expected)
end
def failure_message
return "expected #{@expected.inspect}, got #{@actual.inspect} (using .eql?)", @expected, @actual
end
def negative_failure_message
return "expected #{@actual.inspect} not to equal #{@expected.inspect} (using .eql?)", @expected, @actual
end
def description
"eql #{@expected.inspect}"
end
end
# :call-seq:
# should eql(expected)
# should_not eql(expected)
#
# Passes if actual and expected are of equal value, but not necessarily the same object.
#
# See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
#
# == Examples
#
# 5.should eql(5)
# 5.should_not eql(3)
def eql(expected)
Matchers::Eql.new(expected)
end
end
end

View file

@ -0,0 +1,43 @@
module Spec
module Matchers
class Equal #:nodoc:
def initialize(expected)
@expected = expected
end
def matches?(actual)
@actual = actual
@actual.equal?(@expected)
end
def failure_message
return "expected #{@expected.inspect}, got #{@actual.inspect} (using .equal?)", @expected, @actual
end
def negative_failure_message
return "expected #{@actual.inspect} not to equal #{@expected.inspect} (using .equal?)", @expected, @actual
end
def description
"equal #{@expected.inspect}"
end
end
# :call-seq:
# should equal(expected)
# should_not equal(expected)
#
# Passes if actual and expected are the same object (object identity).
#
# See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
#
# == Examples
#
# 5.should equal(5) #Fixnums are equal
# "5".should_not equal("5") #Strings that look the same are not the same object
def equal(expected)
Matchers::Equal.new(expected)
end
end
end

View file

@ -0,0 +1,17 @@
module Spec
module Matchers
class Exist
def matches? actual
@actual = actual
@actual.exist?
end
def failure_message
"expected #{@actual.inspect} to exist, but it doesn't."
end
def negative_failure_message
"expected #{@actual.inspect} to not exist, but it does."
end
end
def exist; Exist.new; end
end
end

View file

@ -0,0 +1,44 @@
module Spec
module Matchers
class Has #:nodoc:
def initialize(sym, *args)
@sym = sym
@args = args
end
def matches?(target)
@target = target
begin
return target.send(predicate, *@args)
rescue => @error
# This clause should be empty, but rcov will not report it as covered
# unless something (anything) is executed within the clause
rcov_error_report = "http://eigenclass.org/hiki.rb?rcov-0.8.0"
end
return false
end
def failure_message
raise @error if @error
"expected ##{predicate}(#{@args[0].inspect}) to return true, got false"
end
def negative_failure_message
raise @error if @error
"expected ##{predicate}(#{@args[0].inspect}) to return false, got true"
end
def description
"have key #{@args[0].inspect}"
end
private
def predicate
"#{@sym.to_s.sub("have_","has_")}?".to_sym
end
end
end
end

View file

@ -0,0 +1,145 @@
module Spec
module Matchers
class Have #:nodoc:
def initialize(expected, relativity=:exactly)
@expected = (expected == :no ? 0 : expected)
@relativity = relativity
end
def relativities
@relativities ||= {
:exactly => "",
:at_least => "at least ",
:at_most => "at most "
}
end
def method_missing(sym, *args, &block)
@collection_name = sym
@plural_collection_name = Inflector.pluralize(sym.to_s) if Object.const_defined?(:Inflector)
@args = args
@block = block
self
end
def matches?(collection_owner)
if collection_owner.respond_to?(@collection_name)
collection = collection_owner.send(@collection_name, *@args, &@block)
elsif (@plural_collection_name && collection_owner.respond_to?(@plural_collection_name))
collection = collection_owner.send(@plural_collection_name, *@args, &@block)
elsif (collection_owner.respond_to?(:length) || collection_owner.respond_to?(:size))
collection = collection_owner
else
collection_owner.send(@collection_name, *@args, &@block)
end
@actual = collection.size if collection.respond_to?(:size)
@actual = collection.length if collection.respond_to?(:length)
raise not_a_collection if @actual.nil?
return @actual >= @expected if @relativity == :at_least
return @actual <= @expected if @relativity == :at_most
return @actual == @expected
end
def not_a_collection
"expected #{@collection_name} to be a collection but it does not respond to #length or #size"
end
def failure_message
"expected #{relative_expectation} #{@collection_name}, got #{@actual}"
end
def negative_failure_message
if @relativity == :exactly
return "expected target not to have #{@expected} #{@collection_name}, got #{@actual}"
elsif @relativity == :at_most
return <<-EOF
Isn't life confusing enough?
Instead of having to figure out the meaning of this:
should_not have_at_most(#{@expected}).#{@collection_name}
We recommend that you use this instead:
should have_at_least(#{@expected + 1}).#{@collection_name}
EOF
elsif @relativity == :at_least
return <<-EOF
Isn't life confusing enough?
Instead of having to figure out the meaning of this:
should_not have_at_least(#{@expected}).#{@collection_name}
We recommend that you use this instead:
should have_at_most(#{@expected - 1}).#{@collection_name}
EOF
end
end
def description
"have #{relative_expectation} #{@collection_name}"
end
private
def relative_expectation
"#{relativities[@relativity]}#{@expected}"
end
end
# :call-seq:
# should have(number).named_collection__or__sugar
# should_not have(number).named_collection__or__sugar
#
# Passes if receiver is a collection with the submitted
# number of items OR if the receiver OWNS a collection
# with the submitted number of items.
#
# If the receiver OWNS the collection, you must use the name
# of the collection. So if a <tt>Team</tt> instance has a
# collection named <tt>#players</tt>, you must use that name
# to set the expectation.
#
# If the receiver IS the collection, you can use any name
# you like for <tt>named_collection</tt>. We'd recommend using
# either "elements", "members", or "items" as these are all
# standard ways of describing the things IN a collection.
#
# This also works for Strings, letting you set an expectation
# about its length
#
# == Examples
#
# # Passes if team.players.size == 11
# team.should have(11).players
#
# # Passes if [1,2,3].length == 3
# [1,2,3].should have(3).items #"items" is pure sugar
#
# # Passes if "this string".length == 11
# "this string".should have(11).characters #"characters" is pure sugar
def have(n)
Matchers::Have.new(n)
end
alias :have_exactly :have
# :call-seq:
# should have_at_least(number).items
#
# Exactly like have() with >=.
#
# == Warning
#
# +should_not+ +have_at_least+ is not supported
def have_at_least(n)
Matchers::Have.new(n, :at_least)
end
# :call-seq:
# should have_at_most(number).items
#
# Exactly like have() with <=.
#
# == Warning
#
# +should_not+ +have_at_most+ is not supported
def have_at_most(n)
Matchers::Have.new(n, :at_most)
end
end
end

View file

@ -0,0 +1,70 @@
module Spec
module Matchers
class Include #:nodoc:
def initialize(*expecteds)
@expecteds = expecteds
end
def matches?(actual)
@actual = actual
@expecteds.each do |expected|
return false unless actual.include?(expected)
end
true
end
def failure_message
_message
end
def negative_failure_message
_message("not ")
end
def description
"include #{_pretty_print(@expecteds)}"
end
private
def _message(maybe_not="")
"expected #{@actual.inspect} #{maybe_not}to include #{_pretty_print(@expecteds)}"
end
def _pretty_print(array)
result = ""
array.each_with_index do |item, index|
if index < (array.length - 2)
result << "#{item.inspect}, "
elsif index < (array.length - 1)
result << "#{item.inspect} and "
else
result << "#{item.inspect}"
end
end
result
end
end
# :call-seq:
# should include(expected)
# should_not include(expected)
#
# Passes if actual includes expected. This works for
# collections and Strings. You can also pass in multiple args
# and it will only pass if all args are found in collection.
#
# == Examples
#
# [1,2,3].should include(3)
# [1,2,3].should include(2,3) #would pass
# [1,2,3].should include(2,3,4) #would fail
# [1,2,3].should_not include(4)
# "spread".should include("read")
# "spread".should_not include("red")
def include(*expected)
Matchers::Include.new(*expected)
end
end
end

View file

@ -0,0 +1,41 @@
module Spec
module Matchers
class Match #:nodoc:
def initialize(expected)
@expected = expected
end
def matches?(actual)
@actual = actual
return true if actual =~ @expected
return false
end
def failure_message
return "expected #{@actual.inspect} to match #{@expected.inspect}", @expected, @actual
end
def negative_failure_message
return "expected #{@actual.inspect} not to match #{@expected.inspect}", @expected, @actual
end
def description
"match #{@expected.inspect}"
end
end
# :call-seq:
# should match(regexp)
# should_not match(regexp)
#
# Given a Regexp, passes if actual =~ regexp
#
# == Examples
#
# email.should match(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i)
def match(regexp)
Matchers::Match.new(regexp)
end
end
end

View file

@ -0,0 +1,73 @@
module Spec
module Matchers
class BaseOperatorMatcher
attr_reader :generated_description
def initialize(target)
@target = target
end
def ==(expected)
@expected = expected
__delegate_method_missing_to_target("==", expected)
end
def ===(expected)
@expected = expected
__delegate_method_missing_to_target("===", expected)
end
def =~(expected)
@expected = expected
__delegate_method_missing_to_target("=~", expected)
end
def >(expected)
@expected = expected
__delegate_method_missing_to_target(">", expected)
end
def >=(expected)
@expected = expected
__delegate_method_missing_to_target(">=", expected)
end
def <(expected)
@expected = expected
__delegate_method_missing_to_target("<", expected)
end
def <=(expected)
@expected = expected
__delegate_method_missing_to_target("<=", expected)
end
def fail_with_message(message)
Spec::Expectations.fail_with(message, @expected, @target)
end
end
class PositiveOperatorMatcher < BaseOperatorMatcher #:nodoc:
def __delegate_method_missing_to_target(operator, expected)
::Spec::Matchers.generated_description = "should #{operator} #{expected.inspect}"
return if @target.send(operator, expected)
return fail_with_message("expected: #{expected.inspect},\n got: #{@target.inspect} (using #{operator})") if ['==','===', '=~'].include?(operator)
return fail_with_message("expected: #{operator} #{expected.inspect},\n got: #{operator.gsub(/./, ' ')} #{@target.inspect}")
end
end
class NegativeOperatorMatcher < BaseOperatorMatcher #:nodoc:
def __delegate_method_missing_to_target(operator, expected)
::Spec::Matchers.generated_description = "should not #{operator} #{expected.inspect}"
return unless @target.send(operator, expected)
return fail_with_message("expected not: #{operator} #{expected.inspect},\n got: #{operator.gsub(/./, ' ')} #{@target.inspect}")
end
end
end
end

View file

@ -0,0 +1,105 @@
module Spec
module Matchers
class RaiseError #:nodoc:
def initialize(error_or_message=Exception, message=nil)
if String === error_or_message
@expected_error = Exception
@expected_message = error_or_message
else
@expected_error = error_or_message
@expected_message = message
end
end
def matches?(proc)
@raised_expected_error = false
@raised_other = false
begin
proc.call
rescue @expected_error => @actual_error
if @expected_message.nil?
@raised_expected_error = true
else
case @expected_message
when Regexp
if @expected_message =~ @actual_error.message
@raised_expected_error = true
else
@raised_other = true
end
else
if @expected_message == @actual_error.message
@raised_expected_error = true
else
@raised_other = true
end
end
end
rescue => @actual_error
@raised_other = true
ensure
return @raised_expected_error
end
end
def failure_message
return "expected #{expected_error}#{actual_error}" if @raised_other || !@raised_expected_error
end
def negative_failure_message
"expected no #{expected_error}#{actual_error}"
end
def description
"raise #{expected_error}"
end
private
def expected_error
case @expected_message
when nil
@expected_error
when Regexp
"#{@expected_error} with message matching #{@expected_message.inspect}"
else
"#{@expected_error} with #{@expected_message.inspect}"
end
end
def actual_error
@actual_error.nil? ? " but nothing was raised" : ", got #{@actual_error.inspect}"
end
end
# :call-seq:
# should raise_error()
# should raise_error(NamedError)
# should raise_error(NamedError, String)
# should raise_error(NamedError, Regexp)
# should_not raise_error()
# should_not raise_error(NamedError)
# should_not raise_error(NamedError, String)
# should_not raise_error(NamedError, Regexp)
#
# With no args, matches if any error is raised.
# With a named error, matches only if that specific error is raised.
# With a named error and messsage specified as a String, matches only if both match.
# With a named error and messsage specified as a Regexp, matches only if both match.
#
# == Examples
#
# lambda { do_something_risky }.should raise_error
# lambda { do_something_risky }.should raise_error(PoorRiskDecisionError)
# lambda { do_something_risky }.should raise_error(PoorRiskDecisionError, "that was too risky")
# lambda { do_something_risky }.should raise_error(PoorRiskDecisionError, /oo ri/)
#
# lambda { do_something_risky }.should_not raise_error
# lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError)
# lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError, "that was too risky")
# lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError, /oo ri/)
def raise_error(error=Exception, message=nil)
Matchers::RaiseError.new(error, message)
end
end
end

View file

@ -0,0 +1,45 @@
module Spec
module Matchers
class RespondTo #:nodoc:
def initialize(*names)
@names = names
@names_not_responded_to = []
end
def matches?(target)
@names.each do |name|
unless target.respond_to?(name)
@names_not_responded_to << name
end
end
return @names_not_responded_to.empty?
end
def failure_message
"expected target to respond to #{@names_not_responded_to.collect {|name| name.inspect }.join(', ')}"
end
def negative_failure_message
"expected target not to respond to #{@names.collect {|name| name.inspect }.join(', ')}"
end
def description
"respond to ##{@names.to_s}"
end
end
# :call-seq:
# should respond_to(*names)
# should_not respond_to(*names)
#
# Matches if the target object responds to all of the names
# provided. Names can be Strings or Symbols.
#
# == Examples
#
def respond_to(*names)
Matchers::RespondTo.new(*names)
end
end
end

View file

@ -0,0 +1,47 @@
module Spec
module Matchers
class Satisfy #:nodoc:
def initialize(&block)
@block = block
end
def matches?(actual, &block)
@block = block if block
@actual = actual
@block.call(actual)
end
def failure_message
"expected #{@actual} to satisfy block"
end
def negative_failure_message
"expected #{@actual} not to satisfy block"
end
end
# :call-seq:
# should satisfy {}
# should_not satisfy {}
#
# Passes if the submitted block returns true. Yields target to the
# block.
#
# Generally speaking, this should be thought of as a last resort when
# you can't find any other way to specify the behaviour you wish to
# specify.
#
# If you do find yourself in such a situation, you could always write
# a custom matcher, which would likely make your specs more expressive.
#
# == Examples
#
# 5.should satisfy { |n|
# n > 3
# }
def satisfy(&block)
Matchers::Satisfy.new(&block)
end
end
end

View file

@ -0,0 +1,29 @@
module Spec
module Matchers
class SimpleMatcher
attr_reader :description
def initialize(description, &match_block)
@description = description
@match_block = match_block
end
def matches?(actual)
@actual = actual
return @match_block.call(@actual)
end
def failure_message()
return %[expected #{@description.inspect} but got #{@actual.inspect}]
end
def negative_failure_message()
return %[expected not to get #{@description.inspect}, but got #{@actual.inspect}]
end
end
def simple_matcher(message, &match_block)
SimpleMatcher.new(message, &match_block)
end
end
end

View file

@ -0,0 +1,74 @@
module Spec
module Matchers
class ThrowSymbol #:nodoc:
def initialize(expected=nil)
@expected = expected
@actual = nil
end
def matches?(proc)
begin
proc.call
rescue NameError => e
raise e unless e.message =~ /uncaught throw/
@actual = e.name.to_sym
ensure
if @expected.nil?
return @actual.nil? ? false : true
else
return @actual == @expected
end
end
end
def failure_message
if @actual
"expected #{expected}, got #{@actual.inspect}"
else
"expected #{expected} but nothing was thrown"
end
end
def negative_failure_message
if @expected
"expected #{expected} not to be thrown"
else
"expected no Symbol, got :#{@actual}"
end
end
def description
"throw #{expected}"
end
private
def expected
@expected.nil? ? "a Symbol" : @expected.inspect
end
end
# :call-seq:
# should throw_symbol()
# should throw_symbol(:sym)
# should_not throw_symbol()
# should_not throw_symbol(:sym)
#
# Given a Symbol argument, matches if a proc throws the specified Symbol.
#
# Given no argument, matches if a proc throws any Symbol.
#
# == Examples
#
# lambda { do_something_risky }.should throw_symbol
# lambda { do_something_risky }.should throw_symbol(:that_was_risky)
#
# lambda { do_something_risky }.should_not throw_symbol
# lambda { do_something_risky }.should_not throw_symbol(:that_was_risky)
def throw_symbol(sym=nil)
Matchers::ThrowSymbol.new(sym)
end
end
end