rspec-expectations 3.0.0.beta1 → 3.0.0.beta2
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.tar.gz.sig +2 -2
- data/.yardopts +1 -0
- data/Changelog.md +138 -0
- data/README.md +75 -8
- data/features/README.md +2 -2
- data/features/built_in_matchers/README.md +12 -9
- data/features/built_in_matchers/comparisons.feature +2 -2
- data/features/built_in_matchers/contain_exactly.feature +46 -0
- data/features/built_in_matchers/expect_change.feature +2 -2
- data/features/built_in_matchers/include.feature +0 -48
- data/features/built_in_matchers/output.feature +70 -0
- data/features/composing_matchers.feature +250 -0
- data/features/compound_expectations.feature +45 -0
- data/features/custom_matchers/access_running_example.feature +1 -1
- data/features/custom_matchers/define_matcher.feature +6 -6
- data/features/custom_matchers/define_matcher_outside_rspec.feature +4 -8
- data/features/test_frameworks/{test_unit.feature → minitest.feature} +11 -11
- data/lib/rspec/expectations.rb +31 -42
- data/lib/rspec/expectations/diff_presenter.rb +141 -0
- data/lib/rspec/expectations/differ.rb +22 -132
- data/lib/rspec/expectations/encoded_string.rb +56 -0
- data/lib/rspec/expectations/expectation_target.rb +0 -30
- data/lib/rspec/expectations/fail_with.rb +2 -2
- data/lib/rspec/expectations/handler.rb +128 -31
- data/lib/rspec/expectations/minitest_integration.rb +16 -0
- data/lib/rspec/expectations/syntax.rb +4 -58
- data/lib/rspec/expectations/version.rb +1 -1
- data/lib/rspec/matchers.rb +298 -60
- data/lib/rspec/matchers/aliased_matcher.rb +35 -0
- data/lib/rspec/matchers/built_in.rb +37 -33
- data/lib/rspec/matchers/built_in/base_matcher.rb +25 -15
- data/lib/rspec/matchers/built_in/be.rb +23 -31
- data/lib/rspec/matchers/built_in/be_between.rb +55 -0
- data/lib/rspec/matchers/built_in/be_within.rb +15 -11
- data/lib/rspec/matchers/built_in/change.rb +198 -81
- data/lib/rspec/matchers/built_in/compound.rb +106 -0
- data/lib/rspec/matchers/built_in/contain_exactly.rb +245 -0
- data/lib/rspec/matchers/built_in/eq.rb +43 -4
- data/lib/rspec/matchers/built_in/eql.rb +2 -2
- data/lib/rspec/matchers/built_in/equal.rb +35 -18
- data/lib/rspec/matchers/built_in/has.rb +16 -15
- data/lib/rspec/matchers/built_in/include.rb +45 -23
- data/lib/rspec/matchers/built_in/match.rb +6 -3
- data/lib/rspec/matchers/built_in/operators.rb +103 -0
- data/lib/rspec/matchers/built_in/output.rb +108 -0
- data/lib/rspec/matchers/built_in/raise_error.rb +9 -15
- data/lib/rspec/matchers/built_in/respond_to.rb +5 -4
- data/lib/rspec/matchers/built_in/satisfy.rb +4 -3
- data/lib/rspec/matchers/built_in/start_and_end_with.rb +37 -16
- data/lib/rspec/matchers/built_in/throw_symbol.rb +6 -5
- data/lib/rspec/matchers/built_in/yield.rb +31 -29
- data/lib/rspec/matchers/composable.rb +138 -0
- data/lib/rspec/matchers/dsl.rb +330 -0
- data/lib/rspec/matchers/generated_descriptions.rb +6 -6
- data/lib/rspec/matchers/matcher_delegator.rb +33 -0
- data/lib/rspec/matchers/pretty.rb +13 -2
- data/spec/rspec/expectations/{differ_spec.rb → diff_presenter_spec.rb} +56 -36
- data/spec/rspec/expectations/encoded_string_spec.rb +74 -0
- data/spec/rspec/expectations/extensions/kernel_spec.rb +11 -11
- data/spec/rspec/expectations/fail_with_spec.rb +8 -8
- data/spec/rspec/expectations/handler_spec.rb +27 -49
- data/spec/rspec/expectations/minitest_integration_spec.rb +27 -0
- data/spec/rspec/expectations/syntax_spec.rb +17 -67
- data/spec/rspec/expectations_spec.rb +7 -52
- data/spec/rspec/matchers/aliased_matcher_spec.rb +48 -0
- data/spec/rspec/matchers/aliases_spec.rb +449 -0
- data/spec/rspec/matchers/{base_matcher_spec.rb → built_in/base_matcher_spec.rb} +24 -3
- data/spec/rspec/matchers/built_in/be_between_spec.rb +159 -0
- data/spec/rspec/matchers/{be_instance_of_spec.rb → built_in/be_instance_of_spec.rb} +0 -0
- data/spec/rspec/matchers/{be_kind_of_spec.rb → built_in/be_kind_of_spec.rb} +0 -0
- data/spec/rspec/matchers/{be_spec.rb → built_in/be_spec.rb} +76 -32
- data/spec/rspec/matchers/{be_within_spec.rb → built_in/be_within_spec.rb} +6 -2
- data/spec/rspec/matchers/{change_spec.rb → built_in/change_spec.rb} +310 -69
- data/spec/rspec/matchers/built_in/compound_spec.rb +292 -0
- data/spec/rspec/matchers/built_in/contain_exactly_spec.rb +441 -0
- data/spec/rspec/matchers/{cover_spec.rb → built_in/cover_spec.rb} +0 -0
- data/spec/rspec/matchers/built_in/eq_spec.rb +156 -0
- data/spec/rspec/matchers/{eql_spec.rb → built_in/eql_spec.rb} +2 -2
- data/spec/rspec/matchers/built_in/equal_spec.rb +106 -0
- data/spec/rspec/matchers/{exist_spec.rb → built_in/exist_spec.rb} +1 -1
- data/spec/rspec/matchers/{has_spec.rb → built_in/has_spec.rb} +39 -0
- data/spec/rspec/matchers/{include_spec.rb → built_in/include_spec.rb} +118 -109
- data/spec/rspec/matchers/{match_spec.rb → built_in/match_spec.rb} +30 -2
- data/spec/rspec/matchers/{operator_matcher_spec.rb → built_in/operators_spec.rb} +26 -26
- data/spec/rspec/matchers/built_in/output_spec.rb +165 -0
- data/spec/rspec/matchers/{raise_error_spec.rb → built_in/raise_error_spec.rb} +81 -11
- data/spec/rspec/matchers/{respond_to_spec.rb → built_in/respond_to_spec.rb} +0 -0
- data/spec/rspec/matchers/{satisfy_spec.rb → built_in/satisfy_spec.rb} +0 -0
- data/spec/rspec/matchers/{start_with_end_with_spec.rb → built_in/start_and_end_with_spec.rb} +82 -15
- data/spec/rspec/matchers/{throw_symbol_spec.rb → built_in/throw_symbol_spec.rb} +29 -10
- data/spec/rspec/matchers/{yield_spec.rb → built_in/yield_spec.rb} +90 -0
- data/spec/rspec/matchers/configuration_spec.rb +7 -39
- data/spec/rspec/matchers/description_generation_spec.rb +22 -6
- data/spec/rspec/matchers/dsl_spec.rb +838 -0
- data/spec/rspec/matchers/legacy_spec.rb +101 -0
- data/spec/rspec/matchers_spec.rb +74 -0
- data/spec/spec_helper.rb +35 -21
- data/spec/support/shared_examples.rb +26 -4
- metadata +172 -116
- metadata.gz.sig +3 -4
- checksums.yaml +0 -15
- checksums.yaml.gz.sig +0 -0
- data/features/built_in_matchers/match_array.feature +0 -37
- data/lib/rspec/expectations/errors.rb +0 -9
- data/lib/rspec/expectations/extensions.rb +0 -1
- data/lib/rspec/expectations/extensions/object.rb +0 -29
- data/lib/rspec/matchers/built_in/match_array.rb +0 -51
- data/lib/rspec/matchers/compatibility.rb +0 -14
- data/lib/rspec/matchers/matcher.rb +0 -301
- data/lib/rspec/matchers/method_missing.rb +0 -12
- data/lib/rspec/matchers/operator_matcher.rb +0 -99
- data/lib/rspec/matchers/test_unit_integration.rb +0 -11
- data/spec/rspec/matchers/eq_spec.rb +0 -60
- data/spec/rspec/matchers/equal_spec.rb +0 -78
- data/spec/rspec/matchers/include_matcher_integration_spec.rb +0 -30
- data/spec/rspec/matchers/match_array_spec.rb +0 -194
- data/spec/rspec/matchers/matcher_spec.rb +0 -706
- data/spec/rspec/matchers/matchers_spec.rb +0 -36
- data/spec/rspec/matchers/method_missing_spec.rb +0 -28
- data/spec/support/classes.rb +0 -56
- data/spec/support/in_sub_process.rb +0 -37
- data/spec/support/ruby_version.rb +0 -10
@@ -1,12 +0,0 @@
|
|
1
|
-
module RSpec
|
2
|
-
module Matchers
|
3
|
-
|
4
|
-
private
|
5
|
-
|
6
|
-
def method_missing(method, *args, &block)
|
7
|
-
return Matchers::BuiltIn::BePredicate.new(method, *args, &block) if method.to_s =~ /^be_/
|
8
|
-
return Matchers::BuiltIn::Has.new(method, *args, &block) if method.to_s =~ /^have_/
|
9
|
-
super
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
@@ -1,99 +0,0 @@
|
|
1
|
-
module RSpec
|
2
|
-
module Matchers
|
3
|
-
class OperatorMatcher
|
4
|
-
class << self
|
5
|
-
def registry
|
6
|
-
@registry ||= {}
|
7
|
-
end
|
8
|
-
|
9
|
-
def register(klass, operator, matcher)
|
10
|
-
registry[klass] ||= {}
|
11
|
-
registry[klass][operator] = matcher
|
12
|
-
end
|
13
|
-
|
14
|
-
def unregister(klass, operator)
|
15
|
-
registry[klass] && registry[klass].delete(operator)
|
16
|
-
end
|
17
|
-
|
18
|
-
def get(klass, operator)
|
19
|
-
klass.ancestors.each { |ancestor|
|
20
|
-
matcher = registry[ancestor] && registry[ancestor][operator]
|
21
|
-
return matcher if matcher
|
22
|
-
}
|
23
|
-
|
24
|
-
nil
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def initialize(actual)
|
29
|
-
@actual = actual
|
30
|
-
end
|
31
|
-
|
32
|
-
def self.use_custom_matcher_or_delegate(operator)
|
33
|
-
define_method(operator) do |expected|
|
34
|
-
if uses_generic_implementation_of?(operator) && matcher = OperatorMatcher.get(@actual.class, operator)
|
35
|
-
@actual.__send__(::RSpec::Matchers.last_should, matcher.new(expected))
|
36
|
-
else
|
37
|
-
eval_match(@actual, operator, expected)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
negative_operator = operator.sub(/^=/, '!')
|
42
|
-
if negative_operator != operator && respond_to?(negative_operator)
|
43
|
-
define_method(negative_operator) do |expected|
|
44
|
-
opposite_should = ::RSpec::Matchers.last_should == :should ? :should_not : :should
|
45
|
-
raise "RSpec does not support `#{::RSpec::Matchers.last_should} #{negative_operator} expected`. " +
|
46
|
-
"Use `#{opposite_should} #{operator} expected` instead."
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
['==', '===', '=~', '>', '>=', '<', '<='].each do |operator|
|
52
|
-
use_custom_matcher_or_delegate operator
|
53
|
-
end
|
54
|
-
|
55
|
-
def fail_with_message(message)
|
56
|
-
RSpec::Expectations.fail_with(message, @expected, @actual)
|
57
|
-
end
|
58
|
-
|
59
|
-
def description
|
60
|
-
"#{@operator} #{@expected.inspect}"
|
61
|
-
end
|
62
|
-
|
63
|
-
private
|
64
|
-
|
65
|
-
def uses_generic_implementation_of?(op)
|
66
|
-
Expectations.method_handle_for(@actual, op).owner == ::Kernel
|
67
|
-
rescue NameError
|
68
|
-
false
|
69
|
-
end
|
70
|
-
|
71
|
-
def eval_match(actual, operator, expected)
|
72
|
-
::RSpec::Matchers.last_matcher = self
|
73
|
-
@operator, @expected = operator, expected
|
74
|
-
__delegate_operator(actual, operator, expected)
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
module BuiltIn
|
79
|
-
class PositiveOperatorMatcher < OperatorMatcher
|
80
|
-
def __delegate_operator(actual, operator, expected)
|
81
|
-
if actual.__send__(operator, expected)
|
82
|
-
true
|
83
|
-
elsif ['==','===', '=~'].include?(operator)
|
84
|
-
fail_with_message("expected: #{expected.inspect}\n got: #{actual.inspect} (using #{operator})")
|
85
|
-
else
|
86
|
-
fail_with_message("expected: #{operator} #{expected.inspect}\n got: #{operator.gsub(/./, ' ')} #{actual.inspect}")
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
class NegativeOperatorMatcher < OperatorMatcher
|
92
|
-
def __delegate_operator(actual, operator, expected)
|
93
|
-
return false unless actual.__send__(operator, expected)
|
94
|
-
return fail_with_message("expected not: #{operator} #{expected.inspect}\n got: #{operator.gsub(/./, ' ')} #{actual.inspect}")
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
@@ -1,11 +0,0 @@
|
|
1
|
-
# Include Matchers for other test frameworks. Note that MiniTest _must_
|
2
|
-
# come before TU because on ruby 1.9, T::U::TC is a subclass of MT::U::TC
|
3
|
-
# and a 1.9 bug can lead to infinite recursion from the `super` call in our
|
4
|
-
# method_missing hook. See this gist for more info:
|
5
|
-
# https://gist.github.com/845896
|
6
|
-
if defined?(MiniTest::Unit::TestCase)
|
7
|
-
MiniTest::Unit::TestCase.send(:include, RSpec::Matchers)
|
8
|
-
end
|
9
|
-
if defined?(Test::Unit::TestCase)
|
10
|
-
Test::Unit::TestCase.send(:include, RSpec::Matchers)
|
11
|
-
end
|
@@ -1,60 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Matchers
|
5
|
-
describe "eq" do
|
6
|
-
it_behaves_like "an RSpec matcher", :valid_value => 1, :invalid_value => 2 do
|
7
|
-
let(:matcher) { eq(1) }
|
8
|
-
end
|
9
|
-
|
10
|
-
it "is diffable" do
|
11
|
-
expect(eq(1)).to be_diffable
|
12
|
-
end
|
13
|
-
|
14
|
-
it "matches when actual == expected" do
|
15
|
-
expect(1).to eq(1)
|
16
|
-
end
|
17
|
-
|
18
|
-
it "does not match when actual != expected" do
|
19
|
-
expect(1).not_to eq(2)
|
20
|
-
end
|
21
|
-
|
22
|
-
it "compares by sending == to actual (not expected)" do
|
23
|
-
called = false
|
24
|
-
actual = Class.new do
|
25
|
-
define_method :== do |other|
|
26
|
-
called = true
|
27
|
-
end
|
28
|
-
end.new
|
29
|
-
|
30
|
-
expect(actual).to eq :anything # to trigger the matches? method
|
31
|
-
expect(called).to be_truthy
|
32
|
-
end
|
33
|
-
|
34
|
-
it "describes itself" do
|
35
|
-
matcher = eq(1)
|
36
|
-
matcher.matches?(1)
|
37
|
-
expect(matcher.description).to eq "eq 1"
|
38
|
-
end
|
39
|
-
|
40
|
-
it "provides message, expected and actual on #failure_message" do
|
41
|
-
matcher = eq("1")
|
42
|
-
matcher.matches?(1)
|
43
|
-
expect(matcher.failure_message_for_should).to eq "\nexpected: \"1\"\n got: 1\n\n(compared using ==)\n"
|
44
|
-
end
|
45
|
-
|
46
|
-
it "provides message, expected and actual on #negative_failure_message" do
|
47
|
-
matcher = eq(1)
|
48
|
-
matcher.matches?(1)
|
49
|
-
expect(matcher.failure_message_for_should_not).to eq "\nexpected: value != 1\n got: 1\n\n(compared using ==)\n"
|
50
|
-
end
|
51
|
-
|
52
|
-
it 'fails properly when the actual is an array of multiline strings' do
|
53
|
-
expect {
|
54
|
-
expect(["a\nb", "c\nd"]).to eq([])
|
55
|
-
}.to fail_matching("expected: []")
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
@@ -1,78 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
module RSpec
|
3
|
-
module Matchers
|
4
|
-
describe "equal" do
|
5
|
-
it_behaves_like "an RSpec matcher", :valid_value => :a, :invalid_value => :b do
|
6
|
-
let(:matcher) { equal(:a) }
|
7
|
-
end
|
8
|
-
|
9
|
-
def inspect_object(o)
|
10
|
-
"#<#{o.class}:#{o.object_id}> => #{o.inspect}"
|
11
|
-
end
|
12
|
-
|
13
|
-
it "matches when actual.equal?(expected)" do
|
14
|
-
expect(1).to equal(1)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "does not match when !actual.equal?(expected)" do
|
18
|
-
expect("1").not_to equal("1")
|
19
|
-
end
|
20
|
-
|
21
|
-
it "describes itself" do
|
22
|
-
matcher = equal(1)
|
23
|
-
matcher.matches?(1)
|
24
|
-
expect(matcher.description).to eq "equal 1"
|
25
|
-
end
|
26
|
-
|
27
|
-
it "suggests the `eq` matcher on failure" do
|
28
|
-
expected, actual = "1", "1"
|
29
|
-
expect {
|
30
|
-
expect(actual).to equal(expected)
|
31
|
-
}.to fail_with <<-MESSAGE
|
32
|
-
|
33
|
-
expected #{inspect_object(expected)}
|
34
|
-
got #{inspect_object(actual)}
|
35
|
-
|
36
|
-
Compared using equal?, which compares object identity,
|
37
|
-
but expected and actual are not the same object. Use
|
38
|
-
`expect(actual).to eq(expected)` if you don't care about
|
39
|
-
object identity in this example.
|
40
|
-
|
41
|
-
MESSAGE
|
42
|
-
end
|
43
|
-
|
44
|
-
context "when using only `should`", :uses_only_should do
|
45
|
-
it "suggests the `eq` matcher on failure" do
|
46
|
-
expected, actual = "1", "1"
|
47
|
-
lambda {
|
48
|
-
actual.should equal(expected)
|
49
|
-
}.should fail_with <<-MESSAGE
|
50
|
-
|
51
|
-
expected #{inspect_object(expected)}
|
52
|
-
got #{inspect_object(actual)}
|
53
|
-
|
54
|
-
Compared using equal?, which compares object identity,
|
55
|
-
but expected and actual are not the same object. Use
|
56
|
-
`actual.should eq(expected)` if you don't care about
|
57
|
-
object identity in this example.
|
58
|
-
|
59
|
-
MESSAGE
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
it "provides message on #negative_failure_message" do
|
64
|
-
expected = actual = "1"
|
65
|
-
matcher = equal(expected)
|
66
|
-
matcher.matches?(actual)
|
67
|
-
expect(matcher.failure_message_for_should_not).to eq <<-MESSAGE
|
68
|
-
|
69
|
-
expected not #{inspect_object(expected)}
|
70
|
-
got #{inspect_object(actual)}
|
71
|
-
|
72
|
-
Compared using equal?, which compares object identity.
|
73
|
-
|
74
|
-
MESSAGE
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Matchers
|
5
|
-
describe "include() interaction with built-in matchers" do
|
6
|
-
it "works with be_within(delta).of(expected)" do
|
7
|
-
expect([10, 20, 30]).to include( be_within(5).of(24) )
|
8
|
-
expect([10, 20, 30]).not_to include( be_within(3).of(24) )
|
9
|
-
end
|
10
|
-
|
11
|
-
it "works with be_instance_of(klass)" do
|
12
|
-
expect(["foo", 123, {:foo => "bar"}]).to include( be_instance_of(Hash) )
|
13
|
-
expect(["foo", 123, {:foo => "bar"}]).not_to include( be_instance_of(Range) )
|
14
|
-
end
|
15
|
-
|
16
|
-
it "works with be_kind_of(klass)" do
|
17
|
-
class StringSubclass < String; end
|
18
|
-
class NotHashSubclass; end
|
19
|
-
|
20
|
-
expect([StringSubclass.new("baz")]).to include( be_kind_of(String) )
|
21
|
-
expect([NotHashSubclass.new]).not_to include( be_kind_of(Hash) )
|
22
|
-
end
|
23
|
-
|
24
|
-
it "works with be_[some predicate]" do
|
25
|
-
expect([double("actual", :happy? => true)]).to include( be_happy )
|
26
|
-
expect([double("actual", :happy? => false)]).not_to include( be_happy )
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
@@ -1,194 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
class UnsortableObject
|
4
|
-
def initialize(id)
|
5
|
-
@id = id
|
6
|
-
end
|
7
|
-
|
8
|
-
def inspect
|
9
|
-
@id.to_s
|
10
|
-
end
|
11
|
-
|
12
|
-
def ==(other)
|
13
|
-
false
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
describe "should =~ array", :uses_should do
|
18
|
-
it "passes a valid positive expectation" do
|
19
|
-
[1, 2].should =~ [2, 1]
|
20
|
-
end
|
21
|
-
|
22
|
-
it "fails an invalid positive expectation" do
|
23
|
-
expect {
|
24
|
-
[1, 2, 3].should =~ [2, 1]
|
25
|
-
}.to fail_with(/expected collection contained/)
|
26
|
-
end
|
27
|
-
|
28
|
-
context "when the array defines a `=~` method" do
|
29
|
-
it 'delegates to that method rather than using the match_array matcher' do
|
30
|
-
array = []
|
31
|
-
def array.=~(other)
|
32
|
-
other == :foo
|
33
|
-
end
|
34
|
-
|
35
|
-
array.should =~ :foo
|
36
|
-
expect {
|
37
|
-
array.should =~ :bar
|
38
|
-
}.to fail_with(/expected: :bar/)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
context 'when the array defines a `send` method' do
|
43
|
-
it 'still works' do
|
44
|
-
array = [1, 2]
|
45
|
-
def array.send; :sent; end
|
46
|
-
|
47
|
-
array.should =~ array
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
describe "should_not =~ [:with, :multiple, :args]", :uses_should do
|
53
|
-
it "is not supported" do
|
54
|
-
expect {
|
55
|
-
[1,2,3].should_not =~ [1,2,3]
|
56
|
-
}.to fail_with(/Matcher does not support should_not/)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
describe "using match_array with expect" do
|
61
|
-
it "passes a valid positive expectation" do
|
62
|
-
expect([1, 2]).to match_array [2, 1]
|
63
|
-
end
|
64
|
-
|
65
|
-
it "fails an invalid positive expectation" do
|
66
|
-
expect {
|
67
|
-
expect([1, 2, 3]).to match_array [2, 1]
|
68
|
-
}.to fail_with(/expected collection contained/)
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
describe "expect(array).to match_array other_array" do
|
73
|
-
it_behaves_like "an RSpec matcher", :valid_value => [1, 2], :invalid_value => [1] do
|
74
|
-
let(:matcher) { match_array([2, 1]) }
|
75
|
-
end
|
76
|
-
|
77
|
-
it "passes if target contains all items" do
|
78
|
-
expect([1,2,3]).to match_array [1,2,3]
|
79
|
-
end
|
80
|
-
|
81
|
-
it "passes if target contains all items out of order" do
|
82
|
-
expect([1,3,2]).to match_array [1,2,3]
|
83
|
-
end
|
84
|
-
|
85
|
-
it "fails if target includes extra items" do
|
86
|
-
expect {
|
87
|
-
expect([1,2,3,4]).to match_array [1,2,3]
|
88
|
-
}.to fail_with(<<-MESSAGE)
|
89
|
-
expected collection contained: [1, 2, 3]
|
90
|
-
actual collection contained: [1, 2, 3, 4]
|
91
|
-
the extra elements were: [4]
|
92
|
-
MESSAGE
|
93
|
-
end
|
94
|
-
|
95
|
-
it "fails if target is missing items" do
|
96
|
-
expect {
|
97
|
-
expect([1,2]).to match_array [1,2,3]
|
98
|
-
}.to fail_with(<<-MESSAGE)
|
99
|
-
expected collection contained: [1, 2, 3]
|
100
|
-
actual collection contained: [1, 2]
|
101
|
-
the missing elements were: [3]
|
102
|
-
MESSAGE
|
103
|
-
end
|
104
|
-
|
105
|
-
it "fails if target is missing items and has extra items" do
|
106
|
-
expect {
|
107
|
-
expect([1,2,4]).to match_array [1,2,3]
|
108
|
-
}.to fail_with(<<-MESSAGE)
|
109
|
-
expected collection contained: [1, 2, 3]
|
110
|
-
actual collection contained: [1, 2, 4]
|
111
|
-
the missing elements were: [3]
|
112
|
-
the extra elements were: [4]
|
113
|
-
MESSAGE
|
114
|
-
end
|
115
|
-
|
116
|
-
it "sorts items in the error message if they all respond to <=>" do
|
117
|
-
expect {
|
118
|
-
expect([6,2,1,5]).to match_array [4,1,2,3]
|
119
|
-
}.to fail_with(<<-MESSAGE)
|
120
|
-
expected collection contained: [1, 2, 3, 4]
|
121
|
-
actual collection contained: [1, 2, 5, 6]
|
122
|
-
the missing elements were: [3, 4]
|
123
|
-
the extra elements were: [5, 6]
|
124
|
-
MESSAGE
|
125
|
-
end
|
126
|
-
|
127
|
-
it "does not sort items in the error message if they don't all respond to <=>" do
|
128
|
-
expect {
|
129
|
-
expect([UnsortableObject.new(2), UnsortableObject.new(1)]).to match_array [UnsortableObject.new(4), UnsortableObject.new(3)]
|
130
|
-
}.to fail_with(<<-MESSAGE)
|
131
|
-
expected collection contained: [4, 3]
|
132
|
-
actual collection contained: [2, 1]
|
133
|
-
the missing elements were: [4, 3]
|
134
|
-
the extra elements were: [2, 1]
|
135
|
-
MESSAGE
|
136
|
-
end
|
137
|
-
|
138
|
-
it "accurately reports extra elements when there are duplicates" do
|
139
|
-
expect {
|
140
|
-
expect([1,1,1,5]).to match_array [1,5]
|
141
|
-
}.to fail_with(<<-MESSAGE)
|
142
|
-
expected collection contained: [1, 5]
|
143
|
-
actual collection contained: [1, 1, 1, 5]
|
144
|
-
the extra elements were: [1, 1]
|
145
|
-
MESSAGE
|
146
|
-
end
|
147
|
-
|
148
|
-
it "accurately reports missing elements when there are duplicates" do
|
149
|
-
expect {
|
150
|
-
expect([1,5]).to match_array [1,1,5]
|
151
|
-
}.to fail_with(<<-MESSAGE)
|
152
|
-
expected collection contained: [1, 1, 5]
|
153
|
-
actual collection contained: [1, 5]
|
154
|
-
the missing elements were: [1]
|
155
|
-
MESSAGE
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
|
-
describe "expect(...).not_to match_array [:with, :multiple, :args]" do
|
160
|
-
it "is not supported" do
|
161
|
-
expect {
|
162
|
-
expect([1,2,3]).not_to match_array [1,2,3]
|
163
|
-
}.to fail_with(/Matcher does not support should_not/)
|
164
|
-
end
|
165
|
-
end
|
166
|
-
|
167
|
-
describe "matching against things that aren't arrays" do
|
168
|
-
it "fails with nil and the expected error message is given" do
|
169
|
-
expect {
|
170
|
-
expect(nil).to match_array([1,2,3])
|
171
|
-
}.to fail_with(/expected an array/)
|
172
|
-
end
|
173
|
-
|
174
|
-
it "fails with a float and the expected error message is given" do
|
175
|
-
expect {
|
176
|
-
expect((3.7)).to match_array([1,2,3])
|
177
|
-
}.to fail_with(/expected an array/)
|
178
|
-
end
|
179
|
-
|
180
|
-
it "fails with a string and the expected error message is given" do
|
181
|
-
expect {
|
182
|
-
expect("I like turtles").to match_array([1,2,3])
|
183
|
-
}.to fail_with(/expected an array/)
|
184
|
-
end
|
185
|
-
|
186
|
-
context "when using the `should =~` syntax", :uses_should do
|
187
|
-
it 'fails with a clear message when given a hash' do
|
188
|
-
expect {
|
189
|
-
{}.should =~ {}
|
190
|
-
}.to fail_with(/expected an array/)
|
191
|
-
end
|
192
|
-
end
|
193
|
-
end
|
194
|
-
|