Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Conversion to rspec-expecctations

  • Loading branch information...
commit 1d690dca4f5861dbcce24b1acb57dadb98dbe29d 1 parent 628b29c
@spicycode authored
Showing with 31 additions and 4,364 deletions.
  1. +1 −1  Gemfile
  2. +19 −1 Gemfile.lock
  3. +1 −15 lib/micronaut.rb
  4. +1 −1  lib/micronaut/behaviour.rb
  5. +1 −45 lib/micronaut/expectations.rb
  6. +0 −92 lib/micronaut/expectations/extensions/object.rb
  7. +0 −51 lib/micronaut/expectations/handler.rb
  8. +0 −52 lib/micronaut/expectations/wrap_expectation.rb
  9. +1 −1  lib/micronaut/formatters/base_text_formatter.rb
  10. +1 −1  lib/micronaut/formatters/documentation_formatter.rb
  11. +0 −141 lib/micronaut/matchers.rb
  12. +0 −204 lib/micronaut/matchers/be.rb
  13. +0 −37 lib/micronaut/matchers/be_close.rb
  14. +0 −148 lib/micronaut/matchers/change.rb
  15. +0 −26 lib/micronaut/matchers/eql.rb
  16. +0 −26 lib/micronaut/matchers/equal.rb
  17. +0 −36 lib/micronaut/matchers/generated_descriptions.rb
  18. +0 −19 lib/micronaut/matchers/has.rb
  19. +0 −153 lib/micronaut/matchers/have.rb
  20. +0 −80 lib/micronaut/matchers/include.rb
  21. +0 −22 lib/micronaut/matchers/match.rb
  22. +0 −9 lib/micronaut/matchers/method_missing.rb
  23. +0 −50 lib/micronaut/matchers/operator_matcher.rb
  24. +0 −128 lib/micronaut/matchers/raise_error.rb
  25. +0 −50 lib/micronaut/matchers/respond_to.rb
  26. +0 −50 lib/micronaut/matchers/satisfy.rb
  27. +0 −135 lib/micronaut/matchers/simple_matcher.rb
  28. +0 −108 lib/micronaut/matchers/throw_symbol.rb
  29. +4 −1 micronaut.gemspec
  30. +0 −146 spec/lib/micronaut/expectations/extensions/object_spec.rb
  31. +0 −17 spec/lib/micronaut/expectations/fail_with_spec.rb
  32. +0 −27 spec/lib/micronaut/expectations/wrap_expectation_spec.rb
  33. +0 −52 spec/lib/micronaut/matchers/be_close_spec.rb
  34. +0 −298 spec/lib/micronaut/matchers/be_spec.rb
  35. +0 −360 spec/lib/micronaut/matchers/change_spec.rb
  36. +0 −175 spec/lib/micronaut/matchers/description_generation_spec.rb
  37. +0 −35 spec/lib/micronaut/matchers/eql_spec.rb
  38. +0 −35 spec/lib/micronaut/matchers/equal_spec.rb
  39. +0 −69 spec/lib/micronaut/matchers/has_spec.rb
  40. +0 −392 spec/lib/micronaut/matchers/have_spec.rb
  41. +0 −103 spec/lib/micronaut/matchers/include_spec.rb
  42. +0 −43 spec/lib/micronaut/matchers/match_spec.rb
  43. +0 −78 spec/lib/micronaut/matchers/matcher_methods_spec.rb
  44. +0 −193 spec/lib/micronaut/matchers/operator_matcher_spec.rb
  45. +0 −348 spec/lib/micronaut/matchers/raise_error_spec.rb
  46. +0 −54 spec/lib/micronaut/matchers/respond_to_spec.rb
  47. +0 −36 spec/lib/micronaut/matchers/satisfy_spec.rb
  48. +0 −93 spec/lib/micronaut/matchers/simple_matcher_spec.rb
  49. +0 −125 spec/lib/micronaut/matchers/throw_symbol_spec.rb
  50. +2 −2 spec/spec_helper.rb
