Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Conversion to rspec-expecctations

  • Loading branch information...
commit 1d690dca4f5861dbcce24b1acb57dadb98dbe29d 1 parent 628b29c
Chad Humphries authored

Showing 50 changed files with 31 additions and 4,364 deletions. Show diff stats Hide diff stats

  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
2  Gemfile
... ... @@ -1,3 +1,3 @@
1   -source "http://rubygems.org"
  1 +source "https://rubygems.org"
2 2
3 3 gemspec
20 Gemfile.lock
@@ -2,17 +2,35 @@ PATH
2 2 remote: .
3 3 specs:
4 4 micronaut (0.4.0)
  5 + rspec-expectations
5 6
6 7 GEM
7   - remote: http://rubygems.org/
  8 + remote: https://rubygems.org/
8 9 specs:
  10 + coderay (1.0.6)
  11 + diff-lcs (1.1.3)
  12 + ffi (1.0.11)
  13 + guard (1.0.3)
  14 + ffi (>= 0.5.0)
  15 + thor (>= 0.14.6)
9 16 metaclass (0.0.1)
  17 + method_source (0.7.1)
10 18 mocha (0.10.5)
11 19 metaclass (~> 0.0.1)
  20 + pry (0.9.9.6)
  21 + coderay (~> 1.0.5)
  22 + method_source (~> 0.7.1)
  23 + slop (>= 2.4.4, < 3)
  24 + rspec-expectations (2.10.0)
  25 + diff-lcs (~> 1.1.3)
  26 + slop (2.4.4)
  27 + thor (0.15.2)
12 28
13 29 PLATFORMS
14 30 ruby
15 31
16 32 DEPENDENCIES
  33 + guard (~> 1.0.0)
17 34 micronaut!
18 35 mocha (~> 0.10.5)
  36 + pry
16 lib/micronaut.rb
... ... @@ -1,5 +1,4 @@
1 1 require 'micronaut/mocking/with_absolutely_nothing'
2   -require 'micronaut/matchers'
3 2 require 'micronaut/expectations'
4 3 require 'micronaut/world'
5 4 require 'micronaut/configuration'
@@ -10,20 +9,7 @@
10 9 require 'micronaut/formatters'
11 10
12 11 module Micronaut
13   - file = if RUBY_VERSION =~ /^1\.9/ then # bt's expanded, but __FILE__ isn't :(
14   - File.expand_path __FILE__
15   - elsif __FILE__ =~ /^[^\.]/ then # assume both relative
16   - require 'pathname'
17   - pwd = Pathname.new(Dir.pwd)
18   - path_name = Pathname.new(File.expand_path(__FILE__))
19   - path_name = File.join(".", path_name.relative_path_from(pwd)) unless path_name.relative?
20   - path_name.to_s
21   - else # assume both are expanded
22   - __FILE__
23   - end
24   -
25   - # './lib' in project dir, or '/usr/local/blahblah' if installed
26   - InstallDirectory = File.expand_path(File.dirname(File.dirname(file)) + "/lib")
  12 + InstallDirectory = File.expand_path(File.dirname(File.dirname(File.expand_path(__FILE__))) + "/lib")
27 13
28 14 def self.configuration
29 15 @configuration ||= Micronaut::Configuration.new
2  lib/micronaut/behaviour.rb
... ... @@ -1,6 +1,6 @@
1 1 module Micronaut
2 2 class Behaviour
3   - include Micronaut::Matchers
  3 + include ::RSpec::Matchers
4 4
5 5 attr_accessor :running_example, :reporter
6 6
46 lib/micronaut/expectations.rb
... ... @@ -1,45 +1 @@
1   -require 'micronaut/matchers'
2   -require 'micronaut/expectations/extensions/object'
3   -require 'micronaut/expectations/handler'
4   -require 'micronaut/expectations/wrap_expectation'
5   -
6   -module Micronaut
7   -
8   - # Micronaut::Expectations lets you set expectations on your objects.
9   - #
10   - # result.should == 37
11   - # team.should have(11).players_on_the_field
12   - #
13   - # == How Expectations work.
14   - #
15   - # Micronaut::Expectations adds two methods to Object:
16   - #
17   - # should(matcher=nil)
18   - # should_not(matcher=nil)
19   - #
20   - # Both methods take an optional Expression Matcher (See Micronaut::Matchers).
21   - #
22   - # When +should+ receives an Expression Matcher, it calls <tt>matches?(self)</tt>. If
23   - # it returns +true+, the spec passes and execution continues. If it returns
24   - # +false+, then the spec fails with the message returned by <tt>matcher.failure_message</tt>.
25   - #
26   - # Similarly, when +should_not+ receives a matcher, it calls <tt>matches?(self)</tt>. If
27   - # it returns +false+, the spec passes and execution continues. If it returns
28   - # +true+, then the spec fails with the message returned by <tt>matcher.negative_failure_message</tt>.
29   - #
30   - # Micronaut ships with a standard set of useful matchers, and writing your own
31   - # matchers is quite simple. See Micronaut::Matchers for details.
32   - module Expectations
33   -
34   - class ExpectationNotMetError < ::StandardError; end
35   -
36   - def self.fail_with(message, expected=nil, target=nil) # :nodoc:
37   - if Array === message && message.length == 3
38   - message, expected, target = message[0], message[1], message[2]
39   - end
40   - Kernel::raise(Micronaut::Expectations::ExpectationNotMetError.new(message))
41   - end
42   -
43   - end
44   -
45   -end
  1 +require 'rspec/expectations'
