rspec-expectations 2.0.0.a1 → 2.0.0.a2

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.
Files changed (48) hide show
  1. data/.document +3 -3
  2. data/.gitignore +1 -0
  3. data/License.txt +2 -2
  4. data/Rakefile +17 -10
  5. data/Upgrade.markdown +38 -0
  6. data/lib/rspec/expectations.rb +1 -0
  7. data/lib/rspec/expectations/extensions.rb +1 -0
  8. data/lib/rspec/expectations/extensions/rspec/core/example_group.rb +19 -0
  9. data/lib/rspec/expectations/version.rb +16 -0
  10. data/lib/rspec/matchers.rb +0 -2
  11. data/lib/rspec/matchers/exist.rb +2 -2
  12. data/lib/rspec/matchers/extensions/instance_exec.rb +27 -19
  13. data/lib/rspec/matchers/include.rb +17 -16
  14. data/lib/rspec/matchers/matcher.rb +71 -25
  15. data/rspec-expectations.gemspec +66 -19
  16. data/spec/rspec/matchers/be_close_spec.rb +50 -0
  17. data/spec/rspec/matchers/be_instance_of_spec.rb +36 -0
  18. data/spec/rspec/matchers/be_kind_of_spec.rb +33 -0
  19. data/spec/rspec/matchers/be_spec.rb +311 -0
  20. data/spec/rspec/matchers/change_spec.rb +349 -0
  21. data/spec/rspec/matchers/compatibility_spec.rb +28 -0
  22. data/spec/rspec/matchers/description_generation_spec.rb +160 -0
  23. data/spec/rspec/matchers/dsl_spec.rb +25 -0
  24. data/spec/rspec/matchers/eql_spec.rb +33 -0
  25. data/spec/rspec/matchers/equal_spec.rb +57 -0
  26. data/spec/rspec/matchers/exist_spec.rb +65 -0
  27. data/spec/rspec/matchers/has_spec.rb +81 -0
  28. data/spec/rspec/matchers/have_spec.rb +407 -0
  29. data/spec/rspec/matchers/include_spec.rb +88 -0
  30. data/spec/rspec/matchers/match_array_spec.rb +108 -0
  31. data/spec/rspec/matchers/match_spec.rb +57 -0
  32. data/spec/rspec/matchers/matcher_methods_spec.rb +63 -0
  33. data/spec/rspec/matchers/matcher_spec.rb +289 -0
  34. data/spec/rspec/matchers/matchers_spec.rb +2 -0
  35. data/spec/rspec/matchers/operator_matcher_spec.rb +191 -0
  36. data/spec/rspec/matchers/raise_error_spec.rb +333 -0
  37. data/spec/rspec/matchers/respond_to_spec.rb +116 -0
  38. data/spec/rspec/matchers/satisfy_spec.rb +36 -0
  39. data/spec/rspec/matchers/throw_symbol_spec.rb +96 -0
  40. data/spec/spec_helper.rb +13 -1
  41. data/spec/support/classes.rb +51 -0
  42. metadata +60 -15
  43. data/VERSION +0 -1
  44. data/VERSION.yml +0 -5
  45. data/lib/rspec/matchers/simple_matcher.rb +0 -133
  46. data/lib/rspec/matchers/wrap_expectation.rb +0 -55
  47. data/spec/rspec/expectations/wrap_expectation_spec.rb +0 -30
  48. data/spec/support/macros.rb +0 -29
