rspec_multi_matchers 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +8 -7
- data/lib/match_each.rb +28 -10
- data/lib/rspec_multi_matchers.rb +3 -1
- data/spec/lib/match_each_spec.rb +2 -2
- data/spec/lib/match_in_order_spec.rb +5 -5
- data/spec/shared_enum_spec.rb +1 -1
- metadata +88 -9
data/README.rdoc
CHANGED
@@ -2,17 +2,13 @@
|
|
2
2
|
|
3
3
|
== Summary
|
4
4
|
* test collection using each or other enumerable methods
|
5
|
-
*
|
5
|
+
* makes testing more natural and have a friendlier failure message
|
6
6
|
|
7
7
|
== HomePage
|
8
8
|
* http://github.com/gregwebs/rspec-multi-matchers
|
9
9
|
|
10
10
|
== DESCRIPTION:
|
11
11
|
|
12
|
-
* match_each
|
13
|
-
* match_enum
|
14
|
-
* match_in_order
|
15
|
-
|
16
12
|
require 'rubygems'
|
17
13
|
require 'spec'
|
18
14
|
require 'rspec_multi_matchers'
|
@@ -23,6 +19,11 @@
|
|
23
19
|
n.should == 1
|
24
20
|
}
|
25
21
|
end
|
22
|
+
|
23
|
+
# this is a new shortcut for a smaller use case
|
24
|
+
it 'should be all ones' do
|
25
|
+
[1,1,1].should each be_eql(1)
|
26
|
+
end
|
26
27
|
end
|
27
28
|
|
28
29
|
=begin output
|
@@ -41,7 +42,7 @@ the item line gives the index of the item being yielded to the block, and the it
|
|
41
42
|
=== Warning
|
42
43
|
|
43
44
|
Note the use of brackets '{ ... }' instead of 'do ... end'
|
44
|
-
this is necessary because 'do .. end' does not bind
|
45
|
+
this is necessary because 'do .. end' does not bind strongly enough
|
45
46
|
|
46
47
|
== RELATED ARTICLES:
|
47
48
|
|
@@ -55,7 +56,7 @@ this is necessary because 'do .. end' does not bind strong enough
|
|
55
56
|
|
56
57
|
(The MIT License)
|
57
58
|
|
58
|
-
Copyright (c)
|
59
|
+
Copyright (c) 2010 Greg Weber
|
59
60
|
|
60
61
|
Permission is hereby granted, free of charge, to any person obtaining
|
61
62
|
a copy of this software and associated documentation files (the
|
data/lib/match_each.rb
CHANGED
@@ -1,3 +1,24 @@
|
|
1
|
+
def each meta_or_options = nil, &block
|
2
|
+
if block or !meta_or_options
|
3
|
+
MatchEach.new meta_or_options, &block
|
4
|
+
else
|
5
|
+
each_matcher meta_or_options
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
RSpec::Matchers.define :each_matcher do |meta|
|
10
|
+
match do |actual|
|
11
|
+
actual.each_with_index do |i, j|
|
12
|
+
@elem = j
|
13
|
+
i.should meta_or_options
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
failure_message_for_should do |actual|
|
18
|
+
"at[#{@elem}] #{meta_or_options.failure_message_for_should}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
1
22
|
class MatchEach
|
2
23
|
|
3
24
|
class MatchEachError < Exception; end
|
@@ -25,7 +46,7 @@ class MatchEach
|
|
25
46
|
target.each do |obj|
|
26
47
|
begin
|
27
48
|
@block.call(obj)
|
28
|
-
rescue
|
49
|
+
rescue RSpec::Expectations::ExpectationNotMetError => e
|
29
50
|
@error = e
|
30
51
|
@failure_object = obj
|
31
52
|
return false
|
@@ -50,13 +71,14 @@ class MatchEach
|
|
50
71
|
def failure_line
|
51
72
|
# find 'matches?' in statck trace
|
52
73
|
# then move back to the first line number that is not a function call
|
53
|
-
|
74
|
+
error_lines = []
|
54
75
|
@error.backtrace.each do |line|
|
55
|
-
if line
|
56
|
-
|
57
|
-
|
58
|
-
|
76
|
+
if line[/:\d+:in\s*[`'"](.*)[`'"]\s*$/, 1] == 'matches?'
|
77
|
+
error_lines.reverse_each do |line|
|
78
|
+
return line[/^[^:]+:(\d+)/, 1] if line !~ %r!rspec-multi-matchers/lib/match_each\.rb!
|
79
|
+
end
|
59
80
|
end
|
81
|
+
error_lines.push line
|
60
82
|
end
|
61
83
|
|
62
84
|
nil # should not reach here
|
@@ -72,7 +94,3 @@ class MatchEach
|
|
72
94
|
|
73
95
|
# no need for should_not, so no negative_failure_messages
|
74
96
|
end
|
75
|
-
|
76
|
-
def each(options=nil, &block)
|
77
|
-
MatchEach.new options, &block
|
78
|
-
end
|
data/lib/rspec_multi_matchers.rb
CHANGED
data/spec/lib/match_each_spec.rb
CHANGED
@@ -11,12 +11,12 @@ describe MatchEach do
|
|
11
11
|
it 'should add to the inner error message' do
|
12
12
|
begin
|
13
13
|
2.should == 1
|
14
|
-
rescue
|
14
|
+
rescue RSpec::Expectations::ExpectationNotMetError => e
|
15
15
|
@line = __LINE__ + 2
|
16
16
|
lambda{ [1,2,3].should each { |n|
|
17
17
|
n.should == 1
|
18
18
|
} }.should raise_error(
|
19
|
-
|
19
|
+
RSpec::Expectations::ExpectationNotMetError, /^\s*line: #{@line}\s*item 1: 2\s*#{Regexp.escape(e.message)}/m)
|
20
20
|
else fail
|
21
21
|
end
|
22
22
|
end
|
@@ -11,20 +11,20 @@ describe MatchInOrder do
|
|
11
11
|
|
12
12
|
it "should not match the same regular expressions twice" do
|
13
13
|
"a".should_not match_in_order(/a/,/a/)
|
14
|
-
lambda{ "a".should match_in_order(/a/,/a/) }.should raise_error(
|
14
|
+
lambda{ "a".should match_in_order(/a/,/a/) }.should raise_error(RSpec::Expectations::ExpectationNotMetError, 'expected "" to match /a/' << "\n" << 'within string: "a"')
|
15
15
|
|
16
16
|
"abc".should_not match_in_order(/a/,/b/,/c/,/a/)
|
17
|
-
lambda{ "abc".should match_in_order(/a/,/b/,/c/,/a/) }.should raise_error(
|
17
|
+
lambda{ "abc".should match_in_order(/a/,/b/,/c/,/a/) }.should raise_error(RSpec::Expectations::ExpectationNotMetError, 'expected "" to match /a/' << "\n" << 'within string: "abc"')
|
18
18
|
end
|
19
19
|
|
20
20
|
it "should match multiple regular expressions in order" do
|
21
21
|
"abc".should match_in_order(/a/,/b/,/c/)
|
22
|
-
lambda{ "abc".should_not match_in_order(/a/,/b/,/c/) }.should raise_error(
|
22
|
+
lambda{ "abc".should_not match_in_order(/a/,/b/,/c/) }.should raise_error(RSpec::Expectations::ExpectationNotMetError,'expected "abc" to not match in order against: [/a/, /b/, /c/]')
|
23
23
|
|
24
24
|
"abc".should_not match_in_order(/a/,/c/,/b/)
|
25
|
-
lambda{ "abc".should match_in_order(/a/,/c/,/b/) }.should raise_error(
|
25
|
+
lambda{ "abc".should match_in_order(/a/,/c/,/b/) }.should raise_error(RSpec::Expectations::ExpectationNotMetError,'expected "" to match /b/' << "\n" << 'within string: "abc"')
|
26
26
|
|
27
27
|
"abc".should_not match_in_order(/b/,/a/,/c/)
|
28
|
-
lambda{ "abc".should match_in_order(/b/,/a/,/c/) }.should raise_error(
|
28
|
+
lambda{ "abc".should match_in_order(/b/,/a/,/c/) }.should raise_error(RSpec::Expectations::ExpectationNotMetError,'expected "c" to match /a/' << "\n" << 'within string: "abc"')
|
29
29
|
end
|
30
30
|
end
|
data/spec/shared_enum_spec.rb
CHANGED
@@ -12,7 +12,7 @@ shared_examples_for "each matcher" do
|
|
12
12
|
end
|
13
13
|
|
14
14
|
it 'should raise an error if there are no items in the enumerable object' do
|
15
|
-
lambda{ [].should send(*@iterator){} }.should raise_error(@class::BlankEnumerableError)
|
15
|
+
lambda{ [].should send(*@iterator){} }.should raise_error(@class::BlankEnumerableError, /.*/)
|
16
16
|
[false, nil].each do |f|
|
17
17
|
@iterator.push(:empty => f)
|
18
18
|
lambda{ [].should send(*@iterator){}}.should raise_error(@class::BlankEnumerableError)
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec_multi_matchers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 31
|
5
|
+
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 1.
|
10
|
+
version: 1.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Greg Weber
|
@@ -15,12 +15,91 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-08-24 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
22
|
-
description:
|
23
|
-
|
22
|
+
description: |
|
23
|
+
= rspec-multi-matchers
|
24
|
+
|
25
|
+
== Summary
|
26
|
+
* test collection using each or other enumerable methods
|
27
|
+
* makes testing more natural and have a friendlier failure message
|
28
|
+
|
29
|
+
== HomePage
|
30
|
+
* http://github.com/gregwebs/rspec-multi-matchers
|
31
|
+
|
32
|
+
== DESCRIPTION:
|
33
|
+
|
34
|
+
require 'rubygems'
|
35
|
+
require 'spec'
|
36
|
+
require 'rspec_multi_matchers'
|
37
|
+
|
38
|
+
describe 'array of ones' do
|
39
|
+
it 'should be all ones' do
|
40
|
+
[1,2,3].should each { |n|
|
41
|
+
n.should == 1
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
# this is a new shortcut for a smaller use case
|
46
|
+
it 'should be all ones' do
|
47
|
+
[1,1,1].should each be_eql(1)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
=begin output
|
52
|
+
'array of ones should fail on 2' FAILED
|
53
|
+
line: 14
|
54
|
+
item 1: 2
|
55
|
+
expected: 1,
|
56
|
+
got: 2 (using ==)
|
57
|
+
=end
|
58
|
+
|
59
|
+
As expected, the output shows expected and got fields
|
60
|
+
line is the line number of the expectiation inside the block
|
61
|
+
the item line gives the index of the item being yielded to the block, and the item itself
|
62
|
+
|
63
|
+
|
64
|
+
=== Warning
|
65
|
+
|
66
|
+
Note the use of brackets '{ ... }' instead of 'do ... end'
|
67
|
+
this is necessary because 'do .. end' does not bind strongly enough
|
68
|
+
|
69
|
+
== RELATED ARTICLES:
|
70
|
+
|
71
|
+
* http://blog.thoughtfolder.com/2008-11-05-rspec-should-each-matcher.html
|
72
|
+
|
73
|
+
== INSTALL:
|
74
|
+
|
75
|
+
* gem install rspec_multi_matchers
|
76
|
+
|
77
|
+
== LICENSE:
|
78
|
+
|
79
|
+
(The MIT License)
|
80
|
+
|
81
|
+
Copyright (c) 2010 Greg Weber
|
82
|
+
|
83
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
84
|
+
a copy of this software and associated documentation files (the
|
85
|
+
'Software'), to deal in the Software without restriction, including
|
86
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
87
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
88
|
+
permit persons to whom the Software is furnished to do so, subject to
|
89
|
+
the following conditions:
|
90
|
+
|
91
|
+
The above copyright notice and this permission notice shall be
|
92
|
+
included in all copies or substantial portions of the Software.
|
93
|
+
|
94
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
95
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
96
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
97
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
98
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
99
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
100
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
101
|
+
|
102
|
+
email: greg@gregweber.info
|
24
103
|
executables: []
|
25
104
|
|
26
105
|
extensions: []
|
@@ -41,7 +120,7 @@ files:
|
|
41
120
|
- spec/shared_enum_spec.rb
|
42
121
|
- spec/spec.opts
|
43
122
|
has_rdoc: true
|
44
|
-
homepage:
|
123
|
+
homepage: https://github.com/gregwebs/rspec-multi-matchers
|
45
124
|
licenses: []
|
46
125
|
|
47
126
|
post_install_message:
|
@@ -70,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
149
|
requirements: []
|
71
150
|
|
72
151
|
rubyforge_project:
|
73
|
-
rubygems_version: 1.
|
152
|
+
rubygems_version: 1.3.7
|
74
153
|
signing_key:
|
75
154
|
specification_version: 3
|
76
155
|
summary: better collection testing
|