shoulda-context 1.0.0.beta1

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,63 @@
1
+ require 'test/unit'
2
+
3
+ class ConvertToShouldSyntaxTest < Test::Unit::TestCase # :nodoc:
4
+
5
+ BEFORE_FIXTURE = <<-EOS
6
+ class DummyTest < Test::Unit::TestCase
7
+
8
+ should "Not change this_word_with_underscores" do
9
+ end
10
+
11
+ def test_should_be_working
12
+ assert true
13
+ end
14
+
15
+ def test_some_cool_stuff
16
+ assert true
17
+ end
18
+
19
+ def non_test_method
20
+ end
21
+
22
+ end
23
+ EOS
24
+
25
+ AFTER_FIXTURE = <<-EOS
26
+ class DummyTest < Test::Unit::TestCase
27
+
28
+ should "Not change this_word_with_underscores" do
29
+ end
30
+
31
+ should "be working" do
32
+ assert true
33
+ end
34
+
35
+ should "RENAME ME: test some cool stuff" do
36
+ assert true
37
+ end
38
+
39
+ def non_test_method
40
+ end
41
+
42
+ end
43
+ EOS
44
+
45
+ FIXTURE_PATH = "./convert_to_should_syntax_fixture.dat"
46
+
47
+ RUBY = ENV['RUBY'] || 'ruby'
48
+
49
+ def test_convert_to_should_syntax
50
+ File.open(FIXTURE_PATH, "w") {|f| f.write(BEFORE_FIXTURE)}
51
+ cmd = "#{RUBY} #{File.join(File.dirname(__FILE__), '../../bin/convert_to_should_syntax')} #{FIXTURE_PATH}"
52
+ output = `#{cmd}`
53
+ File.unlink($1) if output.match(/has been stored in '([^']+)/)
54
+ assert_match(/has been converted/, output)
55
+ result = IO.read(FIXTURE_PATH)
56
+ assert_equal result, AFTER_FIXTURE
57
+ end
58
+
59
+ def teardown
60
+ File.unlink(FIXTURE_PATH)
61
+ end
62
+
63
+ end
@@ -0,0 +1,124 @@
1
+ require 'test_helper'
2
+
3
+ class HelpersTest < Test::Unit::TestCase # :nodoc:
4
+
5
+ context "an array of values" do
6
+ setup do
7
+ @a = ['abc', 'def', 3]
8
+ end
9
+
10
+ [/b/, 'abc', 3].each do |x|
11
+ should "contain #{x.inspect}" do
12
+ assert_raises(Test::Unit::AssertionFailedError) do
13
+ assert_does_not_contain @a, x
14
+ end
15
+ assert_contains @a, x
16
+ end
17
+ end
18
+
19
+ should "not contain 'wtf'" do
20
+ assert_raises(Test::Unit::AssertionFailedError) {assert_contains @a, 'wtf'}
21
+ assert_does_not_contain @a, 'wtf'
22
+ end
23
+
24
+ should "be the same as another array, ordered differently" do
25
+ assert_same_elements(@a, [3, "def", "abc"])
26
+ assert_raises(Test::Unit::AssertionFailedError) do
27
+ assert_same_elements(@a, [3, 3, "def", "abc"])
28
+ end
29
+ assert_raises(Test::Unit::AssertionFailedError) do
30
+ assert_same_elements([@a, "abc"].flatten, [3, 3, "def", "abc"])
31
+ end
32
+ end
33
+ end
34
+
35
+ context "a matching matcher" do
36
+ setup do
37
+ @matcher = stub('matcher', :matches? => true,
38
+ :failure_message => 'bad failure message',
39
+ :negative_failure_message => 'big time failure')
40
+ end
41
+
42
+ should "pass when given to assert_accepts with no message expectation" do
43
+ assert_accepts @matcher, 'target'
44
+ end
45
+
46
+ should "pass when given to assert_accepts with a matching message" do
47
+ assert_accepts @matcher, 'target', :message => /big time/
48
+ end
49
+
50
+ should "fail when given to assert_accepts with non-matching message" do
51
+ assert_raise Test::Unit::AssertionFailedError do
52
+ assert_accepts @matcher, 'target', :message => /small time/
53
+ end
54
+ end
55
+
56
+ context "when given to assert_rejects" do
57
+ setup do
58
+ begin
59
+ assert_rejects @matcher, 'target'
60
+ rescue Test::Unit::AssertionFailedError => @error
61
+ end
62
+ end
63
+
64
+ should "fail" do
65
+ assert_not_nil @error
66
+ end
67
+
68
+ should "use the error message from the matcher" do
69
+ assert_equal 'big time failure', @error.message
70
+ end
71
+ end
72
+ end
73
+
74
+ context "a non-matching matcher" do
75
+ setup do
76
+ @matcher = stub('matcher', :matches? => false,
77
+ :failure_message => 'big time failure',
78
+ :negative_failure_message => 'bad failure message')
79
+ end
80
+
81
+ should "pass when given to assert_rejects with no message expectation" do
82
+ assert_rejects @matcher, 'target'
83
+ end
84
+
85
+ should "pass when given to assert_rejects with a matching message" do
86
+ assert_rejects @matcher, 'target', :message => /big time/
87
+ end
88
+
89
+ should "fail when given to assert_rejects with a non-matching message" do
90
+ assert_raise Test::Unit::AssertionFailedError do
91
+ assert_rejects @matcher, 'target', :message => /small time/
92
+ end
93
+ end
94
+
95
+ context "when given to assert_accepts" do
96
+ setup do
97
+ begin
98
+ assert_accepts @matcher, 'target'
99
+ rescue Test::Unit::AssertionFailedError => @error
100
+ end
101
+ end
102
+
103
+ should "fail" do
104
+ assert_not_nil @error
105
+ end
106
+
107
+ should "use the error message from the matcher" do
108
+ assert_equal 'big time failure', @error.message
109
+ end
110
+ end
111
+ end
112
+
113
+ should "assign context to a support matching on assert_accepts" do
114
+ matcher = stub('matcher', :matches? => true)
115
+ matcher.expects(:in_context).with(self)
116
+ assert_accepts matcher, nil
117
+ end
118
+
119
+ should "assign context to a support matching on assert_rejects" do
120
+ matcher = stub('matcher', :matches? => false)
121
+ matcher.expects(:in_context).with(self)
122
+ assert_rejects matcher, nil
123
+ end
124
+ end
@@ -0,0 +1,271 @@
1
+ require 'test_helper'
2
+
3
+ class ShouldTest < Test::Unit::TestCase # :nodoc:
4
+ should "be able to define a should statement outside of a context" do
5
+ assert true
6
+ end
7
+
8
+ should "see the name of my class as ShouldTest" do
9
+ assert_equal "ShouldTest", self.class.name
10
+ end
11
+
12
+ def self.should_see_class_methods
13
+ should "be able to see class methods" do
14
+ assert true
15
+ end
16
+ end
17
+
18
+ def self.should_be_able_to_setup_a_should_eventually_in_a_class_method
19
+ should "be able to setup a should eventually in a class method"
20
+ end
21
+
22
+ def self.should_see_a_context_block_like_a_Test_Unit_class
23
+ should "see a context block as a Test::Unit class" do
24
+ assert_equal "ShouldTest", self.class.name
25
+ end
26
+ end
27
+
28
+ def self.should_see_blah
29
+ should "see @blah through a macro" do
30
+ assert @blah
31
+ end
32
+ end
33
+
34
+ def self.should_not_see_blah
35
+ should "not see @blah through a macro" do
36
+ assert_nil @blah
37
+ end
38
+ end
39
+
40
+ def self.should_be_able_to_make_context_macros(prefix = nil)
41
+ context "a macro" do
42
+ should "have the tests named correctly" do
43
+ assert_match(/^test: #{prefix}a macro should have the tests named correctly/, self.to_s)
44
+ end
45
+ end
46
+ end
47
+
48
+ context "Context" do
49
+
50
+ should_see_class_methods
51
+ should_see_a_context_block_like_a_Test_Unit_class
52
+ should_be_able_to_make_context_macros("Context ")
53
+ should_be_able_to_setup_a_should_eventually_in_a_class_method
54
+
55
+ should "not define @blah" do
56
+ assert ! self.instance_variables.include?("@blah")
57
+ end
58
+
59
+ should_not_see_blah
60
+
61
+ should "be able to define a should statement" do
62
+ assert true
63
+ end
64
+
65
+ should "see the name of my class as ShouldTest" do
66
+ assert_equal "ShouldTest", self.class.name
67
+ end
68
+
69
+ context "with a subcontext" do
70
+ should_be_able_to_make_context_macros("Context with a subcontext ")
71
+ end
72
+ end
73
+
74
+ context "Context with setup block" do
75
+ setup do
76
+ @blah = "blah"
77
+ end
78
+
79
+ should "have @blah == 'blah'" do
80
+ assert_equal "blah", @blah
81
+ end
82
+ should_see_blah
83
+
84
+ should "have name set right" do
85
+ assert_match(/^test: Context with setup block/, self.to_s)
86
+ end
87
+
88
+ context "and a subcontext" do
89
+ setup do
90
+ @blah = "#{@blah} twice"
91
+ end
92
+
93
+ should "be named correctly" do
94
+ assert_match(/^test: Context with setup block and a subcontext should be named correctly/, self.to_s)
95
+ end
96
+
97
+ should "run the setup methods in order" do
98
+ assert_equal @blah, "blah twice"
99
+ end
100
+ should_see_blah
101
+ end
102
+ end
103
+
104
+ context "Another context with setup block" do
105
+ setup do
106
+ @blah = "foo"
107
+ end
108
+
109
+ should "have @blah == 'foo'" do
110
+ assert_equal "foo", @blah
111
+ end
112
+
113
+ should "have name set right" do
114
+ assert_match(/^test: Another context with setup block/, self.to_s)
115
+ end
116
+ should_see_blah
117
+ end
118
+
119
+ should_eventually "pass, since it's a should_eventually" do
120
+ flunk "what?"
121
+ end
122
+
123
+ # Context creation and naming
124
+
125
+ def test_should_create_a_new_context
126
+ assert_nothing_raised do
127
+ Shoulda::Context::Context.new("context name", self) do; end
128
+ end
129
+ end
130
+
131
+ def test_should_create_a_nested_context
132
+ assert_nothing_raised do
133
+ parent = Shoulda::Context::Context.new("Parent", self) do; end
134
+ child = Shoulda::Context::Context.new("Child", parent) do; end
135
+ end
136
+ end
137
+
138
+ def test_should_name_a_contexts_correctly
139
+ parent = Shoulda::Context::Context.new("Parent", self) do; end
140
+ child = Shoulda::Context::Context.new("Child", parent) do; end
141
+ grandchild = Shoulda::Context::Context.new("GrandChild", child) do; end
142
+
143
+ assert_equal "Parent", parent.full_name
144
+ assert_equal "Parent Child", child.full_name
145
+ assert_equal "Parent Child GrandChild", grandchild.full_name
146
+ end
147
+
148
+ # Should statements
149
+
150
+ def test_should_have_should_hashes_when_given_should_statements
151
+ context = Shoulda::Context::Context.new("name", self) do
152
+ should "be good" do; end
153
+ should "another" do; end
154
+ end
155
+
156
+ names = context.shoulds.map {|s| s[:name]}
157
+ assert_equal ["another", "be good"], names.sort
158
+ end
159
+
160
+ # setup and teardown
161
+
162
+ def test_should_capture_setup_and_teardown_blocks
163
+ context = Shoulda::Context::Context.new("name", self) do
164
+ setup do; "setup"; end
165
+ teardown do; "teardown"; end
166
+ end
167
+
168
+ assert_equal "setup", context.setup_blocks.first.call
169
+ assert_equal "teardown", context.teardown_blocks.first.call
170
+ end
171
+
172
+ # building
173
+
174
+ def test_should_create_shoulda_test_for_each_should_on_build
175
+ context = Shoulda::Context::Context.new("name", self) do
176
+ should "one" do; end
177
+ should "two" do; end
178
+ end
179
+ context.expects(:create_test_from_should_hash).with(has_entry(:name => "one"))
180
+ context.expects(:create_test_from_should_hash).with(has_entry(:name => "two"))
181
+ context.build
182
+ end
183
+
184
+ def test_should_create_test_methods_on_build
185
+ tu_class = Test::Unit::TestCase
186
+ context = Shoulda::Context::Context.new("A Context", tu_class) do
187
+ should "define the test" do; end
188
+ end
189
+
190
+ tu_class.expects(:define_method).with(:"test: A Context should define the test. ")
191
+ context.build
192
+ end
193
+
194
+ def test_should_create_test_methods_on_build_when_subcontext
195
+ tu_class = Test::Unit::TestCase
196
+ context = Shoulda::Context::Context.new("A Context", tu_class) do
197
+ context "with a child" do
198
+ should "define the test" do; end
199
+ end
200
+ end
201
+
202
+ tu_class.expects(:define_method).with(:"test: A Context with a child should define the test. ")
203
+ context.build
204
+ end
205
+
206
+ # Test::Unit integration
207
+
208
+ def test_should_create_a_new_context_and_build_it_on_Test_Unit_context
209
+ c = mock("context")
210
+ c.expects(:build)
211
+ Shoulda::Context::Context.expects(:new).with("foo", kind_of(Class)).returns(c)
212
+ self.class.context "foo" do; end
213
+ end
214
+
215
+ def test_should_create_a_one_off_context_and_build_it_on_Test_Unit_should
216
+ s = mock("test")
217
+ Shoulda::Context::Context.any_instance.expects(:should).with("rock", {}).returns(s)
218
+ Shoulda::Context::Context.any_instance.expects(:build)
219
+ self.class.should "rock" do; end
220
+ end
221
+
222
+ def test_should_create_a_one_off_context_and_build_it_on_Test_Unit_should_eventually
223
+ s = mock("test")
224
+ Shoulda::Context::Context.any_instance.expects(:should_eventually).with("rock").returns(s)
225
+ Shoulda::Context::Context.any_instance.expects(:build)
226
+ self.class.should_eventually "rock" do; end
227
+ end
228
+
229
+ should "run a :before proc", :before => lambda { @value = "before" } do
230
+ assert_equal "before", @value
231
+ end
232
+
233
+ context "A :before proc" do
234
+ setup do
235
+ assert_equal "before", @value
236
+ @value = "setup"
237
+ end
238
+
239
+ should "run before the current setup", :before => lambda { @value = "before" } do
240
+ assert_equal "setup", @value
241
+ end
242
+ end
243
+
244
+ context "a before statement" do
245
+ setup do
246
+ assert_equal "before", @value
247
+ @value = "setup"
248
+ end
249
+
250
+ before_should "run before the current setup" do
251
+ @value = "before"
252
+ end
253
+ end
254
+
255
+ context "A context" do
256
+ setup do
257
+ @value = "outer"
258
+ end
259
+
260
+ context "with a subcontext and a :before proc" do
261
+ before = lambda do
262
+ assert "outer", @value
263
+ @value = "before"
264
+ end
265
+ should "run after the parent setup", :before => before do
266
+ assert_equal "before", @value
267
+ end
268
+ end
269
+ end
270
+
271
+ end