rspec-expectations 2.8.0 → 2.9.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. data/.document +5 -0
  2. data/.yardopts +3 -0
  3. data/Changelog.md +176 -0
  4. data/README.md +2 -13
  5. data/features/custom_matchers/access_running_example.feature +1 -1
  6. data/features/step_definitions/additional_cli_steps.rb +4 -4
  7. data/lib/rspec/expectations/fail_with.rb +3 -3
  8. data/lib/rspec/expectations/handler.rb +3 -5
  9. data/lib/rspec/expectations/version.rb +1 -1
  10. data/lib/rspec/matchers.rb +387 -21
  11. data/lib/rspec/matchers/built_in.rb +33 -0
  12. data/lib/rspec/matchers/built_in/base_matcher.rb +58 -0
  13. data/lib/rspec/matchers/built_in/be.rb +183 -0
  14. data/lib/rspec/matchers/built_in/be_instance_of.rb +13 -0
  15. data/lib/rspec/matchers/built_in/be_kind_of.rb +13 -0
  16. data/lib/rspec/matchers/built_in/be_within.rb +39 -0
  17. data/lib/rspec/matchers/built_in/change.rb +132 -0
  18. data/lib/rspec/matchers/built_in/cover.rb +22 -0
  19. data/lib/rspec/matchers/built_in/eq.rb +26 -0
  20. data/lib/rspec/matchers/built_in/eql.rb +25 -0
  21. data/lib/rspec/matchers/built_in/equal.rb +48 -0
  22. data/lib/rspec/matchers/built_in/exist.rb +28 -0
  23. data/lib/rspec/matchers/built_in/has.rb +47 -0
  24. data/lib/rspec/matchers/built_in/have.rb +107 -0
  25. data/lib/rspec/matchers/built_in/include.rb +52 -0
  26. data/lib/rspec/matchers/built_in/match.rb +13 -0
  27. data/lib/rspec/matchers/built_in/match_array.rb +52 -0
  28. data/lib/rspec/matchers/built_in/raise_error.rb +96 -0
  29. data/lib/rspec/matchers/built_in/respond_to.rb +73 -0
  30. data/lib/rspec/matchers/built_in/satisfy.rb +29 -0
  31. data/lib/rspec/matchers/built_in/throw_symbol.rb +93 -0
  32. data/lib/rspec/matchers/dsl.rb +1 -1
  33. data/lib/rspec/matchers/matcher.rb +263 -233
  34. data/lib/rspec/matchers/method_missing.rb +2 -2
  35. data/lib/rspec/matchers/operator_matcher.rb +19 -20
  36. data/spec/rspec/expectations/handler_spec.rb +1 -1
  37. data/spec/rspec/matchers/base_matcher_spec.rb +1 -2
  38. data/spec/rspec/matchers/change_spec.rb +3 -3
  39. data/spec/rspec/matchers/cover_spec.rb +46 -46
  40. data/spec/rspec/matchers/dsl_spec.rb +36 -3
  41. data/spec/rspec/matchers/have_spec.rb +2 -2
  42. data/spec/rspec/matchers/include_spec.rb +1 -1
  43. data/spec/rspec/matchers/matcher_spec.rb +319 -305
  44. data/spec/rspec/matchers/method_missing_spec.rb +1 -0
  45. data/spec/rspec/matchers/operator_matcher_spec.rb +2 -2
  46. data/spec/rspec/matchers/throw_symbol_spec.rb +103 -105
  47. metadata +93 -39
  48. data/lib/rspec/matchers/base_matcher.rb +0 -56
  49. data/lib/rspec/matchers/be.rb +0 -232
  50. data/lib/rspec/matchers/be_instance_of.rb +0 -24
  51. data/lib/rspec/matchers/be_kind_of.rb +0 -24
  52. data/lib/rspec/matchers/be_within.rb +0 -47
  53. data/lib/rspec/matchers/change.rb +0 -197
  54. data/lib/rspec/matchers/cover.rb +0 -36
  55. data/lib/rspec/matchers/eq.rb +0 -36
  56. data/lib/rspec/matchers/eql.rb +0 -35
  57. data/lib/rspec/matchers/equal.rb +0 -58
  58. data/lib/rspec/matchers/errors.rb +0 -5
  59. data/lib/rspec/matchers/exist.rb +0 -34
  60. data/lib/rspec/matchers/has.rb +0 -44
  61. data/lib/rspec/matchers/have.rb +0 -162
  62. data/lib/rspec/matchers/include.rb +0 -66
  63. data/lib/rspec/matchers/match.rb +0 -21
  64. data/lib/rspec/matchers/match_array.rb +0 -65
  65. data/lib/rspec/matchers/raise_error.rb +0 -116
  66. data/lib/rspec/matchers/respond_to.rb +0 -80
  67. data/lib/rspec/matchers/satisfy.rb +0 -46
  68. data/lib/rspec/matchers/throw_symbol.rb +0 -112
