rspec-given 2.4.1 → 2.4.2

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.
@@ -3,7 +3,7 @@ module RSpec
3
3
  VERSION_NUMBERS = [
4
4
  VERSION_MAJOR = 2,
5
5
  VERSION_MINOR = 4,
6
- VERSION_BUILD = 1,
6
+ VERSION_BUILD = 2,
7
7
  ]
8
8
  VERSION = VERSION_NUMBERS.join(".")
9
9
  end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Environmental Access" do
4
+ X = 1
5
+ Given(:a) { 2 }
6
+ FauxThen { X + a }
7
+
8
+ Then { block_result.should == 3 }
9
+ Then { ev.eval_string("X").should == "1" }
10
+ Then { ev.eval_string("a").should == "2" }
11
+ Then { ev.eval_string("X+a").should == "3" }
12
+ end
13
+
14
+ module Nested
15
+ X = 1
16
+ describe "Environmental Access with Nested modules" do
17
+ Given(:a) { 2 }
18
+ FauxThen { X + a }
19
+ Then { block_result.should == 3 }
20
+ Then { ev.eval_string("a").should == "2" }
21
+ Then { ev.eval_string("X").should == "1" }
22
+ Then { ev.eval_string("a+X").should == "3" }
23
+ end
24
+ end
25
+
26
+ describe "Evaluator with error object" do
27
+ FauxThen { 1 }
28
+ When(:result) { ev.eval_string("fail 'XYZ'") }
29
+ Then { result.class.should == RSpec::Given::EvalErr }
30
+ Then { result.inspect.should == "RuntimeError: XYZ" }
31
+ end
@@ -0,0 +1,22 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require 'rspec/given'
4
+ require 'rspec/given/fuzzy_shortcuts'
5
+
6
+ describe "Numeric Extensions" do
7
+ use_natural_assertions_if_supported
8
+
9
+ Given(:n) { 10 }
10
+ Given(:about_n) { about(n) }
11
+ Given(:delta_n) { about(n).delta(0.001) }
12
+ Given(:percent_n) { about(n).percent(5) }
13
+ Given(:epsilon_n) {about(n).epsilon(20) }
14
+
15
+ Then { n.±(0.001).exactly_equals?(delta_n) }
16
+ Then { n.‰(5).exactly_equals?(percent_n) }
17
+ Then { n.€(20).exactly_equals?(epsilon_n) }
18
+
19
+ Then { n.±.exactly_equals?(about_n) }
20
+ Then { n.‰.exactly_equals?(about_n) }
21
+ Then { n.€.exactly_equals?(about_n) }
22
+ end
@@ -0,0 +1,22 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require 'rspec/given'
4
+ require 'rspec/given/fuzzy_shortcuts'
5
+
6
+ describe "Numeric Extensions" do
7
+ use_natural_assertions_if_supported
8
+
9
+ Given(:n) { 10 }
10
+ Given(:about_n) { about(n) }
11
+ Given(:delta_n) { about(n).delta(0.001) }
12
+ Given(:percent_n) { about(n).percent(5) }
13
+ Given(:epsilon_n) {about(n).epsilon(20) }
14
+
15
+ Then { n.±(0.001).exactly_equals?(delta_n) }
16
+ Then { n.‰(5).exactly_equals?(percent_n) }
17
+ Then { n.€(20).exactly_equals?(epsilon_n) }
18
+
19
+ Then { n.±.exactly_equals?(about_n) }
20
+ Then { n.‰.exactly_equals?(about_n) }
21
+ Then { n.€.exactly_equals?(about_n) }
22
+ end
@@ -0,0 +1,204 @@
1
+ require 'spec_helper'
2
+
3
+ describe RSpec::Given::ClassExtensions do
4
+ let(:trace) { [] }
5
+
6
+ describe "Given with var" do
7
+ context "with simple given" do
8
+ Given(:a) { 1 }
9
+ Then { a.should == 1 }
10
+ end
11
+
12
+ context "is lazy" do
13
+ Given(:a) { trace << :given; 1 }
14
+ Then { a.should == 1 }
15
+ Then { trace.should == [] }
16
+ Then { a; trace.should == [:given] }
17
+
18
+ context "when nested" do
19
+ Given(:a) { trace << :nested; 2 }
20
+ Then { a.should == 2 }
21
+ Then { trace.should == [] }
22
+ Then { a; trace.should == [:nested] }
23
+ end
24
+ end
25
+ end
26
+
27
+ describe "Given without var" do
28
+ context "is lazy" do
29
+ Given { trace << :given }
30
+ Then { trace.should == [:given] }
31
+
32
+ context "when nested" do
33
+ Given { trace << :nested }
34
+ Then { trace.should == [:given, :nested] }
35
+ end
36
+ end
37
+ end
38
+
39
+ describe "Given!" do
40
+ context "with simple given" do
41
+ Given!(:a) { 1 }
42
+ Then { a.should == 1 }
43
+ end
44
+
45
+ context "is not lazy" do
46
+ Given!(:a) { trace << :given; 1 }
47
+ Then { a.should == 1 }
48
+ Then { trace.should == [:given] }
49
+ Then { a; trace.should == [:given] }
50
+ end
51
+
52
+ context "when preceeded by a Given block" do
53
+ Given { trace << :given }
54
+ Given!(:other) { trace << :given_bang }
55
+ Then { trace.should == [:given, :given_bang] }
56
+ end
57
+ end
58
+
59
+ describe "Given/Given!/before ordering" do
60
+ before { trace << :before_outer }
61
+ Given { trace << :given_outer }
62
+ Given!(:x_outer) { trace << :given_bang_outer }
63
+ before { trace << :before2_outer }
64
+ When { trace << :when_outer }
65
+ When(:result_outer) { trace << :when_result_outer }
66
+
67
+ Then {
68
+ trace.should == [
69
+ :before_outer, :before2_outer,
70
+ :given_outer, :given_bang_outer,
71
+ :when_outer,
72
+ :when_result_outer,
73
+ ]
74
+ }
75
+
76
+ context "with a nested When" do
77
+ before { trace << :before_inner }
78
+ Given { trace << :given_inner }
79
+ Given!(:x_inner) { trace << :given_bang_inner }
80
+ When(:result_inner) { trace << :when_result_inner }
81
+ When { trace << :when_inner }
82
+
83
+ Then {
84
+ trace.should == [
85
+ :before_outer, :before2_outer,
86
+ :given_outer, :given_bang_outer,
87
+ :given_inner, :given_bang_inner,
88
+ :when_outer, :when_result_outer,
89
+ :before_inner,
90
+ :when_result_inner, :when_inner,
91
+ ]
92
+ }
93
+ end
94
+
95
+ context "without a nested When" do
96
+ before { trace << :before_inner }
97
+ Given { trace << :given_inner }
98
+ Given!(:x_inner) { trace << :given_bang_inner }
99
+
100
+ Then {
101
+ trace.should == [
102
+ :before_outer, :before2_outer,
103
+ :given_outer, :given_bang_outer,
104
+ :given_inner, :given_bang_inner,
105
+ :when_outer, :when_result_outer,
106
+ :before_inner,
107
+ ]
108
+ }
109
+ end
110
+ end
111
+
112
+ describe "When without result" do
113
+ Given { trace << :given }
114
+ When { trace << :when }
115
+ Then { trace.should == [:given, :when] }
116
+
117
+ context "with nesting" do
118
+ Given { trace << :nested }
119
+ Then { trace.should == [:given, :nested, :when] }
120
+ end
121
+
122
+ context "with nesting of When" do
123
+ Given { trace << :nested }
124
+ When { trace << :when_nested }
125
+ Then { trace.should == [:given, :nested, :when, :when_nested] }
126
+ end
127
+ end
128
+
129
+ describe "When with result" do
130
+ Given { trace << :given }
131
+ When(:result) { trace << :when; :result }
132
+ Invariant { result.should == :result }
133
+
134
+ Then { trace.should == [:given, :when] }
135
+
136
+ context "with nesting" do
137
+ Given { trace << :nested }
138
+ Then { trace.should == [:given, :nested, :when] }
139
+ end
140
+
141
+ context "with nesting of When" do
142
+ Given { trace << :nested }
143
+ When { trace << :when_nested }
144
+ Then { trace.should == [:given, :nested, :when, :when_nested] }
145
+ end
146
+ end
147
+
148
+ describe "When with unreferenced result" do
149
+ Given { trace << :given }
150
+ When(:result) { trace << :when; :result }
151
+ Then { trace.should == [:given, :when] }
152
+ end
153
+
154
+ describe "Invariant with When" do
155
+ Given { trace << :given }
156
+ Invariant { trace << :invariant }
157
+ When { trace << :when }
158
+ Then { trace.should == [:given, :when, :invariant] }
159
+ end
160
+
161
+ describe "Invariant without When" do
162
+ Given { trace << :given }
163
+ Invariant { trace << :invariant }
164
+ Then { trace.should == [:given, :invariant] }
165
+ end
166
+
167
+ describe "Then" do
168
+ Given { trace << :given }
169
+ Then { trace << :then }
170
+ And { trace.should == [:given, :then] }
171
+ end
172
+
173
+ describe "Then referencing givens" do
174
+ Given(:given_value) { :ok }
175
+ Then { given_value == :ok }
176
+ end
177
+
178
+ describe "Then referencing when results" do
179
+ When(:result) { :ok }
180
+ Then { result == :ok }
181
+ end
182
+
183
+ describe "And" do
184
+ Given { trace << :given }
185
+ Then { trace << :then }
186
+ And { trace << :and}
187
+ And { trace.should == [:given, :then, :and] }
188
+ end
189
+
190
+ end
191
+
192
+ describe "use_natural_assertions" do
193
+ context "when in JRuby" do
194
+ CONTEXT = self
195
+
196
+ When(:result) { CONTEXT.use_natural_assertions }
197
+
198
+ if RSpec::Given::NATURAL_ASSERTIONS_SUPPORTED
199
+ Then { result.should_not have_failed(ArgumentError) }
200
+ else
201
+ Then { result.should have_failed(ArgumentError) }
202
+ end
203
+ end
204
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+ require 'rspec/given/failure'
3
+
4
+ describe RSpec::Given::Failure do
5
+ Given(:exception) { StandardError.new("Oops") }
6
+ Given(:failure) { RSpec::Given::Failure.new(exception) }
7
+
8
+ Then { lambda { failure.to_s }.should raise_error(StandardError, "Oops") }
9
+ Then { lambda { failure.call }.should raise_error(StandardError, "Oops") }
10
+ Then { lambda { failure.nil? }.should raise_error(StandardError, "Oops") }
11
+ Then { lambda { failure == 0 }.should raise_error(StandardError, "Oops") }
12
+ Then { lambda { failure != 0 }.should raise_error(StandardError, "Oops") }
13
+ Then { lambda { failure =~ 0 }.should raise_error(StandardError, "Oops") }
14
+ Then { lambda { ! failure }.should raise_error(StandardError, "Oops") }
15
+
16
+ Then { failure.is_a?(RSpec::Given::Failure).should be_true }
17
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ module RSpec
4
+ module Given
5
+
6
+ DESCRIBE_LINE = __LINE__
7
+ describe FileCache do
8
+ Given(:file_name) { __FILE__ }
9
+ Given(:cache) { FileCache.new }
10
+
11
+ When(:result) { cache.get(file_name) }
12
+
13
+ context "when reading the file" do
14
+ Then { result[DESCRIBE_LINE].should =~ /describe FileCache do/ }
15
+ Then { result.size.should == MAX_LINE }
16
+ end
17
+
18
+ context "when getting the same file twice" do
19
+ Given { cache.should_receive(:read_lines).once.and_return(["A"]) }
20
+ When(:result2) { cache.get(file_name) }
21
+ Then { result.should == ["A"] }
22
+ Then { result2.should == ["A"] }
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ MAX_LINE = __LINE__
@@ -0,0 +1,111 @@
1
+ require 'spec_helper'
2
+
3
+ describe RSpec::Given::Fuzzy::FuzzyNumber do
4
+ use_natural_assertions_if_supported
5
+ include RSpec::Given::Fuzzy
6
+
7
+ describe "attributes" do
8
+ Given(:number) { about(10).delta(0.0001) }
9
+ Then { number.exact_value == 10 }
10
+ Then { number.delta_amount == 0.0001 }
11
+ Then { number.low_limit == (10 - 0.0001) }
12
+ Then { number.high_limit == (10 + 0.0001) }
13
+ end
14
+
15
+ describe "#exactly_equals?" do
16
+ Given(:number) { about(10).delta(0.0001) }
17
+ Given(:same_number) { about(10).delta(0.0001) }
18
+ Given(:different_delta) { about(10).delta(0.001) }
19
+ Given(:different_exact) { about(11).delta(0.0001) }
20
+
21
+ Then { number.exactly_equals?(number) }
22
+ Then { number.exactly_equals?(same_number) }
23
+
24
+ Then { ! number.exactly_equals?(different_exact) }
25
+ Then { ! number.exactly_equals?(different_delta) }
26
+ end
27
+
28
+ describe "fixed deltas" do
29
+ Given(:exact_number) { 10 }
30
+ Given(:number) { about(exact_number).delta(0.001) }
31
+
32
+ Then { exact_number == number }
33
+
34
+ Then { (exact_number + 0.001) == number }
35
+ Then { (exact_number - 0.001) == number }
36
+
37
+ Then { (exact_number + 0.001001) != number }
38
+ Then { (exact_number - 0.001001) != number }
39
+ end
40
+
41
+ describe "negative deltas" do
42
+ Given(:exact_number) { 10 }
43
+ Given(:number) { about(exact_number).delta(-0.001) }
44
+
45
+ Then { exact_number == number }
46
+
47
+ Then { (exact_number + 0.001) == number }
48
+ Then { (exact_number - 0.001) == number }
49
+
50
+ Then { (exact_number + 0.001001) != number }
51
+ Then { (exact_number - 0.001001) != number }
52
+ end
53
+
54
+ describe "percentage deltas" do
55
+ Given(:exact_number) { 1 }
56
+ Given(:number) { about(exact_number).percent(25) }
57
+
58
+ Then { exact_number == number }
59
+
60
+ Then { (exact_number + 0.25) == number }
61
+ Then { (exact_number - 0.25) == number }
62
+
63
+ Then { (exact_number + 0.25001) != number }
64
+ Then { (exact_number - 0.25001) != number }
65
+ end
66
+
67
+ describe "epsilon deltas" do
68
+ Given(:neps) { 10 }
69
+ Given(:hi_in_range) { 1 + neps*Float::EPSILON }
70
+ Given(:lo_in_range) { 1 - neps*Float::EPSILON }
71
+ Given(:hi_out_of_range) { 1 + (neps+1)*Float::EPSILON }
72
+ Given(:lo_out_of_range) { 1 - (neps+1)*Float::EPSILON }
73
+
74
+ Invariant { exact_number*hi_in_range == number }
75
+ Invariant { exact_number*lo_in_range == number }
76
+
77
+ Invariant { exact_number*hi_out_of_range != number }
78
+ Invariant { exact_number*lo_out_of_range != number }
79
+
80
+ context "when created with default delta" do
81
+ Given(:number) { about(exact_number) }
82
+
83
+ context "when 1" do
84
+ Given(:exact_number) { 1 }
85
+ Then { exact_number == number }
86
+ end
87
+
88
+ context "when rather large" do
89
+ Given(:exact_number) { 1_000_000 }
90
+ Then { exact_number == number }
91
+ end
92
+
93
+ context "when rather small" do
94
+ Given(:exact_number) { 0.000_001 }
95
+ Then { exact_number == number }
96
+ end
97
+ end
98
+
99
+ context "when created with small epsilon" do
100
+ Given(:neps) { 100 }
101
+ Given(:exact_number) { 10 }
102
+ Given(:number) { about(exact_number).epsilon(neps) }
103
+ Then { exact_number == number }
104
+ end
105
+ end
106
+
107
+ describe "#to_s" do
108
+ Given(:number) { about(10).delta(0.0001) }
109
+ Then { number.to_s == "<Approximately 10 +/- 0.0001>" }
110
+ end
111
+ end