Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Working on the assertion count for operator expectations.

Signed-off-by: Jeremy McAnally <jeremymcanally@gmail.com>
  • Loading branch information...
commit aa01e908e3b9e3e4f29f9fa3884e34c0c15a2540 1 parent 15bf16d
Matthias Hennemeyer mhennemeyer authored committed
10 README.rdoc
View
@@ -3,7 +3,7 @@
* http://github.com/mhennemeyer/matchy
== DESCRIPTION:
-"A 300loc refactoring of Jeremy Mcanally's Matchy. Original Description:
+A 300loc refactoring of Jeremy Mcanally's Matchy. Original Description:
Hate writing assertions? Need a little behavior-driven love in your tests? Then matchy is for you.
== FEATURES/PROBLEMS:
@@ -34,10 +34,10 @@ Hate writing assertions? Need a little behavior-driven love in your tests? The
* Most of familiar RSpec Matchers are built in
# raise_error matcher
- lambda {raise}.should raise_error #pass
- lambda {raise MyCustomError}.should raise_error(MyCustomError) #pass
- lambda {raise "message"}.should raise_error("message") #pass
- lambda {raise "message"}.should raise_error(/essa/) #pass
+ lambda {raise}.should raise_error #pass
+ lambda {raise MyCustomError.new}.should raise_error(MyCustomError) #pass
+ lambda {raise "message"}.should raise_error("message") #pass
+ lambda {raise "message"}.should raise_error(/essa/) #pass
# change matcher
lambda {@var+=1}.should change {@var}
19 lib/matchy/built_in/enumerable_expectations.rb
View
@@ -12,11 +12,7 @@ module TestCaseExtensions
# ['a', 'b', 'c'].should include('a', 'c')
#
def include(*obj)
- build_matcher(:include, obj) do |given, matcher, args|
- matcher.positive_msg = "Expected #{given.inspect} to include #{args.inspect}."
- matcher.negative_msg = "Expected #{given.inspect} to not include #{args.inspect}."
- args.inject(true) {|m,o| m && given.include?(o) }
- end
+ _clude(:include, obj)
end
# Expects the receiver to exclude the given object(s). You can provide
@@ -29,10 +25,15 @@ def include(*obj)
# ['a', 'b', 'c'].should exclude('e', 'f', 'g')
#
def exclude(*obj)
- build_matcher(:exlude, obj) do |given, matcher, args|
- matcher.positive_msg = "Expected #{given.inspect} to exclude #{args.inspect}."
- matcher.negative_msg = "Expected #{given.inspect} to not exclude #{args.inspect}."
- args.inject(true) {|m,o| m && !given.include?(o) }
+ _clude(:exclude, obj)
+ end
+
+ private
+ def _clude(sym, obj)
+ build_matcher(sym, obj) do |given, matcher, args|
+ matcher.positive_msg = "Expected #{given.inspect} to #{sym} #{args.inspect}."
+ matcher.negative_msg = "Expected #{given.inspect} to not #{sym} #{args.inspect}."
+ args.inject(true) {|m,o| m && (given.include?(o) == (sym == :include)) }
end
end
end
26 lib/matchy/built_in/error_expectations.rb
View
@@ -23,9 +23,8 @@ def raise_error(*obj)
end
if expected.respond_to?(:ancestors) && expected.ancestors.include?(Exception)
- extra = "but none was raised"
- extra = "but #{error.class.name} was raised instead" if error
- matcher.positive_msg = "Expected #{@receiver.inspect} to raise #{expected.name}, #{extra}."
+ matcher.positive_msg = "Expected #{@receiver.inspect} to raise #{expected.name}, " +
+ (error ? "but #{error.class.name} was raised instead." : "but none was raised.")
matcher.negative_msg = "Expected #{@receiver.inspect} to not raise #{expected.name}."
comparison = (raised && error.class.ancestors.include?(expected))
else
@@ -46,22 +45,21 @@ def raise_error(*obj)
# lambda { "not this time" }.should_not throw_symbol(:hello)
#
def throw_symbol(*obj)
- build_matcher(:throw_symbol, obj) do |given, matcher, args|
- raised = false
- thrown_symbol = nil
+ build_matcher(:throw_symbol, obj) do |receiver, matcher, args|
+ raised, thrown_symbol, expected = false, nil, args[0]
begin
- given.call
+ receiver.call
rescue NameError => e
raise e unless e.message =~ /uncaught throw/
raised = true
- thrown_symbol = e.name.to_sym
+ thrown_symbol = e.name.to_sym if e.respond_to?(:name)
+ rescue ArgumentError => e
+ raise e unless e.message =~ /uncaught throw/
+ thrown_symbol = e.message.match(/uncaught throw :(.+)/)[1].to_sym
end
- expected = args[0]
- matcher.positive_msg = <<-END
-Expected #{given.inspect} to throw :#{expected}, but \
-#{thrown_symbol ? ':' + thrown_symbol.to_s + ' was thrown instead' : 'no symbol was thrown'}.
-END
- matcher.negative_msg = "Expected #{given.inspect} to not throw :#{expected}."
+ matcher.positive_msg = "Expected #{receiver.inspect} to throw :#{expected}, but " +
+ "#{thrown_symbol ? ':' + thrown_symbol.to_s + ' was thrown instead' : 'no symbol was thrown'}."
+ matcher.negative_msg = "Expected #{receiver.inspect} to not throw :#{expected}."
expected == thrown_symbol
end
end
4 lib/matchy/matcher_builder.rb
View
@@ -14,8 +14,7 @@ def self.matcher_name
attr_accessor :positive_msg, :negative_msg, :msgs
attr_reader :matcher_name
def initialize match_block, test_case
- @test_case = test_case
- @match_block = match_block
+ @match_block, @test_case = match_block, test_case
@matcher_name = self.class.matcher_name
end
@@ -41,6 +40,7 @@ def pass!(which)
alias_method :failure_message, :positive_msg
alias_method :negative_failure_message, :negative_msg
end
+ p self
Class.new(&body).new(match_block, self)
end
end
2  test/test_error_expectations.rb
View
@@ -58,11 +58,13 @@ def test_negative_throws_symbol
end
def test_negative_throws_symbol_fails_with_different_symbol
+
lambda{
lambda {
throw :fail
}.should_not throw_symbol(:fail)
}.should raise_error(Test::Unit::AssertionFailedError)
+
end
def test_error_fail_message
Please sign in to comment.
Something went wrong with that request. Please try again.