shared_should 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.2"
12
+ gem "rcov", ">= 0"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.5.2)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.8.7)
10
+ rcov (0.9.9)
11
+ shoulda (2.10.3)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.0.0)
18
+ jeweler (~> 1.5.2)
19
+ rcov
20
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Michael Pearce
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = shared_should
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to shared_should
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Michael Pearce. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
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 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "shared_should"
16
+ gem.homepage = "http://github.com/michaelgpearce/shared_should"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Share and reuse shoulds, contexts, and setup in Shoulda.}
19
+ gem.description = %Q{Share and reuse shoulds, contexts, and setup in Shoulda.}
20
+ gem.email = "michael.pearce@bookrenter.com"
21
+ gem.authors = ["Michael Pearce"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ gem.add_runtime_dependency 'shoulda', '>= 0'
25
+ gem.add_development_dependency 'shoulda', '>= 0'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "shared_should #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,296 @@
1
+ module Shoulda::SharedContext
2
+ # class methods for Test::Unit::TestCase
3
+ def self.extended(klass)
4
+ class << klass
5
+ # these methods need to be aliased for both the test class and the should context
6
+ alias_method :context_without_shared_proxies_executing, :context
7
+ alias_method :should_without_param_support, :should
8
+ alias_method :setup_without_param_support, :setup
9
+ end
10
+
11
+ klass.extend(SharedContextMethods)
12
+ end
13
+
14
+ # instance methods for Shoulda::Context
15
+ def self.included(klass)
16
+ klass.class_eval do
17
+ # these methods need to be aliased for both the test class and the should context
18
+ alias :context_without_shared_proxies_executing :context
19
+ alias :should_without_param_support :should
20
+ alias :setup_without_param_support :setup
21
+
22
+ # remove any methods we are going to define with our included module
23
+ SharedContextMethods.instance_methods.each do |method_name|
24
+ remove_method method_name if method_defined? method_name
25
+ end
26
+ end
27
+
28
+ klass.send(:include, SharedContextMethods)
29
+ end
30
+
31
+ module SharedContextMethods
32
+ def shared_context_blocks
33
+ @shared_context_blocks ||= {}
34
+ end
35
+
36
+ def shared_setup_blocks
37
+ @shared_setup_blocks ||= {}
38
+ end
39
+
40
+ def shared_proxies
41
+ @shared_proxies ||= []
42
+ end
43
+
44
+ def should_be(shared_name)
45
+ shared_proxy = Shoulda::SharedProxy.new(shared_name)
46
+ shared_proxies << shared_proxy
47
+ return shared_proxy
48
+ end
49
+
50
+ def setup_for(shared_name)
51
+ shared_proxy = Shoulda::SharedProxy.new(shared_name)
52
+ shared_setup_block = find_shared_setup_block(shared_name)
53
+ setup do
54
+ if initialization_block = shared_proxy.initialization_block
55
+ setup_shared_values(shared_proxy.when_description, shared_proxy.initialization_block)
56
+ end
57
+ call_block_with_shared_value(shared_setup_block)
58
+ end
59
+ return shared_proxy
60
+ end
61
+
62
+ def context(name = nil, &block)
63
+ if block
64
+ shared_proxies_executing_block = Proc.new do
65
+ block.bind(self).call
66
+
67
+ shared_proxies.each do |shared_proxy|
68
+ shared_proxy.execute(self)
69
+ end
70
+ end
71
+ shared_proxies_executing_block.bind(eval("self", block.binding))
72
+ context_without_shared_proxies_executing(name, &shared_proxies_executing_block)
73
+ end
74
+ end
75
+
76
+ def should(name = nil, options = {}, &block)
77
+ if block.nil?
78
+ should_without_param_support(name, options)
79
+ else
80
+ should_without_param_support(name, options) do
81
+ call_block_with_shared_value(block)
82
+ end
83
+ end
84
+ end
85
+
86
+ def setup(&block)
87
+ if block.nil?
88
+ setup_without_param_support()
89
+ else
90
+ setup_without_param_support() do
91
+ call_block_with_shared_value(block)
92
+ end
93
+ end
94
+ end
95
+
96
+ def shared_context_should(shared_context_name, &shared_context_block)
97
+ shared_context_for(shared_context_name, &shared_context_block)
98
+ end
99
+
100
+ def shared_context_for(shared_context_name, &shared_context_block)
101
+ wrapping_shared_context_block = Proc.new do
102
+ context "for #{shared_context_name}" do
103
+ merge_block(&shared_context_block)
104
+ end
105
+ end
106
+
107
+ do_shared_context(shared_context_name, shared_context_for_block(shared_context_block), &wrapping_shared_context_block)
108
+ end
109
+
110
+ def shared_should(shared_should_name, &shared_should_block)
111
+ shared_should_for(shared_should_name, &shared_should_block)
112
+ end
113
+
114
+ def shared_should_for(shared_should_name, &shared_should_block)
115
+ shared_context_block = Proc.new do
116
+ should "be #{shared_should_name}" do
117
+ call_block_with_shared_value(shared_should_block)
118
+ end
119
+ end
120
+
121
+ do_shared_context(shared_should_name, shared_context_for_block(shared_should_block), &shared_context_block)
122
+ end
123
+
124
+ def shared_setup(shared_name, &setup_block)
125
+ shared_setup_block = Proc.new do
126
+ call_block_with_shared_value(setup_block)
127
+ end
128
+
129
+ do_shared_setup(shared_name, shared_context_for_block(setup_block), &shared_setup_block)
130
+ end
131
+
132
+ def shared_setup_for(shared_name, &setup_block)
133
+ current_context = eval('self', setup_block.binding)
134
+ Test::Unit::TestCase.shared_context_block_owner(current_context).shared_setup_blocks[shared_name] = setup_block
135
+ end
136
+
137
+ def shared_context_blocks
138
+ @shared_context_blocks ||= {}
139
+ end
140
+
141
+ def shared_setup_blocks
142
+ @shared_setup_blocks ||= {}
143
+ end
144
+
145
+ private
146
+
147
+ def shared_context_for_block(shared_block)
148
+ eval("self", shared_block.binding)
149
+ end
150
+
151
+ def do_shared_context(shared_context_name, destination_context, &shared_context_block)
152
+ do_shared_helper(shared_context_name, destination_context, :should, :merge_shared_context, &shared_context_block)
153
+ end
154
+
155
+ def merge_shared_context(shared_context_block, caller_context, name, initialization_block)
156
+ name = '' if name.nil?
157
+
158
+ caller_context.context name do
159
+ setup do
160
+ setup_shared_values(name, initialization_block)
161
+ end
162
+
163
+ merge_block(&shared_context_block)
164
+ end
165
+ end
166
+
167
+ def do_shared_setup(shared_setup_name, destination_context, &shared_setup_block)
168
+ do_shared_helper(shared_setup_name, destination_context, :setup, :merge_shared_setup, &shared_setup_block)
169
+ end
170
+
171
+ def merge_shared_setup(shared_setup_block, caller_context, name, setup_block)
172
+ # note: the binding for the block of the TestCase's setup method is not an instance of the TestCase - its the TestCase class.
173
+ # Handle the TestCase class by chaining it to the setup method.
174
+ if caller_context == self
175
+ @@shared_setup_alias_index = 0 unless defined?(@@shared_setup_alias_index)
176
+ @@shared_setup_alias_index += 1
177
+ with_method = :"setup_with_shared_setup_#{@@shared_setup_alias_index}"
178
+ without_method = :"setup_without_shared_setup_#{@@shared_setup_alias_index}"
179
+ caller_context.send(:alias_method, without_method, :setup)
180
+ caller_context.send(:define_method, with_method) do
181
+ send(without_method)
182
+ setup_shared_values(name, setup_block)
183
+ shared_setup_block.bind(self).call
184
+ end
185
+ caller_context.send(:alias_method, :setup, with_method)
186
+ else
187
+ caller_context.setup do
188
+ setup_shared_values(name, setup_block)
189
+ shared_setup_block.bind(self).call
190
+ end
191
+ end
192
+ end
193
+
194
+ def do_shared_helper(shared_name, destination_context, method_prefix, merge_method, &shared_setup_block)
195
+ method_name = shared_method_name(method_prefix, shared_name)
196
+ Test::Unit::TestCase.shared_context_block_owner(destination_context).shared_context_blocks[method_name] = shared_setup_block
197
+
198
+ # Ruby 1.8 workaround for define_method with a block
199
+ # http://coderrr.wordpress.com/2008/10/29/using-define_method-with-blocks-in-ruby-18/
200
+ eval <<-EOM
201
+ def destination_context.#{method_name}(name = nil, context = self, &setup_block)
202
+ #{merge_method}(Test::Unit::TestCase.shared_context_block_owner(self).shared_context_blocks['#{method_name}'], context, name, block_given? ? setup_block : nil)
203
+ end
204
+ EOM
205
+ end
206
+
207
+ def shared_method_name(method_prefix, context_name)
208
+ "#{method_prefix}_#{context_name.downcase.gsub(' ', '_').gsub(/[^_A-Za-z0-9]/, '')}"
209
+ end
210
+
211
+ private
212
+
213
+ def find_shared_setup_block(shared_name)
214
+ current_context = self
215
+ while current_context.kind_of?(Shoulda::Context) || current_context < Test::Unit::TestCase do
216
+ if shared_setup_block = Test::Unit::TestCase.shared_context_block_owner(current_context).shared_setup_blocks[shared_name]
217
+ return shared_setup_block
218
+ end
219
+ raise "Unable to find shared_setup_for('#{shared_name}')" if current_context.kind_of?(Class)
220
+ current_context = current_context.parent
221
+ end
222
+ raise "Unable to find shared_setup_for('#{shared_name}')"
223
+ end
224
+ end
225
+ end
226
+
227
+ class Shoulda::Context
228
+ 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
+ end
241
+
242
+ class Test::Unit::TestCase
243
+ 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
+ end
268
+
269
+
270
+ class Shoulda::SharedProxy
271
+ attr_accessor :shared_name, :when_description, :initialization_block
272
+
273
+ def initialize(shared_name)
274
+ self.shared_name = shared_name
275
+ end
276
+
277
+ def when(when_description = nil, &initialization_block)
278
+ when_helper("when", when_description, &initialization_block)
279
+ end
280
+
281
+ def with(with_description = nil, &initialization_block)
282
+ when_helper("with", with_description, &initialization_block)
283
+ end
284
+
285
+ def execute(context)
286
+ method_name = context.send(:shared_method_name, :should, shared_name)
287
+ context.send(method_name, when_description, &initialization_block)
288
+ end
289
+
290
+ private
291
+
292
+ def when_helper(when_name, when_description = nil, &initialization_block)
293
+ self.when_description = when_description ? "#{when_name} #{when_description}" : nil
294
+ self.initialization_block = initialization_block
295
+ end
296
+ end
data/test/helper.rb ADDED
@@ -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
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
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"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shared_should
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 0
10
+ version: 0.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Michael Pearce
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-02 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :development
32
+ name: shoulda
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ hash: 23
42
+ segments:
43
+ - 1
44
+ - 0
45
+ - 0
46
+ version: 1.0.0
47
+ type: :development
48
+ name: bundler
49
+ prerelease: false
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ hash: 7
58
+ segments:
59
+ - 1
60
+ - 5
61
+ - 2
62
+ version: 1.5.2
63
+ type: :development
64
+ name: jeweler
65
+ prerelease: false
66
+ version_requirements: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ type: :development
78
+ name: rcov
79
+ prerelease: false
80
+ version_requirements: *id004
81
+ - !ruby/object:Gem::Dependency
82
+ requirement: &id005 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ type: :runtime
92
+ name: shoulda
93
+ prerelease: false
94
+ version_requirements: *id005
95
+ - !ruby/object:Gem::Dependency
96
+ requirement: &id006 !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ hash: 3
102
+ segments:
103
+ - 0
104
+ version: "0"
105
+ type: :development
106
+ name: shoulda
107
+ prerelease: false
108
+ version_requirements: *id006
109
+ description: Share and reuse shoulds, contexts, and setup in Shoulda.
110
+ email: michael.pearce@bookrenter.com
111
+ executables: []
112
+
113
+ extensions: []
114
+
115
+ extra_rdoc_files:
116
+ - LICENSE.txt
117
+ - README.rdoc
118
+ files:
119
+ - .document
120
+ - Gemfile
121
+ - Gemfile.lock
122
+ - LICENSE.txt
123
+ - README.rdoc
124
+ - Rakefile
125
+ - VERSION
126
+ - lib/shared_should.rb
127
+ - test/helper.rb
128
+ - test/test_shared_should.rb
129
+ has_rdoc: true
130
+ homepage: http://github.com/michaelgpearce/shared_should
131
+ licenses:
132
+ - MIT
133
+ post_install_message:
134
+ rdoc_options: []
135
+
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ hash: 3
144
+ segments:
145
+ - 0
146
+ version: "0"
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ none: false
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ hash: 3
153
+ segments:
154
+ - 0
155
+ version: "0"
156
+ requirements: []
157
+
158
+ rubyforge_project:
159
+ rubygems_version: 1.3.7
160
+ signing_key:
161
+ specification_version: 3
162
+ summary: Share and reuse shoulds, contexts, and setup in Shoulda.
163
+ test_files:
164
+ - test/helper.rb
165
+ - test/test_shared_should.rb