shared_should 0.0.0 → 0.5.0

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.5.0
data/lib/shared_should.rb CHANGED
@@ -1,3 +1,75 @@
1
+ require 'shoulda'
2
+
3
+ class Shoulda::Context
4
+ def method_missing(method, *args, &blk)
5
+ current_context = self
6
+ while current_context.kind_of?(Shoulda::Context) || current_context < Test::Unit::TestCase do
7
+ if Test::Unit::TestCase.shared_context_block_owner(current_context).shared_context_blocks[method.to_s]
8
+ current_context.send(method, args[0], self, &blk)
9
+ return
10
+ end
11
+ current_context = current_context.parent
12
+ end
13
+ end
14
+ end
15
+
16
+ class Test::Unit::TestCase
17
+ attr_accessor :shared_value
18
+ attr_accessor :shared_name
19
+ @@shared_proxies_executed = false
20
+ @@setup_blocks = []
21
+
22
+ class << self
23
+ # these methods need to be aliased for both the test class and the should context
24
+ alias_method :suite_without_shared_should_execute, :suite
25
+ end
26
+
27
+ def self.suite
28
+ unless @@shared_proxies_executed
29
+ shared_proxies.each do |shared_proxy|
30
+ shared_proxy.execute(self)
31
+ end
32
+ @@shared_proxies_executed = true
33
+ end
34
+
35
+ suite_without_shared_should_execute
36
+ end
37
+
38
+ def self.shared_context_block_owner(context_or_test_class)
39
+ return context_or_test_class.kind_of?(Shoulda::Context) ? context_or_test_class : Test::Unit::TestCase
40
+ end
41
+
42
+ def setup
43
+ @@setup_blocks.each do |setup_block|
44
+ setup_block.bind(self).call
45
+ end
46
+ end
47
+
48
+ def self.setup(&setup_block)
49
+ @@setup_blocks << setup_block
50
+ end
51
+
52
+ def self.parent
53
+ nil
54
+ end
55
+
56
+ def setup_shared_values(name, initialization_block)
57
+ self.shared_value = initialization_block.nil? ? nil : initialization_block.bind(self).call
58
+ self.shared_name = name
59
+ end
60
+
61
+ def call_block_with_shared_value(test_block)
62
+ if test_block.arity == 1
63
+ # check arity of 1 before checking if value is an array. If one parameter, never treat the shared_value as variable args
64
+ test_block.bind(self).call(self.shared_value)
65
+ elsif self.shared_value.class == Array && test_block.arity == self.shared_value.length
66
+ test_block.bind(self).call(*self.shared_value)
67
+ else
68
+ test_block.bind(self).call()
69
+ end
70
+ end
71
+ end
72
+
1
73
  module Shoulda::SharedContext
2
74
  # class methods for Test::Unit::TestCase
3
75
  def self.extended(klass)
@@ -226,44 +298,10 @@ end
226
298
 
227
299
  class Shoulda::Context
228
300
  include Shoulda::SharedContext
229
-
230
- def method_missing(method, *args, &blk)
231
- current_context = self
232
- while current_context.kind_of?(Shoulda::Context) || current_context < Test::Unit::TestCase do
233
- if Test::Unit::TestCase.shared_context_block_owner(current_context).shared_context_blocks[method.to_s]
234
- current_context.send(method, args[0], self, &blk)
235
- return
236
- end
237
- current_context = current_context.parent
238
- end
239
- end
240
301
  end
241
302
 
242
303
  class Test::Unit::TestCase
243
304
  extend Shoulda::SharedContext
244
-
245
- attr_accessor :shared_value
246
- attr_accessor :shared_name
247
-
248
- def self.shared_context_block_owner(context_or_test_class)
249
- return context_or_test_class.kind_of?(Shoulda::Context) ? context_or_test_class : Test::Unit::TestCase
250
- end
251
-
252
- def setup_shared_values(name, initialization_block)
253
- self.shared_value = initialization_block.nil? ? nil : initialization_block.bind(self).call
254
- self.shared_name = name
255
- end
256
-
257
- def call_block_with_shared_value(test_block)
258
- if test_block.arity == 1
259
- # check arity of 1 before checking if value is an array. If one parameter, never treat the shared_value as variable args
260
- test_block.bind(self).call(self.shared_value)
261
- elsif self.shared_value.class == Array && test_block.arity == self.shared_value.length
262
- test_block.bind(self).call(*self.shared_value)
263
- else
264
- test_block.bind(self).call()
265
- end
266
- end
267
305
  end
