jcnetdev-shoulda 4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. data/CONTRIBUTION_GUIDELINES.rdoc +12 -0
  2. data/MIT-LICENSE +22 -0
  3. data/README.rdoc +123 -0
  4. data/Rakefile +32 -0
  5. data/bin/convert_to_should_syntax +40 -0
  6. data/init.rb +1 -0
  7. data/lib/shoulda.rb +43 -0
  8. data/lib/shoulda/active_record_helpers.rb +670 -0
  9. data/lib/shoulda/color.rb +77 -0
  10. data/lib/shoulda/controller_tests/controller_tests.rb +467 -0
  11. data/lib/shoulda/controller_tests/formats/html.rb +201 -0
  12. data/lib/shoulda/controller_tests/formats/xml.rb +170 -0
  13. data/lib/shoulda/gem/proc_extensions.rb +14 -0
  14. data/lib/shoulda/gem/shoulda.rb +246 -0
  15. data/lib/shoulda/general.rb +118 -0
  16. data/lib/shoulda/private_helpers.rb +22 -0
  17. data/rails/init.rb +1 -0
  18. data/shoulda.gemspec +109 -0
  19. data/tasks/list_tests.rake +23 -0
  20. data/tasks/yaml_to_shoulda.rake +28 -0
  21. data/test/README +36 -0
  22. data/test/fixtures/addresses.yml +3 -0
  23. data/test/fixtures/posts.yml +5 -0
  24. data/test/fixtures/taggings.yml +0 -0
  25. data/test/fixtures/tags.yml +9 -0
  26. data/test/fixtures/users.yml +6 -0
  27. data/test/functional/posts_controller_test.rb +43 -0
  28. data/test/functional/users_controller_test.rb +36 -0
  29. data/test/other/context_test.rb +115 -0
  30. data/test/other/helpers_test.rb +80 -0
  31. data/test/other/private_helpers_test.rb +26 -0
  32. data/test/rails_root/app/controllers/application.rb +25 -0
  33. data/test/rails_root/app/controllers/posts_controller.rb +78 -0
  34. data/test/rails_root/app/controllers/users_controller.rb +81 -0
  35. data/test/rails_root/app/helpers/application_helper.rb +3 -0
  36. data/test/rails_root/app/helpers/posts_helper.rb +2 -0
  37. data/test/rails_root/app/helpers/users_helper.rb +2 -0
  38. data/test/rails_root/app/models/address.rb +4 -0
  39. data/test/rails_root/app/models/dog.rb +4 -0
  40. data/test/rails_root/app/models/flea.rb +3 -0
  41. data/test/rails_root/app/models/post.rb +11 -0
  42. data/test/rails_root/app/models/tag.rb +8 -0
  43. data/test/rails_root/app/models/tagging.rb +4 -0
  44. data/test/rails_root/app/models/user.rb +17 -0
  45. data/test/rails_root/app/views/layouts/posts.rhtml +17 -0
  46. data/test/rails_root/app/views/layouts/users.rhtml +17 -0
  47. data/test/rails_root/app/views/posts/edit.rhtml +27 -0
  48. data/test/rails_root/app/views/posts/index.rhtml +25 -0
  49. data/test/rails_root/app/views/posts/new.rhtml +26 -0
  50. data/test/rails_root/app/views/posts/show.rhtml +18 -0
  51. data/test/rails_root/app/views/users/edit.rhtml +22 -0
  52. data/test/rails_root/app/views/users/index.rhtml +22 -0
  53. data/test/rails_root/app/views/users/new.rhtml +21 -0
  54. data/test/rails_root/app/views/users/show.rhtml +13 -0
  55. data/test/rails_root/config/boot.rb +109 -0
  56. data/test/rails_root/config/database.yml +4 -0
  57. data/test/rails_root/config/environment.rb +18 -0
  58. data/test/rails_root/config/environments/sqlite3.rb +0 -0
  59. data/test/rails_root/config/initializers/new_rails_defaults.rb +15 -0
  60. data/test/rails_root/config/routes.rb +6 -0
  61. data/test/rails_root/db/migrate/001_create_users.rb +17 -0
  62. data/test/rails_root/db/migrate/002_create_posts.rb +13 -0
  63. data/test/rails_root/db/migrate/003_create_taggings.rb +12 -0
  64. data/test/rails_root/db/migrate/004_create_tags.rb +11 -0
  65. data/test/rails_root/db/migrate/005_create_dogs.rb +11 -0
  66. data/test/rails_root/db/migrate/006_create_addresses.rb +13 -0
  67. data/test/rails_root/db/migrate/007_create_fleas.rb +11 -0
  68. data/test/rails_root/db/migrate/008_create_dogs_fleas.rb +12 -0
  69. data/test/rails_root/db/migrate/009_add_ssn_to_users.rb +9 -0
  70. data/test/rails_root/db/schema.rb +0 -0
  71. data/test/rails_root/log/.keep +0 -0
  72. data/test/rails_root/public/.htaccess +40 -0
  73. data/test/rails_root/public/404.html +30 -0
  74. data/test/rails_root/public/422.html +30 -0
  75. data/test/rails_root/public/500.html +30 -0
  76. data/test/rails_root/script/console +3 -0
  77. data/test/rails_root/script/generate +3 -0
  78. data/test/rails_root/vendor/plugins/.keep +0 -0
  79. data/test/test_helper.rb +35 -0
  80. data/test/unit/address_test.rb +7 -0
  81. data/test/unit/dog_test.rb +7 -0
  82. data/test/unit/flea_test.rb +7 -0
  83. data/test/unit/post_test.rb +14 -0
  84. data/test/unit/tag_test.rb +12 -0
  85. data/test/unit/tagging_test.rb +8 -0
  86. data/test/unit/user_test.rb +32 -0
  87. metadata +148 -0