@@ -1,36 +0,0 @@
1
- module RSpec
2
- module Matchers
3
- class Cover
4
- include BaseMatcher
5
-
6
- def initialize(*expected)
7
- super(expected)
8
- end
9
-
10
- def matches?(range)
11
- expected.all? {|e| super(range).cover?(e)}
12
- end
13
-
14
- def does_not_match?(range)
15
- @actual = range
16
- expected.none? {|e| range.cover?(e)}
17
- end
18
- end
19
-
20
- # Passes if actual covers expected. This works for
21
- # Ranges. You can also pass in multiple args
22
- # and it will only pass if all args are found in Range.
23
- #
24
- # @example
25
- # (1..10).should cover(5)
26
- # (1..10).should cover(4, 6)
27
- # (1..10).should cover(4, 6, 11) # will fail
28
- # (1..10).should_not cover(11)
29
- # (1..10).should_not cover(5) # will fail
30
- #
31
- # ### Warning:: Ruby >= 1.9 only
32
- def cover(*values)
33
- Cover.new(*values)
34
- end
35
- end
36
- end
@@ -1,36 +0,0 @@
1
- module RSpec
2
- module Matchers
3
- class Eq
4
- include BaseMatcher
5
-
6
- def matches?(actual)
7
- super(actual) == expected
8
- end
9
-
10
- def failure_message_for_should
11
- "\nexpected: #{expected.inspect}\n got: #{actual.inspect}\n\n(compared using ==)\n"
12
- end
13
-
14
- def failure_message_for_should_not
15
- "\nexpected: value != #{expected.inspect}\n got: #{actual.inspect}\n\n(compared using ==)\n"
16
- end
17
-
18
- def diffable?
19
- true
20
- end
21
- end
22
-
23
- # Passes if <tt>actual == expected</tt>.
24
- #
25
- # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
26
- #
27
- # @example
28
- #
29
- # 5.should eq(5)
30
- # 5.should_not eq(3)
31
- def eq(expected)
32
- Eq.new(expected)
33
- end
34
- end
35
- end
36
-
@@ -1,35 +0,0 @@
1
- module RSpec
2
- module Matchers
3
- class Eql
4
- include BaseMatcher
5
-
6
- def matches?(actual)
7
- super(actual).eql?(expected)
8
- end
9
-
10
- def failure_message_for_should
11
- "\nexpected: #{expected.inspect}\n got: #{actual.inspect}\n\n(compared using eql?)\n"
12
- end
13
-
14
- def failure_message_for_should_not
15
- "\nexpected: value != #{expected.inspect}\n got: #{actual.inspect}\n\n(compared using eql?)\n"
16
- end
17
-
18
- def diffable?
19
- true
20
- end
21
- end
22
-
23
- # Passes if +actual.eql?(expected)+
24
- #
25
- # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
26
- #
27
- # @example
28
- #
29
- # 5.should eql(5)
30
- # 5.should_not eql(3)
31
- def eql(expected)
32
- Eql.new(expected)
33
- end
34
- end
35
- end
@@ -1,58 +0,0 @@
1
- module RSpec
2
- module Matchers
3
- class Equal
4
- include BaseMatcher
5
-
6
- def matches?(actual)
7
- super(actual).equal?(expected)
8
- end
9
-
10
- def failure_message_for_should
11
- return <<-MESSAGE
12
-
13
- expected #{inspect_object(expected)}
14
- got #{inspect_object(actual)}
15
-
16
- Compared using equal?, which compares object identity,
17
- but expected and actual are not the same object. Use
18
- 'actual.should == expected' if you don't care about
19
- object identity in this example.
20
-
21
- MESSAGE
22
- end
23
-
24
- def failure_message_for_should_not
25
- return <<-MESSAGE
26
-
27
- expected not #{inspect_object(actual)}
28
- got #{inspect_object(expected)}
29
-
30
- Compared using equal?, which compares object identity.
31
-
32
- MESSAGE
33
- end
34
-
35
- def diffable?
36
- true
37
- end
38
-
39
- private
40
-
41
- def inspect_object(o)
42
- "#<#{o.class}:#{o.object_id}> => #{o.inspect}"
43
- end
44
- end
45
-
46
- # Passes if <tt>actual.equal?(expected)</tt> (object identity).
47
- #
48
- # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
49
- #
50
- # @example
51
- #
52
- # 5.should equal(5) # Fixnums are equal
53
- # "5".should_not equal("5") # Strings that look the same are not the same object
54
- def equal(expected)
55
- Equal.new(expected)
56
- end
57
- end
58
- end
@@ -1,5 +0,0 @@
1
- module RSpec
2
- module Matchers
3
- class MatcherError < StandardError; end
4
- end
5
- end
@@ -1,34 +0,0 @@
1
- module RSpec
2
- module Matchers
3
- class Exist
4
- include BaseMatcher
5
-
6
- def initialize(*args)
7
- super(args)
8
- end
9
-
10
- def matches?(actual)
11
- super(actual)
12
- predicates = [:exist?, :exists?].select { |p| actual.respond_to?(p) }
13
- existance_values = predicates.map { |p| actual.send(p, *expected) }
14
- uniq_truthy_values = existance_values.map { |v| !!v }.uniq
15
-
16
- case uniq_truthy_values.size
17
- when 0; raise NoMethodError.new("#{actual.inspect} does not respond to either #exist? or #exists?")
18
- when 1; existance_values.first
19
- else raise "#exist? and #exists? returned different values:\n\n" +
20
- " exist?: #{existance_values.first}\n" +
21
- "exists?: #{existance_values.last}"
22
- end
23
- end
24
- end
25
-
26
- # Passes if `actual.exist?` or `actual.exists?`
27
- #
28
- # @example
29
- # File.should exist("path/to/file")
30
- def exist(*args)
31
- Exist.new(*args)
32
- end
33
- end
34
- end
@@ -1,44 +0,0 @@
1
- module RSpec
2
- module Matchers
3
- class Has
4
- def initialize(expected, *args)
5
- @expected, @args = expected, args
6
- end
7
-
8
- def matches?(actual)
9
- actual.__send__(predicate(@expected), *@args)
10
- end
11
-
12
- def failure_message_for_should
13
- "expected ##{predicate(@expected)}#{failure_message_args_description} to return true, got false"
14
- end
15
-
16
- def failure_message_for_should_not
17
- "expected ##{predicate(@expected)}#{failure_message_args_description} to return false, got true"
18
- end
19
-
20
- def description
21
- [method_description(@expected), args_description].compact.join(' ')
22
- end
23
-
24
- private
25
- def predicate(sym)
26
- "#{sym.to_s.sub("have_","has_")}?".to_sym
27
- end
28
-
29
- def method_description(method)
30
- method.to_s.gsub('_', ' ')
31
- end
32
-
33
- def args_description
34
- return nil if @args.empty?
35
- @args.map { |arg| arg.inspect }.join(', ')
36
- end
37
-
38
- def failure_message_args_description
39
- desc = args_description
40
- "(#{desc})" if desc
41
- end
42
- end
43
- end
44
- end
@@ -1,162 +0,0 @@
1
- module RSpec
2
- module Matchers
3
- class Have
4
- def initialize(expected, relativity=:exactly)
5
- @expected = case expected
6
- when :no then 0
7
- when String then expected.to_i
8
- else expected
9
- end
10
- @relativity = relativity
11
- @actual = @collection_name = @plural_collection_name = nil
12
- end
13
-
14
- def relativities
15
- @relativities ||= {
16
- :exactly => "",
17
- :at_least => "at least ",
18
- :at_most => "at most "
19
- }
20
- end
21
-
22
- def matches?(collection_or_owner)
23
- collection = determine_collection(collection_or_owner)
24
- query_method = determine_query_method(collection)
25
- raise not_a_collection unless query_method
26
- @actual = collection.send(query_method)
27
- case @relativity
28
- when :at_least then @actual >= @expected
29
- when :at_most then @actual <= @expected
30
- else @actual == @expected
31
- end
32
- end
33
-
34
- def determine_collection(collection_or_owner)
35
- if collection_or_owner.respond_to?(@collection_name)
36
- collection_or_owner.send(@collection_name, *@args, &@block)
37
- elsif (@plural_collection_name && collection_or_owner.respond_to?(@plural_collection_name))
38
- collection_or_owner.send(@plural_collection_name, *@args, &@block)
39
- elsif determine_query_method(collection_or_owner)
40
- collection_or_owner
41
- else
42
- collection_or_owner.send(@collection_name, *@args, &@block)
43
- end
44
- end
45
-
46
- def determine_query_method(collection)
47
- [:size, :length, :count].detect {|m| collection.respond_to?(m)}
48
- end
49
-
50
- def not_a_collection
51
- "expected #{@collection_name} to be a collection but it does not respond to #length, #size or #count"
52
- end
53
-
54
- def failure_message_for_should
55
- "expected #{relative_expectation} #{@collection_name}, got #{@actual}"
56
- end
57
-
58
- def failure_message_for_should_not
59
- if @relativity == :exactly
60
- return "expected target not to have #{@expected} #{@collection_name}, got #{@actual}"
61
- elsif @relativity == :at_most
62
- return <<-EOF
63
- Isn't life confusing enough?
64
- Instead of having to figure out the meaning of this:
65
- should_not have_at_most(#{@expected}).#{@collection_name}
66
- We recommend that you use this instead:
67
- should have_at_least(#{@expected + 1}).#{@collection_name}
68
- EOF
69
- elsif @relativity == :at_least
70
- return <<-EOF
71
- Isn't life confusing enough?
72
- Instead of having to figure out the meaning of this:
73
- should_not have_at_least(#{@expected}).#{@collection_name}
74
- We recommend that you use this instead:
75
- should have_at_most(#{@expected - 1}).#{@collection_name}
76
- EOF
77
- end
78
- end
79
-
80
- def description
81
- "have #{relative_expectation} #{@collection_name}"
82
- end
83
-
84
- def respond_to?(m)
85
- @expected.respond_to?(m) || super
86
- end
87
-
88
- private
89
-
90
- def method_missing(method, *args, &block)
91
- @collection_name = method
92
- if inflector = (defined?(ActiveSupport::Inflector) && ActiveSupport::Inflector.respond_to?(:pluralize) ? ActiveSupport::Inflector : (defined?(Inflector) ? Inflector : nil))
93
- @plural_collection_name = inflector.pluralize(method.to_s)
94
- end
95
- @args = args
96
- @block = block
97
- self
98
- end
99
-
100
- def relative_expectation
101
- "#{relativities[@relativity]}#{@expected}"
102
- end
103
- end
104
-
105
- # Passes if receiver is a collection with the submitted number of items OR
106
- # if the receiver OWNS a collection with the submitted number of items.
107
- #
108
- # If the receiver OWNS the collection, you must use the name of the
109
- # collection. So if a `Team` instance has a collection named `#players`,
110
- # you must use that name to set the expectation.
111
- #
112
- # If the receiver IS the collection, you can use any name you like for
113
- # `named_collection`. We'd recommend using either "elements", "members", or
114
- # "items" as these are all standard ways of describing the things IN a
115
- # collection.
116
- #
117
- # This also works for Strings, letting you set expectations about their
118
- # lengths.
119
- #
120
- # @example
121
- #
122
- # # Passes if team.players.size == 11
123
- # team.should have(11).players
124
- #
125
- # # Passes if [1,2,3].length == 3
126
- # [1,2,3].should have(3).items #"items" is pure sugar
127
- #
128
- # # Passes if ['a', 'b', 'c'].count == 3
129
- # [1,2,3].should have(3).items #"items" is pure sugar
130
- #
131
- # # Passes if "this string".length == 11
132
- # "this string".should have(11).characters #"characters" is pure sugar
133
- def have(n)
134
- Matchers::Have.new(n)
135
- end
136
- alias :have_exactly :have
137
-
138
- # Exactly like have() with >=.
139
- #
140
- # @example
141
- # "this".should have_at_least(3).letters
142
- #
143
- # ### Warning:
144
- #
145
- # `should_not have_at_least` is not supported
146
- def have_at_least(n)
147
- Matchers::Have.new(n, :at_least)
148
- end
149
-
150
- # Exactly like have() with <=.
151
- #
152
- # @example
153
- # should have_at_most(number).items
154
- #
155
- # ### Warning:
156
- #
157
- # `should_not have_at_most` is not supported
158
- def have_at_most(n)
159
- Matchers::Have.new(n, :at_most)
160
- end
161
- end
162
- end
@@ -1,66 +0,0 @@
1
- module RSpec
2
- module Matchers
3
- class Include
4
- include BaseMatcher
5
-
6
- def initialize(*expected)
7
- super(expected)
8
- end
9
-
10
- def matches?(actual)
11
- perform_match(:all?, :all?, super(actual), expected)
12
- end
13
-
14
- def does_not_match?(actual)
15
- @actual = actual
16
- perform_match(:none?, :any?, actual, expected)
17
- end
18
-
19
- def description
20
- "include#{expected_to_sentence}"
21
- end
22
-
23
- def diffable?
24
- true
25
- end
26
-
27
- private
28
-
29
- def perform_match(predicate, hash_predicate, actuals, expecteds)
30
- expecteds.send(predicate) do |expected|
31
- if comparing_hash_values?(actuals, expected)
32
- expected.send(hash_predicate) {|k,v| actuals[k] == v}
33
- elsif comparing_hash_keys?(actuals, expected)
34
- actuals.has_key?(expected)
35
- else
36
- actuals.include?(expected)
37
- end
38
- end
39
- end
40
-
41
- def comparing_hash_keys?(actual, expected)
42
- actual.is_a?(Hash) && !expected.is_a?(Hash)
43
- end
44
-
45
- def comparing_hash_values?(actual, expected)
46
- actual.is_a?(Hash) && expected.is_a?(Hash)
47
- end
48
- end
49
-
50
- # Passes if actual includes expected. This works for
51
- # collections and Strings. You can also pass in multiple args
52
- # and it will only pass if all args are found in collection.
53
- #
54
- # @example
55
- #
56
- # [1,2,3].should include(3)
57
- # [1,2,3].should include(2,3) #would pass
58
- # [1,2,3].should include(2,3,4) #would fail
59
- # [1,2,3].should_not include(4)
60
- # "spread".should include("read")
61
- # "spread".should_not include("red")
62
- def include(*expected)
63
- Include.new(*expected)
64
- end
65
- end
66
- end