Skip to content

Commit

Permalink
Changed macros to use should with a matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Jun 7, 2010
1 parent 8f983a9 commit f1f371e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 115 deletions.
97 changes: 19 additions & 78 deletions lib/shoulda/active_record/macros.rb
Expand Up @@ -36,10 +36,7 @@ def should_validate_presence_of(*attributes)
message = get_options!(attributes, :message)

attributes.each do |attribute|
matcher = validate_presence_of(attribute).with_message(message)
should matcher.description do
assert_accepts(matcher, subject)
end
should validate_presence_of(attribute).with_message(message)
end
end

Expand Down Expand Up @@ -70,9 +67,7 @@ def should_validate_uniqueness_of(*attributes)
matcher = validate_uniqueness_of(attribute).
with_message(message).scoped_to(scope)
matcher = matcher.case_insensitive unless case_sensitive
should matcher.description do
assert_accepts(matcher, subject)
end
should matcher
end
end

Expand All @@ -84,10 +79,7 @@ def should_allow_mass_assignment_of(*attributes)
get_options!(attributes)

attributes.each do |attribute|
matcher = allow_mass_assignment_of(attribute)
should matcher.description do
assert_accepts matcher, subject
end
should allow_mass_assignment_of(attribute)
end
end

Expand All @@ -99,10 +91,7 @@ def should_not_allow_mass_assignment_of(*attributes)
get_options!(attributes)

attributes.each do |attribute|
matcher = allow_mass_assignment_of(attribute)
should "not #{matcher.description}" do
assert_rejects matcher, subject
end
should_not allow_mass_assignment_of(attribute)
end
end

Expand All @@ -114,10 +103,7 @@ def should_have_readonly_attributes(*attributes)
get_options!(attributes)

attributes.each do |attribute|
matcher = have_readonly_attribute(attribute)
should matcher.description do
assert_accepts matcher, subject
end
should have_readonly_attribute(attribute)
end
end

Expand All @@ -134,10 +120,7 @@ def should_have_readonly_attributes(*attributes)
def should_not_allow_values_for(attribute, *bad_values)
message = get_options!(bad_values, :message)
bad_values.each do |value|
matcher = allow_value(value).for(attribute).with_message(message)
should "not #{matcher.description}" do
assert_rejects matcher, subject
end
should_not allow_value(value).for(attribute).with_message(message)
end
end

Expand All @@ -149,10 +132,7 @@ def should_not_allow_values_for(attribute, *bad_values)
def should_allow_values_for(attribute, *good_values)
get_options!(good_values)
good_values.each do |value|
matcher = allow_value(value).for(attribute)
should matcher.description do
assert_accepts matcher, subject
end
should allow_value(value).for(attribute)
end
end

Expand All @@ -171,15 +151,11 @@ def should_ensure_length_in_range(attribute, range, opts = {})
short_message, long_message = get_options!([opts],
:short_message,
:long_message)
matcher = ensure_length_of(attribute).
should ensure_length_of(attribute).
is_at_least(range.first).
with_short_message(short_message).
is_at_most(range.last).
with_long_message(long_message)

should matcher.description do
assert_accepts matcher, subject
end
end

# Ensures that the length of the attribute is at least a certain length
Expand All @@ -194,13 +170,9 @@ def should_ensure_length_in_range(attribute, range, opts = {})
def should_ensure_length_at_least(attribute, min_length, opts = {})
short_message = get_options!([opts], :short_message)

matcher = ensure_length_of(attribute).
should ensure_length_of(attribute).
is_at_least(min_length).
with_short_message(short_message)

should matcher.description do
assert_accepts matcher, subject
end
end

# Ensures that the length of the attribute is exactly a certain length
Expand All @@ -214,13 +186,9 @@ def should_ensure_length_at_least(attribute, min_length, opts = {})
#
def should_ensure_length_is(attribute, length, opts = {})
message = get_options!([opts], :message)
matcher = ensure_length_of(attribute).
should ensure_length_of(attribute).
is_equal_to(length).
with_message(message)

should matcher.description do
assert_accepts matcher, subject
end
end

# Ensure that the attribute is in the range specified
Expand All @@ -239,14 +207,11 @@ def should_ensure_value_in_range(attribute, range, opts = {})
:message,
:low_message,
:high_message)
matcher = ensure_inclusion_of(attribute).
should ensure_inclusion_of(attribute).
in_range(range).
with_message(message).
with_low_message(low_message).
with_high_message(high_message)
should matcher.description do
assert_accepts matcher, subject
end
end

# Ensure that the attribute is numeric
Expand All @@ -261,11 +226,8 @@ def should_ensure_value_in_range(attribute, range, opts = {})
def should_validate_numericality_of(*attributes)
message = get_options!(attributes, :message)
attributes.each do |attribute|
matcher = validate_numericality_of(attribute).
should validate_numericality_of(attribute).
with_message(message)
should matcher.description do
assert_accepts matcher, subject
end
end
end

