Skip to content
Browse files

Getting the words right - use 'given' in matchers.

  • Loading branch information...
1 parent ab67f2b commit 8af8ff250cd54c9f6e21c7fb6e730159163e26fd @dchelimsky committed
View
36 lib/spec/matchers/be.rb
@@ -12,11 +12,11 @@ def initialize(*args)
@comparison = ""
end
- def matches?(actual)
- @actual = actual
+ def matches?(given)
+ @given = given
if handling_predicate?
begin
- return @result = actual.__send__(predicate, *@args)
+ return @result = given.__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
@@ -24,7 +24,7 @@ def matches?(actual)
end
begin
- return @result = actual.__send__(present_tense_predicate, *@args)
+ return @result = given.__send__(present_tense_predicate, *@args)
rescue
raise predicate_error
end
@@ -34,12 +34,12 @@ def matches?(actual)
end
def failure_message
- return "expected #{@comparison}#{expected}, got #{@actual.inspect}" unless handling_predicate?
+ return "expected #{@comparison}#{expected}, got #{@given.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 not #{expected}, got #{@given.inspect}" unless handling_predicate?
return "expected #{predicate}#{args_to_s} to return false, got #{@result.inspect}"
end
@@ -52,17 +52,17 @@ def expected
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)
+ return @given ? true : false if @expected == :satisfy_if
+ return @given == true if @expected == :true
+ return @given == false if @expected == :false
+ return @given.nil? if @expected == :nil
+ return @given < @expected if @less_than
+ return @given <= @expected if @less_than_or_equal
+ return @given >= @expected if @greater_than_or_equal
+ return @given > @expected if @greater_than
+ return @given == @expected if @double_equal
+ return @given === @expected if @triple_equal
+ return @given.equal?(@expected)
end
def ==(expected)
@@ -189,7 +189,7 @@ def args_to_sentence
# should_not be_nil
# should_not be_arbitrary_predicate(*args)
#
- # Given true, false, or nil, will pass if actual is
+ # Given true, false, or nil, will pass if given value is
# true, false or nil (respectively). Given no args means
# the caller should satisfy an if condition (to be or not to be).
#
View
10 lib/spec/matchers/be_close.rb
@@ -7,13 +7,13 @@ def initialize(expected, delta)
@delta = delta
end
- def matches?(actual)
- @actual = actual
- (@actual - @expected).abs < @delta
+ def matches?(given)
+ @given = given
+ (@given - @expected).abs < @delta
end
def failure_message
- "expected #{@expected} +/- (< #{@delta}), got #{@actual}"
+ "expected #{@expected} +/- (< #{@delta}), got #{@given}"
end
def description
@@ -25,7 +25,7 @@ def description
# should be_close(expected, delta)
# should_not be_close(expected, delta)
#
- # Passes if actual == expected +/- delta
+ # Passes if given == expected +/- delta
#
# == Example
#
View
12 lib/spec/matchers/eql.rb
@@ -6,17 +6,17 @@ def initialize(expected)
@expected = expected
end
- def matches?(actual)
- @actual = actual
- @actual.eql?(@expected)
+ def matches?(given)
+ @given = given
+ @given.eql?(@expected)
end
def failure_message
- return "expected #{@expected.inspect}, got #{@actual.inspect} (using .eql?)", @expected, @actual
+ return "expected #{@expected.inspect}, got #{@given.inspect} (using .eql?)", @expected, @given
end
def negative_failure_message
- return "expected #{@actual.inspect} not to equal #{@expected.inspect} (using .eql?)", @expected, @actual
+ return "expected #{@given.inspect} not to equal #{@expected.inspect} (using .eql?)", @expected, @given
end
def description
@@ -28,7 +28,7 @@ def description
# should eql(expected)
# should_not eql(expected)
#
- # Passes if actual and expected are of equal value, but not necessarily the same object.
+ # Passes if given 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.
#
View
12 lib/spec/matchers/equal.rb
@@ -6,17 +6,17 @@ def initialize(expected)
@expected = expected
end
- def matches?(actual)
- @actual = actual
- @actual.equal?(@expected)
+ def matches?(given)
+ @given = given
+ @given.equal?(@expected)
end
def failure_message
- return "expected #{@expected.inspect}, got #{@actual.inspect} (using .equal?)", @expected, @actual
+ return "expected #{@expected.inspect}, got #{@given.inspect} (using .equal?)", @expected, @given
end
def negative_failure_message
- return "expected #{@actual.inspect} not to equal #{@expected.inspect} (using .equal?)", @expected, @actual
+ return "expected #{@given.inspect} not to equal #{@expected.inspect} (using .equal?)", @expected, @given
end
def description
@@ -28,7 +28,7 @@ def description
# should equal(expected)
# should_not equal(expected)
#
- # Passes if actual and expected are the same object (object identity).
+ # Passes if given 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.
#
View
15 lib/spec/matchers/exist.rb
@@ -1,17 +1,22 @@
module Spec
module Matchers
class Exist
- def matches? actual
- @actual = actual
- @actual.exist?
+ def matches?(given)
+ @given = given
+ @given.exist?
end
def failure_message
- "expected #{@actual.inspect} to exist, but it doesn't."
+ "expected #{@given.inspect} to exist, but it doesn't."
end
def negative_failure_message
- "expected #{@actual.inspect} to not exist, but it does."
+ "expected #{@given.inspect} to not exist, but it does."
end
end
+ # :call-seq:
+ # should exist
+ # should_not exist
+ #
+ # Passes if given.exist?
def exist; Exist.new; end
end
end
View
4 lib/spec/matchers/has.rb
@@ -7,8 +7,8 @@ def initialize(sym, *args)
@args = args
end
- def matches?(target)
- target.__send__(predicate, *@args)
+ def matches?(given)
+ given.__send__(predicate, *@args)
end
def failure_message
View
10 lib/spec/matchers/include.rb
@@ -7,10 +7,10 @@ def initialize(*expecteds)
@expecteds = expecteds
end
- def matches?(actual)
- @actual = actual
+ def matches?(given)
+ @given = given
@expecteds.each do |expected|
- return false unless actual.include?(expected)
+ return false unless given.include?(expected)
end
true
end
@@ -29,7 +29,7 @@ def description
private
def _message(maybe_not="")
- "expected #{@actual.inspect} #{maybe_not}to include #{_pretty_print(@expecteds)}"
+ "expected #{@given.inspect} #{maybe_not}to include #{_pretty_print(@expecteds)}"
end
def _pretty_print(array)
@@ -51,7 +51,7 @@ def _pretty_print(array)
# should include(expected)
# should_not include(expected)
#
- # Passes if actual includes expected. This works for
+ # Passes if given 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.
#
View
18 lib/spec/matchers/match.rb
@@ -2,26 +2,26 @@ module Spec
module Matchers
class Match #:nodoc:
- def initialize(expected)
- @expected = expected
+ def initialize(regexp)
+ @regexp = regexp
end
- def matches?(actual)
- @actual = actual
- return true if actual =~ @expected
+ def matches?(given)
+ @given = given
+ return true if given =~ @regexp
return false
end
def failure_message
- return "expected #{@actual.inspect} to match #{@expected.inspect}", @expected, @actual
+ return "expected #{@given.inspect} to match #{@regexp.inspect}", @regexp, @given
end
def negative_failure_message
- return "expected #{@actual.inspect} not to match #{@expected.inspect}", @expected, @actual
+ return "expected #{@given.inspect} not to match #{@regexp.inspect}", @regexp, @given
end
def description
- "match #{@expected.inspect}"
+ "match #{@regexp.inspect}"
end
end
@@ -29,7 +29,7 @@ def description
# should match(regexp)
# should_not match(regexp)
#
- # Given a Regexp, passes if actual =~ regexp
+ # Given a Regexp, passes if given =~ regexp
#
# == Examples
#
View
34 lib/spec/matchers/operator_matcher.rb
@@ -3,47 +3,47 @@ module Matchers
class BaseOperatorMatcher
attr_reader :generated_description
- def initialize(target)
- @target = target
+ def initialize(given)
+ @given = given
end
def ==(expected)
@expected = expected
- __delegate_method_missing_to_target("==", expected)
+ __delegate_method_missing_to_given("==", expected)
end
def ===(expected)
@expected = expected
- __delegate_method_missing_to_target("===", expected)
+ __delegate_method_missing_to_given("===", expected)
end
def =~(expected)
@expected = expected
- __delegate_method_missing_to_target("=~", expected)
+ __delegate_method_missing_to_given("=~", expected)
end
def >(expected)
@expected = expected
- __delegate_method_missing_to_target(">", expected)
+ __delegate_method_missing_to_given(">", expected)
end
def >=(expected)
@expected = expected
- __delegate_method_missing_to_target(">=", expected)
+ __delegate_method_missing_to_given(">=", expected)
end
def <(expected)
@expected = expected
- __delegate_method_missing_to_target("<", expected)
+ __delegate_method_missing_to_given("<", expected)
end
def <=(expected)
@expected = expected
- __delegate_method_missing_to_target("<=", expected)
+ __delegate_method_missing_to_given("<=", expected)
end
def fail_with_message(message)
- Spec::Expectations.fail_with(message, @expected, @target)
+ Spec::Expectations.fail_with(message, @expected, @given)
end
def description
@@ -54,23 +54,23 @@ def description
class PositiveOperatorMatcher < BaseOperatorMatcher #:nodoc:
- def __delegate_method_missing_to_target(operator, expected)
+ def __delegate_method_missing_to_given(operator, expected)
@operator = operator
::Spec::Matchers.last_matcher = self
- return true 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}")
+ return true if @given.__send__(operator, expected)
+ return fail_with_message("expected: #{expected.inspect},\n got: #{@given.inspect} (using #{operator})") if ['==','===', '=~'].include?(operator)
+ return fail_with_message("expected: #{operator} #{expected.inspect},\n got: #{operator.gsub(/./, ' ')} #{@given.inspect}")
end
end
class NegativeOperatorMatcher < BaseOperatorMatcher #:nodoc:
- def __delegate_method_missing_to_target(operator, expected)
+ def __delegate_method_missing_to_given(operator, expected)
@operator = operator
::Spec::Matchers.last_matcher = self
- return true unless @target.__send__(operator, expected)
- return fail_with_message("expected not: #{operator} #{expected.inspect},\n got: #{operator.gsub(/./, ' ')} #{@target.inspect}")
+ return true unless @given.__send__(operator, expected)
+ return fail_with_message("expected not: #{operator} #{expected.inspect},\n got: #{operator.gsub(/./, ' ')} #{@given.inspect}")
end
end
View
34 lib/spec/matchers/raise_error.rb
@@ -1,27 +1,27 @@
module Spec
module Matchers
class RaiseError #:nodoc:
- def initialize(error_or_message=Exception, message=nil, &block)
+ def initialize(expected_error_or_message=Exception, expected_message=nil, &block)
@block = block
- case error_or_message
+ case expected_error_or_message
when String, Regexp
- @expected_error, @expected_message = Exception, error_or_message
+ @expected_error, @expected_message = Exception, expected_error_or_message
else
- @expected_error, @expected_message = error_or_message, message
+ @expected_error, @expected_message = expected_error_or_message, expected_message
end
end
- def matches?(proc)
+ def matches?(given_proc)
@raised_expected_error = false
@with_expected_message = false
@eval_block = false
@eval_block_passed = false
begin
- proc.call
- rescue @expected_error => @actual_error
+ given_proc.call
+ rescue @expected_error => @given_error
@raised_expected_error = true
@with_expected_message = verify_message
- rescue Exception => @actual_error
+ rescue Exception => @given_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"
@@ -37,10 +37,10 @@ def matches?(proc)
def eval_block
@eval_block = true
begin
- @block[@actual_error]
+ @block[@given_error]
@eval_block_passed = true
rescue Exception => err
- @actual_error = err
+ @given_error = err
end
end
@@ -49,22 +49,22 @@ def verify_message
when nil
return true
when Regexp
- return @expected_message =~ @actual_error.message
+ return @expected_message =~ @given_error.message
else
- return @expected_message == @actual_error.message
+ return @expected_message == @given_error.message
end
end
def failure_message
if @eval_block
- return @actual_error.message
+ return @given_error.message
else
- return "expected #{expected_error}#{actual_error}"
+ return "expected #{expected_error}#{given_error}"
end
end
def negative_failure_message
- "expected no #{expected_error}#{actual_error}"
+ "expected no #{expected_error}#{given_error}"
end
def description
@@ -83,8 +83,8 @@ def expected_error
end
end
- def actual_error
- @actual_error.nil? ? " but nothing was raised" : ", got #{@actual_error.inspect}"
+ def given_error
+ @given_error.nil? ? " but nothing was raised" : ", got #{@given_error.inspect}"
end
def negative_expectation?
View
9 lib/spec/matchers/respond_to.rb
@@ -7,9 +7,10 @@ def initialize(*names)
@names_not_responded_to = []
end
- def matches?(target)
+ def matches?(given)
+ @given = given
@names.each do |name|
- unless target.respond_to?(name)
+ unless given.respond_to?(name)
@names_not_responded_to << name
end
end
@@ -17,11 +18,11 @@ def matches?(target)
end
def failure_message
- "expected target to respond to #{@names_not_responded_to.collect {|name| name.inspect }.join(', ')}"
+ "expected #{@given.inspect} 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(', ')}"
+ "expected #{@given.inspect} not to respond to #{@names.collect {|name| name.inspect }.join(', ')}"
end
def description
View
10 lib/spec/matchers/satisfy.rb
@@ -6,18 +6,18 @@ def initialize(&block)
@block = block
end
- def matches?(actual, &block)
+ def matches?(given, &block)
@block = block if block
- @actual = actual
- @block.call(actual)
+ @given = given
+ @block.call(given)
end
def failure_message
- "expected #{@actual} to satisfy block"
+ "expected #{@given} to satisfy block"
end
def negative_failure_message
- "expected #{@actual} not to satisfy block"
+ "expected #{@given} not to satisfy block"
end
end
View
16 lib/spec/matchers/simple_matcher.rb
@@ -8,13 +8,13 @@ def initialize(description, &match_block)
@match_block = match_block
end
- def matches?(actual)
- @actual = actual
+ def matches?(given)
+ @given = given
case @match_block.arity
when 2
- @match_block.call(@actual, self)
+ @match_block.call(@given, self)
else
- @match_block.call(@actual)
+ @match_block.call(@given)
end
end
@@ -23,11 +23,11 @@ def description
end
def failure_message
- @failure_message || (@description.nil? ? explanation : %[expected #{@description.inspect} but got #{@actual.inspect}])
+ @failure_message || (@description.nil? ? explanation : %[expected #{@description.inspect} but got #{@given.inspect}])
end
def negative_failure_message
- @negative_failure_message || (@description.nil? ? explanation : %[expected not to get #{@description.inspect}, but got #{@actual.inspect}])
+ @negative_failure_message || (@description.nil? ? explanation : %[expected not to get #{@description.inspect}, but got #{@given.inspect}])
end
def explanation
@@ -81,7 +81,7 @@ def explanation
# simple_matcher("rhyme with #{expected.inspect}") do |given, matcher|
# matcher.failure_message = "expected #{given.inspect} to rhyme with #{expected.inspect}"
# matcher.negative_failure_message = "expected #{given.inspect} not to rhyme with #{expected.inspect}"
- # actual.rhymes_with? expected
+ # given.rhymes_with? expected
# end
# end
#
@@ -92,7 +92,7 @@ def explanation
# matcher.description = "rhyme with #{expected.inspect}"
# matcher.failure_message = "expected #{given.inspect} to rhyme with #{expected.inspect}"
# matcher.negative_failure_message = "expected #{given.inspect} not to rhyme with #{expected.inspect}"
- # actual.rhymes_with? expected
+ # given.rhymes_with? expected
# end
# end
#
View
6 lib/spec/matchers/throw_symbol.rb
@@ -7,9 +7,9 @@ def initialize(expected=nil)
@actual = nil
end
- def matches?(proc)
+ def matches?(given_proc)
begin
- proc.call
+ given_proc.call
rescue NameError => e
raise e unless e.message =~ /uncaught throw/
@actual = e.name.to_sym
@@ -56,7 +56,7 @@ def expected
# should_not throw_symbol()
# should_not throw_symbol(:sym)
#
- # Given a Symbol argument, matches if a proc throws the specified Symbol.
+ # Given a Symbol argument, matches if the given proc throws the specified Symbol.
#
# Given no argument, matches if a proc throws any Symbol.
#
View
12 spec/spec/matchers/respond_to_spec.rb
@@ -8,8 +8,8 @@
it "should fail target does not respond to :sym" do
lambda {
- Object.new.should respond_to(:some_method)
- }.should fail_with("expected target to respond to :some_method")
+ "this string".should respond_to(:some_method)
+ }.should fail_with("expected \"this string\" to respond to :some_method")
end
end
@@ -23,19 +23,19 @@
it "should fail target does not respond to first message" do
lambda {
Object.new.should respond_to('method_one', 'inspect')
- }.should fail_with('expected target to respond to "method_one"')
+ }.should fail_with(/expected #<Object:.*> to respond to "method_one"/)
end
it "should fail target does not respond to second message" do
lambda {
Object.new.should respond_to('inspect', 'method_one')
- }.should fail_with('expected target to respond to "method_one"')
+ }.should fail_with(/expected #<Object:.*> to respond to "method_one"/)
end
it "should fail target does not respond to either message" do
lambda {
Object.new.should respond_to('method_one', 'method_two')
- }.should fail_with('expected target to respond to "method_one", "method_two"')
+ }.should fail_with(/expected #<Object:.*> to respond to "method_one", "method_two"/)
end
end
@@ -48,7 +48,7 @@
it "should fail target responds to :sym" do
lambda {
Object.new.should_not respond_to(:methods)
- }.should fail_with("expected target not to respond to :methods")
+ }.should fail_with(/expected #<Object:.*> not to respond to :methods/)
end
end

0 comments on commit 8af8ff2

Please sign in to comment.
Something went wrong with that request. Please try again.