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.
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#have_failed" do
4
+ CustomError = Class.new(StandardError)
5
+ DifferentError = Class.new(StandardError)
6
+ ExpectationError = RSpec::Expectations::ExpectationNotMetError
7
+
8
+ context "with a failure" do
9
+ When(:result) { fail CustomError, "Ouch" }
10
+
11
+ Then { result.should raise_error(CustomError, "Ouch") }
12
+ Then { result.should have_failed(CustomError, "Ouch") }
13
+ Then { result.should have_raised(CustomError, "Ouch") }
14
+
15
+ Then { expect { result.should be_nil }.to raise_error(CustomError, "Ouch") }
16
+ Then { expect { result.should == 0 }.to raise_error(CustomError, "Ouch") }
17
+ Then { expect { result.should_not == 0 }.to raise_error(CustomError, "Ouch") }
18
+
19
+ Then { expect { result.should_not have_failed }.to raise_error(ExpectationError) }
20
+ end
21
+
22
+ context "with a standard failure" do
23
+ When(:result) { fail "Ouch" }
24
+
25
+ Then { result.should raise_error(StandardError, "Ouch") }
26
+ Then { result.should raise_error(StandardError, /^O/) }
27
+ Then { result.should raise_error(StandardError) }
28
+ Then { result.should raise_error }
29
+
30
+ Then { result.should have_failed(StandardError, "Ouch") }
31
+ Then { result.should have_failed(StandardError, /^O/) }
32
+ Then { result.should have_failed(StandardError) }
33
+ Then { result.should have_failed }
34
+ end
35
+
36
+ context "with a different failure" do
37
+ When(:result) { fail CustomError, "Ouch" }
38
+ Then { result.should_not have_failed(DifferentError) }
39
+ Then { expect { result.should have_failed(DifferentError) }.to raise_error(ExpectationError) }
40
+ end
41
+
42
+ context "with a pending exception" do
43
+ When(:result) { fail RSpec::Core::Pending::PendingDeclaredInExample, "Required pending in example ... please ignore" }
44
+ Then { RSpec::Given.fail_with "This example should have been pending" }
45
+ end
46
+
47
+ context "with a non-failure" do
48
+ When(:result) { :ok }
49
+ Then { result.should_not have_failed }
50
+ Then { expect { result.should have_failed }.to raise_error(RSpec::Expectations::ExpectationNotMetError) }
51
+ end
52
+
53
+ context "with natural assertions" do
54
+ use_natural_assertions_if_supported
55
+
56
+ context "with failure" do
57
+ When(:result) { fail CustomError, "Ouch" }
58
+ Then { result == have_failed(CustomError, "Ouch") }
59
+ Then { ! (result != have_failed(CustomError, "Ouch")) }
60
+ Then { expect { result == :something }.to raise_error(CustomError, "Ouch") }
61
+ end
62
+
63
+ context "with different failure" do
64
+ When(:result) { fail DifferentError, "Ouch" }
65
+ Then { ! (result == have_failed(CustomError, "Ouch")) }
66
+ Then { result != have_failed(CustomError, "Ouch") }
67
+ end
68
+
69
+ end
70
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ LEXICAL_PURITY_GLOBAL_CONSTANT = 3
4
+
5
+ describe "Lexical Purity" do
6
+ use_natural_assertions_if_supported
7
+
8
+ A = 1
9
+ Given(:avalue) { 1 }
10
+ context "nested" do
11
+ B = 2
12
+ Given(:bvalue) { 2 }
13
+ Then { A == 1 }
14
+ Then { avalue == 1 }
15
+ Then { B == 2 }
16
+ Then { bvalue == 2 }
17
+ Then { LEXICAL_PURITY_GLOBAL_CONSTANT == 3 }
18
+ end
19
+ end
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+
3
+ module RSpec
4
+ module Given
5
+
6
+ describe LineExtractor do
7
+
8
+ class FauxFileCache
9
+ def initialize(lines)
10
+ @lines = lines.split(/\n/).map { |ln| ln + "\n" }
11
+ end
12
+ def get(file_name)
13
+ @lines
14
+ end
15
+ end
16
+
17
+ Given(:line) { 2 }
18
+ Given(:file_cache) { FauxFileCache.new(input) }
19
+ Given(:extractor) { LineExtractor.new(file_cache) }
20
+
21
+ When(:result) { extractor.line("FILENAME", line) }
22
+
23
+ describe "reading a line" do
24
+ Given(:input) {
25
+ " Now is the time\n" +
26
+ " for all good men\n" +
27
+ " to come to the aid\n" +
28
+ " of their fellowmen\n"
29
+ }
30
+ Given(:expected_line) { " for all good men\n" }
31
+ Then { result.should == expected_line }
32
+ end
33
+
34
+ context "when the line doesn't exist" do
35
+ Given(:input) { "" }
36
+ Then { result.should be_nil }
37
+ end
38
+
39
+ context "when the line has leading and trailing white space" do
40
+ Given(:input) {
41
+ " Then { y } \n" +
42
+ " Then { x }\n"
43
+ }
44
+ Then { result.should == " Then { x }\n" }
45
+ end
46
+
47
+ context "when the Then is split over several lines with {}" do
48
+ Given(:input) {
49
+ "describe 'foobar' do\n" +
50
+ " Then {\n" +
51
+ " x\n" +
52
+ " }\n" +
53
+ "end\n"
54
+ }
55
+ Then { result.should == " Then {\n x\n }\n" }
56
+ end
57
+
58
+ context "when the Then is has blank lines" do
59
+ Given(:input) {
60
+ "describe 'foobar' do\n" +
61
+ " Then {\n\n" +
62
+ " x\n" +
63
+ " }\n" +
64
+ "end\n"
65
+ }
66
+ Then { result.should == " Then {\n\n x\n }\n" }
67
+ end
68
+
69
+ context "when the Then is split over several lines with do/end" do
70
+ Given(:input) {
71
+ "describe 'foobar' do\n" +
72
+ " Then do\n" +
73
+ " x\n" +
74
+ " end\n" +
75
+ "end\n"
76
+ }
77
+ Then { result.should == " Then do\n x\n end\n" }
78
+ end
79
+
80
+ describe "converting to a string" do
81
+ Given(:input) { "" }
82
+ Then { extractor.to_s.should =~ /line *extractor/i }
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe "RSpec::Given.use_natural_assertions" do
4
+ context "when in JRuby" do
5
+ When(:result) { RSpec::Given.use_natural_assertions }
6
+
7
+ if RSpec::Given::NATURAL_ASSERTIONS_SUPPORTED
8
+ Then { result.should_not have_failed(ArgumentError) }
9
+ else
10
+ Then { result.should have_failed(ArgumentError) }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,178 @@
1
+ require 'rspec/given'
2
+ require 'spec_helper'
3
+
4
+ describe RSpec::Given::NaturalAssertion do
5
+ before do
6
+ pending "Natural Assertions disabled for JRuby" unless RSpec::Given::NATURAL_ASSERTIONS_SUPPORTED
7
+ end
8
+
9
+ describe "#content?" do
10
+ context "with empty block" do
11
+ FauxThen { }
12
+ Then { na.should_not have_content }
13
+ end
14
+ context "with block returning false" do
15
+ FauxThen { false }
16
+ Then { na.should have_content }
17
+ end
18
+ end
19
+
20
+ describe "detecting RSpec Assertions" do
21
+ context "with should" do
22
+ FauxThen { a.should == 1 }
23
+ Then { na.should be_using_rspec_assertion }
24
+ end
25
+
26
+ context "with should_not" do
27
+ FauxThen { a.should_not == 1 }
28
+ Then { na.should be_using_rspec_assertion }
29
+ end
30
+
31
+ context "with expect/to" do
32
+ FauxThen { expect(a).to eq(1) }
33
+ Then { na.should be_using_rspec_assertion }
34
+ end
35
+
36
+ context "with expect/not_to" do
37
+ FauxThen { expect(a).not_to eq(1) }
38
+ Then { na.should be_using_rspec_assertion }
39
+ end
40
+
41
+ context "with expect and block" do
42
+ FauxThen { expect { a }.to eq(1) }
43
+ Then { na.should be_using_rspec_assertion }
44
+ end
45
+
46
+ context "with natural assertion" do
47
+ FauxThen { a == 1 }
48
+ Then { na.should_not be_using_rspec_assertion }
49
+ end
50
+ end
51
+
52
+ describe "failure messages" do
53
+ let(:msg) { na.message }
54
+ Invariant { msg.should =~ /^FauxThen expression/ }
55
+
56
+ context "with equals assertion" do
57
+ Given(:a) { 1 }
58
+ FauxThen { a == 2 }
59
+ Then { msg.should =~ /\bexpected: +1\b/ }
60
+ Then { msg.should =~ /\bto equal: +2\b/ }
61
+ Then { msg.should =~ /\bfalse +<- +a == 2\b/ }
62
+ Then { msg.should =~ /\b1 +<- +a\b/ }
63
+ end
64
+
65
+ context "with equals assertion with do/end" do
66
+ Given(:a) { 1 }
67
+ FauxThen do a == 2 end
68
+ Then { msg.should =~ /\bexpected: +1\b/ }
69
+ Then { msg.should =~ /\bto equal: +2\b/ }
70
+ Then { msg.should =~ /\bfalse +<- +a == 2\b/ }
71
+ Then { msg.should =~ /\b1 +<- +a\b/ }
72
+ end
73
+
74
+ context "with not-equals assertion" do
75
+ Given(:a) { 1 }
76
+ FauxThen { a != 1 }
77
+ Then { msg.should =~ /\bexpected: +1\b/ }
78
+ Then { msg.should =~ /\bto not equal: +1\b/ }
79
+ Then { msg.should =~ /\bfalse +<- +a != 1\b/ }
80
+ Then { msg.should =~ /\b1 +<- +a\b/ }
81
+ end
82
+
83
+ context "with less than assertion" do
84
+ Given(:a) { 1 }
85
+ FauxThen { a < 1 }
86
+ Then { msg.should =~ /\bexpected: +1\b/ }
87
+ Then { msg.should =~ /\bto be less than: +1\b/ }
88
+ Then { msg.should =~ /\bfalse +<- +a < 1\b/ }
89
+ Then { msg.should =~ /\b1 +<- +a\b/ }
90
+ end
91
+
92
+ context "with less than or equal to assertion" do
93
+ Given(:a) { 1 }
94
+ FauxThen { a <= 0 }
95
+ Then { msg.should =~ /\bexpected: +1\b/ }
96
+ Then { msg.should =~ /\bto be less or equal to: +0\b/ }
97
+ Then { msg.should =~ /\bfalse +<- +a <= 0\b/ }
98
+ Then { msg.should =~ /\b1 +<- +a\b/ }
99
+ end
100
+
101
+ context "with greater than assertion" do
102
+ Given(:a) { 1 }
103
+ FauxThen { a > 1 }
104
+ Then { msg.should =~ /\bexpected: +1\b/ }
105
+ Then { msg.should =~ /\bto be greater than: +1\b/ }
106
+ Then { msg.should =~ /\bfalse +<- +a > 1\b/ }
107
+ Then { msg.should =~ /\b1 +<- +a\b/ }
108
+ end
109
+
110
+ context "with greater than or equal to assertion" do
111
+ Given(:a) { 1 }
112
+ FauxThen { a >= 3 }
113
+ Then { msg.should =~ /\bexpected: +1\b/ }
114
+ Then { msg.should =~ /\bto be greater or equal to: +3\b/ }
115
+ Then { msg.should =~ /\bfalse +<- +a >= 3\b/ }
116
+ Then { msg.should =~ /\b1 +<- +a\b/ }
117
+ end
118
+
119
+ context "with match assertion" do
120
+ Given(:s) { "Hello" }
121
+ FauxThen { s =~ /HI/ }
122
+ Then { msg.should =~ /\bexpected: +"Hello"$/ }
123
+ Then { msg.should =~ /\bto match: +\/HI\/$/ }
124
+ Then { msg.should =~ /\bnil +<- +s =~ \/HI\/$/ }
125
+ Then { msg.should =~ /"Hello" +<- +s$/ }
126
+ end
127
+
128
+ context "with not match assertion" do
129
+ Given(:s) { "Hello" }
130
+ FauxThen { s !~ /Hello/ }
131
+ Then { msg.should =~ /\bexpected: +"Hello"$/ }
132
+ Then { msg.should =~ /\bto not match: +\/Hello\/$/ }
133
+ Then { msg.should =~ /\bfalse +<- +s !~ \/Hello\/$/ }
134
+ Then { msg.should =~ /"Hello" +<- +s$/ }
135
+ end
136
+
137
+ context "with exception" do
138
+ Given(:ary) { nil }
139
+ FauxThen { ary[1] == 3 }
140
+ Then { msg.should =~ /\bexpected: +NoMethodError/ }
141
+ Then { msg.should =~ /\bto equal: +3$/ }
142
+ Then { msg.should =~ /\bNoMethodError.+NilClass\n +<- +ary\[1\] == 3$/ }
143
+ Then { msg.should =~ /\bNoMethodError.+NilClass\n +<- +ary\[1\]$/ }
144
+ Then { msg.should =~ /\bnil +<- +ary$/ }
145
+ end
146
+
147
+ context "with value with newlines" do
148
+ class FunkyInspect
149
+ def inspect
150
+ "XXXX\nYYYY"
151
+ end
152
+ end
153
+ Given(:zzzz) { FunkyInspect.new }
154
+ FauxThen { zzzz == nil }
155
+ Then { msg.should =~ /\n XXXX\n/ }
156
+ Then { msg.should =~ /\n YYYY\n/ }
157
+ Then { msg.should =~ /\n +<- zzzz$/ }
158
+ end
159
+ end
160
+
161
+ describe "bad Then blocks" do
162
+ context "with no statements" do
163
+ FauxThen { }
164
+ When(:result) { na.message }
165
+ Then { result.should_not have_failed(RSpec::Given::InvalidThenError) }
166
+ end
167
+
168
+ context "with multiple statements" do
169
+ FauxThen {
170
+ ary = nil
171
+ ary[1] == 3
172
+ }
173
+ When(:result) { na.message }
174
+ Then { result.should have_failed(RSpec::Given::InvalidThenError, /multiple.*statements/i) }
175
+ end
176
+
177
+ end
178
+ end
@@ -0,0 +1,139 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Configuration Options" do
4
+ let(:trace) { [] }
5
+
6
+ describe "inherited options" do
7
+ context "Outer" do
8
+ _rgc_context_info[:name] = "Outer"
9
+ _rgc_context_info[:outer] = true
10
+
11
+ Then { _rg_info(:name).should == "Outer" }
12
+ Then { _rg_info(:outer).should == true }
13
+ Then { _rg_info(:inner).should == nil }
14
+
15
+ context "Inner" do
16
+ _rgc_context_info[:name] = "Inner"
17
+ _rgc_context_info[:inner] = true
18
+
19
+ Then { _rg_info(:name).should == "Inner" }
20
+ Then { _rg_info(:outer).should == true }
21
+ Then { _rg_info(:inner).should == true }
22
+ end
23
+ end
24
+ end
25
+
26
+ describe "Global natural assertion configuration" do
27
+ unless RSpec::Given::NATURAL_ASSERTIONS_SUPPORTED
28
+ before do
29
+ pending "Natural assertions are not supported in JRuby"
30
+ end
31
+ end
32
+
33
+ before do
34
+ RSpec::Given.use_natural_assertions false
35
+ end
36
+
37
+ Given(:rspec) { false }
38
+ Given(:content) { true }
39
+ Given(:nassert) { stub(:using_rspec_assertion? => rspec, :has_content? => content) }
40
+
41
+ after do
42
+ RSpec::Given.use_natural_assertions false
43
+ end
44
+
45
+ context "with no explicit word on natural assertions" do
46
+ Then { _rg_need_na_message?(nassert).should be_false }
47
+
48
+ context "overridden locally" do
49
+ use_natural_assertions_if_supported
50
+ Then { _rg_need_na_message?(nassert).should be_true }
51
+ end
52
+ end
53
+
54
+ context "with global configuration enabled" do
55
+ When { RSpec::Given.use_natural_assertions }
56
+ Then { _rg_need_na_message?(nassert).should be_true }
57
+
58
+ context "overridden locally" do
59
+ use_natural_assertions false
60
+ Then { _rg_need_na_message?(nassert).should be_false }
61
+ end
62
+
63
+ context "with rspec assertion" do
64
+ Given(:rspec) { true }
65
+ Then {
66
+ if RSpec::Given::MONKEY
67
+ # If RSpec was successfully patched to record matchers,
68
+ # then the "need NA" logic will ignore possible matches in
69
+ # the source code.
70
+ _rg_need_na_message?(nassert).should be_true
71
+ else
72
+ # If RSpec was not successfully patched to record
73
+ # matchers, then the "need NA" logic will check for
74
+ # should/expect in the source.
75
+ _rg_need_na_message?(nassert).should be_false
76
+ end
77
+ }
78
+ end
79
+
80
+ context "without rspec assertion" do
81
+ Given(:rspec) { false }
82
+ Then { _rg_need_na_message?(nassert).should be_true }
83
+ end
84
+
85
+ context "without rspec assertion and no content" do
86
+ Given(:rspec) { false }
87
+ Given(:content) { false }
88
+ Then { _rg_need_na_message?(nassert).should be_false }
89
+ end
90
+ end
91
+
92
+ context "with global configuration set to always" do
93
+ When { RSpec::Given.use_natural_assertions :always }
94
+ Then { _rg_need_na_message?(nassert).should be_true }
95
+
96
+ context "overridden locally" do
97
+ use_natural_assertions false
98
+ Then { _rg_need_na_message?(nassert).should be_false }
99
+ end
100
+
101
+ context "with rspec assertion" do
102
+ Given(:rspec) { true }
103
+ Then { _rg_need_na_message?(nassert).should be_true }
104
+ end
105
+
106
+ context "without rspec assertion" do
107
+ Given(:rspec) { false }
108
+ Then { _rg_need_na_message?(nassert).should be_true }
109
+ end
110
+
111
+ context "without rspec assertion and no content" do
112
+ Given(:rspec) { false }
113
+ Given(:content) { false }
114
+ Then { _rg_need_na_message?(nassert).should be_false }
115
+ end
116
+ end
117
+
118
+ context "with global configuration disabled" do
119
+ When { RSpec::Given.use_natural_assertions false }
120
+ Then { _rg_need_na_message?(nassert).should be_false }
121
+
122
+ context "overridden locally" do
123
+ use_natural_assertions_if_supported(true)
124
+ Then { _rg_need_na_message?(nassert).should be_true }
125
+ end
126
+
127
+ context "with rspec assertion" do
128
+ Given(:rspec) { true }
129
+ Then { _rg_need_na_message?(nassert).should be_false }
130
+ end
131
+
132
+ context "without rspec assertion" do
133
+ Given(:rspec) { false }
134
+ Then { _rg_need_na_message?(nassert).should be_false }
135
+ end
136
+ end
137
+
138
+ end
139
+ end