@@ -0,0 +1,28 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ Rspec::Matchers.define :have_public_instance_method do |method|
4
+ match do |klass|
5
+ klass.public_instance_methods.any? {|m| [method, method.to_sym].include?(m)}
6
+ end
7
+ end
8
+
9
+ (Rspec::Matchers.constants.sort).each do |c|
10
+ if (Class === (klass = Rspec::Matchers.const_get(c)))
11
+ describe klass do
12
+ if klass.public_instance_methods.any? {|m| ['failure_message_for_should',:failure_message_for_should].include?(m)}
13
+ describe "called with should" do
14
+ subject { klass }
15
+ it { should have_public_instance_method('failure_message_for_should')}
16
+ it { should have_public_instance_method('failure_message')}
17
+ end
18
+ end
19
+ if klass.public_instance_methods.any? {|m| ['failure_message_for_should_not',:failure_message_for_should_not].include?(m)}
20
+ describe "called with should not" do
21
+ subject { klass }
22
+ it { should have_public_instance_method('failure_message_for_should_not')}
23
+ it { should have_public_instance_method('negative_failure_message')}
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,160 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ describe "Matchers should be able to generate their own descriptions" do
4
+ after(:each) do
5
+ Rspec::Matchers.clear_generated_description
6
+ end
7
+
8
+ it "should == expected" do
9
+ "this".should == "this"
10
+ Rspec::Matchers.generated_description.should == "should == \"this\""
11
+ end
12
+
13
+ it "should not == expected" do
14
+ "this".should_not == "that"
15
+ Rspec::Matchers.generated_description.should == "should not == \"that\""
16
+ end
17
+
18
+ it "should be empty (arbitrary predicate)" do
19
+ [].should be_empty
20
+ Rspec::Matchers.generated_description.should == "should be empty"
21
+ end
22
+
23
+ it "should not be empty (arbitrary predicate)" do
24
+ [1].should_not be_empty
25
+ Rspec::Matchers.generated_description.should == "should not be empty"
26
+ end
27
+
28
+ it "should be true" do
29
+ true.should be_true
30
+ Rspec::Matchers.generated_description.should == "should be true"
31
+ end
32
+
33
+ it "should be false" do
34
+ false.should be_false
35
+ Rspec::Matchers.generated_description.should == "should be false"
36
+ end
37
+
38
+ it "should be nil" do
39
+ nil.should be_nil
40
+ Rspec::Matchers.generated_description.should == "should be nil"
41
+ end
42
+
43
+ it "should be > n" do
44
+ 5.should be > 3
45
+ Rspec::Matchers.generated_description.should == "should be > 3"
46
+ end
47
+
48
+ it "should be predicate arg1, arg2 and arg3" do
49
+ 5.0.should be_between(0,10)
50
+ Rspec::Matchers.generated_description.should == "should be between 0 and 10"
51
+ end
52
+
53
+ it "should equal" do
54
+ expected = "expected"
55
+ expected.should equal(expected)
56
+ Rspec::Matchers.generated_description.should == "should equal \"expected\""
57
+ end
58
+
59
+ it "should_not equal" do
60
+ 5.should_not equal(37)
61
+ Rspec::Matchers.generated_description.should == "should not equal 37"
62
+ end
63
+
64
+ it "should eql" do
65
+ "string".should eql("string")
66
+ Rspec::Matchers.generated_description.should == "should eql \"string\""
67
+ end
68
+
69
+ it "should not eql" do
70
+ "a".should_not eql(:a)
71
+ Rspec::Matchers.generated_description.should == "should not eql :a"
72
+ end
73
+
74
+ it "should have_key" do
75
+ {:a => "a"}.should have_key(:a)
76
+ Rspec::Matchers.generated_description.should == "should have key :a"
77
+ end
78
+
79
+ it "should have n items" do
80
+ team.should have(3).players
81
+ Rspec::Matchers.generated_description.should == "should have 3 players"
82
+ end
83
+
84
+ it "should have at least n items" do
85
+ team.should have_at_least(2).players
86
+ Rspec::Matchers.generated_description.should == "should have at least 2 players"
87
+ end
88
+
89
+ it "should have at most n items" do
90
+ team.should have_at_most(4).players
91
+ Rspec::Matchers.generated_description.should == "should have at most 4 players"
92
+ end
93
+
94
+ it "should include" do
95
+ [1,2,3].should include(3)
96
+ Rspec::Matchers.generated_description.should == "should include 3"
97
+ end
98
+
99
+ it "array.should =~ [1,2,3]" do
100
+ [1,2,3].should =~ [1,2,3]
101
+ Rspec::Matchers.generated_description.should == "should contain exactly 1, 2 and 3"
102
+ end
103
+
104
+ it "should match" do
105
+ "this string".should match(/this string/)
106
+ Rspec::Matchers.generated_description.should == "should match /this string/"
107
+ end
108
+
109
+ it "should raise_error" do
110
+ lambda { raise }.should raise_error
111
+ Rspec::Matchers.generated_description.should == "should raise Exception"
112
+ end
113
+
114
+ it "should raise_error with type" do
115
+ lambda { raise }.should raise_error(RuntimeError)
116
+ Rspec::Matchers.generated_description.should == "should raise RuntimeError"
117
+ end
118
+
119
+ it "should raise_error with type and message" do
120
+ lambda { raise "there was an error" }.should raise_error(RuntimeError, "there was an error")
121
+ Rspec::Matchers.generated_description.should == "should raise RuntimeError with \"there was an error\""
122
+ end
123
+
124
+ it "should respond_to" do
125
+ [].should respond_to(:insert)
126
+ Rspec::Matchers.generated_description.should == "should respond to #insert"
127
+ end
128
+
129
+ it "should throw symbol" do
130
+ lambda { throw :what_a_mess }.should throw_symbol
131
+ Rspec::Matchers.generated_description.should == "should throw a Symbol"
132
+ end
133
+
134
+ it "should throw symbol (with named symbol)" do
135
+ lambda { throw :what_a_mess }.should throw_symbol(:what_a_mess)
136
+ Rspec::Matchers.generated_description.should == "should throw :what_a_mess"
137
+ end
138
+
139
+ def team
140
+ Class.new do
141
+ def players
142
+ [1,2,3]
143
+ end
144
+ end.new
145
+ end
146
+ end
147
+
148
+ describe "a Matcher with no description" do
149
+ def matcher
150
+ Class.new do
151
+ def matches?(ignore); true; end
152
+ def failure_message; ""; end
153
+ end.new
154
+ end
155
+
156
+ it "should provide a helpful message when used in a string-less example block" do
157
+ 5.should matcher
158
+ Rspec::Matchers.generated_description.should =~ /When you call.*description method/m
159
+ end
160
+ end
@@ -0,0 +1,25 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ module Rspec
4
+ module Matchers
5
+ module DSL
6
+ describe "#define" do
7
+ it "creates a method that initializes a new matcher with the submitted name and expected arg" do
8
+ # FIXME - this expects new to be called, but we need something
9
+ # more robust - that expects new to be called with a specific
10
+ # block (lambda, proc, whatever)
11
+ mod = Module.new
12
+ mod.extend Rspec::Matchers::DSL
13
+ mod.define(:foo)
14
+
15
+ obj = Object.new
16
+ obj.extend mod
17
+
18
+ Rspec::Matchers::Matcher.should_receive(:new).with(:foo, 3)
19
+
20
+ obj.foo(3)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,33 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ module Rspec
4
+ module Matchers
5
+ describe "eql" do
6
+ it "should match when actual.eql?(expected)" do
7
+ 1.should eql(1)
8
+ end
9
+
10
+ it "should not match when !actual.eql?(expected)" do
11
+ 1.should_not eql(2)
12
+ end
13
+
14
+ it "should describe itself" do
15
+ matcher = eql(1)
16
+ matcher.matches?(1)
17
+ matcher.description.should == "eql 1"
18
+ end
19
+
20
+ it "should provide message, expected and actual on #failure_message" do
21
+ matcher = eql("1")
22
+ matcher.matches?(1)
23
+ matcher.failure_message_for_should.should == "\nexpected \"1\"\n got 1\n\n(compared using eql?)\n"
24
+ end
25
+
26
+ it "should provide message, expected and actual on #negative_failure_message" do
27
+ matcher = eql(1)
28
+ matcher.matches?(1)
29
+ matcher.failure_message_for_should_not.should == "\nexpected 1 not to equal 1\n\n(compared using eql?)\n"
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,57 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+ module Rspec
3
+ module Matchers
4
+ describe "equal" do
5
+
6
+ def inspect_object(o)
7
+ "#<#{o.class}:#{o.object_id}> => #{o.inspect}"
8
+ end
9
+
10
+ it "should match when actual.equal?(expected)" do
11
+ 1.should equal(1)
12
+ end
13
+
14
+ it "should not match when !actual.equal?(expected)" do
15
+ 1.should_not equal("1")
16
+ end
17
+
18
+ it "should describe itself" do
19
+ matcher = equal(1)
20
+ matcher.matches?(1)
21
+ matcher.description.should == "equal 1"
22
+ end
23
+
24
+ it "should provide message on #failure_message" do
25
+ expected, actual = "1", "1"
26
+ matcher = equal(expected)
27
+ matcher.matches?(actual)
28
+
29
+ matcher.failure_message_for_should.should == <<-MESSAGE
30
+
31
+ expected #{inspect_object(expected)}
32
+ got #{inspect_object(actual)}
33
+
34
+ Compared using equal?, which compares object identity,
35
+ but expected and actual are not the same object. Use
36
+ 'actual.should == expected' if you don't care about
37
+ object identity in this example.
38
+
39
+ MESSAGE
40
+ end
41
+
42
+ it "should provide message on #negative_failure_message" do
43
+ expected = actual = "1"
44
+ matcher = equal(expected)
45
+ matcher.matches?(actual)
46
+ matcher.failure_message_for_should_not.should == <<-MESSAGE
47
+
48
+ expected not #{inspect_object(expected)}
49
+ got #{inspect_object(actual)}
50
+
51
+ Compared using equal?, which compares object identity.
52
+
53
+ MESSAGE
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,65 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ class Substance
4
+ def initialize exists, description
5
+ @exists = exists
6
+ @description = description
7
+ end
8
+ def exist?(arg=nil)
9
+ @exists
10
+ end
11
+ def inspect
12
+ @description
13
+ end
14
+ end
15
+
16
+ class SubstanceTester
17
+ include Rspec::Matchers
18
+ def initialize substance
19
+ @substance = substance
20
+ end
21
+ def should_exist
22
+ @substance.should exist
23
+ end
24
+ end
25
+
26
+ describe "should exist" do
27
+
28
+ before(:each) do
29
+ @real = Substance.new true, 'something real'
30
+ @imaginary = Substance.new false, 'something imaginary'
31
+ end
32
+
33
+ describe "within an example group" do
34
+
35
+ it "passes if target exists" do
36
+ @real.should exist
37
+ end
38
+
39
+ it "passes if target exists with args" do
40
+ @real.should exist('this arg')
41
+ end
42
+
43
+ it "fails if target does not exist" do
44
+ lambda { @imaginary.should exist }.should fail
45
+ end
46
+
47
+ it "describes itself" do
48
+ exist.description.should == "exist"
49
+ end
50
+
51
+ it "passes should_not exist if target doesn't exist" do
52
+ lambda { @real.should_not exist }.should fail
53
+ end
54
+ end
55
+
56
+ describe "outside of an example group" do
57
+
58
+ it "should pass if target exists" do
59
+ real_tester = SubstanceTester.new @real
60
+ real_tester.should_exist
61
+ end
62
+
63
+ end
64
+
65
+ end
@@ -0,0 +1,81 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ describe "should have_sym(*args)" do
4
+ it "should pass if #has_sym?(*args) returns true" do
5
+ {:a => "A"}.should have_key(:a)
6
+ end
7
+
8
+ it "should fail if #has_sym?(*args) returns false" do
9
+ lambda {
10
+ {:b => "B"}.should have_key(:a)
11
+ }.should fail_with("expected #has_key?(:a) to return true, got false")
12
+ end
13
+
14
+ it "should fail if #has_sym?(*args) returns nil" do
15
+ klass = Class.new do
16
+ def has_foo?
17
+ end
18
+ end
19
+ lambda {
20
+ klass.new.should have_foo
21
+ }.should fail_with("expected #has_foo?(nil) to return true, got false")
22
+ end
23
+
24
+ it "should fail if target does not respond to #has_sym?" do
25
+ lambda {
26
+ Object.new.should have_key(:a)
27
+ }.should raise_error(NoMethodError)
28
+ end
29
+
30
+ it "should reraise an exception thrown in #has_sym?(*args)" do
31
+ o = Object.new
32
+ def o.has_sym?(*args)
33
+ raise "Funky exception"
34
+ end
35
+ lambda { o.should have_sym(:foo) }.should raise_error("Funky exception")
36
+ end
37
+ end
38
+
39
+ describe "should_not have_sym(*args)" do
40
+ it "should pass if #has_sym?(*args) returns false" do
41
+ {:a => "A"}.should_not have_key(:b)
42
+ end
43
+
44
+ it "should pass if #has_sym?(*args) returns nil" do
45
+ klass = Class.new do
46
+ def has_foo?
47
+ end
48
+ end
49
+ klass.new.should_not have_foo
50
+ end
51
+
52
+ it "should fail if #has_sym?(*args) returns true" do
53
+ lambda {
54
+ {:a => "A"}.should_not have_key(:a)
55
+ }.should fail_with("expected #has_key?(:a) to return false, got true")
56
+ end
57
+
58
+ it "should fail if target does not respond to #has_sym?" do
59
+ lambda {
60
+ Object.new.should have_key(:a)
61
+ }.should raise_error(NoMethodError)
62
+ end
63
+
64
+ it "should reraise an exception thrown in #has_sym?(*args)" do
65
+ o = Object.new
66
+ def o.has_sym?(*args)
67
+ raise "Funky exception"
68
+ end
69
+ lambda { o.should_not have_sym(:foo) }.should raise_error("Funky exception")
70
+ end
71
+ end
72
+
73
+ describe "has" do
74
+ it "should work when the target implements #send" do
75
+ o = {:a => "A"}
76
+ def o.send(*args); raise "DOH! Library developers shouldn't use #send!" end
77
+ lambda {
78
+ o.should have_key(:a)
79
+ }.should_not raise_error
80
+ end
81
+ end