Report abuse


			
# decribe_properties(attribute, valid_vales, invalid_values) # {{{
# Ensures that a Property with the specified attribute is:
#   -Valid for each of the values in the 'valid_values' array.
#   -Invalid for each of the values in the 'invalid_values' array.
#
# The 'attribute' arg must be a symbol.
# The '*_values' args must be arrays in this format:
#   [ 'Description', 'value' ]
# Eg:
#   [ 'can have multiple spaces', '123 A Street With A Long Name',
#     "can have a '",             "123 O'Connor Ave" ]
#
# Returns
#   -1 if the 'attribute' arg is invalid.
#   -2 if the 'valid_values' arg is invalid.
#   -3 if the 'invalid_values' arg is invalid.
#
def describe_properties(attribute, valid_values, invalid_values)
  return -1 unless attribute.is_a?      Symbol
  return -2 unless valid_values.is_a?   Array
  return -3 unless invalid_values.is_a? Array

  describe Property, "attribute '#{attribute}' that is valid" do
    # Ensure that each valid value produces a valid property and no error message.
    valid_values.in_groups_of(2) do |description, value|
      it description do
        p = build_property({:"#{attribute}" => value})
        p.save
        p.should be_valid
        p.should have(0).error_on(:"#{attribute}")
      end 
    end 

    # Ensure that each invalid value produces an invalid property and related error message.
    invalid_values.in_groups_of(2) do |description, value|
      it description do
        p = build_property({:"#{attribute}" => value})
        p.save
        p.should_not be_valid
        p.should have_at_least(1).error_on(:"#{attribute}")
      end 
    end 
  end 
end # }}}


# Test data {{{
valid_addresses = [ # {{{
  'can have multiple spaces',                   '267 A Street With A Long Name',
  'can have 1 letter after the street number',  '267B Albany Ave',
  "can have a '",                               "123 S'omewhere Street",
  'can have a -',                               '123 Some-Where',
  'can have a ,',                               '123 Some, Weird Street',
  'can have only 4 characters',                 '1 Fo',     
  'can have only 5 characters',                 '1 Foo',
  'can have 127 characters',                    '123 Almost at maximum lengthxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'can have 128 characters',                    '123 Maximum lengthxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  ] # }}}

invalid_addresses = [ # {{{
  "can't be empty",                               '',
  "can't be only 3 characters",                   'xxx',
  "can't be missing a street number",             'Bloor Street',
  "can't have 2 letters after the street number", '123AB Some Street',
  "can't have a \"",                              '123 Double " Quote',
  "can't have an !",                              '123 Exclamation!',
  "can't have an &",                              '123 Amp&ersand',
  "can't have a 1-letter street name",            '123 F',
  "can't be 129 characters",                      '123 Too longxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  ] # }}}
# End test data. }}}


describe_properties :address, valid_addresses, invalid_addresses