92 lib/micronaut/expectations/extensions/object.rb
... ... @@ -1,92 +0,0 @@
1   -module Micronaut
2   - module Expectations
3   - class InvalidMatcherError < ArgumentError; end
4   -
5   - module ObjectExpectations
6   - # :call-seq:
7   - # should(matcher)
8   - # should == expected
9   - # should === expected
10   - # should =~ expected
11   - #
12   - # receiver.should(matcher)
13   - # => Passes if matcher.matches?(receiver)
14   - #
15   - # receiver.should == expected #any value
16   - # => Passes if (receiver == expected)
17   - #
18   - # receiver.should === expected #any value
19   - # => Passes if (receiver === expected)
20   - #
21   - # receiver.should =~ regexp
22   - # => Passes if (receiver =~ regexp)
23   - #
24   - # See Micronaut::Matchers for more information about matchers
25   - #
26   - # == Warning
27   - # NOTE that this does NOT support receiver.should != expected.
28   - # Instead, use receiver.should_not == expected
29   - def should(matcher=nil, &block)
30   - ::Micronaut::Matchers.last_should = "should"
31   - return ::Micronaut::Matchers::PositiveOperatorMatcher.new(self) if matcher.nil?
32   -
33   - unless matcher.respond_to?(:matches?)
34   - raise InvalidMatcherError, "Expected a matcher, got #{matcher.inspect}."
35   - end
36   -
37   - match_found = matcher.matches?(self, &block)
38   - ::Micronaut::Matchers.last_matcher = matcher
39   -
40   - ::Micronaut::Expectations.fail_with(matcher.failure_message) unless match_found
41   - match_found
42   - end
43   -
44   - # :call-seq:
45   - # should_not(matcher)
46   - # should_not == expected
47   - # should_not === expected
48   - # should_not =~ expected
49   - #
50   - # receiver.should_not(matcher)
51   - # => Passes unless matcher.matches?(receiver)
52   - #
53   - # receiver.should_not == expected
54   - # => Passes unless (receiver == expected)
55   - #
56   - # receiver.should_not === expected
57   - # => Passes unless (receiver === expected)
58   - #
59   - # receiver.should_not =~ regexp
60   - # => Passes unless (receiver =~ regexp)
61   - #
62   - # See Micronaut::Matchers for more information about matchers
63   - def should_not(matcher=nil, &block)
64   - ::Micronaut::Matchers.last_should = "should not"
65   - return ::Micronaut::Matchers::NegativeOperatorMatcher.new(self) if matcher.nil?
66   -
67   - unless matcher.respond_to?(:matches?)
68   - raise InvalidMatcherError, "Expected a matcher, got #{matcher.inspect}."
69   - end
70   -
71   - unless matcher.respond_to?(:negative_failure_message)
72   - ::Micronaut::Expectations.fail_with(
73   - <<-EOF
74   - Matcher does not support should_not.
75   - See Micronaut::Matchers for more information about matchers.
76   - EOF
77   - )
78   - end
79   - match_found = matcher.matches?(self, &block)
80   - ::Micronaut::Matchers.last_matcher = matcher
81   -
82   - ::Micronaut::Expectations.fail_with(matcher.negative_failure_message) if match_found
83   - match_found
84   - end
85   -
86   - end
87   - end
88   -end
89   -
90   -class Object
91   - include Micronaut::Expectations::ObjectExpectations
92   -end
51 lib/micronaut/expectations/handler.rb
... ... @@ -1,51 +0,0 @@
1   -module Micronaut
2   - module Expectations
3   -
4   -
5   - # class ExpectationMatcherHandler
6   - #
7   - # def self.handle_matcher(actual, matcher, &block)
8   - # ::Micronaut::Matchers.last_should = "should"
9   - # return Micronaut::Matchers::PositiveOperatorMatcher.new(actual) if matcher.nil?
10   - #
11   - # unless matcher.respond_to?(:matches?)
12   - # raise InvalidMatcherError, "Expected a matcher, got #{matcher.inspect}."
13   - # end
14   - #
15   - # match = matcher.matches?(actual, &block)
16   - # ::Micronaut::Matchers.last_matcher = matcher
17   - # Micronaut::Expectations.fail_with(matcher.failure_message) unless match
18   - # match
19   - # end
20   - #
21   - # end
22   -
23   - # class NegativeExpectationMatcherHandler
24   - #
25   - # def self.handle_matcher(actual, matcher, &block)
26   - # ::Micronaut::Matchers.last_should = "should not"
27   - # return Micronaut::Matchers::NegativeOperatorMatcher.new(actual) if matcher.nil?
28   - #
29   - # unless matcher.respond_to?(:matches?)
30   - # raise InvalidMatcherError, "Expected a matcher, got #{matcher.inspect}."
31   - # end
32   - #
33   - # unless matcher.respond_to?(:negative_failure_message)
34   - # Micronaut::Expectations.fail_with(
35   - # <<-EOF
36   - # Matcher does not support should_not.
37   - # See Micronaut::Matchers for more information
38   - # about matchers.
39   - # EOF
40   - # )
41   - # end
42   - # match = matcher.matches?(actual, &block)
43   - # ::Micronaut::Matchers.last_matcher = matcher
44   - # Micronaut::Expectations.fail_with(matcher.negative_failure_message) if match
45   - # match
46   - # end
47   -
48   - # end
49   -
50   - end
51   -end
52 lib/micronaut/expectations/wrap_expectation.rb
... ... @@ -1,52 +0,0 @@
1   -module Micronaut
2   - module Matchers
3   -
4   - # wraps an expectation in a block that will return true if the
5   - # expectation passes and false if it fails (without bubbling up
6   - # the failure).
7   - #
8   - # This is intended to be used in the context of a simple matcher,
9   - # and is especially useful for wrapping multiple expectations or
10   - # one or more assertions from test/unit extensions when running
11   - # with test/unit.
12   - #
13   - # == Examples
14   - #
15   - # def eat_cheese(cheese)
16   - # simple_matcher do |mouse, matcher|
17   - # matcher.negative_failure_message = "expected #{mouse} not to eat cheese"
18   - # wrap_expectation do |matcher|
19   - # assert_eats_cheese(mouse)
20   - # end
21   - # end
22   - # end
23   - #
24   - # describe Mouse do
25   - # it "eats cheese" do
26   - # Mouse.new.should eat_cheese
27   - # end
28   - # end
29   - #
30   - # The other benefit you get is that you can use the negative version
31   - # of the matcher:
32   - #
33   - # describe Cat do
34   - # it "does not eat cheese" do
35   - # Cat.new.should_not eat_cheese
36   - # end
37   - # end
38   - #
39   - # So in the event there is no assert_does_not_eat_cheese available,
40   - # you're all set!
41   - def wrap_expectation(matcher, &block)
42   - begin
43   - block.call(matcher)
44   - return true
45   - rescue Exception => e
46   - matcher.failure_message = e.message
47   - return false
48   - end
49   - end
50   -
51   - end
52   -end
2  lib/micronaut/formatters/base_text_formatter.rb
@@ -27,7 +27,7 @@ def dump_failures
27 27 end
28 28
29 29 def colorise(s, failure)
30   - if failure.is_a?(Micronaut::Expectations::ExpectationNotMetError)
  30 + if failure.is_a?(RSpec::Expectations::ExpectationNotMetError)
