shoulda 2.11.3 → 3.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +9 -0
- data/Gemfile.lock +108 -0
- data/README.md +95 -0
- data/Rakefile +6 -50
- data/lib/shoulda/version.rb +1 -1
- metadata +50 -269
- data/README.rdoc +0 -154
- data/bin/convert_to_should_syntax +0 -42
- data/lib/shoulda.rb +0 -9
- data/lib/shoulda/action_controller.rb +0 -34
- data/lib/shoulda/action_controller/macros.rb +0 -221
- data/lib/shoulda/action_controller/matchers.rb +0 -39
- data/lib/shoulda/action_controller/matchers/assign_to_matcher.rb +0 -112
- data/lib/shoulda/action_controller/matchers/filter_param_matcher.rb +0 -74
- data/lib/shoulda/action_controller/matchers/redirect_to_matcher.rb +0 -62
- data/lib/shoulda/action_controller/matchers/render_template_matcher.rb +0 -54
- data/lib/shoulda/action_controller/matchers/render_with_layout_matcher.rb +0 -99
- data/lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb +0 -74
- data/lib/shoulda/action_controller/matchers/respond_with_matcher.rb +0 -85
- data/lib/shoulda/action_controller/matchers/route_matcher.rb +0 -93
- data/lib/shoulda/action_controller/matchers/set_session_matcher.rb +0 -98
- data/lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb +0 -100
- data/lib/shoulda/action_mailer.rb +0 -13
- data/lib/shoulda/action_mailer/assertions.rb +0 -42
- data/lib/shoulda/action_mailer/matchers.rb +0 -22
- data/lib/shoulda/action_mailer/matchers/have_sent_email.rb +0 -110
- data/lib/shoulda/active_record.rb +0 -16
- data/lib/shoulda/active_record/assertions.rb +0 -69
- data/lib/shoulda/active_record/helpers.rb +0 -32
- data/lib/shoulda/active_record/macros.rb +0 -457
- data/lib/shoulda/active_record/matchers.rb +0 -42
- data/lib/shoulda/active_record/matchers/allow_mass_assignment_of_matcher.rb +0 -83
- data/lib/shoulda/active_record/matchers/allow_value_matcher.rb +0 -110
- data/lib/shoulda/active_record/matchers/association_matcher.rb +0 -226
- data/lib/shoulda/active_record/matchers/ensure_inclusion_of_matcher.rb +0 -87
- data/lib/shoulda/active_record/matchers/ensure_length_of_matcher.rb +0 -141
- data/lib/shoulda/active_record/matchers/have_db_column_matcher.rb +0 -169
- data/lib/shoulda/active_record/matchers/have_db_index_matcher.rb +0 -112
- data/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb +0 -59
- data/lib/shoulda/active_record/matchers/validate_acceptance_of_matcher.rb +0 -41
- data/lib/shoulda/active_record/matchers/validate_format_of_matcher.rb +0 -65
- data/lib/shoulda/active_record/matchers/validate_numericality_of_matcher.rb +0 -39
- data/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb +0 -60
- data/lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb +0 -148
- data/lib/shoulda/active_record/matchers/validation_matcher.rb +0 -56
- data/lib/shoulda/assertions.rb +0 -79
- data/lib/shoulda/autoload_macros.rb +0 -46
- data/lib/shoulda/context.rb +0 -433
- data/lib/shoulda/helpers.rb +0 -8
- data/lib/shoulda/integrations/rspec.rb +0 -13
- data/lib/shoulda/integrations/rspec2.rb +0 -22
- data/lib/shoulda/integrations/test_unit.rb +0 -22
- data/lib/shoulda/macros.rb +0 -161
- data/lib/shoulda/private_helpers.rb +0 -13
- data/lib/shoulda/proc_extensions.rb +0 -14
- data/lib/shoulda/rails.rb +0 -8
- data/lib/shoulda/tasks.rb +0 -3
- data/lib/shoulda/tasks/list_tests.rake +0 -29
- data/lib/shoulda/tasks/yaml_to_shoulda.rake +0 -28
- data/rails/init.rb +0 -8
- data/test/README +0 -36
- data/test/fail_macros.rb +0 -55
- data/test/fixtures/addresses.yml +0 -3
- data/test/fixtures/friendships.yml +0 -0
- data/test/fixtures/posts.yml +0 -5
- data/test/fixtures/products.yml +0 -0
- data/test/fixtures/taggings.yml +0 -0
- data/test/fixtures/tags.yml +0 -9
- data/test/fixtures/users.yml +0 -6
- data/test/functional/posts_controller_test.rb +0 -121
- data/test/functional/users_controller_test.rb +0 -19
- data/test/matchers/action_mailer/have_sent_email_test.rb +0 -70
- data/test/matchers/active_record/allow_mass_assignment_of_matcher_test.rb +0 -74
- data/test/matchers/active_record/allow_value_matcher_test.rb +0 -64
- data/test/matchers/active_record/association_matcher_test.rb +0 -263
- data/test/matchers/active_record/ensure_inclusion_of_matcher_test.rb +0 -81
- data/test/matchers/active_record/ensure_length_of_matcher_test.rb +0 -158
- data/test/matchers/active_record/have_db_column_matcher_test.rb +0 -169
- data/test/matchers/active_record/have_db_index_matcher_test.rb +0 -91
- data/test/matchers/active_record/have_readonly_attributes_matcher_test.rb +0 -29
- data/test/matchers/active_record/validate_acceptance_of_matcher_test.rb +0 -44
- data/test/matchers/active_record/validate_format_of_matcher_test.rb +0 -39
- data/test/matchers/active_record/validate_numericality_of_matcher_test.rb +0 -52
- data/test/matchers/active_record/validate_presence_of_matcher_test.rb +0 -86
- data/test/matchers/active_record/validate_uniqueness_of_matcher_test.rb +0 -147
- data/test/matchers/controller/assign_to_matcher_test.rb +0 -45
- data/test/matchers/controller/filter_param_matcher_test.rb +0 -40
- data/test/matchers/controller/redirect_to_matcher_test.rb +0 -37
- data/test/matchers/controller/render_template_matcher_test.rb +0 -37
- data/test/matchers/controller/render_with_layout_matcher_test.rb +0 -47
- data/test/matchers/controller/respond_with_content_type_matcher_test.rb +0 -32
- data/test/matchers/controller/respond_with_matcher_test.rb +0 -96
- data/test/matchers/controller/route_matcher_test.rb +0 -75
- data/test/matchers/controller/set_session_matcher_test.rb +0 -48
- data/test/matchers/controller/set_the_flash_matcher.rb +0 -95
- data/test/other/autoload_macro_test.rb +0 -18
- data/test/other/context_test.rb +0 -372
- data/test/other/convert_to_should_syntax_test.rb +0 -63
- data/test/other/helpers_test.rb +0 -317
- data/test/other/private_helpers_test.rb +0 -32
- data/test/other/should_test.rb +0 -271
- data/test/rails2_model_builder.rb +0 -130
- data/test/rails2_root/app/controllers/application_controller.rb +0 -22
- data/test/rails2_root/app/controllers/posts_controller.rb +0 -87
- data/test/rails2_root/app/controllers/users_controller.rb +0 -84
- data/test/rails2_root/app/helpers/application_helper.rb +0 -3
- data/test/rails2_root/app/helpers/posts_helper.rb +0 -2
- data/test/rails2_root/app/helpers/users_helper.rb +0 -2
- data/test/rails2_root/app/models/address.rb +0 -7
- data/test/rails2_root/app/models/flea.rb +0 -11
- data/test/rails2_root/app/models/friendship.rb +0 -4
- data/test/rails2_root/app/models/notifier.rb +0 -8
- data/test/rails2_root/app/models/pets/cat.rb +0 -7
- data/test/rails2_root/app/models/pets/dog.rb +0 -10
- data/test/rails2_root/app/models/post.rb +0 -12
- data/test/rails2_root/app/models/product.rb +0 -12
- data/test/rails2_root/app/models/profile.rb +0 -2
- data/test/rails2_root/app/models/registration.rb +0 -2
- data/test/rails2_root/app/models/tag.rb +0 -8
- data/test/rails2_root/app/models/tagging.rb +0 -4
- data/test/rails2_root/app/models/treat.rb +0 -3
- data/test/rails2_root/app/models/user.rb +0 -32
- data/test/rails2_root/app/views/layouts/posts.rhtml +0 -19
- data/test/rails2_root/app/views/layouts/users.rhtml +0 -17
- data/test/rails2_root/app/views/layouts/wide.html.erb +0 -1
- data/test/rails2_root/app/views/notifier/the_email.html.erb +0 -1
- data/test/rails2_root/app/views/posts/edit.rhtml +0 -27
- data/test/rails2_root/app/views/posts/index.rhtml +0 -25
- data/test/rails2_root/app/views/posts/new.rhtml +0 -26
- data/test/rails2_root/app/views/posts/show.rhtml +0 -18
- data/test/rails2_root/app/views/users/edit.rhtml +0 -22
- data/test/rails2_root/app/views/users/index.rhtml +0 -22
- data/test/rails2_root/app/views/users/new.rhtml +0 -21
- data/test/rails2_root/app/views/users/show.rhtml +0 -13
- data/test/rails2_root/config/boot.rb +0 -110
- data/test/rails2_root/config/database.yml +0 -4
- data/test/rails2_root/config/environment.rb +0 -17
- data/test/rails2_root/config/environments/test.rb +0 -23
- data/test/rails2_root/config/initializers/new_rails_defaults.rb +0 -15
- data/test/rails2_root/config/initializers/shoulda.rb +0 -8
- data/test/rails2_root/config/routes.rb +0 -6
- data/test/rails2_root/db/migrate/001_create_users.rb +0 -19
- data/test/rails2_root/db/migrate/002_create_posts.rb +0 -13
- data/test/rails2_root/db/migrate/003_create_taggings.rb +0 -12
- data/test/rails2_root/db/migrate/004_create_tags.rb +0 -11
- data/test/rails2_root/db/migrate/005_create_dogs.rb +0 -12
- data/test/rails2_root/db/migrate/006_create_addresses.rb +0 -14
- data/test/rails2_root/db/migrate/007_create_fleas.rb +0 -11
- data/test/rails2_root/db/migrate/008_create_dogs_fleas.rb +0 -12
- data/test/rails2_root/db/migrate/009_create_products.rb +0 -17
- data/test/rails2_root/db/migrate/010_create_friendships.rb +0 -14
- data/test/rails2_root/db/migrate/011_create_treats.rb +0 -12
- data/test/rails2_root/db/migrate/20090506203502_create_profiles.rb +0 -12
- data/test/rails2_root/db/migrate/20090506203536_create_registrations.rb +0 -14
- data/test/rails2_root/db/migrate/20090513104502_create_cats.rb +0 -12
- data/test/rails2_root/db/schema.rb +0 -0
- data/test/rails2_root/log/test.log +0 -50381
- data/test/rails2_root/public/404.html +0 -30
- data/test/rails2_root/public/422.html +0 -30
- data/test/rails2_root/public/500.html +0 -30
- data/test/rails2_root/script/console +0 -3
- data/test/rails2_root/script/generate +0 -3
- data/test/rails2_root/test/shoulda_macros/custom_macro.rb +0 -6
- data/test/rails2_root/vendor/gems/gem_with_macro-0.0.1/shoulda_macros/gem_macro.rb +0 -6
- data/test/rails2_root/vendor/plugins/plugin_with_macro/shoulda_macros/plugin_macro.rb +0 -6
- data/test/rails2_test_helper.rb +0 -6
- data/test/rails3_model_builder.rb +0 -118
- data/test/rails3_root/Gemfile +0 -28
- data/test/rails3_root/README +0 -244
- data/test/rails3_root/Rakefile +0 -10
- data/test/rails3_root/app/controllers/application_controller.rb +0 -22
- data/test/rails3_root/app/controllers/posts_controller.rb +0 -87
- data/test/rails3_root/app/controllers/users_controller.rb +0 -82
- data/test/rails3_root/app/helpers/application_helper.rb +0 -2
- data/test/rails3_root/app/models/address.rb +0 -7
- data/test/rails3_root/app/models/flea.rb +0 -11
- data/test/rails3_root/app/models/friendship.rb +0 -4
- data/test/rails3_root/app/models/notifier.rb +0 -8
- data/test/rails3_root/app/models/pets/cat.rb +0 -7
- data/test/rails3_root/app/models/pets/dog.rb +0 -10
- data/test/rails3_root/app/models/post.rb +0 -12
- data/test/rails3_root/app/models/product.rb +0 -12
- data/test/rails3_root/app/models/profile.rb +0 -2
- data/test/rails3_root/app/models/registration.rb +0 -2
- data/test/rails3_root/app/models/tag.rb +0 -8
- data/test/rails3_root/app/models/tagging.rb +0 -4
- data/test/rails3_root/app/models/treat.rb +0 -3
- data/test/rails3_root/app/models/user.rb +0 -32
- data/test/rails3_root/app/views/layouts/application.html.erb +0 -14
- data/test/rails3_root/app/views/layouts/posts.rhtml +0 -19
- data/test/rails3_root/app/views/layouts/users.rhtml +0 -17
- data/test/rails3_root/app/views/layouts/wide.html.erb +0 -1
- data/test/rails3_root/app/views/notifier/the_email.html.erb +0 -1
- data/test/rails3_root/app/views/posts/edit.rhtml +0 -27
- data/test/rails3_root/app/views/posts/index.rhtml +0 -25
- data/test/rails3_root/app/views/posts/new.rhtml +0 -24
- data/test/rails3_root/app/views/posts/show.rhtml +0 -18
- data/test/rails3_root/app/views/users/edit.rhtml +0 -22
- data/test/rails3_root/app/views/users/index.rhtml +0 -22
- data/test/rails3_root/app/views/users/new.rhtml +0 -21
- data/test/rails3_root/app/views/users/show.rhtml +0 -13
- data/test/rails3_root/config.ru +0 -4
- data/test/rails3_root/config/application.rb +0 -46
- data/test/rails3_root/config/boot.rb +0 -6
- data/test/rails3_root/config/database.yml +0 -22
- data/test/rails3_root/config/environment.rb +0 -5
- data/test/rails3_root/config/environments/development.rb +0 -19
- data/test/rails3_root/config/environments/production.rb +0 -42
- data/test/rails3_root/config/environments/test.rb +0 -32
- data/test/rails3_root/config/initializers/backtrace_silencers.rb +0 -7
- data/test/rails3_root/config/initializers/inflections.rb +0 -10
- data/test/rails3_root/config/initializers/mime_types.rb +0 -5
- data/test/rails3_root/config/initializers/secret_token.rb +0 -7
- data/test/rails3_root/config/initializers/session_store.rb +0 -8
- data/test/rails3_root/config/locales/en.yml +0 -5
- data/test/rails3_root/config/routes.rb +0 -4
- data/test/rails3_root/db/migrate/001_create_users.rb +0 -19
- data/test/rails3_root/db/migrate/002_create_posts.rb +0 -13
- data/test/rails3_root/db/migrate/003_create_taggings.rb +0 -12
- data/test/rails3_root/db/migrate/004_create_tags.rb +0 -11
- data/test/rails3_root/db/migrate/005_create_dogs.rb +0 -12
- data/test/rails3_root/db/migrate/006_create_addresses.rb +0 -14
- data/test/rails3_root/db/migrate/007_create_fleas.rb +0 -11
- data/test/rails3_root/db/migrate/008_create_dogs_fleas.rb +0 -12
- data/test/rails3_root/db/migrate/009_create_products.rb +0 -17
- data/test/rails3_root/db/migrate/010_create_friendships.rb +0 -14
- data/test/rails3_root/db/migrate/011_create_treats.rb +0 -12
- data/test/rails3_root/db/migrate/20090506203502_create_profiles.rb +0 -12
- data/test/rails3_root/db/migrate/20090506203536_create_registrations.rb +0 -14
- data/test/rails3_root/db/migrate/20090513104502_create_cats.rb +0 -12
- data/test/rails3_root/db/seeds.rb +0 -7
- data/test/rails3_root/db/test.sqlite3 +0 -0
- data/test/rails3_root/log/test.log +0 -10834
- data/test/rails3_root/public/404.html +0 -26
- data/test/rails3_root/public/422.html +0 -26
- data/test/rails3_root/public/500.html +0 -26
- data/test/rails3_root/public/favicon.ico +0 -0
- data/test/rails3_root/public/images/rails.png +0 -0
- data/test/rails3_root/public/index.html +0 -279
- data/test/rails3_root/public/javascripts/application.js +0 -2
- data/test/rails3_root/public/javascripts/controls.js +0 -965
- data/test/rails3_root/public/javascripts/dragdrop.js +0 -974
- data/test/rails3_root/public/javascripts/effects.js +0 -1123
- data/test/rails3_root/public/javascripts/prototype.js +0 -4874
- data/test/rails3_root/public/javascripts/rails.js +0 -118
- data/test/rails3_root/public/robots.txt +0 -5
- data/test/rails3_root/script/rails +0 -9
- data/test/rails3_root/test/performance/browsing_test.rb +0 -9
- data/test/rails3_root/test/test_helper.rb +0 -13
- data/test/rails3_test_helper.rb +0 -6
- data/test/rspec_test.rb +0 -207
- data/test/test_helper.rb +0 -36
- data/test/unit/address_test.rb +0 -10
- data/test/unit/cat_test.rb +0 -7
- data/test/unit/dog_test.rb +0 -9
- data/test/unit/flea_test.rb +0 -14
- data/test/unit/friendship_test.rb +0 -6
- data/test/unit/post_test.rb +0 -15
- data/test/unit/product_test.rb +0 -23
- data/test/unit/tag_test.rb +0 -11
- data/test/unit/tagging_test.rb +0 -6
- data/test/unit/user_test.rb +0 -46
data/lib/shoulda/assertions.rb
DELETED
@@ -1,79 +0,0 @@
|
|
1
|
-
module Shoulda # :nodoc:
|
2
|
-
module Assertions
|
3
|
-
# Asserts that two arrays contain the same elements, the same number of times. Essentially ==, but unordered.
|
4
|
-
#
|
5
|
-
# assert_same_elements([:a, :b, :c], [:c, :a, :b]) => passes
|
6
|
-
def assert_same_elements(a1, a2, msg = nil)
|
7
|
-
[:select, :inject, :size].each do |m|
|
8
|
-
[a1, a2].each {|a| assert_respond_to(a, m, "Are you sure that #{a.inspect} is an array? It doesn't respond to #{m}.") }
|
9
|
-
end
|
10
|
-
|
11
|
-
assert a1h = a1.inject({}) { |h,e| h[e] = a1.select { |i| i == e }.size; h }
|
12
|
-
assert a2h = a2.inject({}) { |h,e| h[e] = a2.select { |i| i == e }.size; h }
|
13
|
-
|
14
|
-
assert_equal(a1h, a2h, msg)
|
15
|
-
end
|
16
|
-
|
17
|
-
# Asserts that the given collection contains item x. If x is a regular expression, ensure that
|
18
|
-
# at least one element from the collection matches x. +extra_msg+ is appended to the error message if the assertion fails.
|
19
|
-
#
|
20
|
-
# assert_contains(['a', '1'], /\d/) => passes
|
21
|
-
# assert_contains(['a', '1'], 'a') => passes
|
22
|
-
# assert_contains(['a', '1'], /not there/) => fails
|
23
|
-
def assert_contains(collection, x, extra_msg = "")
|
24
|
-
collection = [collection] unless collection.is_a?(Array)
|
25
|
-
msg = "#{x.inspect} not found in #{collection.to_a.inspect} #{extra_msg}"
|
26
|
-
case x
|
27
|
-
when Regexp
|
28
|
-
assert(collection.detect { |e| e =~ x }, msg)
|
29
|
-
else
|
30
|
-
assert(collection.include?(x), msg)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
# Asserts that the given collection does not contain item x. If x is a regular expression, ensure that
|
35
|
-
# none of the elements from the collection match x.
|
36
|
-
def assert_does_not_contain(collection, x, extra_msg = "")
|
37
|
-
collection = [collection] unless collection.is_a?(Array)
|
38
|
-
msg = "#{x.inspect} found in #{collection.to_a.inspect} " + extra_msg
|
39
|
-
case x
|
40
|
-
when Regexp
|
41
|
-
assert(!collection.detect { |e| e =~ x }, msg)
|
42
|
-
else
|
43
|
-
assert(!collection.include?(x), msg)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
# Asserts that the given matcher returns true when +target+ is passed to #matches?
|
48
|
-
def assert_accepts(matcher, target, options = {})
|
49
|
-
if matcher.respond_to?(:in_context)
|
50
|
-
matcher.in_context(self)
|
51
|
-
end
|
52
|
-
|
53
|
-
if matcher.matches?(target)
|
54
|
-
assert_block { true }
|
55
|
-
if options[:message]
|
56
|
-
assert_match options[:message], matcher.negative_failure_message
|
57
|
-
end
|
58
|
-
else
|
59
|
-
assert_block(matcher.failure_message) { false }
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
# Asserts that the given matcher returns false when +target+ is passed to #matches?
|
64
|
-
def assert_rejects(matcher, target, options = {})
|
65
|
-
if matcher.respond_to?(:in_context)
|
66
|
-
matcher.in_context(self)
|
67
|
-
end
|
68
|
-
|
69
|
-
unless matcher.matches?(target)
|
70
|
-
assert_block { true }
|
71
|
-
if options[:message]
|
72
|
-
assert_match options[:message], matcher.failure_message
|
73
|
-
end
|
74
|
-
else
|
75
|
-
assert_block(matcher.negative_failure_message) { false }
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
@@ -1,46 +0,0 @@
|
|
1
|
-
module Shoulda # :nodoc:
|
2
|
-
# Call autoload_macros when you want to load test macros automatically in a non-Rails
|
3
|
-
# project (it's done automatically for Rails projects).
|
4
|
-
# You don't need to specify ROOT/test/shoulda_macros explicitly. Your custom macros
|
5
|
-
# are loaded automatically when you call autoload_macros.
|
6
|
-
#
|
7
|
-
# The first argument is the path to you application's root directory.
|
8
|
-
# All following arguments are directories relative to your root, which contain
|
9
|
-
# shoulda_macros subdirectories. These directories support the same kinds of globs as the
|
10
|
-
# Dir class.
|
11
|
-
#
|
12
|
-
# Basic usage (from a test_helper):
|
13
|
-
# Shoulda.autoload_macros(File.dirname(__FILE__) + '/..')
|
14
|
-
# will load everything in
|
15
|
-
# - your_app/test/shoulda_macros
|
16
|
-
#
|
17
|
-
# To load vendored macros as well:
|
18
|
-
# Shoulda.autoload_macros(APP_ROOT, 'vendor/*')
|
19
|
-
# will load everything in
|
20
|
-
# - APP_ROOT/vendor/*/shoulda_macros
|
21
|
-
# - APP_ROOT/test/shoulda_macros
|
22
|
-
#
|
23
|
-
# To load macros in an app with a vendor directory laid out like Rails':
|
24
|
-
# Shoulda.autoload_macros(APP_ROOT, 'vendor/{plugins,gems}/*')
|
25
|
-
# or
|
26
|
-
# Shoulda.autoload_macros(APP_ROOT, 'vendor/plugins/*', 'vendor/gems/*')
|
27
|
-
# will load everything in
|
28
|
-
# - APP_ROOT/vendor/plugins/*/shoulda_macros
|
29
|
-
# - APP_ROOT/vendor/gems/*/shoulda_macros
|
30
|
-
# - APP_ROOT/test/shoulda_macros
|
31
|
-
#
|
32
|
-
# If you prefer to stick testing dependencies away from your production dependencies:
|
33
|
-
# Shoulda.autoload_macros(APP_ROOT, 'vendor/*', 'test/vendor/*')
|
34
|
-
# will load everything in
|
35
|
-
# - APP_ROOT/vendor/*/shoulda_macros
|
36
|
-
# - APP_ROOT/test/vendor/*/shoulda_macros
|
37
|
-
# - APP_ROOT/test/shoulda_macros
|
38
|
-
def self.autoload_macros(root, *dirs)
|
39
|
-
dirs << File.join('test')
|
40
|
-
complete_dirs = dirs.map{|d| File.join(root, d, 'shoulda_macros')}
|
41
|
-
all_files = complete_dirs.inject([]){ |files, dir| files + Dir[File.join(dir, '*.rb')] }
|
42
|
-
all_files.each do |file|
|
43
|
-
require file
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
data/lib/shoulda/context.rb
DELETED
@@ -1,433 +0,0 @@
|
|
1
|
-
module Shoulda
|
2
|
-
class << self
|
3
|
-
attr_accessor :contexts
|
4
|
-
def contexts # :nodoc:
|
5
|
-
@contexts ||= []
|
6
|
-
end
|
7
|
-
|
8
|
-
def current_context # :nodoc:
|
9
|
-
self.contexts.last
|
10
|
-
end
|
11
|
-
|
12
|
-
def add_context(context) # :nodoc:
|
13
|
-
self.contexts.push(context)
|
14
|
-
end
|
15
|
-
|
16
|
-
def remove_context # :nodoc:
|
17
|
-
self.contexts.pop
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
module ClassMethods
|
22
|
-
# == Should statements
|
23
|
-
#
|
24
|
-
# Should statements are just syntactic sugar over normal Test::Unit test
|
25
|
-
# methods. A should block contains all the normal code and assertions
|
26
|
-
# you're used to seeing, with the added benefit that they can be wrapped
|
27
|
-
# inside context blocks (see below).
|
28
|
-
#
|
29
|
-
# === Example:
|
30
|
-
#
|
31
|
-
# class UserTest < Test::Unit::TestCase
|
32
|
-
#
|
33
|
-
# def setup
|
34
|
-
# @user = User.new("John", "Doe")
|
35
|
-
# end
|
36
|
-
#
|
37
|
-
# should "return its full name"
|
38
|
-
# assert_equal 'John Doe', @user.full_name
|
39
|
-
# end
|
40
|
-
#
|
41
|
-
# end
|
42
|
-
#
|
43
|
-
# ...will produce the following test:
|
44
|
-
# * <tt>"test: User should return its full name. "</tt>
|
45
|
-
#
|
46
|
-
# Note: The part before <tt>should</tt> in the test name is gleamed from the name of the Test::Unit class.
|
47
|
-
#
|
48
|
-
# Should statements can also take a Proc as a <tt>:before </tt>option. This proc runs after any
|
49
|
-
# parent context's setups but before the current context's setup.
|
50
|
-
#
|
51
|
-
# === Example:
|
52
|
-
#
|
53
|
-
# context "Some context" do
|
54
|
-
# setup { puts("I run after the :before proc") }
|
55
|
-
#
|
56
|
-
# should "run a :before proc", :before => lambda { puts("I run before the setup") } do
|
57
|
-
# assert true
|
58
|
-
# end
|
59
|
-
# end
|
60
|
-
#
|
61
|
-
# Should statements can also wrap matchers, making virtually any matcher
|
62
|
-
# usable in a macro style. The matcher's description is used to generate a
|
63
|
-
# test name and failure message, and the test will pass if the matcher
|
64
|
-
# matches the subject.
|
65
|
-
#
|
66
|
-
# === Example:
|
67
|
-
#
|
68
|
-
# should validate_presence_of(:first_name).with_message(/gotta be there/)
|
69
|
-
#
|
70
|
-
|
71
|
-
def should(name_or_matcher, options = {}, &blk)
|
72
|
-
if Shoulda.current_context
|
73
|
-
Shoulda.current_context.should(name_or_matcher, options, &blk)
|
74
|
-
else
|
75
|
-
context_name = self.name.gsub(/Test/, "")
|
76
|
-
context = Shoulda::Context.new(context_name, self) do
|
77
|
-
should(name_or_matcher, options, &blk)
|
78
|
-
end
|
79
|
-
context.build
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
# Allows negative tests using matchers. The matcher's description is used
|
84
|
-
# to generate a test name and negative failure message, and the test will
|
85
|
-
# pass unless the matcher matches the subject.
|
86
|
-
#
|
87
|
-
# === Example:
|
88
|
-
#
|
89
|
-
# should_not set_the_flash
|
90
|
-
def should_not(matcher)
|
91
|
-
if Shoulda.current_context
|
92
|
-
Shoulda.current_context.should_not(matcher)
|
93
|
-
else
|
94
|
-
context_name = self.name.gsub(/Test/, "")
|
95
|
-
context = Shoulda::Context.new(context_name, self) do
|
96
|
-
should_not(matcher)
|
97
|
-
end
|
98
|
-
context.build
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
# == Before statements
|
103
|
-
#
|
104
|
-
# Before statements are should statements that run before the current
|
105
|
-
# context's setup. These are especially useful when setting expectations.
|
106
|
-
#
|
107
|
-
# === Example:
|
108
|
-
#
|
109
|
-
# class UserControllerTest < Test::Unit::TestCase
|
110
|
-
# context "the index action" do
|
111
|
-
# setup do
|
112
|
-
# @users = [Factory(:user)]
|
113
|
-
# User.stubs(:find).returns(@users)
|
114
|
-
# end
|
115
|
-
#
|
116
|
-
# context "on GET" do
|
117
|
-
# setup { get :index }
|
118
|
-
#
|
119
|
-
# should respond_with(:success)
|
120
|
-
#
|
121
|
-
# # runs before "get :index"
|
122
|
-
# before_should "find all users" do
|
123
|
-
# User.expects(:find).with(:all).returns(@users)
|
124
|
-
# end
|
125
|
-
# end
|
126
|
-
# end
|
127
|
-
# end
|
128
|
-
def before_should(name, &blk)
|
129
|
-
should(name, :before => blk) { assert true }
|
130
|
-
end
|
131
|
-
|
132
|
-
# Just like should, but never runs, and instead prints an 'X' in the Test::Unit output.
|
133
|
-
def should_eventually(name, options = {}, &blk)
|
134
|
-
context_name = self.name.gsub(/Test/, "")
|
135
|
-
context = Shoulda::Context.new(context_name, self) do
|
136
|
-
should_eventually(name, &blk)
|
137
|
-
end
|
138
|
-
context.build
|
139
|
-
end
|
140
|
-
|
141
|
-
# == Contexts
|
142
|
-
#
|
143
|
-
# A context block groups should statements under a common set of setup/teardown methods.
|
144
|
-
# Context blocks can be arbitrarily nested, and can do wonders for improving the maintainability
|
145
|
-
# and readability of your test code.
|
146
|
-
#
|
147
|
-
# A context block can contain setup, should, should_eventually, and teardown blocks.
|
148
|
-
#
|
149
|
-
# class UserTest < Test::Unit::TestCase
|
150
|
-
# context "A User instance" do
|
151
|
-
# setup do
|
152
|
-
# @user = User.find(:first)
|
153
|
-
# end
|
154
|
-
#
|
155
|
-
# should "return its full name"
|
156
|
-
# assert_equal 'John Doe', @user.full_name
|
157
|
-
# end
|
158
|
-
# end
|
159
|
-
# end
|
160
|
-
#
|
161
|
-
# This code will produce the method <tt>"test: A User instance should return its full name. "</tt>.
|
162
|
-
#
|
163
|
-
# Contexts may be nested. Nested contexts run their setup blocks from out to in before each
|
164
|
-
# should statement. They then run their teardown blocks from in to out after each should statement.
|
165
|
-
#
|
166
|
-
# class UserTest < Test::Unit::TestCase
|
167
|
-
# context "A User instance" do
|
168
|
-
# setup do
|
169
|
-
# @user = User.find(:first)
|
170
|
-
# end
|
171
|
-
#
|
172
|
-
# should "return its full name"
|
173
|
-
# assert_equal 'John Doe', @user.full_name
|
174
|
-
# end
|
175
|
-
#
|
176
|
-
# context "with a profile" do
|
177
|
-
# setup do
|
178
|
-
# @user.profile = Profile.find(:first)
|
179
|
-
# end
|
180
|
-
#
|
181
|
-
# should "return true when sent :has_profile?"
|
182
|
-
# assert @user.has_profile?
|
183
|
-
# end
|
184
|
-
# end
|
185
|
-
# end
|
186
|
-
# end
|
187
|
-
#
|
188
|
-
# This code will produce the following methods
|
189
|
-
# * <tt>"test: A User instance should return its full name. "</tt>
|
190
|
-
# * <tt>"test: A User instance with a profile should return true when sent :has_profile?. "</tt>
|
191
|
-
#
|
192
|
-
# <b>Just like should statements, a context block can exist next to normal <tt>def test_the_old_way; end</tt>
|
193
|
-
# tests</b>. This means you do not have to fully commit to the context/should syntax in a test file.
|
194
|
-
|
195
|
-
def context(name, &blk)
|
196
|
-
if Shoulda.current_context
|
197
|
-
Shoulda.current_context.context(name, &blk)
|
198
|
-
else
|
199
|
-
context = Shoulda::Context.new(name, self, &blk)
|
200
|
-
context.build
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
# Returns the class being tested, as determined by the test class name.
|
205
|
-
#
|
206
|
-
# class UserTest; described_type; end
|
207
|
-
# # => User
|
208
|
-
def described_type
|
209
|
-
self.name.gsub(/Test$/, '').constantize
|
210
|
-
end
|
211
|
-
|
212
|
-
# Sets the return value of the subject instance method:
|
213
|
-
#
|
214
|
-
# class UserTest < Test::Unit::TestCase
|
215
|
-
# subject { User.first }
|
216
|
-
#
|
217
|
-
# # uses the existing user
|
218
|
-
# should validate_uniqueness_of(:email)
|
219
|
-
# end
|
220
|
-
def subject(&block)
|
221
|
-
@subject_block = block
|
222
|
-
end
|
223
|
-
|
224
|
-
def subject_block # :nodoc:
|
225
|
-
@subject_block
|
226
|
-
end
|
227
|
-
end
|
228
|
-
|
229
|
-
module InstanceMethods
|
230
|
-
# Returns an instance of the class under test.
|
231
|
-
#
|
232
|
-
# class UserTest
|
233
|
-
# should "be a user" do
|
234
|
-
# assert_kind_of User, subject # passes
|
235
|
-
# end
|
236
|
-
# end
|
237
|
-
#
|
238
|
-
# The subject can be explicitly set using the subject class method:
|
239
|
-
#
|
240
|
-
# class UserTest
|
241
|
-
# subject { User.first }
|
242
|
-
# should "be an existing user" do
|
243
|
-
# assert !subject.new_record? # uses the first user
|
244
|
-
# end
|
245
|
-
# end
|
246
|
-
#
|
247
|
-
# The subject is used by all macros that require an instance of the class
|
248
|
-
# being tested.
|
249
|
-
def subject
|
250
|
-
@shoulda_subject ||= construct_subject
|
251
|
-
end
|
252
|
-
|
253
|
-
def subject_block # :nodoc:
|
254
|
-
(@shoulda_context && @shoulda_context.subject_block) || self.class.subject_block
|
255
|
-
end
|
256
|
-
|
257
|
-
def get_instance_of(object_or_klass) # :nodoc:
|
258
|
-
if object_or_klass.is_a?(Class)
|
259
|
-
object_or_klass.new
|
260
|
-
else
|
261
|
-
object_or_klass
|
262
|
-
end
|
263
|
-
end
|
264
|
-
|
265
|
-
def instance_variable_name_for(klass) # :nodoc:
|
266
|
-
klass.to_s.split('::').last.underscore
|
267
|
-
end
|
268
|
-
|
269
|
-
private
|
270
|
-
|
271
|
-
def construct_subject
|
272
|
-
if subject_block
|
273
|
-
instance_eval(&subject_block)
|
274
|
-
else
|
275
|
-
get_instance_of(self.class.described_type)
|
276
|
-
end
|
277
|
-
end
|
278
|
-
end
|
279
|
-
|
280
|
-
class Context # :nodoc:
|
281
|
-
|
282
|
-
attr_accessor :name # my name
|
283
|
-
attr_accessor :parent # may be another context, or the original test::unit class.
|
284
|
-
attr_accessor :subcontexts # array of contexts nested under myself
|
285
|
-
attr_accessor :setup_blocks # blocks given via setup methods
|
286
|
-
attr_accessor :teardown_blocks # blocks given via teardown methods
|
287
|
-
attr_accessor :shoulds # array of hashes representing the should statements
|
288
|
-
attr_accessor :should_eventuallys # array of hashes representing the should eventually statements
|
289
|
-
attr_accessor :subject_block
|
290
|
-
|
291
|
-
def initialize(name, parent, &blk)
|
292
|
-
Shoulda.add_context(self)
|
293
|
-
self.name = name
|
294
|
-
self.parent = parent
|
295
|
-
self.setup_blocks = []
|
296
|
-
self.teardown_blocks = []
|
297
|
-
self.shoulds = []
|
298
|
-
self.should_eventuallys = []
|
299
|
-
self.subcontexts = []
|
300
|
-
|
301
|
-
merge_block(&blk)
|
302
|
-
Shoulda.remove_context
|
303
|
-
end
|
304
|
-
|
305
|
-
def merge_block(&blk)
|
306
|
-
blk.bind(self).call
|
307
|
-
end
|
308
|
-
|
309
|
-
def context(name, &blk)
|
310
|
-
self.subcontexts << Context.new(name, self, &blk)
|
311
|
-
end
|
312
|
-
|
313
|
-
def setup(&blk)
|
314
|
-
self.setup_blocks << blk
|
315
|
-
end
|
316
|
-
|
317
|
-
def teardown(&blk)
|
318
|
-
self.teardown_blocks << blk
|
319
|
-
end
|
320
|
-
|
321
|
-
def should(name_or_matcher, options = {}, &blk)
|
322
|
-
if name_or_matcher.respond_to?(:description) && name_or_matcher.respond_to?(:matches?)
|
323
|
-
name = name_or_matcher.description
|
324
|
-
blk = lambda { assert_accepts name_or_matcher, subject }
|
325
|
-
else
|
326
|
-
name = name_or_matcher
|
327
|
-
end
|
328
|
-
|
329
|
-
if blk
|
330
|
-
self.shoulds << { :name => name, :before => options[:before], :block => blk }
|
331
|
-
else
|
332
|
-
self.should_eventuallys << { :name => name }
|
333
|
-
end
|
334
|
-
end
|
335
|
-
|
336
|
-
def should_not(matcher)
|
337
|
-
name = matcher.description
|
338
|
-
blk = lambda { assert_rejects matcher, subject }
|
339
|
-
self.shoulds << { :name => "not #{name}", :block => blk }
|
340
|
-
end
|
341
|
-
|
342
|
-
def should_eventually(name, &blk)
|
343
|
-
self.should_eventuallys << { :name => name, :block => blk }
|
344
|
-
end
|
345
|
-
|
346
|
-
def subject(&block)
|
347
|
-
self.subject_block = block
|
348
|
-
end
|
349
|
-
|
350
|
-
def subject_block
|
351
|
-
return @subject_block if @subject_block
|
352
|
-
parent.subject_block
|
353
|
-
end
|
354
|
-
|
355
|
-
def full_name
|
356
|
-
parent_name = parent.full_name if am_subcontext?
|
357
|
-
return [parent_name, name].join(" ").strip
|
358
|
-
end
|
359
|
-
|
360
|
-
def am_subcontext?
|
361
|
-
parent.is_a?(self.class) # my parent is the same class as myself.
|
362
|
-
end
|
363
|
-
|
364
|
-
def test_unit_class
|
365
|
-
am_subcontext? ? parent.test_unit_class : parent
|
366
|
-
end
|
367
|
-
|
368
|
-
def create_test_from_should_hash(should)
|
369
|
-
test_name = ["test:", full_name, "should", "#{should[:name]}. "].flatten.join(' ').to_sym
|
370
|
-
|
371
|
-
if test_unit_class.instance_methods.include?(test_name.to_s)
|
372
|
-
warn " * WARNING: '#{test_name}' is already defined"
|
373
|
-
end
|
374
|
-
|
375
|
-
context = self
|
376
|
-
test_unit_class.send(:define_method, test_name) do
|
377
|
-
@shoulda_context = context
|
378
|
-
begin
|
379
|
-
context.run_parent_setup_blocks(self)
|
380
|
-
should[:before].bind(self).call if should[:before]
|
381
|
-
context.run_current_setup_blocks(self)
|
382
|
-
should[:block].bind(self).call
|
383
|
-
ensure
|
384
|
-
context.run_all_teardown_blocks(self)
|
385
|
-
end
|
386
|
-
end
|
387
|
-
end
|
388
|
-
|
389
|
-
def run_all_setup_blocks(binding)
|
390
|
-
run_parent_setup_blocks(binding)
|
391
|
-
run_current_setup_blocks(binding)
|
392
|
-
end
|
393
|
-
|
394
|
-
def run_parent_setup_blocks(binding)
|
395
|
-
self.parent.run_all_setup_blocks(binding) if am_subcontext?
|
396
|
-
end
|
397
|
-
|
398
|
-
def run_current_setup_blocks(binding)
|
399
|
-
setup_blocks.each do |setup_block|
|
400
|
-
setup_block.bind(binding).call
|
401
|
-
end
|
402
|
-
end
|
403
|
-
|
404
|
-
def run_all_teardown_blocks(binding)
|
405
|
-
teardown_blocks.reverse.each do |teardown_block|
|
406
|
-
teardown_block.bind(binding).call
|
407
|
-
end
|
408
|
-
self.parent.run_all_teardown_blocks(binding) if am_subcontext?
|
409
|
-
end
|
410
|
-
|
411
|
-
def print_should_eventuallys
|
412
|
-
should_eventuallys.each do |should|
|
413
|
-
test_name = [full_name, "should", "#{should[:name]}. "].flatten.join(' ')
|
414
|
-
puts " * DEFERRED: " + test_name
|
415
|
-
end
|
416
|
-
end
|
417
|
-
|
418
|
-
def build
|
419
|
-
shoulds.each do |should|
|
420
|
-
create_test_from_should_hash(should)
|
421
|
-
end
|
422
|
-
|
423
|
-
subcontexts.each { |context| context.build }
|
424
|
-
|
425
|
-
print_should_eventuallys
|
426
|
-
end
|
427
|
-
|
428
|
-
def method_missing(method, *args, &blk)
|
429
|
-
test_unit_class.send(method, *args, &blk)
|
430
|
-
end
|
431
|
-
|
432
|
-
end
|
433
|
-
end
|