Report abuse


			
# decribe_properties(model, attribute, valid_vales, invalid_values) # {{{
# Ensures that a model 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 +model+ arg should be the Model class
# The +attribute+ arg must be a symbol.
# The +valid_values+ and +invalid_values+ args must be Arrays in this format:
#   [ 'Description', 'value', 'Description', 'value' ... ]
# Eg:
#   [ 'multiple spaces', '123 A Street With A Long Name',
#     "a '",             "123 O'Connor Ave" ]
#
def describe_attribute(model, attribute, valid_values, invalid_values)
  raise "the 1st argument (model) should be a Model class object"                unless attribute.is_a? Class
  raise "the 2nd argument (attribute) should be a Symbol"                        unless attribute.is_a? Symbol
  raise "the 3rd argument (valid_values) should be an Array of valid values"     unless attribute.is_a? Array
  raise "the 4th argument (invalid_values) should be an Array of invalid values" unless attribute.is_a? Array

  describe "#{model}##{attribute}" do
    valid_values.in_groups_of(2) do |description, value|
      it "should be valid with #{description}" do
        p = build_property({:"#{attribute}" => value})
        p.save
        p.should be_valid
        p.should have(0).errors_on(:"#{attribute}")
      end 
    end 

    invalid_values.in_groups_of(2) do |description, value|
      it "should not be valid with #{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 = [ # {{{
  'multiple spaces',                   '267 A Street With A Long Name',
  '1 letter after the street number',  '267B Albany Ave',
  "a '",                               "123 S'omewhere Street",
  'a -',                               '123 Some-Where',
  'a ,',                               '123 Some, Weird Street',
  'only 4 characters',                 '1 Fo',     
  'only 5 characters',                 '1 Foo',
  '127 characters',                    '123 Almost at maximum lengthxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  '128 characters',                    '123 Maximum lengthxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  ] # }}}

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

describe_attribute Property, :address, valid_addresses, invalid_addresses