268
306
 
269
307
 
@@ -0,0 +1,74 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{shared_should}
8
+ s.version = "0.5.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Michael Pearce"]
12
+ s.date = %q{2011-02-03}
13
+ s.description = %q{Share and reuse shoulds, contexts, and setup in Shoulda.}
14
+ s.email = %q{michael.pearce@bookrenter.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/shared_should.rb",
28
+ "shared_should.gemspec",
29
+ "test/helper.rb",
30
+ "test/test_helper.rb",
31
+ "test/test_shared_should.rb",
32
+ "test/test_shared_should_helper.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/michaelgpearce/shared_should}
35
+ s.licenses = ["MIT"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.7}
38
+ s.summary = %q{Share and reuse shoulds, contexts, and setup in Shoulda.}
39
+ s.test_files = [
40
+ "test/helper.rb",
41
+ "test/test_helper.rb",
42
+ "test/test_shared_should.rb",
43
+ "test/test_shared_should_helper.rb"
44
+ ]
45
+
46
+ if s.respond_to? :specification_version then
47
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
+ s.specification_version = 3
49
+
50
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
51
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
52
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
53
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
54
+ s.add_development_dependency(%q<rcov>, [">= 0"])
55
+ s.add_runtime_dependency(%q<shoulda>, [">= 0"])
56
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
57
+ else
58
+ s.add_dependency(%q<shoulda>, [">= 0"])
59
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
60
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
61
+ s.add_dependency(%q<rcov>, [">= 0"])
62
+ s.add_dependency(%q<shoulda>, [">= 0"])
63
+ s.add_dependency(%q<shoulda>, [">= 0"])
64
+ end
65
+ else
66
+ s.add_dependency(%q<shoulda>, [">= 0"])
67
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
68
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
69
+ s.add_dependency(%q<rcov>, [">= 0"])
70
+ s.add_dependency(%q<shoulda>, [">= 0"])
71
+ s.add_dependency(%q<shoulda>, [">= 0"])
72
+ end
73
+ end
74
+
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'shared_should'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -1,7 +1,406 @@
1
1
  require 'helper'
2
2
 
3
3
  class TestSharedShould < Test::Unit::TestCase
4
- should "probably rename this file and start testing for real" do
5
- flunk "hey buddy, you should probably rename this file and start testing for real"
4
+ def setup
5
+ super
6
+ @setup_instance_method_executed = true
7
+ end
8
+
9
+ should "execute setup instance method" do
10
+ assert @setup_instance_method_executed
11
+ end
12
+
13
+ context ".shared_context_for" do
14
+ context "without params" do
15
+ shared_context_for "a valid value" do
16
+ setup do
17
+ @context_value = true
18
+ end
19
+
20
+ should "have true value" do
21
+ assert @value
22
+ end
23
+
24
+ should "call setup in shared context" do
25
+ assert @context_value
26
+ end
27
+ end
28
+
29
+ context "with value in setup" do
30
+ setup do
31
+ @value = true
32
+ end
33
+
34
+ should_be("a valid value")
35
+ end
36
+
37
+ context "with value in initializer" do
38
+ should_be("a valid value").when("a true value") { @value = true }
39
+ end
40
+ end
41
+
42
+ context "with params" do
43
+ setup do
44
+ @value = true
45
+ end
46
+
47
+ shared_context_for "a valid specified value" do
48
+ setup do |value|
49
+ @expected_value = value
50
+ @context_value = true
51
+ end
52
+
53
+ should "have specified value" do |value|
54
+ assert_equal value, @value
55
+ end
56
+
57
+ should "setup @expected_value" do |value|
58
+ assert_equal value, @expected_value
59
+ end
60
+
61
+ should "call setup in shared context" do
62
+ assert @context_value
63
+ end
64
+ end
65
+
66
+ should_be("a valid specified value").when("true") { true }
67
+ end
68
+ end
69
+
70
+ context ".shared_context_should" do
71
+ context "without params" do
72
+ setup do
73
+ @value = true
74
+ end
75
+
76
+ shared_context_should "be valid" do
77
+ setup do
78
+ @context_value = true
79
+ end
80
+
81
+ should "have true value" do
82
+ assert @value
83
+ end
84
+
85
+ should "call setup in shared context" do
86
+ assert @context_value
87
+ end
88
+ end
89
+
90
+ should_be_valid
91
+ end
92
+
93
+ context "with params" do
94
+ setup do
95
+ @value = true
96
+ end
97
+
98
+ shared_context_should "be valid for specified value" do
99
+ setup do |value|
100
+ @expected_value = value
101
+ @context_value = true
102
+ end
103
+
104
+ should "have specified value" do |value|
105
+ assert_equal value, @value
106
+ end
107
+
108
+ should "setup @expected_value" do |value|
109
+ assert_equal value, @expected_value
110
+ end
111
+
112
+ should "call setup in shared context" do
113
+ assert @context_value
114
+ end
115
+ end
116
+
117
+ should_be_valid_for_specified_value { true }
118
+ end
119
+ end
120
+
121
+ context ".shared_should_for" do
122
+ context "without params" do
123
+ shared_should_for "a true value" do
124
+ assert @value
125
+ end
126
+
127
+ context "with value in setup" do
128
+ setup do
129
+ @value = true
130
+ end
131
+
132
+ should_be("a true value")
133
+ end
134
+
135
+ context "when value in initializer" do
136
+ should_be("a true value").when("value is true") { @value = true }
137
+ end
138
+
139
+ context "with value in initializer" do
140
+ should_be("a true value").with("true value") { @value = true }
141
+ end
142
+ end
143
+
144
+ context "with params" do
145
+ setup do
146
+ @value = true
147
+ end
148
+
149
+ shared_should_for "a valid specified value" do |value|
150
+ assert_equal value, @value
151
+ end
152
+
153
+ should_be("a valid specified value").when("true") { true }
154
+ end
155
+ end
156
+
157
+ context ".shared_should" do
158
+ context "without params" do
159
+ setup do
160
+ @value = true
161
+ end
162
+
163
+ shared_should "have true value" do
164
+ assert @value
165
+ end
166
+
167
+ should_have_true_value
168
+ end
169
+
170
+ context "with params" do
171
+ setup do
172
+ @value = true
173
+ end
174
+
175
+ shared_should "have specified value" do |value|
176
+ assert_equal value, @value
177
+ end
178
+
179
+ should_have_specified_value { true }
180
+ end
181
+ end
182
+
183
+ context ".shared_setup" do
184
+ context "without params" do
185
+ shared_setup "for value" do
186
+ @value = true
187
+ end
188
+
189
+ context "with shared setup value" do
190
+ setup do
191
+ @value = false
192
+ end
193
+
194
+ setup_for_value
195
+
196
+ should "have a true value from shared setup" do
197
+ assert @value
198
+ end
199
+ end
200
+ end
201
+
202
+ context "with params" do
203
+ shared_setup "for value" do |value|
204
+ @value = value
205
+ end
206
+
207
+ context "with shared setup value" do
208
+ setup do
209
+ @value = false
210
+ end
211
+
212
+ setup_for_value("with true") { true }
213
+
214
+ should "have a true value from shared setup" do
215
+ assert @value
216
+ end
217
+ end
218
+ end
219
+ end
220
+
221
+ context ".shared_setup_for" do
222
+ context "without params" do
223
+ context "without initialization block" do
224
+ setup do
225
+ # value that will be overwritten
226
+ @value = false
227
+ end
228
+
229
+ shared_setup_for "true value" do
230
+ @value = true
231
+ end
232
+
233
+ setup_for("true value")
234
+
235
+ should "have a true value from shared setup" do
236
+ assert @value
237
+ end
238
+ end
239
+
240
+ context "with initialization block" do
241
+ setup do
242
+ # value that will be overwritten
243
+ @value = false
244
+ end
245
+
246
+ shared_setup_for "value" do
247
+ @value = @initialization_value
248
+ end
249
+
250
+ setup_for("value").when("initialization value is true") { @initialization_value = true }
251
+
252
+ should "have a true value from shared setup" do
253
+ assert @value
254
+ end
255
+ end
256
+ end
257
+
258
+ context "with parameterized initialization block" do
259
+ shared_setup_for "value" do |value|
260
+ @value = value
261
+ end
262
+
263
+ context "with shared setup value" do
264
+ setup do
265
+ # value that will be overwritten
266
+ @value = false
267
+ end
268
+
269
+ setup_for("value").when("initialization value is true") { true }
270
+
271
+ should "have a true value from shared setup" do
272
+ assert @value
273
+ end
274
+ end
275
+ end
276
+ end
277
+
278
+ context "parameterized block" do
279
+ shared_context_should "be valid with shared context" do
280
+ setup do
281
+ assert [1, 2, 3], shared_value
282
+ end
283
+
284
+ setup do |value|
285
+ assert [1, 2, 3], value
286
+ end
287
+
288
+ setup do |first, second, third|
289
+ assert 1, first
290
+ assert 2, second
291
+ assert 3, third
292
+ end
293
+
294
+ should "do something with shared_value" do
295
+ assert [1, 2, 3], shared_value
296
+ end
297
+
298
+ should "do something with value block param" do |value|
299
+ assert [1, 2, 3], value
300
+ end
301
+
302
+ should "do something with value block params" do |first, second, third|
303
+ assert 1, first
304
+ assert 2, second
305
+ assert 3, third
306
+ end
307
+ end
308
+
309
+ shared_should "be valid with shared should" do |first, second, third|
310
+ assert 1, first
311
+ assert 2, second
312
+ assert 3, third
313
+ end
314
+
315
+ should_be_valid_with_shared_context("with an array") { ['1', '2', '3'] }
316
+
317
+ should_be_valid_with_shared_should("with an array") { ['1', '2', '3'] }
318
+ end
319
+
320
+ context "context directly under test class" do
321
+ shared_setup_for("a true value") do
322
+ @value = true
323
+ end
324
+
325
+ shared_should_for("a valid should test") do
326
+ assert @value
327
+ end
328
+
329
+ shared_context_for("a valid context test") do
330
+ should "have a true value" do
331
+ assert @value
332
+ end
333
+ end
334
+
335
+ setup_for("a true value")
336
+
337
+ should_be("a valid should test")
338
+
339
+ should_be("a valid context test")
340
+ end
341
+
342
+ # test class as context
343
+ shared_setup_for("a true value in class") do
344
+ @class_value = true
345
+ end
346
+
347
+ shared_should_for("a valid should test in class") do
348
+ assert @class_value
349
+ end
350
+
351
+ shared_context_for("a valid context test in class") do
352
+ should "have a true value" do
353
+ assert @class_value
354
+ end
355
+ end
356
+
357
+ setup_for("a true value in class")
358
+
359
+ should_be("a valid should test in class")
360
+
361
+ should_be("a valid context test in class")
362
+
363
+
364
+ # ensure test methods are created
365
+ test_method_names = suite.tests.inject({}) do |test_method_names, test_case|
366
+ test_method_names[test_case.method_name] = true
367
+ test_method_names
368
+ end
369
+ [
370
+ "test: .shared_context_for with params when true for a valid specified value should call setup in shared context. ",
371
+ "test: .shared_context_for with params when true for a valid specified value should call setup in shared context. ",
372
+ "test: .shared_context_for with params when true for a valid specified value should have specified value. ",
373
+ "test: .shared_context_for with params when true for a valid specified value should setup @expected_value. ",
374
+ "test: .shared_context_for without params with value in initializer when a true value for a valid value should call setup in shared context. ",
375
+ "test: .shared_context_for without params with value in initializer when a true value for a valid value should have true value. ",
376
+ "test: .shared_context_for without params with value in setup for a valid value should call setup in shared context. ",
377
+ "test: .shared_context_for without params with value in setup for a valid value should have true value. ",
378
+ "test: .shared_context_should with params for be valid for specified value should call setup in shared context. ",
379
+ "test: .shared_context_should with params for be valid for specified value should have specified value. ",
380
+ "test: .shared_context_should with params for be valid for specified value should setup @expected_value. ",
381
+ "test: .shared_context_should without params for be valid should call setup in shared context. ",
382
+ "test: .shared_context_should without params for be valid should have true value. ",
383
+ "test: .shared_setup with params with shared setup value should have a true value from shared setup. ",
384
+ "test: .shared_setup without params with shared setup value should have a true value from shared setup. ",
385
+ "test: .shared_setup_for with parameterized initialization block with shared setup value should have a true value from shared setup. ",
386
+ "test: .shared_setup_for without params with initialization block should have a true value from shared setup. ",
387
+ "test: .shared_setup_for without params without initialization block should have a true value from shared setup. ",
388
+ "test: .shared_should with params should be have specified value. ",
389
+ "test: .shared_should without params should be have true value. ",
390
+ "test: .shared_should_for with params when true should be a valid specified value. ",
391
+ "test: .shared_should_for without params when value in initializer when value is true should be a true value. ",
392
+ "test: .shared_should_for without params with value in initializer with true value should be a true value. ",
393
+ "test: .shared_should_for without params with value in setup should be a true value. ",
394
+ "test: context directly under test class for a valid context test should have a true value. ",
395
+ "test: context directly under test class should be a valid should test. ",
396
+ "test: parameterized block with an array for be valid with shared context should do something with shared_value. ",
397
+ "test: parameterized block with an array for be valid with shared context should do something with value block param. ",
398
+ "test: parameterized block with an array for be valid with shared context should do something with value block params. ",
399
+ "test: parameterized block with an array should be be valid with shared should. ",
400
+ "test: should be a valid should test in class. ",
401
+ "test: for a valid context test in class should have a true value. ",
402
+ "test: SharedShould should execute setup instance method. "
403
+ ].each do |method_name|
404
+ raise "Test method not found: '#{method_name}'" unless test_method_names.include?(method_name)
6
405
  end
7
406
  end
@@ -0,0 +1,79 @@
1
+ require 'helper'
2
+
3
+ # re-open ActiveSupport::TestCase class for some shares
4
+ class SubclassTestCase < Test::Unit::TestCase
5
+ shared_should "have true value for shared should helper" do
6
+ assert @value
7
+ end
8
+
9
+ shared_should_for "a true value for shared should helper" do
10
+ assert @value
11
+ end
12
+
13
+ should "have a test" do
14
+ assert true
15
+ end
16
+ end
17
+
18
+ # re-open Test::Unit::TestCase class for some shares
19
+ class Test::Unit::TestCase
20
+ shared_context_should "be valid for shared context helper" do
21
+ should "be true value" do
22
+ assert @value
23
+ end
24
+ end
25
+
26
+ shared_context_for "a true value for shared context helper" do
27
+ should "be true value" do
28
+ assert @value
29
+ end
30
+ end
31
+
32
+ shared_setup "for shared setup helper" do
33
+ @value = true
34
+ end
35
+
36
+ shared_setup_for "a true value for shared setup helper" do
37
+ @value = true
38
+ end
39
+ end
40
+
41
+ class TestSharedShouldHelper < SubclassTestCase
42
+ context "with helper module" do
43
+ context "with shared should helper" do
44
+ setup do
45
+ @value = true
46
+ end
47
+
48
+ should_have_true_value_for_shared_should_helper
49
+
50
+ should_be("a true value for shared should helper")
51
+ end
52
+
53
+ context "with shared context helper" do
54
+ setup do
55
+ @value = true
56
+ end
57
+
58
+ should_be_valid_for_shared_context_helper
59
+
60
+ should_be("a true value for shared context helper")
61
+ end
62
+
63
+ context "with shared_setup helper" do
64
+ setup_for_shared_setup_helper
65
+
66
+ should "be true value" do
67
+ assert @value
68
+ end
69
+ end
70
+
71
+ context "with shared_setup_for helper" do
72
+ setup_for("a true value for shared setup helper")
73
+
74
+ should "be true value" do
75
+ assert @value
76
+ end
77
+ end
78
+ end
79
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shared_should
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
+ - 5
8
9
  - 0
9
- - 0
10
- version: 0.0.0
10
+ version: 0.5.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Pearce
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-02 00:00:00 -08:00
18
+ date: 2011-02-03 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -124,8 +124,11 @@ files:
124
124
  - Rakefile
125
125
  - VERSION
126
126
  - lib/shared_should.rb
127
+ - shared_should.gemspec
127
128
  - test/helper.rb
129
+ - test/test_helper.rb
128
130
  - test/test_shared_should.rb
131
+ - test/test_shared_should_helper.rb
129
132
  has_rdoc: true
130
133
  homepage: http://github.com/michaelgpearce/shared_should
131
134
  licenses:
@@ -162,4 +165,6 @@ specification_version: 3
162
165
  summary: Share and reuse shoulds, contexts, and setup in Shoulda.
163
166
  test_files:
164
167
  - test/helper.rb
168
+ - test/test_helper.rb
165
169
  - test/test_shared_should.rb
170
+ - test/test_shared_should_helper.rb