@@ -0,0 +1,201 @@
1
+ module ThoughtBot # :nodoc:
2
+ module Shoulda # :nodoc:
3
+ module Controller # :nodoc:
4
+ module HTML # :nodoc: all
5
+ def self.included(other)
6
+ other.class_eval do
7
+ extend ThoughtBot::Shoulda::Controller::HTML::ClassMethods
8
+ end
9
+ end
10
+
11
+ module ClassMethods
12
+ def controller_name_from_class
13
+ self.name.gsub(/Test/, '')
14
+ end
15
+
16
+ def make_show_html_tests(res)
17
+ context "on GET to #{controller_name_from_class}#show" do
18
+ setup do
19
+ record = get_existing_record(res)
20
+ parent_params = make_parent_params(res, record)
21
+ get :show, parent_params.merge({ res.identifier => record.to_param })
22
+ end
23
+
24
+ if res.denied.actions.include?(:show)
25
+ should_not_assign_to res.object
26
+ should_redirect_to res.denied.redirect
27
+ should_set_the_flash_to res.denied.flash
28
+ else
29
+ should_assign_to res.object
30
+ should_respond_with :success
31
+ should_render_template :show
32
+ should_not_set_the_flash
33
+ end
34
+ end
35
+ end
36
+
37
+ def make_edit_html_tests(res)
38
+ context "on GET to #{controller_name_from_class}#edit" do
39
+ setup do
40
+ @record = get_existing_record(res)
41
+ parent_params = make_parent_params(res, @record)
42
+ get :edit, parent_params.merge({ res.identifier => @record.to_param })
43
+ end
44
+
45
+ if res.denied.actions.include?(:edit)
46
+ should_not_assign_to res.object
47
+ should_redirect_to res.denied.redirect
48
+ should_set_the_flash_to res.denied.flash
49
+ else
50
+ should_assign_to res.object
51
+ should_respond_with :success
52
+ should_render_template :edit
53
+ should_not_set_the_flash
54
+ should_render_a_form
55
+ should "set @#{res.object} to requested instance" do
56
+ assert_equal @record, assigns(res.object)
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ def make_index_html_tests(res)
63
+ context "on GET to #{controller_name_from_class}#index" do
64
+ setup do
65
+ record = get_existing_record(res) rescue nil
66
+ parent_params = make_parent_params(res, record)
67
+ get(:index, parent_params)
68
+ end
69
+
70
+ if res.denied.actions.include?(:index)
71
+ should_not_assign_to res.object.to_s.pluralize
72
+ should_redirect_to res.denied.redirect
73
+ should_set_the_flash_to res.denied.flash
74
+ else
75
+ should_respond_with :success
76
+ should_assign_to res.object.to_s.pluralize
77
+ should_render_template :index
78
+ should_not_set_the_flash
79
+ end
80
+ end
81
+ end
82
+
83
+ def make_new_html_tests(res)
84
+ context "on GET to #{controller_name_from_class}#new" do
85
+ setup do
86
+ record = get_existing_record(res) rescue nil
87
+ parent_params = make_parent_params(res, record)
88
+ get(:new, parent_params)
89
+ end
90
+
91
+ if res.denied.actions.include?(:new)
92
+ should_not_assign_to res.object
93
+ should_redirect_to res.denied.redirect
94
+ should_set_the_flash_to res.denied.flash
95
+ else
96
+ should_respond_with :success
97
+ should_assign_to res.object
98
+ should_not_set_the_flash
99
+ should_render_template :new
100
+ should_render_a_form
101
+ end
102
+ end
103
+ end
104
+
105
+ def make_destroy_html_tests(res)
106
+ context "on DELETE to #{controller_name_from_class}#destroy" do
107
+ setup do
108
+ @record = get_existing_record(res)
109
+ parent_params = make_parent_params(res, @record)
110
+ delete :destroy, parent_params.merge({ res.identifier => @record.to_param })
111
+ end
112
+
113
+ if res.denied.actions.include?(:destroy)
114
+ should_redirect_to res.denied.redirect
115
+ should_set_the_flash_to res.denied.flash
116
+
117
+ should "not destroy record" do
118
+ assert_nothing_raised { assert @record.reload }
119
+ end
120
+ else
121
+ should_set_the_flash_to res.destroy.flash
122
+ if res.destroy.redirect.is_a? Symbol
123
+ should_respond_with res.destroy.redirect
124
+ else
125
+ should_redirect_to res.destroy.redirect
126
+ end
127
+
128
+ should "destroy record" do
129
+ assert_raises(::ActiveRecord::RecordNotFound, "@#{res.object} was not destroyed.") do
130
+ @record.reload
131
+ end
132
+ end
133
+ end
134
+ end
135
+ end
136
+
137
+ def make_create_html_tests(res)
138
+ context "on POST to #{controller_name_from_class}#create with #{res.create.params.inspect}" do
139
+ setup do
140
+ record = get_existing_record(res) rescue nil
141
+ parent_params = make_parent_params(res, record)
142
+ @count = res.klass.count
143
+ post :create, parent_params.merge(res.object => res.create.params)
144
+ end
145
+
146
+ if res.denied.actions.include?(:create)
147
+ should_redirect_to res.denied.redirect
148
+ should_set_the_flash_to res.denied.flash
149
+ should_not_assign_to res.object
150
+
151
+ should "not create new record" do
152
+ assert_equal @count, res.klass.count
153
+ end
154
+ else
155
+ should_assign_to res.object
156
+ should_set_the_flash_to res.create.flash
157
+ if res.create.redirect.is_a? Symbol
158
+ should_respond_with res.create.redirect
159
+ else
160
+ should_redirect_to res.create.redirect
161
+ end
162
+
163
+ should "not have errors on @#{res.object}" do
164
+ assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:"
165
+ end
166
+ end
167
+ end
168
+ end
169
+
170
+ def make_update_html_tests(res)
171
+ context "on PUT to #{controller_name_from_class}#update with #{res.create.params.inspect}" do
172
+ setup do
173
+ @record = get_existing_record(res)
174
+ parent_params = make_parent_params(res, @record)
175
+ put :update, parent_params.merge(res.identifier => @record.to_param, res.object => res.update.params)
176
+ end
177
+
178
+ if res.denied.actions.include?(:update)
179
+ should_not_assign_to res.object
180
+ should_redirect_to res.denied.redirect
181
+ should_set_the_flash_to res.denied.flash
182
+ else
183
+ should_assign_to res.object
184
+ should_set_the_flash_to(res.update.flash)
185
+ if res.update.redirect.is_a? Symbol
186
+ should_respond_with res.update.redirect
187
+ else
188
+ should_redirect_to res.update.redirect
189
+ end
190
+
191
+ should "not have errors on @#{res.object}" do
192
+ assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:"
193
+ end
194
+ end
195
+ end
196
+ end
197
+ end
198
+ end
199
+ end
200
+ end
201
+ end
@@ -0,0 +1,170 @@
1
+ module ThoughtBot # :nodoc:
2
+ module Shoulda # :nodoc:
3
+ module Controller # :nodoc:
4
+ module XML
5
+ def self.included(other) #:nodoc:
6
+ other.class_eval do
7
+ extend ThoughtBot::Shoulda::Controller::XML::ClassMethods
8
+ end
9
+ end
10
+
11
+ module ClassMethods
12
+ # Macro that creates a test asserting that the controller responded with an XML content-type
13
+ # and that the XML contains +<name/>+ as the root element.
14
+ def should_respond_with_xml_for(name = nil)
15
+ should "have ContentType set to 'application/xml'" do
16
+ assert_xml_response
17
+ end
18
+
19
+ if name
20
+ should "return <#{name}/> as the root element" do
21
+ body = @response.body.first(100).map {|l| " #{l}"}
22
+ assert_select name.to_s.dasherize, 1, "Body:\n#{body}...\nDoes not have <#{name}/> as the root element."
23
+ end
24
+ end
25
+ end
26
+ alias should_respond_with_xml should_respond_with_xml_for
27
+
28
+ protected
29
+
30
+ def make_show_xml_tests(res) # :nodoc:
31
+ context "on GET to #{controller_name_from_class}#show as xml" do
32
+ setup do
33
+ request_xml
34
+ record = get_existing_record(res)
35
+ parent_params = make_parent_params(res, record)
36
+ get :show, parent_params.merge({ res.identifier => record.to_param })
37
+ end
38
+
39
+ if res.denied.actions.include?(:show)
40
+ should_not_assign_to res.object
41
+ should_respond_with 401
42
+ else
43
+ should_assign_to res.object
44
+ should_respond_with :success
45
+ should_respond_with_xml_for res.object
46
+ end
47
+ end
48
+ end
49
+
50
+ def make_edit_xml_tests(res) # :nodoc:
51
+ # XML doesn't need an :edit action
52
+ end
53
+
54
+ def make_new_xml_tests(res) # :nodoc:
55
+ # XML doesn't need a :new action
56
+ end
57
+
58
+ def make_index_xml_tests(res) # :nodoc:
59
+ context "on GET to #{controller_name_from_class}#index as xml" do
60
+ setup do
61
+ request_xml
62
+ parent_params = make_parent_params(res)
63
+ get(:index, parent_params)
64
+ end
65
+
66
+ if res.denied.actions.include?(:index)
67
+ should_not_assign_to res.object.to_s.pluralize
68
+ should_respond_with 401
69
+ else
70
+ should_respond_with :success
71
+ should_respond_with_xml_for res.object.to_s.pluralize
72
+ should_assign_to res.object.to_s.pluralize
73
+ end
74
+ end
75
+ end
76
+
77
+ def make_destroy_xml_tests(res) # :nodoc:
78
+ context "on DELETE to #{controller_name_from_class}#destroy as xml" do
79
+ setup do
80
+ request_xml
81
+ @record = get_existing_record(res)
82
+ parent_params = make_parent_params(res, @record)
83
+ delete :destroy, parent_params.merge({ res.identifier => @record.to_param })
84
+ end
85
+
86
+ if res.denied.actions.include?(:destroy)
87
+ should_respond_with 401
88
+
89
+ should "not destroy record" do
90
+ assert @record.reload
91
+ end
92
+ else
93
+ should "destroy record" do
94
+ assert_raises(::ActiveRecord::RecordNotFound, "@#{res.object} was not destroyed.") do
95
+ @record.reload
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
101
+
102
+ def make_create_xml_tests(res) # :nodoc:
103
+ context "on POST to #{controller_name_from_class}#create as xml" do
104
+ setup do
105
+ request_xml
106
+ parent_params = make_parent_params(res)
107
+ @count = res.klass.count
108
+ post :create, parent_params.merge(res.object => res.create.params)
109
+ end
110
+
111
+ if res.denied.actions.include?(:create)
112
+ should_respond_with 401
113
+ should_not_assign_to res.object
114
+
115
+ should "not create new record" do
116
+ assert_equal @count, res.klass.count
117
+ end
118
+ else
119
+ should_assign_to res.object
120
+
121
+ should "not have errors on @#{res.object}" do
122
+ assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:"
123
+ end
124
+ end
125
+ end
126
+ end
127
+
128
+ def make_update_xml_tests(res) # :nodoc:
129
+ context "on PUT to #{controller_name_from_class}#update as xml" do
130
+ setup do
131
+ request_xml
132
+ @record = get_existing_record(res)
133
+ parent_params = make_parent_params(res, @record)
134
+ put :update, parent_params.merge(res.identifier => @record.to_param, res.object => res.update.params)
135
+ end
136
+
137
+ if res.denied.actions.include?(:update)
138
+ should_not_assign_to res.object
139
+ should_respond_with 401
140
+ else
141
+ should_assign_to res.object
142
+
143
+ should "not have errors on @#{res.object}" do
144
+ assert_equal [], assigns(res.object).errors.full_messages, "@#{res.object} has errors:"
145
+ end
146
+ end
147
+ end
148
+ end
149
+ end
150
+
151
+ # Sets the next request's format to 'application/xml'
152
+ def request_xml
153
+ @request.accept = "application/xml"
154
+ end
155
+
156
+ # Asserts that the controller's response was 'application/xml'
157
+ def assert_xml_response
158
+ content_type = (@response.headers["Content-Type"] || @response.headers["type"]).to_s
159
+ regex = %r{\bapplication/xml\b}
160
+
161
+ msg = "Content Type '#{content_type.inspect}' doesn't match '#{regex.inspect}'\n"
162
+ msg += "Body: #{@response.body.first(100).chomp} ..."
163
+
164
+ assert_match regex, content_type, msg
165
+ end
166
+
167
+ end
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,14 @@
1
+ # Stolen straight from ActiveSupport
2
+
3
+ class Proc #:nodoc:
4
+ def bind(object)
5
+ block, time = self, Time.now
6
+ (class << object; self end).class_eval do
7
+ method_name = "__bind_#{time.to_i}_#{time.usec}"
8
+ define_method(method_name, &block)
9
+ method = instance_method(method_name)
10
+ remove_method(method_name)
11
+ method
12
+ end.bind(object)
13
+ end
14
+ end
@@ -0,0 +1,246 @@
1
+ require File.join(File.dirname(__FILE__), 'proc_extensions')
2
+
3
+ module Thoughtbot
4
+ module Shoulda
5
+ class << self
6
+ attr_accessor :current_context
7
+ end
8
+
9
+ VERSION = '1.1.1'
10
+
11
+ # = Should statements
12
+ #
13
+ # Should statements are just syntactic sugar over normal Test::Unit test methods. A should block
14
+ # contains all the normal code and assertions you're used to seeing, with the added benefit that
15
+ # they can be wrapped inside context blocks (see below).
16
+ #
17
+ # == Example:
18
+ #
19
+ # class UserTest << Test::Unit::TestCase
20
+ #
21
+ # def setup
22
+ # @user = User.new("John", "Doe")
23
+ # end
24
+ #
25
+ # should "return its full name"
26
+ # assert_equal 'John Doe', @user.full_name
27
+ # end
28
+ #
29
+ # end
30
+ #
31
+ # ...will produce the following test:
32
+ # * <tt>"test: User should return its full name. "</tt>
33
+ #
34
+ # Note: The part before <tt>should</tt> in the test name is gleamed from the name of the Test::Unit class.
35
+
36
+ def should(name, &blk)
37
+ if Shoulda.current_context
38
+ block_given? ? Shoulda.current_context.should(name, &blk) : Should.current_context.should_eventually(name)
39
+ else
40
+ context_name = self.name.gsub(/Test/, "")
41
+ context = Thoughtbot::Shoulda::Context.new(context_name, self) do
42
+ block_given? ? should(name, &blk) : should_eventually(name)
43
+ end
44
+ context.build
45
+ end
46
+ end
47
+
48
+ # Just like should, but never runs, and instead prints an 'X' in the Test::Unit output.
49
+ def should_eventually(name, &blk)
50
+ context_name = self.name.gsub(/Test/, "")
51
+ context = Thoughtbot::Shoulda::Context.new(context_name, self) do
52
+ should_eventually(name, &blk)
53
+ end
54
+ context.build
55
+ end
56
+
57
+ # = Contexts
58
+ #
59
+ # A context block groups should statements under a common set of setup/teardown methods.
60
+ # Context blocks can be arbitrarily nested, and can do wonders for improving the maintainability
61
+ # and readability of your test code.
62
+ #
63
+ # A context block can contain setup, should, should_eventually, and teardown blocks.
64
+ #
65
+ # class UserTest << Test::Unit::TestCase
66
+ # context "A User instance" do
67
+ # setup do
68
+ # @user = User.find(:first)
69
+ # end
70
+ #
71
+ # should "return its full name"
72
+ # assert_equal 'John Doe', @user.full_name
73
+ # end
74
+ # end
75
+ # end
76
+ #
77
+ # This code will produce the method <tt>"test: A User instance should return its full name. "</tt>.
78
+ #
79
+ # Contexts may be nested. Nested contexts run their setup blocks from out to in before each
80
+ # should statement. They then run their teardown blocks from in to out after each should statement.
81
+ #
82
+ # class UserTest << Test::Unit::TestCase
83
+ # context "A User instance" do
84
+ # setup do
85
+ # @user = User.find(:first)
86
+ # end
87
+ #
88
+ # should "return its full name"
89
+ # assert_equal 'John Doe', @user.full_name
90
+ # end
91
+ #
92
+ # context "with a profile" do
93
+ # setup do
94
+ # @user.profile = Profile.find(:first)
95
+ # end
96
+ #
97
+ # should "return true when sent :has_profile?"
98
+ # assert @user.has_profile?
99
+ # end
100
+ # end
101
+ # end
102
+ # end
103
+ #
104
+ # This code will produce the following methods
105
+ # * <tt>"test: A User instance should return its full name. "</tt>
106
+ # * <tt>"test: A User instance with a profile should return true when sent :has_profile?. "</tt>
107
+ #
108
+ # <b>Just like should statements, a context block can exist next to normal <tt>def test_the_old_way; end</tt>
109
+ # tests</b>. This means you do not have to fully commit to the context/should syntax in a test file.
110
+
111
+ def context(name, &blk)
112
+ if Shoulda.current_context
113
+ Shoulda.current_context.context(name, &blk)
114
+ else
115
+ context = Thoughtbot::Shoulda::Context.new(name, self, &blk)
116
+ context.build
117
+ end
118
+ end
119
+
120
+ class Context # :nodoc:
121
+
122
+ attr_accessor :name # my name
123
+ attr_accessor :parent # may be another context, or the original test::unit class.
124
+ attr_accessor :subcontexts # array of contexts nested under myself
125
+ attr_accessor :setup_blocks # block given via a setup method
126
+ attr_accessor :teardown_blocks # block given via a teardown method
127
+ attr_accessor :shoulds # array of hashes representing the should statements
128
+ attr_accessor :should_eventuallys # array of hashes representing the should eventually statements
129
+
130
+ def initialize(name, parent, &blk)
131
+ Shoulda.current_context = self
132
+ self.name = name
133
+ self.parent = parent
134
+ self.setup_blocks = []
135
+ self.teardown_blocks = []
136
+ self.shoulds = []
137
+ self.should_eventuallys = []
138
+ self.subcontexts = []
139
+
140
+ blk.bind(self).call
141
+ Shoulda.current_context = nil
142
+ end
143
+
144
+ def context(name, &blk)
145
+ subcontexts << Context.new(name, self, &blk)
146
+ Shoulda.current_context = self
147
+ end
148
+
149
+ def setup(&blk)
150
+ self.setup_blocks << blk
151
+ end
152
+
153
+ def teardown(&blk)
154
+ self.teardown_blocks << blk
155
+ end
156
+
157
+ def should(name, &blk)
158
+ if block_given?
159
+ self.shoulds << { :name => name, :block => blk }
160
+ else
161
+ self.should_eventuallys << { :name => name }
162
+ end
163
+ end
164
+
165
+ def should_eventually(name, &blk)
166
+ self.should_eventuallys << { :name => name, :block => blk }
167
+ end
168
+
169
+ def full_name
170
+ parent_name = parent.full_name if am_subcontext?
171
+ return [parent_name, name].join(" ").strip
172
+ end
173
+
174
+ def am_subcontext?
175
+ parent.is_a?(self.class) # my parent is the same class as myself.
176
+ end
177
+
178
+ def test_unit_class
179
+ am_subcontext? ? parent.test_unit_class : parent
180
+ end
181
+
182
+ def create_test_from_should_hash(should)
183
+ test_name = ["test:", full_name, "should", "#{should[:name]}. "].flatten.join(' ').to_sym
184
+
185
+ if test_unit_class.instance_methods.include?(test_name.to_s)
186
+ warn " * WARNING: '#{test_name}' is already defined"
187
+ end
188
+
189
+ context = self
190
+ test_unit_class.send(:define_method, test_name) do
191
+ begin
192
+ context.run_all_setup_blocks(self)
193
+ should[:block].bind(self).call
194
+ ensure
195
+ context.run_all_teardown_blocks(self)
196
+ end
197
+ end
198
+ end
199
+
200
+ def run_all_setup_blocks(binding)
201
+ self.parent.run_all_setup_blocks(binding) if am_subcontext?
202
+ setup_blocks.each do |setup_block|
203
+ setup_block.bind(binding).call
204
+ end
205
+ end
206
+
207
+ def run_all_teardown_blocks(binding)
208
+ teardown_blocks.reverse.each do |teardown_block|
209
+ teardown_block.bind(binding).call
210
+ end
211
+ self.parent.run_all_teardown_blocks(binding) if am_subcontext?
212
+ end
213
+
214
+ def print_should_eventuallys
215
+ should_eventuallys.each do |should|
216
+ test_name = [full_name, "should", "#{should[:name]}. "].flatten.join(' ')
217
+ puts " * DEFERRED: " + test_name
218
+ end
219
+ end
220
+
221
+ def build
222
+ shoulds.each do |should|
223
+ create_test_from_should_hash(should)
224
+ end
225
+
226
+ subcontexts.each { |context| context.build }
227
+
228
+ print_should_eventuallys
229
+ end
230
+
231
+ def method_missing(method, *args, &blk)
232
+ test_unit_class.send(method, *args, &blk)
233
+ end
234
+
235
+ end
236
+ end
237
+ end
238
+
239
+ module Test # :nodoc: all
240
+ module Unit
241
+ class TestCase
242
+ extend Thoughtbot::Shoulda
243
+ end
244
+ end
245
+ end
246
+