0
@@ -32,13 +32,8 @@ module ThoughtBot # :nodoc:
0
def should_require_attributes(*attributes)
0
message = get_options!(attributes, :message)
0
attributes.each do |attribute|
0
- should "require #{attribute} to be set" do
0
- assert_bad_value(object, attribute, nil, message)
0
+ should_not_allow_values_for attribute, nil, :message => message, :should => "require #{attribute} to be set"
0
@@ -144,16 +139,19 @@ module ThoughtBot # :nodoc:
0
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
# Regexp or string. Default = <tt>/invalid/</tt>
0
+ # * <tt>:should</tt> - an override of the default should test name.
0
+ # Usually only used by other macros.
0
# should_not_allow_values_for :isbn, "bad 1", "bad 2"
0
def should_not_allow_values_for(attribute, *bad_values)
0
- message
= get_options!(bad_values, :message)
0
+ message
, name = get_options!(bad_values, :message, :should)
0
- should "not allow #{attribute} to be set to #{v.inspect}" do
0
+ name ||= "not allow #{attribute} to be set to #{v.inspect}"
0
assert_bad_value(object, attribute, v, message)
0
@@ -163,16 +161,24 @@ module ThoughtBot # :nodoc:
0
# Ensures that the attribute can be set to the given values.
0
# Requires an existing record
0
+ # * <tt>:message</tt> - value the test shouldn't find in <tt>errors.on(:attribute)</tt>.
0
+ # Regexp or string. Default = <tt>//</tt>
0
+ # * <tt>:should</tt> - an override of the default should test name.
0
+ # Usually only used by other macros.
0
# should_allow_values_for :isbn, "isbn 1 2345 6789 0", "ISBN 1-2345-6789-0"
0
def should_allow_values_for(attribute, *good_values)
0
- get_options!(good_values)
0
+ message, name = get_options!(good_values, :message, :should)
0
good_values.each do |v|
0
- should "allow #{attribute} to be set to #{v.inspect}" do
0
+ name ||= "allow #{attribute} to be set to #{v.inspect}"
0
- assert_good_value(object, attribute, v
)
0
+ assert_good_value(object, attribute, v
, message)
0
@@ -194,39 +200,26 @@ module ThoughtBot # :nodoc:
0
short_message ||= /short/
0
long_message ||= /long/
0
min_length = range.first
0
max_length = range.last
0
same_length = (min_length == max_length)
0
- should "not allow #{attribute} to be less than #{min_length} chars long" do
0
- min_value = "x" * (min_length - 1)
0
- assert_bad_value(object, attribute, min_value, short_message)
0
+ min_value = "x" * (min_length - 1)
0
+ should_not_allow_values_for attribute, min_value, :message => short_message, :should => "not allow #{attribute} to be less than #{min_length} chars long"
0
- should "allow #{attribute} to be exactly #{min_length} chars long" do
0
- min_value = "x" * min_length
0
- assert_good_value(object, attribute, min_value, short_message)
0
+ min_value = "x" * min_length
0
+ should_allow_values_for attribute, min_value, :message => short_message, :should => "allow #{attribute} to be exactly #{min_length} chars long"
0
- should "not allow #{attribute} to be more than #{max_length} chars long" do
0
- max_value = "x" * (max_length + 1)
0
- assert_bad_value(object, attribute, max_value, long_message)
0
+ max_value = "x" * (max_length + 1)
0
+ should_not_allow_values_for attribute, max_value, :message => long_message, :should => "not allow #{attribute} to be more than #{max_length} chars long"
0
- should "allow #{attribute} to be exactly #{max_length} chars long" do
0
- max_value = "x" * max_length
0
- assert_good_value(object, attribute, max_value, long_message)
0
+ max_value = "x" * max_length
0
+ should_allow_values_for attribute, max_value, :message => long_message, :should => "allow #{attribute} to be exactly #{max_length} chars long"
0
@@ -244,20 +237,13 @@ module ThoughtBot # :nodoc:
0
short_message = get_options!([opts], :short_message)
0
short_message ||= /short/
0
min_value = "x" * (min_length - 1)
0
- should "not allow #{attribute} to be less than #{min_length} chars long" do
0
- assert_bad_value(object, attribute, min_value, short_message)
0
- should "allow #{attribute} to be at least #{min_length} chars long" do
0
- valid_value = "x" * (min_length)
0
- assert_good_value(object, attribute, valid_value, short_message)
0
+ should_not_allow_values_for attribute, min_value, :message => short_message, :should => "not allow #{attribute} to be less than #{min_length} chars long"
0
+ valid_value = "x" * (min_length)
0
+ should_allow_values_for attribute, valid_value, :message => short_message, :should => "allow #{attribute} to be at least #{min_length} chars long"
0
# Ensures that the length of the attribute is exactly a certain length
0
@@ -274,25 +260,14 @@ module ThoughtBot # :nodoc:
0
message = get_options!([opts], :message)
0
message ||= /wrong length/
0
- should "not allow #{attribute} to be less than #{length} chars long" do
0
- min_value = "x" * (length - 1)
0
- assert_bad_value(object, attribute, min_value, message)
0
+ min_value = "x" * (length - 1)
0
+ should_not_allow_values_for attribute, min_value, :message => message, :should => "not allow #{attribute} to be less than #{length} chars long"
0
- should "not allow #{attribute} to be greater than #{length} chars long" do
0
- max_value = "x" * (length + 1)
0
- assert_bad_value(object, attribute, max_value, message)
0
+ max_value = "x" * (length + 1)
0
+ should_not_allow_values_for attribute, max_value, :message => message, :should => "not allow #{attribute} to be greater than #{length} chars long"
0
- should "allow #{attribute} to be #{length} chars long" do
0
- valid_value = "x" * (length)
0
- assert_good_value(object, attribute, valid_value, message)
0
+ valid_value = "x" * (length)
0
+ should_allow_values_for attribute, valid_value, :message => message, :should => "allow #{attribute} to be #{length} chars long"
0
# Ensure that the attribute is in the range specified
0
@@ -312,33 +287,13 @@ module ThoughtBot # :nodoc:
0
low_message ||= /included/
0
high_message ||= /included/
0
- should "not allow #{attribute} to be less than #{min}" do
0
- assert_bad_value(object, attribute, v, low_message)
0
- should "allow #{attribute} to be #{min}" do
0
- assert_good_value(object, attribute, v, low_message)
0
- should "not allow #{attribute} to be more than #{max}" do
0
- assert_bad_value(object, attribute, v, high_message)
0
- should "allow #{attribute} to be #{max}" do
0
- assert_good_value(object, attribute, v, high_message)
0
+ should_not_allow_values_for attribute, (min - 1), :message => low_message, :should => "not allow #{attribute} to be less than #{min}"
0
+ should_allow_values_for attribute, min, :message => low_message, :should => "allow #{attribute} to be #{min}"
0
+ should_not_allow_values_for attribute, (max + 1), :message => high_message, :should => "not allow #{attribute} to be more than #{max}"
0
+ should_allow_values_for attribute, max, :message => high_message, :should => "allow #{attribute} to be #{max}"
0
# Ensure that the attribute is numeric
0
@@ -354,13 +309,8 @@ module ThoughtBot # :nodoc:
0
def should_only_allow_numeric_values_for(*attributes)
0
message = get_options!(attributes, :message)
0
attributes.each do |attribute|
0
- attribute = attribute.to_sym
0
- should "only allow numeric values for #{attribute}" do
0
- assert_bad_value(object, attribute, "abcd", message)
0
+ should_not_allow_values_for attribute, "abcd", :message => message, :should => "only allow numeric values for #{attribute}"
0
@@ -593,13 +543,9 @@ module ThoughtBot # :nodoc:
0
def should_require_acceptance_of(*attributes)
0
message = get_options!(attributes, :message)
0
message ||= /must be accepted/
0
attributes.each do |attribute|
0
- should "require #{attribute} to be accepted" do
0
- assert_bad_value(object, attribute, false, message)
0
+ should_not_allow_values_for attribute, false, :message => message, :should => "require #{attribute} to be accepted"