31 31 red(s)
32 32 else
33 33 magenta(s)
2  lib/micronaut/formatters/documentation_formatter.rb
@@ -45,7 +45,7 @@ def example_finished(example)
45 45 end
46 46
47 47 def failure_output(example, exception)
48   - expectation_not_met = exception.is_a?(::Micronaut::Expectations::ExpectationNotMetError)
  48 + expectation_not_met = exception.is_a?(::RSpec::Expectations::ExpectationNotMetError)
49 49
50 50 message = if expectation_not_met
51 51 "#{current_indentation}#{example.description} (FAILED)"
141 lib/micronaut/matchers.rb
... ... @@ -1,141 +0,0 @@
1   -require 'micronaut/matchers/generated_descriptions'
2   -require 'micronaut/matchers/simple_matcher'
3   -require 'micronaut/matchers/be'
4   -require 'micronaut/matchers/be_close'
5   -require 'micronaut/matchers/change'
6   -require 'micronaut/matchers/eql'
7   -require 'micronaut/matchers/equal'
8   -require 'micronaut/matchers/has'
9   -require 'micronaut/matchers/have'
10   -require 'micronaut/matchers/include'
11   -require 'micronaut/matchers/match'
12   -require 'micronaut/matchers/raise_error'
13   -require 'micronaut/matchers/respond_to'
14   -require 'micronaut/matchers/satisfy'
15   -require 'micronaut/matchers/throw_symbol'
16   -require 'micronaut/matchers/operator_matcher'
17   -
18   -module Micronaut
19   -
20   - # We ship (courtesy of RSpec and Micronaut) with a number of useful Expression Matchers. An Expression Matcher
21   - # is any object that responds to the following methods:
22   - #
23   - # matches?(actual)
24   - # failure_message
25   - # negative_failure_message #optional
26   - # description #optional
27   - #
28   - # See Micronaut::Expectations to learn how to use these as Expectation Matchers.
29   - # See Micronaut::Mocks to learn how to use them as Mock Argument Constraints.
30   - #
31   - # == Predicates
32   - #
33   - # In addition to those Expression Matchers that are defined explicitly, we will
34   - # create custom Matchers on the fly for any arbitrary predicate, giving your specs
35   - # a much more natural language feel.
36   - #
37   - # A Ruby predicate is a method that ends with a "?" and returns true or false.
38   - # Common examples are +empty?+, +nil?+, and +instance_of?+.
39   - #
40   - # All you need to do is write +should be_+ followed by the predicate without
41   - # the question mark, and we will figure it out from there. For example:
42   - #
43   - # [].should be_empty => [].empty? #passes
44   - # [].should_not be_empty => [].empty? #fails
45   - #
46   - # In addtion to prefixing the predicate matchers with "be_", you can also use "be_a_"
47   - # and "be_an_", making your specs read much more naturally:
48   - #
49   - # "a string".should be_an_instance_of(String) =>"a string".instance_of?(String) #passes
50   - #
51   - # 3.should be_a_kind_of(Fixnum) => 3.kind_of?(Numeric) #passes
52   - # 3.should be_a_kind_of(Numeric) => 3.kind_of?(Numeric) #passes
53   - # 3.should be_an_instance_of(Fixnum) => 3.instance_of?(Fixnum) #passes
54   - # 3.should_not be_instance_of(Numeric) => 3.instance_of?(Numeric) #fails
55   - #
56   - # We will also create custom matchers for predicates like +has_key?+. To
57   - # use this feature, just state that the object should have_key(:key) and we will
58   - # call has_key?(:key) on the target. For example:
59   - #
60   - # {:a => "A"}.should have_key(:a) => {:a => "A"}.has_key?(:a) #passes
61   - # {:a => "A"}.should have_key(:b) => {:a => "A"}.has_key?(:b) #fails
62   - #
63   - # You can use this feature to invoke any predicate that begins with "has_", whether it is
64   - # part of the Ruby libraries (like +Hash#has_key?+) or a method you wrote on your own class.
65   - #
66   - # == Custom Expectation Matchers
67   - #
68   - # When you find that none of the stock Expectation Matchers provide a natural
69   - # feeling expectation, you can very easily write your own.
70   - #
71   - # For example, imagine that you are writing a game in which players can
72   - # be in various zones on a virtual board. To specify that bob should
73   - # be in zone 4, you could say:
74   - #
75   - # bob.current_zone.should eql(Zone.new("4"))
76   - #
77   - # But you might find it more expressive to say:
78   - #
79   - # bob.should be_in_zone("4")
80   - #
81   - # and/or
82   - #
83   - # bob.should_not be_in_zone("3")
84   - #
85   - # To do this, you would need to write a class like this:
86   - #
87   - # class BeInZone
88   - # def initialize(expected)
89   - # @expected = expected
90   - # end
91   - # def matches?(target)
92   - # @target = target
93   - # @target.current_zone.eql?(Zone.new(@expected))
94   - # end
95   - # def failure_message
96   - # "expected #{@target.inspect} to be in Zone #{@expected}"
97   - # end
98   - # def negative_failure_message
99   - # "expected #{@target.inspect} not to be in Zone #{@expected}"
100   - # end
101   - # end
102   - #
103   - # ... and a method like this:
104   - #
105   - # def be_in_zone(expected)
106   - # BeInZone.new(expected)
107   - # end
108   - #
109   - # And then expose the method to your specs. This is normally done
110   - # by including the method and the class in a module, which is then
111   - # included in your spec:
112   - #
113   - # module CustomGameMatchers
114   - # class BeInZone
115   - # ...
116   - # end
117   - #
118   - # def be_in_zone(expected)
119   - # ...
120   - # end
121   - # end
122   - #
123   - # describe "Player behaviour" do
124   - # include CustomGameMatchers
125   - # ...
126   - # end
127   -
128   - module Matchers
129   -
130   - class MatcherError < StandardError; end
131   -
132   - private
133   - def method_missing(sym, *args, &block) # :nodoc:
134   - return Matchers::Be.new(sym, *args) if sym.to_s =~ /^be_/
135   - return has(sym, *args) if sym.to_s =~ /^have_/
136   - super
137   - end
138   -
139   - end
140   -
141   -end
204 lib/micronaut/matchers/be.rb
... ... @@ -1,204 +0,0 @@
1   -module Micronaut
2   - module Matchers
3   -
4   - class Be #:nodoc:
5   - def initialize(*args)
6   - @expected = args.empty? ? true : set_expected(args.shift)
7   - @args = args
8   - end
9   -
10   - def matches?(actual)
11   - @actual = actual
12   - handling_predicate? ? run_predicate_on(actual) : match_or_compare(actual)
13   - end
14   -
15   - def run_predicate_on(actual)
16   - begin
17   - return @result = actual.__send__(predicate, *@args)
18   - rescue NameError => predicate_missing_error
19   - "this needs to be here or rcov will not count this branch even though it's executed in a code example"
20   - end
21   -
22   - begin
23   - return @result = actual.__send__(present_tense_predicate, *@args)
24   - rescue NameError
25   - raise predicate_missing_error
26   - end
27   - end
28   -
29   - def failure_message
30   - handling_predicate? ?
31   - "expected #{predicate}#{args_to_s} to return true, got #{@result.inspect}" :
32   - "expected #{@comparison_method} #{expected}, got #{@actual.inspect}".gsub(' ',' ')
33   - end
34   -
35   - def negative_failure_message
36   - if handling_predicate?
37   - "expected #{predicate}#{args_to_s} to return false, got #{@result.inspect}"
38   - else
39   - message = <<-MESSAGE
40   -'should_not be #{@comparison_method} #{expected}' not only FAILED,
41   -it reads really poorly.
42   - MESSAGE
43   -
44   - raise message << ([:===,:==].include?(@comparison_method) ?
45   - "Why don't you try expressing it without the \"be\"?" :
46   - "Why don't you try expressing it in the positive?")
47   - end
48   - end
49   -
50   - def description
51   - "#{prefix_to_sentence}#{comparison} #{expected_to_sentence}#{args_to_sentence}".gsub(/\s+/,' ')
52   - end
53   -
54   - [:==, :<, :<=, :>=, :>, :===].each do |method|
55   - define_method method do |expected|
56   - compare_to(expected, :using => method)
57   - self
58   - end
59   - end
60   -
61   - private
62   - def match_or_compare(actual)
63   - case @expected
64   - when TrueClass
65   - @actual
66   - else
67   - @actual.__send__(comparison_method, @expected)
68   - end
69   - end
70   -
71   - def comparison_method
72   - @comparison_method || :equal?
73   - end
74   -
75   - def expected
76   - @expected
77   - end
78   -
79   - def compare_to(expected, opts)
80   - @expected, @comparison_method = expected, opts[:using]
81   - end
82   -
83   - def set_expected(expected)
84   - Symbol === expected ? parse_expected(expected) : expected
85   - end
86   -
87   - def parse_expected(expected)
88   - ["be_an_","be_a_","be_"].each do |prefix|
89   - handling_predicate!
90   - if expected.to_s =~ /^#{prefix}/
91   - set_prefix(prefix)
92   - expected = expected.to_s.sub(prefix,"")
93   - [true, false, nil].each do |val|
94   - return val if val.to_s == expected
95   - end
96   - return expected.to_sym
97   - end
98   - end
99   - end
100   -
101   - def set_prefix(prefix)
102   - @prefix = prefix
103   - end
104   -
105   - def prefix
106   - @prefix
107   - end
108   -
109   - def handling_predicate!
110   - @handling_predicate = true
111   - end
112   -
113   - def handling_predicate?
114   - return false if [true, false, nil].include?(expected)
115   - return @handling_predicate
116   - end
117   -
118   - def predicate
119   - "#{@expected.to_s}?".to_sym
120   - end
121   -
122   - def present_tense_predicate
123   - "#{@expected.to_s}s?".to_sym
124   - end
125   -
126   - def args_to_s
127   - @args.empty? ? "" : parenthesize(inspected_args.join(', '))
128   - end
129   -
130   - def parenthesize(string)
131   - return "(#{string})"
132   - end
133   -
134   - def inspected_args
135   - @args.map { |a| a.inspect }
136   - end
137   -
138   - def comparison
139   - @comparison_method.nil? ? " " : "be #{@comparison_method.to_s} "
140   - end
141   -
142   - def expected_to_sentence
143   - split_words(expected)
144   - end
145   -
146   - def prefix_to_sentence
147   - split_words(prefix)
148   - end
149   -
150   - def split_words(sym)
151   - sym.to_s.gsub(/_/,' ')
152   - end
153   -
154   - def args_to_sentence
155   - case @args.length
156   - when 0
157   - ""
158   - when 1
159   - " #{@args[0]}"
160   - else
161   - " #{@args[0...-1].join(', ')} and #{@args[-1]}"
162   - end
163   - end
164   -
165   - end
166   -
167   - # :call-seq:
168   - # should be_true
169   - # should be_false
170   - # should be_nil
171   - # should be_arbitrary_predicate(*args)
172   - # should_not be_nil
173   - # should_not be_arbitrary_predicate(*args)
174   - #
175   - # Given true, false, or nil, will pass if actual value is
176   - # true, false or nil (respectively). Given no args means
177   - # the caller should satisfy an if condition (to be or not to be).
178   - #
179   - # Predicates are any Ruby method that ends in a "?" and returns true or false.
180   - # Given be_ followed by arbitrary_predicate (without the "?"), we will match
181   - # convert that into a query against the target object.
182   - #
183   - # The arbitrary_predicate feature will handle any predicate
184   - # prefixed with "be_an_" (e.g. be_an_instance_of), "be_a_" (e.g. be_a_kind_of)
185   - # or "be_" (e.g. be_empty), letting you choose the prefix that best suits the predicate.
186   - #
187   - # == Examples
188   - #
189   - # target.should be_true
190   - # target.should be_false
191   - # target.should be_nil
192   - # target.should_not be_nil
193   - #
194   - # collection.should be_empty #passes if target.empty?
195   - # "this string".should be_an_intance_of(String)
196   - #
197   - # target.should_not be_empty #passes unless target.empty?
198   - # target.should_not be_old_enough(16) #passes unless target.old_enough?(16)
199   - def be(*args)
200   - Matchers::Be.new(*args)
201   - end
202   -
203   - end
204   -end
37 lib/micronaut/matchers/be_close.rb
... ... @@ -1,37 +0,0 @@
1   -module Micronaut
2   - module Matchers
3   -
4   - class BeClose
5   - def initialize(expected, delta)
6   - @expected, @delta = expected, delta
7   - end
8   -
9   - def matches?(actual)
10   - @actual = actual
11   - (@actual - @expected).abs < @delta
12   - end
13   -
14   - def failure_message
15   - "expected #{@expected} +/- (< #{@delta}), got #{@actual}"
16   - end
17   -
18   - def description
19   - "be close to #{@expected} (within +- #{@delta})"
20   - end
21   - end
22   -
23   - # :call-seq:
24   - # should be_close(expected, delta)
25   - # should_not be_close(expected, delta)
26   - #
27   - # Passes if actual == expected +/- delta
28   - #
29   - # == Example
30   - #
31   - # result.should be_close(3.0, 0.5)
32   - def be_close(expected, delta)
33   - BeClose.new(expected, delta)
34   - end
35   -
36   - end
37   -end
148 lib/micronaut/matchers/change.rb
... ... @@ -1,148 +0,0 @@
1   -module Micronaut
2   - module Matchers
3   -
4   - #Based on patch from Wilson Bilkovich
5   - class Change #:nodoc:
6   - def initialize(receiver=nil, message=nil, &block)
7   - @message = message || "result"
8   - @value_proc = block || lambda {
9   - receiver.__send__(message)
10   - }
11   - end
12   -
13   - def matches?(event_proc)
14   - raise_block_syntax_error if block_given?
15   -
16   - @before = evaluate_value_proc
17   - event_proc.call
18   - @after = evaluate_value_proc
19   -
20   - return false if @from unless @from == @before
21   - return false if @to unless @to == @after
22   - return (@before + @amount == @after) if @amount
23   - return ((@after - @before) >= @minimum) if @minimum
24   - return ((@after - @before) <= @maximum) if @maximum
25   - @before != @after
26   - end
27   -
28   - def raise_block_syntax_error
29   - raise MatcherError.new(<<-MESSAGE
30   -block passed to should or should_not change must use {} instead of do/end
31   -MESSAGE
32   - )
33   - end
34   -
35   - def evaluate_value_proc
36   - @value_proc.call
37   - end
38   -
39   - def failure_message
40   - if @to
41   - "#{@message} should have been changed to #{@to.inspect}, but is now #{@after.inspect}"
42   - elsif @from
43   - "#{@message} should have initially been #{@from.inspect}, but was #{@before.inspect}"
44   - elsif @amount
45   - "#{@message} should have been changed by #{@amount.inspect}, but was changed by #{actual_delta.inspect}"
46   - elsif @minimum
47   - "#{@message} should have been changed by at least #{@minimum.inspect}, but was changed by #{actual_delta.inspect}"
48   - elsif @maximum
49   - "#{@message} should have been changed by at most #{@maximum.inspect}, but was changed by #{actual_delta.inspect}"
50   - else
51   - "#{@message} should have changed, but is still #{@before.inspect}"
52   - end
53   - end
54   -
55   - def actual_delta
56   - @after - @before
57   - end
58   -
59   - def negative_failure_message
60   - "#{@message} should not have changed, but did change from #{@before.inspect} to #{@after.inspect}"
61   - end
62   -
63   - def by(amount)
64   - @amount = amount
65   - self
66   - end
67   -
68   - def by_at_least(minimum)
69   - @minimum = minimum
70   - self
71   - end
72   -
73   - def by_at_most(maximum)
74   - @maximum = maximum
75   - self
76   - end
77   -
78   - def to(to)
79   - @to = to
80   - self
81   - end
82   -
83   - def from (from)
84   - @from = from
85   - self
86   - end
87   - end
88   -
89   - # :call-seq:
90   - # should change(receiver, message, &block)
91   - # should change(receiver, message, &block).by(value)
92   - # should change(receiver, message, &block).from(old).to(new)
93   - # should_not change(receiver, message, &block)
94   - #
95   - # Allows you to specify that a Proc will cause some value to change.
96   - #
97   - # == Examples
98   - #
99   - # lambda {
100   - # team.add_player(player)
101   - # }.should change(roster, :count)
102   - #
103   - # lambda {
104   - # team.add_player(player)
105   - # }.should change(roster, :count).by(1)
106   - #
107   - # lambda {
108   - # team.add_player(player)
109   - # }.should change(roster, :count).by_at_least(1)
110   - #
111   - # lambda {
112   - # team.add_player(player)
113   - # }.should change(roster, :count).by_at_most(1)
114   - #
115   - # string = "string"
116   - # lambda {
117   - # string.reverse!
118   - # }.should change { string }.from("string").to("gnirts")
119   - #
120   - # lambda {
121   - # person.happy_birthday
122   - # }.should change(person, :birthday).from(32).to(33)
123   - #
124   - # lambda {
125   - # employee.develop_great_new_social_networking_app
126   - # }.should change(employee, :title).from("Mail Clerk").to("CEO")
127   - #
128   - # Evaluates <tt>receiver.message</tt> or <tt>block</tt> before and after
129   - # it evaluates the c object (generated by the lambdas in the examples
130   - # above).
131   - #
132   - # Then compares the values before and after the <tt>receiver.message</tt>
133   - # and evaluates the difference compared to the expected difference.
134   - #
135   - # == WARNING
136   - # <tt>should_not change</tt> only supports the form with no
137   - # subsequent calls to <tt>by</tt>, <tt>by_at_least</tt>,
138   - # <tt>by_at_most</tt>, <tt>to</tt> or <tt>from</tt>.
139   - #
140   - # blocks passed to <tt>should</tt> <tt>change</tt> and <tt>should_not</tt>
141   - # <tt>change</tt> must use the <tt>{}</tt> form (<tt>do/end</tt> is not
142   - # supported).
143   - #
144   - def change(receiver=nil, message=nil, &block)
145   - Matchers::Change.new(receiver, message, &block)
146   - end
147   - end
148   -end
26 lib/micronaut/matchers/eql.rb
... ... @@ -1,26 +0,0 @@
1   -module Micronaut
2   - module Matchers
3   -
4   - # :call-seq:
5   - # should eql(expected)
6   - # should_not eql(expected)
7   - #
8   - # Passes if actual and expected are of equal value, but not necessarily the same object.
9   - #
10   - # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
11   - #
12   - # == Examples
13   - #
14   - # 5.should eql(5)
15   - # 5.should_not eql(3)
16   - def eql(expected)
17   - simple_matcher do |actual, matcher|
18   - matcher.failure_message = "expected #{expected.inspect}, got #{actual.inspect} (using .eql?)", expected, actual
19   - matcher.negative_failure_message = "expected #{actual.inspect} not to equal #{expected.inspect} (using .eql?)", expected, actual
20   - matcher.description = "eql #{expected.inspect}"
21   - actual.eql?(expected)
22   - end
23   - end
24   -
25   - end
26   -end
26 lib/micronaut/matchers/equal.rb
... ... @@ -1,26 +0,0 @@
1   -module Micronaut
2   - module Matchers
3   -
4   - # :call-seq:
5   - # should equal(expected)
6   - # should_not equal(expected)
7   - #
8   - # Passes if given and expected are the same object (object identity).
9   - #
10   - # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
11   - #
12   - # == Examples
13   - #
14   - # 5.should equal(5) #Fixnums are equal
15   - # "5".should_not equal("5") #Strings that look the same are not the same object
16   - def equal(expected)
17   - simple_matcher do |actual, matcher|
18   - matcher.failure_message = "expected #{expected.inspect}, got #{actual.inspect} (using .equal?)", expected, actual
19   - matcher.negative_failure_message = "expected #{actual.inspect} not to equal #{expected.inspect} (using .equal?)", expected, actual
20   - matcher.description = "equal #{expected.inspect}"
21   - actual.equal?(expected)
22   - end
23   - end
24   -
25   - end
26   -end
36 lib/micronaut/matchers/generated_descriptions.rb
... ... @@ -1,36 +0,0 @@
1   -module Micronaut
2   - module Matchers
3   - class << self
4   - attr_accessor :last_matcher, :last_should
5   -
6   - def clear_generated_description
7   - self.last_matcher = nil
8   - self.last_should = nil
9   - end
10   -
11   - def generated_description
12   - return nil if last_should.nil?
13   - "#{last_should} #{last_description}"
14   - end
15   -
16   - private
17   -
18   - def last_description
19   - last_matcher.respond_to?(:description) ? last_matcher.description : <<-MESSAGE
20   -When you call a matcher in an example without a String, like this:
21   -
22   - specify { object.should matcher }
23   -
24   -or this:
25   -
26   - it { should matcher }
27   -
28   -the runner expects the matcher to have a #describe method. You should either
29   -add a String to the example this matcher is being used in, or give it a
30   -description method. Then you won't have to suffer this lengthy warning again.
31   -MESSAGE
32   - end
33   - end
34   - end
35   -end
36   -
19 lib/micronaut/matchers/has.rb
... ... @@ -1,19 +0,0 @@
1   -module Micronaut
2   - module Matchers
3   -
4   - def has(sym, *args) # :nodoc:
5   - simple_matcher do |actual, matcher|
6   - matcher.failure_message = "expected ##{predicate(sym)}(#{args[0].inspect}) to return true, got false"
7   - matcher.negative_failure_message = "expected ##{predicate(sym)}(#{args[0].inspect}) to return false, got true"
8   - matcher.description = "have key #{args[0].inspect}"
9   - actual.__send__(predicate(sym), *args)
10   - end
11   - end
12   -
13   - private
14   - def predicate(sym)
15   - "#{sym.to_s.sub("have_","has_")}?".to_sym
16   - end
17   -
18   - end
19   -end
153 lib/micronaut/matchers/have.rb
... ... @@ -1,153 +0,0 @@
1   -module Micronaut
2   - module Matchers
3   -
4   - class Have #:nodoc:
5   -
6   - def initialize(expected, relativity=:exactly)
7   - @expected = (expected == :no ? 0 : expected)
8   - @relativity = relativity
9   - end
10   -
11   - def relativities
12   - @relativities ||= {
13   - :exactly => "",
14   - :at_least => "at least ",
15   - :at_most => "at most "
16   - }
17   - end
18   -
19   - def matches?(collection_owner)
20   - if collection_owner.respond_to?(@collection_name)
21   - collection = collection_owner.__send__(@collection_name, *@args, &@block)
22   - elsif (@plural_collection_name && collection_owner.respond_to?(@plural_collection_name))
23   - collection = collection_owner.__send__(@plural_collection_name, *@args, &@block)
24   - elsif (collection_owner.respond_to?(:length) || collection_owner.respond_to?(:size))
25   - collection = collection_owner
26   - else
27   - collection_owner.__send__(@collection_name, *@args, &@block)
28   - end
29   - @given = collection.size if collection.respond_to?(:size)
30   - @given = collection.length if collection.respond_to?(:length)
31   - raise not_a_collection if @given.nil?
32   - return @given >= @expected if @relativity == :at_least
33   - return @given <= @expected if @relativity == :at_most
34   - @given == @expected
35   - end
36   -
37   - def not_a_collection
38   - "expected #{@collection_name} to be a collection but it does not respond to #length or #size"
39   - end
40   -
41   - def failure_message
42   - "expected #{relative_expectation} #{@collection_name}, got #{@given}"
43   - end
44   -
45   - def negative_failure_message
46   - if @relativity == :exactly
47   - return "expected target not to have #{@expected} #{@collection_name}, got #{@given}"
48   - elsif @relativity == :at_most
49   - return <<-EOF
50   -Isn't life confusing enough?
51   -Instead of having to figure out the meaning of this:
52   - should_not have_at_most(#{@expected}).#{@collection_name}
53   -We recommend that you use this instead:
54   - should have_at_least(#{@expected + 1}).#{@collection_name}
55   -EOF
56   - elsif @relativity == :at_least
57   - return <<-EOF
58   -Isn't life confusing enough?
59   -Instead of having to figure out the meaning of this:
60   - should_not have_at_least(#{@expected}).#{@collection_name}
61   -We recommend that you use this instead:
62   - should have_at_most(#{@expected - 1}).#{@collection_name}
63   -EOF
64   - end
65   - end
66   -
67   - def description
68   - "have #{relative_expectation} #{@collection_name}"
69   - end
70   -
71   - def respond_to?(sym)
72   - @expected.respond_to?(sym) || super
73   - end
74   -
75   - private
76   -
77   - def method_missing(sym, *args, &block)
78   - @collection_name = sym
79   - if inflector = (defined?(ActiveSupport::Inflector) ? ActiveSupport::Inflector : (defined?(Inflector) ? Inflector : nil))
80   - @plural_collection_name = inflector.pluralize(sym.to_s)
81   - end
82   - @args = args
83   - @block = block
84   - self
85   - end
86   -
87   - def relative_expectation
88   - "#{relativities[@relativity]}#{@expected}"
89   - end
90   - end
91   -
92   - # :call-seq:
93   - # should have(number).named_collection__or__sugar
94   - # should_not have(number).named_collection__or__sugar
95   - #
96   - # Passes if receiver is a collection with the submitted
97   - # number of items OR if the receiver OWNS a collection
98   - # with the submitted number of items.
99   - #
100   - # If the receiver OWNS the collection, you must use the name
101   - # of the collection. So if a <tt>Team</tt> instance has a
102   - # collection named <tt>#players</tt>, you must use that name
103   - # to set the expectation.
104   - #
105   - # If the receiver IS the collection, you can use any name
106   - # you like for <tt>named_collection</tt>. We'd recommend using
107   - # either "elements", "members", or "items" as these are all
108   - # standard ways of describing the things IN a collection.
109   - #
110   - # This also works for Strings, letting you set an expectation
111   - # about its length
112   - #
113   - # == Examples
114   - #
115   - # # Passes if team.players.size == 11
116   - # team.should have(11).players
117   - #
118   - # # Passes if [1,2,3].length == 3
119   - # [1,2,3].should have(3).items #"items" is pure sugar
120   - #
121   - # # Passes if "this string".length == 11
122   - # "this string".should have(11).characters #"characters" is pure sugar
123   - def have(n)
124   - Matchers::Have.new(n)
125   - end
126   - alias :have_exactly :have
127   -
128   - # :call-seq:
129   - # should have_at_least(number).items