View
2  Gemfile
@@ -1,3 +1,3 @@
-source "http://rubygems.org"
+source "https://rubygems.org"
gemspec
View
20 Gemfile.lock
@@ -2,17 +2,35 @@ PATH
remote: .
specs:
micronaut (0.4.0)
+ rspec-expectations
GEM
- remote: http://rubygems.org/
+ remote: https://rubygems.org/
specs:
+ coderay (1.0.6)
+ diff-lcs (1.1.3)
+ ffi (1.0.11)
+ guard (1.0.3)
+ ffi (>= 0.5.0)
+ thor (>= 0.14.6)
metaclass (0.0.1)
+ method_source (0.7.1)
mocha (0.10.5)
metaclass (~> 0.0.1)
+ pry (0.9.9.6)
+ coderay (~> 1.0.5)
+ method_source (~> 0.7.1)
+ slop (>= 2.4.4, < 3)
+ rspec-expectations (2.10.0)
+ diff-lcs (~> 1.1.3)
+ slop (2.4.4)
+ thor (0.15.2)
PLATFORMS
ruby
DEPENDENCIES
+ guard (~> 1.0.0)
micronaut!
mocha (~> 0.10.5)
+ pry
View
16 lib/micronaut.rb
@@ -1,5 +1,4 @@
require 'micronaut/mocking/with_absolutely_nothing'
-require 'micronaut/matchers'
require 'micronaut/expectations'
require 'micronaut/world'
require 'micronaut/configuration'
@@ -10,20 +9,7 @@
require 'micronaut/formatters'
module Micronaut
- file = if RUBY_VERSION =~ /^1\.9/ then # bt's expanded, but __FILE__ isn't :(
- File.expand_path __FILE__
- elsif __FILE__ =~ /^[^\.]/ then # assume both relative
- require 'pathname'
- pwd = Pathname.new(Dir.pwd)
- path_name = Pathname.new(File.expand_path(__FILE__))
- path_name = File.join(".", path_name.relative_path_from(pwd)) unless path_name.relative?
- path_name.to_s
- else # assume both are expanded
- __FILE__
- end
-
- # './lib' in project dir, or '/usr/local/blahblah' if installed
- InstallDirectory = File.expand_path(File.dirname(File.dirname(file)) + "/lib")
+ InstallDirectory = File.expand_path(File.dirname(File.dirname(File.expand_path(__FILE__))) + "/lib")
def self.configuration
@configuration ||= Micronaut::Configuration.new
View
2  lib/micronaut/behaviour.rb
@@ -1,6 +1,6 @@
module Micronaut
class Behaviour
- include Micronaut::Matchers
+ include ::RSpec::Matchers
attr_accessor :running_example, :reporter
View
46 lib/micronaut/expectations.rb
@@ -1,45 +1 @@
-require 'micronaut/matchers'
-require 'micronaut/expectations/extensions/object'
-require 'micronaut/expectations/handler'
-require 'micronaut/expectations/wrap_expectation'
-
-module Micronaut
-
- # Micronaut::Expectations lets you set expectations on your objects.
- #
- # result.should == 37
- # team.should have(11).players_on_the_field
- #
- # == How Expectations work.
- #
- # Micronaut::Expectations adds two methods to Object:
- #
- # should(matcher=nil)
- # should_not(matcher=nil)
- #
- # Both methods take an optional Expression Matcher (See Micronaut::Matchers).
- #
- # When +should+ receives an Expression Matcher, it calls <tt>matches?(self)</tt>. If
- # it returns +true+, the spec passes and execution continues. If it returns
- # +false+, then the spec fails with the message returned by <tt>matcher.failure_message</tt>.
- #
- # Similarly, when +should_not+ receives a matcher, it calls <tt>matches?(self)</tt>. If
- # it returns +false+, the spec passes and execution continues. If it returns
- # +true+, then the spec fails with the message returned by <tt>matcher.negative_failure_message</tt>.
- #
- # Micronaut ships with a standard set of useful matchers, and writing your own
- # matchers is quite simple. See Micronaut::Matchers for details.
- module Expectations
-
- class ExpectationNotMetError < ::StandardError; end
-
- def self.fail_with(message, expected=nil, target=nil) # :nodoc:
- if Array === message && message.length == 3
- message, expected, target = message[0], message[1], message[2]
- end
- Kernel::raise(Micronaut::Expectations::ExpectationNotMetError.new(message))
- end
-
- end
-
-end
+require 'rspec/expectations'
View
92 lib/micronaut/expectations/extensions/object.rb
@@ -1,92 +0,0 @@
-module Micronaut
- module Expectations
- class InvalidMatcherError < ArgumentError; end
-
- 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 Micronaut::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)
- ::Micronaut::Matchers.last_should = "should"
- return ::Micronaut::Matchers::PositiveOperatorMatcher.new(self) if matcher.nil?
-
- unless matcher.respond_to?(:matches?)
- raise InvalidMatcherError, "Expected a matcher, got #{matcher.inspect}."
- end
-
- match_found = matcher.matches?(self, &block)
- ::Micronaut::Matchers.last_matcher = matcher
-
- ::Micronaut::Expectations.fail_with(matcher.failure_message) unless match_found
- match_found
- 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 Micronaut::Matchers for more information about matchers
- def should_not(matcher=nil, &block)
- ::Micronaut::Matchers.last_should = "should not"
- return ::Micronaut::Matchers::NegativeOperatorMatcher.new(self) if matcher.nil?
-
- unless matcher.respond_to?(:matches?)
- raise InvalidMatcherError, "Expected a matcher, got #{matcher.inspect}."
- end
-
- unless matcher.respond_to?(:negative_failure_message)
- ::Micronaut::Expectations.fail_with(
- <<-EOF
- Matcher does not support should_not.
- See Micronaut::Matchers for more information about matchers.
- EOF
- )
- end
- match_found = matcher.matches?(self, &block)
- ::Micronaut::Matchers.last_matcher = matcher
-
- ::Micronaut::Expectations.fail_with(matcher.negative_failure_message) if match_found
- match_found
- end
-
- end
- end
-end
-
-class Object
- include Micronaut::Expectations::ObjectExpectations
-end
View
51 lib/micronaut/expectations/handler.rb
@@ -1,51 +0,0 @@
-module Micronaut
- module Expectations
-
-
- # class ExpectationMatcherHandler
- #
- # def self.handle_matcher(actual, matcher, &block)
- # ::Micronaut::Matchers.last_should = "should"
- # return Micronaut::Matchers::PositiveOperatorMatcher.new(actual) if matcher.nil?
- #
- # unless matcher.respond_to?(:matches?)
- # raise InvalidMatcherError, "Expected a matcher, got #{matcher.inspect}."
- # end
- #
- # match = matcher.matches?(actual, &block)
- # ::Micronaut::Matchers.last_matcher = matcher
- # Micronaut::Expectations.fail_with(matcher.failure_message) unless match
- # match
- # end
- #
- # end
-
- # class NegativeExpectationMatcherHandler
- #
- # def self.handle_matcher(actual, matcher, &block)
- # ::Micronaut::Matchers.last_should = "should not"
- # return Micronaut::Matchers::NegativeOperatorMatcher.new(actual) if matcher.nil?
- #
- # unless matcher.respond_to?(:matches?)
- # raise InvalidMatcherError, "Expected a matcher, got #{matcher.inspect}."
- # end
- #
- # unless matcher.respond_to?(:negative_failure_message)
- # Micronaut::Expectations.fail_with(
- # <<-EOF
- # Matcher does not support should_not.
- # See Micronaut::Matchers for more information
- # about matchers.
- # EOF
- # )
- # end
- # match = matcher.matches?(actual, &block)
- # ::Micronaut::Matchers.last_matcher = matcher
- # Micronaut::Expectations.fail_with(matcher.negative_failure_message) if match
- # match
- # end
-
- # end
-
- end
-end
View
52 lib/micronaut/expectations/wrap_expectation.rb
@@ -1,52 +0,0 @@
-module Micronaut
- module Matchers
-
- # wraps an expectation in a block that will return true if the
- # expectation passes and false if it fails (without bubbling up
- # the failure).
- #
- # This is intended to be used in the context of a simple matcher,
- # and is especially useful for wrapping multiple expectations or
- # one or more assertions from test/unit extensions when running
- # with test/unit.
- #
- # == Examples
- #
- # def eat_cheese(cheese)
- # simple_matcher do |mouse, matcher|
- # matcher.negative_failure_message = "expected #{mouse} not to eat cheese"
- # wrap_expectation do |matcher|
- # assert_eats_cheese(mouse)
- # end
- # end
- # end
- #
- # describe Mouse do
- # it "eats cheese" do
- # Mouse.new.should eat_cheese
- # end
- # end
- #
- # The other benefit you get is that you can use the negative version
- # of the matcher:
- #
- # describe Cat do
- # it "does not eat cheese" do
- # Cat.new.should_not eat_cheese
- # end
- # end
- #
- # So in the event there is no assert_does_not_eat_cheese available,
- # you're all set!
- def wrap_expectation(matcher, &block)
- begin
- block.call(matcher)
- return true
- rescue Exception => e
- matcher.failure_message = e.message
- return false
- end
- end
-
- end
-end
View
2  lib/micronaut/formatters/base_text_formatter.rb
@@ -27,7 +27,7 @@ def dump_failures
end
def colorise(s, failure)
- if failure.is_a?(Micronaut::Expectations::ExpectationNotMetError)
+ if failure.is_a?(RSpec::Expectations::ExpectationNotMetError)
red(s)
else
magenta(s)
View
2  lib/micronaut/formatters/documentation_formatter.rb
@@ -45,7 +45,7 @@ def example_finished(example)
end
def failure_output(example, exception)
- expectation_not_met = exception.is_a?(::Micronaut::Expectations::ExpectationNotMetError)
+ expectation_not_met = exception.is_a?(::RSpec::Expectations::ExpectationNotMetError)
message = if expectation_not_met
"#{current_indentation}#{example.description} (FAILED)"
View
141 lib/micronaut/matchers.rb
@@ -1,141 +0,0 @@
-require 'micronaut/matchers/generated_descriptions'
-require 'micronaut/matchers/simple_matcher'
-require 'micronaut/matchers/be'
-require 'micronaut/matchers/be_close'
-require 'micronaut/matchers/change'
-require 'micronaut/matchers/eql'
-require 'micronaut/matchers/equal'
-require 'micronaut/matchers/has'
-require 'micronaut/matchers/have'
-require 'micronaut/matchers/include'
-require 'micronaut/matchers/match'
-require 'micronaut/matchers/raise_error'
-require 'micronaut/matchers/respond_to'
-require 'micronaut/matchers/satisfy'
-require 'micronaut/matchers/throw_symbol'
-require 'micronaut/matchers/operator_matcher'
-
-module Micronaut
-
- # We ship (courtesy of RSpec and Micronaut) with a number of useful Expression Matchers. An Expression Matcher
- # is any object that responds to the following methods:
- #
- # matches?(actual)
- # failure_message
- # negative_failure_message #optional
- # description #optional
- #
- # See Micronaut::Expectations to learn how to use these as Expectation Matchers.
- # See Micronaut::Mocks to learn how to use them as Mock Argument Constraints.
- #
- # == Predicates
- #
- # In addition to those Expression Matchers that are defined explicitly, we will
- # create custom Matchers on the fly for any arbitrary predicate, giving your specs
- # a much more natural language feel.
- #
- # A Ruby predicate is a method that ends with a "?" and returns true or false.
- # Common examples are +empty?+, +nil?+, and +instance_of?+.
- #
- # All you need to do is write +should be_+ followed by the predicate without
- # the question mark, and we will figure it out from there. For example:
- #
- # [].should be_empty => [].empty? #passes
- # [].should_not be_empty => [].empty? #fails
- #
- # In addtion to prefixing the predicate matchers with "be_", you can also use "be_a_"
- # and "be_an_", making your specs read much more naturally:
- #
- # "a string".should be_an_instance_of(String) =>"a string".instance_of?(String) #passes
- #
- # 3.should be_a_kind_of(Fixnum) => 3.kind_of?(Numeric) #passes
- # 3.should be_a_kind_of(Numeric) => 3.kind_of?(Numeric) #passes
- # 3.should be_an_instance_of(Fixnum) => 3.instance_of?(Fixnum) #passes
- # 3.should_not be_instance_of(Numeric) => 3.instance_of?(Numeric) #fails
- #
- # We will also create custom matchers for predicates like +has_key?+. To
- # use this feature, just state that the object should have_key(:key) and we will
- # call has_key?(:key) on the target. For example:
- #
- # {:a => "A"}.should have_key(:a) => {:a => "A"}.has_key?(:a) #passes
- # {:a => "A"}.should have_key(:b) => {:a => "A"}.has_key?(:b) #fails
- #
- # You can use this feature to invoke any predicate that begins with "has_", whether it is
- # part of the Ruby libraries (like +Hash#has_key?+) or a method you wrote on your own class.
- #
- # == Custom Expectation Matchers
- #
- # When you find that none of the stock Expectation Matchers provide a natural
- # feeling expectation, you can very easily write your own.
- #
- # For example, imagine that you are writing a game in which players can
- # be in various zones on a virtual board. To specify that bob should
- # be in zone 4, you could say:
- #
- # bob.current_zone.should eql(Zone.new("4"))
- #
- # But you might find it more expressive to say:
- #
- # bob.should be_in_zone("4")
- #
- # and/or
- #
- # bob.should_not be_in_zone("3")
- #
- # To do this, you would need to write a class like this:
- #
- # class BeInZone
- # def initialize(expected)
- # @expected = expected
- # end
- # def matches?(target)
- # @target = target
- # @target.current_zone.eql?(Zone.new(@expected))
- # end
- # def failure_message
- # "expected #{@target.inspect} to be in Zone #{@expected}"
- # end
- # def negative_failure_message
- # "expected #{@target.inspect} not to be in Zone #{@expected}"
- # end
- # end
- #
- # ... and a method like this:
- #
- # def be_in_zone(expected)
- # BeInZone.new(expected)
- # end
- #
- # And then expose the method to your specs. This is normally done
- # by including the method and the class in a module, which is then
- # included in your spec:
- #
- # module CustomGameMatchers
- # class BeInZone
- # ...
- # end
- #
- # def be_in_zone(expected)
- # ...
- # end
- # end
- #
- # describe "Player behaviour" do
- # include CustomGameMatchers
- # ...
- # end
-
- module Matchers
-
- class MatcherError < StandardError; end
-
- private
- def method_missing(sym, *args, &block) # :nodoc:
- return Matchers::Be.new(sym, *args) if sym.to_s =~ /^be_/
- return has(sym, *args) if sym.to_s =~ /^have_/
- super
- end
-
- end
-
-end
View
204 lib/micronaut/matchers/be.rb
@@ -1,204 +0,0 @@
-module Micronaut
- module Matchers
-
- class Be #:nodoc:
- def initialize(*args)
- @expected = args.empty? ? true : set_expected(args.shift)
- @args = args
- end
-
- def matches?(actual)
- @actual = actual
- handling_predicate? ? run_predicate_on(actual) : match_or_compare(actual)
- end
-
- def run_predicate_on(actual)
- begin
- return @result = actual.__send__(predicate, *@args)
- rescue NameError => predicate_missing_error
- "this needs to be here or rcov will not count this branch even though it's executed in a code example"
- end
-
- begin
- return @result = actual.__send__(present_tense_predicate, *@args)
- rescue NameError
- raise predicate_missing_error
- end
- end
-
- def failure_message
- handling_predicate? ?
- "expected #{predicate}#{args_to_s} to return true, got #{@result.inspect}" :
- "expected #{@comparison_method} #{expected}, got #{@actual.inspect}".gsub(' ',' ')
- end
-
- def negative_failure_message
- if handling_predicate?
- "expected #{predicate}#{args_to_s} to return false, got #{@result.inspect}"
- else
- message = <<-MESSAGE
-'should_not be #{@comparison_method} #{expected}' not only FAILED,
-it reads really poorly.
- MESSAGE
-
- raise message << ([:===,:==].include?(@comparison_method) ?
- "Why don't you try expressing it without the \"be\"?" :
- "Why don't you try expressing it in the positive?")
- end
- end
-
- def description
- "#{prefix_to_sentence}#{comparison} #{expected_to_sentence}#{args_to_sentence}".gsub(/\s+/,' ')
- end
-
- [:==, :<, :<=, :>=, :>, :===].each do |method|
- define_method method do |expected|
- compare_to(expected, :using => method)
- self
- end
- end
-
- private
- def match_or_compare(actual)
- case @expected
- when TrueClass
- @actual
- else
- @actual.__send__(comparison_method, @expected)
- end
- end
-
- def comparison_method
- @comparison_method || :equal?
- end
-
- def expected
- @expected
- end
-
- def compare_to(expected, opts)
- @expected, @comparison_method = expected, opts[:using]
- end
-
- def set_expected(expected)
- Symbol === expected ? parse_expected(expected) : expected
- end
-
- def parse_expected(expected)
- ["be_an_","be_a_","be_"].each do |prefix|
- handling_predicate!
- if expected.to_s =~ /^#{prefix}/
- set_prefix(prefix)
- expected = expected.to_s.sub(prefix,"")
- [true, false, nil].each do |val|
- return val if val.to_s == expected
- end
- return expected.to_sym
- end
- end
- end
-
- def set_prefix(prefix)
- @prefix = prefix
- end
-
- def prefix
- @prefix
- end
-
- def handling_predicate!
- @handling_predicate = true
- 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
- @args.empty? ? "" : parenthesize(inspected_args.join(', '))
- end
-
- def parenthesize(string)
- return "(#{string})"
- end
-
- def inspected_args
- @args.map { |a| a.inspect }
- end
-
- def comparison
- @comparison_method.nil? ? " " : "be #{@comparison_method.to_s} "
- 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_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 value 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 "?"), we 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_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
37 lib/micronaut/matchers/be_close.rb
@@ -1,37 +0,0 @@
-module Micronaut
- module Matchers
-
- class BeClose
- def initialize(expected, delta)
- @expected, @delta = expected, 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)
- BeClose.new(expected, delta)
- end
-
- end
-end
View
148 lib/micronaut/matchers/change.rb
@@ -1,148 +0,0 @@
-module Micronaut
- module Matchers
-
- #Based on patch from Wilson Bilkovich
- class Change #:nodoc:
- def initialize(receiver=nil, message=nil, &block)
- @message = message || "result"
- @value_proc = block || lambda {
- receiver.__send__(message)
- }
- end
-
- def matches?(event_proc)
- raise_block_syntax_error if block_given?
-
- @before = evaluate_value_proc
- event_proc.call
- @after = evaluate_value_proc
-
- return false if @from unless @from == @before
- return false if @to unless @to == @after
- return (@before + @amount == @after) if @amount
- return ((@after - @before) >= @minimum) if @minimum
- return ((@after - @before) <= @maximum) if @maximum
- @before != @after
- end
-
- def raise_block_syntax_error
- raise MatcherError.new(<<-MESSAGE
-block passed to should or should_not change must use {} instead of do/end
-MESSAGE
- )
- end
-
- def evaluate_value_proc
- @value_proc.call
- end
-
- def failure_message
- if @to
- "#{@message} should have been changed to #{@to.inspect}, but is now #{@after.inspect}"
- elsif @from
- "#{@message} should have initially been #{@from.inspect}, but was #{@before.inspect}"
- elsif @amount
- "#{@message} should have been changed by #{@amount.inspect}, but was changed by #{actual_delta.inspect}"
- elsif @minimum
- "#{@message} should have been changed by at least #{@minimum.inspect}, but was changed by #{actual_delta.inspect}"
- elsif @maximum
- "#{@message} should have been changed by at most #{@maximum.inspect}, but was changed by #{actual_delta.inspect}"
- else
- "#{@message} should have changed, but is still #{@before.inspect}"
- end
- end
-
- def actual_delta
- @after - @before
- end
-
- def negative_failure_message
- "#{@message} 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 <tt>receiver.message</tt> or <tt>block</tt> before and after
- # it evaluates the c object (generated by the lambdas in the examples
- # above).
- #
- # Then compares the values before and after the <tt>receiver.message</tt>
- # and evaluates the difference compared to the expected difference.
- #
- # == WARNING
- # <tt>should_not change</tt> only supports the form with no
- # subsequent calls to <tt>by</tt>, <tt>by_at_least</tt>,
- # <tt>by_at_most</tt>, <tt>to</tt> or <tt>from</tt>.
- #
- # blocks passed to <tt>should</tt> <tt>change</tt> and <tt>should_not</tt>
- # <tt>change</tt> must use the <tt>{}</tt> form (<tt>do/end</tt> is not
- # supported).
- #
- def change(receiver=nil, message=nil, &block)
- Matchers::Change.new(receiver, message, &block)
- end
- end
-end
View
26 lib/micronaut/matchers/eql.rb
@@ -1,26 +0,0 @@
-module Micronaut
- module Matchers
-
- # :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)
- simple_matcher do |actual, matcher|
- matcher.failure_message = "expected #{expected.inspect}, got #{actual.inspect} (using .eql?)", expected, actual
- matcher.negative_failure_message = "expected #{actual.inspect} not to equal #{expected.inspect} (using .eql?)", expected, actual
- matcher.description = "eql #{expected.inspect}"
- actual.eql?(expected)
- end
- end
-
- end
-end
View
26 lib/micronaut/matchers/equal.rb
@@ -1,26 +0,0 @@
-module Micronaut
- module Matchers
-
- # :call-seq:
- # should equal(expected)
- # should_not equal(expected)
- #
- # 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.
- #
- # == 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)
- simple_matcher do |actual, matcher|
- matcher.failure_message = "expected #{expected.inspect}, got #{actual.inspect} (using .equal?)", expected, actual
- matcher.negative_failure_message = "expected #{actual.inspect} not to equal #{expected.inspect} (using .equal?)", expected, actual
- matcher.description = "equal #{expected.inspect}"
- actual.equal?(expected)
- end
- end
-
- end
-end
View
36 lib/micronaut/matchers/generated_descriptions.rb
@@ -1,36 +0,0 @@
-module Micronaut
- module Matchers
- class << self
- attr_accessor :last_matcher, :last_should
-
- def clear_generated_description
- self.last_matcher = nil
- self.last_should = nil
- end
-
- def generated_description
- return nil if last_should.nil?
- "#{last_should} #{last_description}"
- end
-
- private
-
- def last_description
- last_matcher.respond_to?(:description) ? last_matcher.description : <<-MESSAGE
-When you call a matcher in an example without a String, like this:
-
- specify { object.should matcher }
-
-or this:
-
- it { should matcher }
-
-the runner expects the matcher to have a #describe method. You should either
-add a String to the example this matcher is being used in, or give it a
-description method. Then you won't have to suffer this lengthy warning again.
-MESSAGE
- end
- end
- end
-end
-
View
19 lib/micronaut/matchers/has.rb
@@ -1,19 +0,0 @@
-module Micronaut
- module Matchers
-
- def has(sym, *args) # :nodoc:
- simple_matcher do |actual, matcher|
- matcher.failure_message = "expected ##{predicate(sym)}(#{args[0].inspect}) to return true, got false"
- matcher.negative_failure_message = "expected ##{predicate(sym)}(#{args[0].inspect}) to return false, got true"
- matcher.description = "have key #{args[0].inspect}"
- actual.__send__(predicate(sym), *args)
- end
- end
-
- private
- def predicate(sym)
- "#{sym.to_s.sub("have_","has_")}?".to_sym
- end
-
- end
-end
View
153 lib/micronaut/matchers/have.rb
@@ -1,153 +0,0 @@
-module Micronaut
- 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 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
- @given = collection.size if collection.respond_to?(:size)
- @given = collection.length if collection.respond_to?(:length)
- raise not_a_collection if @given.nil?
- return @given >= @expected if @relativity == :at_least
- return @given <= @expected if @relativity == :at_most
- @given == @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 #{@given}"
- end
-
- def negative_failure_message
- if @relativity == :exactly
- return "expected target not to have #{@expected} #{@collection_name}, got #{@given}"
- 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
-
- def respond_to?(sym)
- @expected.respond_to?(sym) || super
- end
-
- private
-
- def method_missing(sym, *args, &block)
- @collection_name = sym
- if inflector = (defined?(ActiveSupport::Inflector) ? ActiveSupport::Inflector : (defined?(Inflector) ? Inflector : nil))
- @plural_collection_name = inflector.pluralize(sym.to_s)
- end
- @args = args
- @block = block
- self
- end
-
- 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
80 lib/micronaut/matchers/include.rb
@@ -1,80 +0,0 @@
-module Micronaut
- module Matchers
-
- class Include #:nodoc:
-
- def initialize(*expecteds)
- @expecteds = expecteds
- end
-
- def matches?(actual)
- @actual = actual
- @expecteds.each do |expected|
- if actual.is_a?(Hash)
- if expected.is_a?(Hash)
- expected.each_pair do |k,v|
- return false unless actual[k] == v
- end
- else
- return false unless actual.has_key?(expected)
- end
- else
- return false unless actual.include?(expected)
- end
- 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
22 lib/micronaut/matchers/match.rb
@@ -1,22 +0,0 @@
-module Micronaut
- module Matchers
-
- # :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)
- simple_matcher do |actual, matcher|
- matcher.failure_message = "expected #{actual.inspect} to match #{regexp.inspect}", regexp, actual
- matcher.negative_failure_message = "expected #{actual.inspect} not to match #{regexp.inspect}", regexp, actual
- matcher.description = "match #{regexp.inspect}"
- actual =~ regexp
- end
- end
- end
-end
View
9 lib/micronaut/matchers/method_missing.rb
@@ -1,9 +0,0 @@
-module Micronaut
- module Matchers
- def method_missing(sym, *args, &block) # :nodoc:
- return Matchers::Be.new(sym, *args) if sym.to_s =~ /^be_/
- return has(sym, *args) if sym.to_s =~ /^have_/
- super
- end
- end
-end
View
50 lib/micronaut/matchers/operator_matcher.rb
@@ -1,50 +0,0 @@
-module Micronaut
- module Matchers
- class OperatorMatcher
-
- def initialize(actual)
- @actual = actual
- end
-
- ['==','===','<','<=','>=','>','=~'].each do |operator|
- define_method operator do |expected|
- ::Micronaut::Matchers.last_matcher = self
- @operator, @expected = operator, expected
- __delegate_operator(@actual, operator, expected)
- end
- end
-
- def fail_with_message(message)
- Micronaut::Expectations.fail_with(message, @expected, @actual)
- end
-
- def description
- "#{@operator} #{@expected.inspect}"
- end
-
- end
-
- class PositiveOperatorMatcher < OperatorMatcher #:nodoc:
-
- def __delegate_operator(actual, operator, expected)
- return true if actual.__send__(operator, expected)
- if ['==','===', '=~'].include?(operator)
- fail_with_message("expected: #{expected.inspect},\n got: #{actual.inspect} (using #{operator})")
- else
- fail_with_message("expected: #{operator} #{expected.inspect},\n got: #{operator.gsub(/./, ' ')} #{actual.inspect}")
- end
- end
-
- end
-
- class NegativeOperatorMatcher < OperatorMatcher #:nodoc:
-
- def __delegate_operator(actual, operator, expected)
- return true unless actual.__send__(operator, expected)
- return fail_with_message("expected not: #{operator} #{expected.inspect},\n got: #{operator.gsub(/./, ' ')} #{actual.inspect}")
- end
-
- end
-
- end
-end
View
128 lib/micronaut/matchers/raise_error.rb
@@ -1,128 +0,0 @@
-module Micronaut
- module Matchers
- class RaiseError #:nodoc:
- def initialize(expected_error_or_message=Exception, expected_message=nil, &block)
- @block = block
- case expected_error_or_message
- when String, Regexp
- @expected_error, @expected_message = Exception, expected_error_or_message
- else
- @expected_error, @expected_message = expected_error_or_message, expected_message
- end
- end
-
- def matches?(given_proc)
- @raised_expected_error = false
- @with_expected_message = false
- @eval_block = false
- @eval_block_passed = false
- begin
- given_proc.call
- rescue @expected_error => @given_error
- @raised_expected_error = true
- @with_expected_message = verify_message
- 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"
- end
-
- unless negative_expectation?
- eval_block if @raised_expected_error && @with_expected_message && @block
- end
- ensure
- return (@raised_expected_error && @with_expected_message) ? (@eval_block ? @eval_block_passed : true) : false
- end
-
- def eval_block
- @eval_block = true
- begin
- @block[@given_error]
- @eval_block_passed = true
- rescue Exception => err
- @given_error = err
- end
- end
-
- def verify_message
- case @expected_message
- when nil
- true
- when Regexp
- @expected_message =~ @given_error.message
- else
- @expected_message == @given_error.message
- end
- end
-
- def failure_message
- @eval_block ? @given_error.message : "expected #{expected_error}#{given_error}"
- end
-
- def negative_failure_message
- "expected no #{expected_error}#{given_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 given_error
- @given_error.nil? ? " but nothing was raised" : ", got #{@given_error.inspect}"
- end
-
- def negative_expectation?
- # YES - I'm a bad person... help me find a better way - ryand
- caller.first(3).find { |s| s =~ /should_not/ }
- end
- end
-
- # :call-seq:
- # should raise_error()
- # should raise_error(NamedError)
- # should raise_error(NamedError, String)
- # should raise_error(NamedError, Regexp)
- # should raise_error() { |error| ... }
- # should raise_error(NamedError) { |error| ... }
- # should raise_error(NamedError, String) { |error| ... }
- # should raise_error(NamedError, Regexp) { |error| ... }
- # 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.
- # Pass an optional block to perform extra verifications on the exception matched
- #
- # == Examples
- #
- # lambda { do_something_risky }.should raise_error
- # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError)
- # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError) { |error| error.data.should == 42 }
- # 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, &block)
- Matchers::RaiseError.new(error, message, &block)
- end
- end
-end
View
50 lib/micronaut/matchers/respond_to.rb
@@ -1,50 +0,0 @@
-module Micronaut
- module Matchers
-
- class RespondTo #:nodoc:
-
- def initialize(*names)
- @names = names
- @names_not_responded_to = []
- end
-
- def matches?(given)
- @given = given
- @names.each do |name|
- unless given.respond_to?(name)
- @names_not_responded_to << name
- end
- end
- @names_not_responded_to.empty?
- end
-
- def failure_message
- "expected #{@given.inspect} to respond to #{@names_not_responded_to.map { |name| name.inspect }.join(', ')}"
- end
-
- def negative_failure_message
- "expected #{@given.inspect} not to respond to #{@names.map { |name| name.inspect }.join(', ')}"
- end
-
- def description
- # Ruby 1.9 returns the same thing for array.to_s as array.inspect, so just use array.inspect here
- "respond to #{@names.inspect}"
- 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
50 lib/micronaut/matchers/satisfy.rb
@@ -1,50 +0,0 @@
-module Micronaut
- module Matchers
-
- class Satisfy #:nodoc:
-
- def initialize(&block)
- @block = block
- end
-
- def matches?(given, &block)
- @block = block if block
- @given = given
- @block.call(given)
- end
-
- def failure_message
- "expected #{@given} to satisfy block"
- end
-
- def negative_failure_message
- "expected #{@given} 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
135 lib/micronaut/matchers/simple_matcher.rb
@@ -1,135 +0,0 @@
-module Micronaut
- module Matchers
- class SimpleMatcher
-
- attr_writer :failure_message, :negative_failure_message, :description
-
- def initialize(description, &match_block)
- @description = description
- @match_block = match_block
- end
-
- def matches?(given)
- @given = given
- case @match_block.arity
- when 2
- @match_block.call(@given, self)
- else
- @match_block.call(@given)
- end
- end
-
- def description
- @description || explanation
- end
-
- def failure_message
- @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 #{@given.inspect}])
- end
-
- def explanation
- "No description provided. See RDoc for simple_matcher()"
- end
-
- end
-
- # simple_matcher makes it easy for you to create your own custom matchers
- # in just a few lines of code when you don't need all the power of a
- # completely custom matcher object.
- #
- # The <tt>description</tt> argument will appear as part of any failure
- # message, and is also the source for auto-generated descriptions.
- #
- # The <tt>match_block</tt> can have an arity of 1 or 2. The first block
- # argument will be the given value. The second, if the block accepts it
- # will be the matcher itself, giving you access to set custom failure
- # messages in favor of the defaults.
- #
- # The <tt>match_block</tt> should return a boolean: <tt>true</tt>
- # indicates a match, which will pass if you use <tt>should</tt> and fail
- # if you use <tt>should_not</tt>. false (or nil) indicates no match,
- # which will do the reverse: fail if you use <tt>should</tt> and pass if
- # you use <tt>should_not</tt>.
- #
- # An error in the <tt>match_block</tt> will bubble up, resulting in a
- # failure.
- #
- # == Example with default messages
- #
- # def be_even
- # simple_matcher("an even number") { |given| given % 2 == 0 }
- # end
- #
- # describe 2 do
- # it "should be even" do
- # 2.should be_even
- # end
- # end
- #
- # Given an odd number, this example would produce an error message stating:
- # expected "an even number", got 3.
- #
- # Unfortunately, if you're a fan of auto-generated descriptions, this will
- # produce "should an even number." Not the most desirable result. You can
- # control that using custom messages:
- #
- # == Example with custom messages
- #
- # def rhyme_with(expected)
- # 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}"
- # given.rhymes_with? expected
- # end
- # end
- #
- # # OR
- #
- # def rhyme_with(expected)
- # simple_matcher do |given, matcher|
- # 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}"
- # given.rhymes_with? expected
- # end
- # end
- #
- # describe "pecan" do
- # it "should rhyme with 'be gone'" do
- # nut = "pecan"
- # nut.extend Rhymer
- # nut.should rhyme_with("be gone")
- # end
- # end
- #
- # The resulting messages would be:
- # description: rhyme with "be gone"
- # failure_message: expected "pecan" to rhyme with "be gone"
- # negative failure_message: expected "pecan" not to rhyme with "be gone"
- #
- # == Wrapped Expectations
- #
- # Because errors will bubble up, it is possible to wrap other expectations
- # in a SimpleMatcher.
- #
- # def be_even
- # simple_matcher("an even number") { |given| (given % 2).should == 0 }
- # end
- #
- # BE VERY CAREFUL when you do this. Only use wrapped expectations for
- # matchers that will always be used in only the positive
- # (<tt>should</tt>) or negative (<tt>should_not</tt>), but not both.
- # The reason is that is you wrap a <tt>should</tt> and call the wrapper
- # with <tt>should_not</tt>, the correct result (the <tt>should</tt>
- # failing), will fail when you want it to pass.
- #
- def simple_matcher(description=nil, &match_block)
- SimpleMatcher.new(description, &match_block)
- end
-
- end
-end
View
108 lib/micronaut/matchers/throw_symbol.rb
@@ -1,108 +0,0 @@
-module Micronaut
- module Matchers
-
- class ThrowSymbol #:nodoc:
-
- def initialize(expected_symbol = nil, expected_arg=nil)
- @expected_symbol = expected_symbol
- @expected_arg = expected_arg
- @caught_symbol = nil
- end
-
- def matches?(given_proc)
- begin
- if @expected_symbol.nil?
- given_proc.call
- else
- @caught_arg = catch :proc_did_not_throw_anything do
- catch @expected_symbol do
- given_proc.call
- throw :proc_did_not_throw_anything, :nothing_thrown
- end
- end
- @caught_symbol = @expected_symbol unless @caught_arg == :nothing_thrown
- end
-
- # Ruby 1.8 uses NameError with `symbol'
- # Ruby 1.9 uses ArgumentError with :symbol
- rescue NameError, ArgumentError => e
- raise e unless e.message =~ /uncaught throw (`|\:)([a-zA-Z0-9_]*)(')?/
- @caught_symbol = $2.to_sym
- ensure
- if @expected_symbol.nil?
- return !@caught_symbol.nil?
- else
- if @expected_arg.nil?
- return @caught_symbol == @expected_symbol
- else
- # puts [@caught_symbol, @expected_symbol].inspect
- # puts [@caught_arg, @expected_arg].inspect
- return @caught_symbol == @expected_symbol && @caught_arg == @expected_arg
- end
- end
- end
-
- end
-
- def failure_message
- if @caught_symbol
- "expected #{expected}, got #{@caught_symbol.inspect}"
- else
- "expected #{expected} but nothing was thrown"
- end
- end
-
- def negative_failure_message
- if @expected_symbol
- "expected #{expected} not to be thrown"
- else
- "expected no Symbol, got :#{@caught_symbol}"
- end
- end
-
- def description
- "throw #{expected}"
- end
-
- private
-
- def expected
- @expected_symbol.nil? ? "a Symbol" : "#{@expected_symbol.inspect}#{args}"
- end
-
- def args
- @expected_arg.nil? ? "" : " with #{@expected_arg.inspect}"
- end
-
- end
-
- # :call-seq:
- # should throw_symbol()
- # should throw_symbol(:sym)
- # should throw_symbol(:sym, arg)
- # should_not throw_symbol()
- # should_not throw_symbol(:sym)
- # should_not throw_symbol(:sym, arg)
- #
- # Given no argument, matches if a proc throws any Symbol.
- #
- # Given a Symbol, matches if the given proc throws the specified Symbol.
- #
- # Given a Symbol and an arg, matches if the given proc throws the
- # specified Symbol with the specified arg.
- #
- # == Examples
- #
- # lambda { do_something_risky }.should throw_symbol
- # lambda { do_something_risky }.should throw_symbol(:that_was_risky)
- # lambda { do_something_risky }.should throw_symbol(:that_was_risky, culprit)
- #
- # lambda { do_something_risky }.should_not throw_symbol
- # lambda { do_something_risky }.should_not throw_symbol(:that_was_risky)
- # lambda { do_something_risky }.should_not throw_symbol(:that_was_risky, culprit)
- def throw_symbol(sym=nil)
- Matchers::ThrowSymbol.new(sym)
- end
-
- end
-end
View
5 micronaut.gemspec
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.license = "MIT"
s.authors = ["Chad Humphries"]
- s.date = %q{2009-08-10}
+ s.date = %q{2012-05-10}
s.default_executable = %q{micronaut}
s.description = %q{An excellent replacement for the wheel...}
s.email = %q{chad@spicycode.com}
@@ -34,7 +34,10 @@ Gem::Specification.new do |s|
# s.add_development_dependency "fakefs", "0.4.0"
# s.add_development_dependency "syntax", "1.0.0"
+ s.add_dependency "rspec-expectations"
s.add_development_dependency "mocha", "~> 0.10.5"
+ s.add_development_dependency "guard", "~> 1.0.0"
+ s.add_development_dependency "pry"
# s.add_development_dependency "rr", "~> 1.0.4"
# s.add_development_dependency "flexmock", "~> 0.9.0"
end
View
146 spec/lib/micronaut/expectations/extensions/object_spec.rb
@@ -1,146 +0,0 @@
-require 'spec_helper'
-
-describe Object do
-
- describe "#should" do
-
- before do
- @target = "target"
- @matcher = mock("matcher")
- @matcher.stubs(:matches?).returns(true)
- @matcher.stubs(:failure_message)
- end
-
- it "should accept and interact with a matcher" do
- @matcher.expects(:matches?).with(@target).returns(true)
- @target.should @matcher
- end
-
- it "should ask for a failure_message when matches? returns false" do
- @matcher.expects(:matches?).with(@target).returns(false)
- @matcher.expects(:failure_message).returns("the failure message")
- lambda { @target.should @matcher }.should fail_with("the failure message")
- end
-
- it "should raise error if it receives false directly" do
- lambda { @target.should false }.should raise_error(Micronaut::Expectations::InvalidMatcherError)
- end
-
- it "should raise error if it receives false (evaluated)" do
- lambda { @target.should eql?("foo") }.should raise_error(Micronaut::Expectations::InvalidMatcherError)
- end
-
- it "should raise error if it receives true" do
- lambda { @target.should true }.should raise_error(Micronaut::Expectations::InvalidMatcherError)
- end
-
- end
-
- describe "#should_not" do
-
- before do
- @target = "target"
- @matcher = mock("matcher")
- end
-
- it "should accept and interact with a matcher" do
- @matcher.expects(:matches?).with(@target).returns(false)
- @matcher.stubs(:negative_failure_message)
- @target.should_not @matcher
- end
-
- it "should ask for a negative_failure_message when matches? returns true" do
- @matcher.expects(:matches?).with(@target).returns(true)
- @matcher.expects(:negative_failure_message).returns("the negative failure message")
- lambda { @target.should_not @matcher }.should fail_with("the negative failure message")
- end
-
- it "should raise error if it receives false directly" do
- lambda { @target.should_not false }.should raise_error(Micronaut::Expectations::InvalidMatcherError)
- end
-
- it "should raise error if it receives false (evaluated)" do
- lambda { @target.should_not eql?("foo") }.should raise_error(Micronaut::Expectations::InvalidMatcherError)
- end
-
- it "should raise error if it receives true" do
- lambda { @target.should_not true }.should raise_error(Micronaut::Expectations::InvalidMatcherError)
- end
-
- end
-
- module ExampleExpectations
-
- class ArbitraryMatcher
- def initialize(*args, &block)
- if args.last.is_a? Hash
- @expected = args.last[:expected]
- end
- if block_given?
- @expected = block.call
- end
- @block = block
- end
-
- def matches?(target)
- @target = target
- return @expected == target
- end
-
- def with(new_value)
- @expected = new_value
- self
- end
-
- def failure_message
- "expected #{@expected}, got #{@target}"
- end
-
- def negative_failure_message
- "expected not #{@expected}, got #{@target}"
- end
- end
-
- class PositiveOnlyMatcher < ArbitraryMatcher
- undef negative_failure_message rescue nil
- end
-
- def arbitrary_matcher(*args, &block)
- ArbitraryMatcher.new(*args, &block)
- end
-
- def positive_only_matcher(*args, &block)
- PositiveOnlyMatcher.new(*args, &block)
- end
-
- end
-
- describe "should and should not matcher handling" do
- include ExampleExpectations
-
- it "should handle submitted args" do
- 5.should arbitrary_matcher(:expected => 5)
- 5.should arbitrary_matcher(:expected => "wrong").with(5)
- lambda { 5.should arbitrary_matcher(:expected => 4) }.should fail_with("expected 4, got 5")
- lambda { 5.should arbitrary_matcher(:expected => 5).with(4) }.should fail_with("expected 4, got 5")
- 5.should_not arbitrary_matcher(:expected => 4)
- 5.should_not arbitrary_matcher(:expected => 5).with(4)
- lambda { 5.should_not arbitrary_matcher(:expected => 5) }.should fail_with("expected not 5, got 5")
- lambda { 5.should_not arbitrary_matcher(:expected => 4).with(5) }.should fail_with("expected not 5, got 5")
- end
-
- it "should handle the submitted block" do
- 5.should arbitrary_matcher { 5 }
- 5.should arbitrary_matcher(:expected => 4) { 5 }
- 5.should arbitrary_matcher(:expected => 4).with(5) { 3 }
- end
-
- it "should explain when matcher does not support should_not" do
- lambda {
- 5.should_not positive_only_matcher(:expected => 5)
- }.should fail_with(/Matcher does not support should_not.\n/)
- end
-
- end
-
-end
View
17 spec/lib/micronaut/expectations/fail_with_spec.rb
@@ -1,17 +0,0 @@
-require 'spec_helper'
-
-describe Micronaut::Expectations do
-
- describe "#fail_with" do
-
- it "should handle just a message" do
- lambda { Micronaut::Expectations.fail_with "the message" }.should fail_with("the message")
- end
-
- it "should handle an Array" do
- lambda { Micronaut::Expectations.fail_with ["the message","expected","actual"] }.should fail_with("the message")
- end
-
- end
-
-end
View
27 spec/lib/micronaut/expectations/wrap_expectation_spec.rb
@@ -1,27 +0,0 @@
-require 'spec_helper'
-
-describe Micronaut::Matchers, '#wrap_expectation' do
-
- def stub_matcher
- @_stub_matcher ||= simple_matcher do
- end
- end
-
- def failing_matcher
- @_failing_matcher ||= simple_matcher do
- 1.should == 2
- end
- end
-
- it "should return true if there is no error" do
- wrap_expectation stub_matcher do
- end.should be_true
- end
-
- it "should return false if there is an error" do
- wrap_expectation failing_matcher do
- raise "error"
- end.should be_false
- end
-
-end
View
52 spec/lib/micronaut/matchers/be_close_spec.rb
@@ -1,52 +0,0 @@
-require 'spec_helper'
-
-describe Micronaut::Matchers do
-
- describe "be_close" do
-
- it "should match when value == target" do
- be_close(5.0, 0.5).matches?(5.0).should be_true
- end
-
- it "should match when value < (target + delta)" do
- be_close(5.0, 0.5).matches?(5.49).should be_true
- end
-
- it "should match when value > (target - delta)" do
- be_close(5.0, 0.5).matches?(4.51).should be_true
- end
-
- it "should not match when value == (target - delta)" do
- be_close(5.0, 0.5).matches?(4.5).should be_false
- end
-
- it "should not match when value < (target - delta)" do
- be_close(5.0, 0.5).matches?(4.49).should be_false
- end
-
- it "should not match when value == (target + delta)" do
- be_close(5.0, 0.5).matches?(5.5).should be_false
- end
-
- it "should not match when value > (target + delta)" do
- be_close(5.0, 0.5).matches?(5.51).should be_false
- end
-
- it "should provide a useful failure message" do
- #given
- matcher = be_close(5.0, 0.5)
- #when
- matcher.matches?(5.51)
- #then
- matcher.failure_message.should == "expected 5.0 +/- (< 0.5), got 5.51"
- end
-
- it "should describe itself" do
- matcher = be_close(5.0, 0.5)
- matcher.matches?(5.1)
- matcher.description.should == "be close to 5.0 (within +- 0.5)"
- end
-
- end
-
-end
View
298 spec/lib/micronaut/matchers/be_spec.rb
@@ -1,298 +0,0 @@
-require 'spec_helper'
-
-describe Micronaut::Matchers do
-
- describe "should be_predicate" do
-
- it "should pass when actual returns true for :predicate?" do
- actual = stub("actual", :happy? => true)
- actual.should be_happy
- end
-
- it "should pass when actual returns true for :predicates? (present tense)" do
- actual = stub("actual", :exists? => true, :exist? => true)
- actual.should be_exist
- end
-
- it "should fail when actual returns false for :predicate?" do
- actual = stub("actual", :happy? => false)
- lambda do
- actual.should be_happy
- end.should fail_with("expected happy? to return true, got false")
- end
-
- it "should fail when actual does not respond to :predicate?" do
- lambda do
- Object.new.should be_happy
- end.should raise_error(NameError, /happy\?/)
- end
-
- it "should fail on error other than NameError" do
- actual = stub("actual")
- actual.expects(:foo?).raises("aaaah")
- lambda do
- actual.should be_foo
- end.should raise_error(/aaaah/)
- end
-
- it "should fail on error other than NameError (with the present tense predicate)" do
- actual = Object.new
- actual.expects(:foos?).raises("aaaah")
- lambda do
- actual.should be_foo
- end.should raise_error(/aaaah/)
- end
-
- end
-
- describe "should_not be_predicate" do
-
- it "should pass when actual returns false for :sym?" do
- actual = stub("actual", :happy? => false)
- actual.should_not be_happy
- end
-
- it "should fail when actual returns true for :sym?" do
- actual = stub("actual", :happy? => true)
- lambda do
- actual.should_not be_happy
- end.should fail_with("expected happy? to return false, got true")
- end
-
- it "should fail when actual does not respond to :sym?" do
- lambda do
- Object.new.should_not be_happy
- end.should raise_error(NameError)
- end
-
- end
-
- describe "should be_predicate(*args)" do
-
- it "should pass when actual returns true for :predicate?(*args)" do
- actual = mock("actual")
- actual.expects(:older_than?).with(3).returns(true)
- actual.should be_older_than(3)
- end
-
- it "should fail when actual returns false for :predicate?(*args)" do
- actual = mock("actual")
- actual.expects(:older_than?).with(3).returns(false)
- lambda do
- actual.should be_older_than(3)
- end.should fail_with("expected older_than?(3) to return true, got false")
- end
-
- it "should fail when actual does not respond to :predicate?" do
- lambda do
- Object.new.should be_older_than(3)
- end.should raise_error(NameError)
- end
-
- end
-
- describe "should_not be_predicate(*args)" do
-
- it "should pass when actual returns false for :predicate?(*args)" do
- actual = mock("actual")
- actual.expects(:older_than?).with(3).returns(false)
- actual.should_not be_older_than(3)
- end
-
- it "should fail when actual returns true for :predicate?(*args)" do
- actual = mock("actual")
- actual.expects(:older_than?).with(3).returns(true)
- lambda do
- actual.should_not be_older_than(3)
- end.should fail_with("expected older_than?(3) to return false, got true")
- end
-
- it "should fail when actual does not respond to :predicate?" do
- lambda do
- Object.new.should_not be_older_than(3)
- end.should raise_error(NameError)
- end
-
- end
-
- describe "should be_true" do
-
- it "should pass when actual equal(true)" do
- true.should be_true
- end
-
- it "should fail when actual equal(false)" do
- lambda do
- false.should be_true
- end.should fail_with("expected true, got false")
- end
-
- end
-
- describe "should be_false" do
-
- it "should pass when actual equal(false)" do
- false.should be_false
- end
-
- it "should fail when actual equal(true)" do
- lambda do
- true.should be_false
- end.should fail_with("expected false, got true")
- end
-
- end
-
- describe "should be_nil" do
-
- it "should pass when actual is nil" do
- nil.should be_nil
- end
-
- it "should fail when actual is not nil" do
- lambda do
- :not_nil.should be_nil
- end.should fail_with("expected nil? to return true, got false")
- end
-
- end
-
- describe "should_not be_nil" do
-
- it "should pass when actual is not nil" do
- :not_nil.should_not be_nil
- end
-
- it "should fail when actual is nil" do
- lambda do
- nil.should_not be_nil
- end.should fail_with("expected nil? to return false, got true")
- end
-
-