Expand All @@ -285,10 +247,7 @@ def should_validate_numericality_of(*attributes)
def should_have_many(*associations)
through, dependent = get_options!(associations, :through, :dependent)
associations.each do |association|
matcher = have_many(association).through(through).dependent(dependent)
should matcher.description do
assert_accepts(matcher, subject)
end
should have_many(association).through(through).dependent(dependent)
end
end

Expand All @@ -305,10 +264,7 @@ def should_have_many(*associations)
def should_have_one(*associations)
dependent, through = get_options!(associations, :dependent, :through)
associations.each do |association|
matcher = have_one(association).dependent(dependent).through(through)
should matcher.description do
assert_accepts(matcher, subject)
end
should have_one(association).dependent(dependent).through(through)
end
end

Expand All @@ -321,10 +277,7 @@ def should_have_and_belong_to_many(*associations)
get_options!(associations)

associations.each do |association|
matcher = have_and_belong_to_many(association)
should matcher.description do
assert_accepts(matcher, subject)
end
should have_and_belong_to_many(association)
end
end

Expand All @@ -335,10 +288,7 @@ def should_have_and_belong_to_many(*associations)
def should_belong_to(*associations)
dependent = get_options!(associations, :dependent)
associations.each do |association|
matcher = belong_to(association).dependent(dependent)
should matcher.description do
assert_accepts(matcher, subject)
end
should belong_to(association).dependent(dependent)
end
end

Expand Down Expand Up @@ -388,14 +338,11 @@ def should_have_db_columns(*columns)
get_options!(columns, :type, :precision, :limit,
:default, :null, :scale, :sql_type)
columns.each do |name|
matcher = have_db_column(name).
should have_db_column(name).
of_type(column_type).
with_options(:precision => precision, :limit => limit,
:default => default, :null => null,
:scale => scale, :sql_type => sql_type)
should matcher.description do
assert_accepts(matcher, subject)
end
end
end

Expand All @@ -421,10 +368,7 @@ def should_have_db_indices(*columns)
unique = get_options!(columns, :unique)

columns.each do |column|
matcher = have_db_index(column).unique(unique)
should matcher.description do
assert_accepts(matcher, subject)
end
should have_db_index(column).unique(unique)
end
end

Expand All @@ -443,10 +387,7 @@ def should_validate_acceptance_of(*attributes)
message = get_options!(attributes, :message)

attributes.each do |attribute|
matcher = validate_acceptance_of(attribute).with_message(message)
should matcher.description do
assert_accepts matcher, subject
end
should validate_acceptance_of(attribute).with_message(message)
end
end
end
Expand Down
4 changes: 0 additions & 4 deletions test/unit/post_test.rb
Expand Up @@ -12,8 +12,4 @@ class PostTest < ActiveSupport::TestCase
should_validate_presence_of :body, :message => /wtf/
should_validate_presence_of :title
should_validate_numericality_of :user_id

should_fail do
should_validate_uniqueness_of :title, :case_sensitive => false
end
end
4 changes: 0 additions & 4 deletions test/unit/tag_test.rb
Expand Up @@ -8,8 +8,4 @@ class TagTest < ActiveSupport::TestCase

should_not_allow_mass_assignment_of :secret
should_allow_mass_assignment_of :name

should_fail do
should_not_allow_mass_assignment_of :name
end
end
29 changes: 0 additions & 29 deletions test/unit/user_test.rb
Expand Up @@ -18,12 +18,6 @@ class UserTest < ActiveSupport::TestCase
should_have_db_index [:email, :name], :unique => true
should_have_db_index :age, :unique => false

should_fail do
should_have_db_index :phone
should_have_db_index :email, :unique => false
should_have_db_index :age, :unique => true
end

should_not_allow_values_for :email, "blah", "b lah"
should_allow_values_for :email, "a@b.com", "asdf@asdf.com"
should_allow_values_for :age, 1, 10, 99
Expand All @@ -33,20 +27,6 @@ class UserTest < ActiveSupport::TestCase
should_ensure_value_in_range :age, 1..100, :low_message => /greater/,
:high_message => /less/

context "with a different low message" do
should_fail do
should_ensure_value_in_range :age, 1..100, :low_message => /more/,
:high_message => /less/
end
end

context "with a different high message" do
should_fail do
should_ensure_value_in_range :age, 1..100, :low_message => /greater/,
:high_message => /fewer/
end
end

should_not_allow_mass_assignment_of :password
should_have_class_methods :find, :destroy
should_have_instance_methods :email, :age, :email=, :valid?
Expand All @@ -62,14 +42,5 @@ class UserTest < ActiveSupport::TestCase

should_have_readonly_attributes :name

should_fail do
should_not_allow_mass_assignment_of :name, :age
end

should_have_one :profile, :through => :registration

should_fail do
should_have_one :profile, :through => :interview
should_have_one :address, :through => :registration
end
end

0 comments on commit f1f371e

Please sign in to comment.