Fix some deprecation warnings in the file_column plugin
[rails.git] / vendor / plugins / validates_email_format_of / shoulda_macros / validates_email_format_of.rb
1 module ValidatesEmailFormatOf
2   module Shoulda
3     def should_validate_email_format_of(field)
4       metaclass = (class << self; self; end)
5       metaclass.send(:define_method,:should_allow_values) do |klass,*values|
6         should_allow_values_for(field, *values)
7       end
8       metaclass.send(:define_method,:should_not_allow_values) do |klass, *values|
9         should_not_allow_values_for(field, values, :message => /valid e-mail/)
10       end
11       should_validate_email_format_of_klass(model_class, field)
12     end
13
14     def should_validate_email_format_of_klass(klass, field)
15       context 'Typical valid email' do
16         should_allow_values(klass,
17           'valid@example.com',
18           'Valid@test.example.com',              
19           'valid+valid123@test.example.com',     
20           'valid_valid123@test.example.com',     
21           'valid-valid+123@test.example.co.uk',  
22           'valid-valid+1.23@test.example.com.au',
23           'valid@example.co.uk',                 
24           'v@example.com',                       
25           'valid@example.ca',                    
26           'valid_@example.com',                  
27           'valid123.456@example.org',            
28           'valid123.456@example.travel',         
29           'valid123.456@example.museum',         
30           'valid@example.mobi',                  
31           'valid@example.info',                  
32           'valid-@example.com')
33       end
34       
35       context 'valid email from RFC 3696, page 6' do
36         should_allow_values(klass,
37           'customer/department=shipping@example.com',
38           '$A12345@example.com',
39           '!def!xyz%abc@example.com',
40           '_somename@example.com')
41       end
42       
43       context 'valid email with apostrophe' do
44         should_allow_values(klass, "test'test@example.com")
45       end
46       
47       context 'valid email from http://www.rfc-editor.org/errata_search.php?rfc=3696' do
48         should_allow_values(klass,
49           '"Abc\@def"@example.com',     
50           '"Fred\ Bloggs"@example.com',
51           '"Joe.\\Blow"@example.com')
52       end
53       
54       context 'Typical invalid email' do
55         should_not_allow_values(klass,
56           'invalid@example-com',
57           'invalid@example.com.',
58           'invalid@example.com_',
59           'invalid@example.com-',
60           'invalid-example.com',
61           'invalid@example.b#r.com',
62           'invalid@example.c',
63           'invali d@example.com',
64           'invalidexample.com',
65           'invalid@example.')
66       end
67       
68       context 'invalid email with period starting local part' do
69         should_not_allow_values(klass,'.invalid@example.com')
70       end
71       
72       context 'invalid email with period ending local part' do
73         should_not_allow_values(klass, 'invalid.@example.com')
74       end
75       
76       context 'invalid email with consecutive periods' do
77         should_not_allow_values(klass, 'invali..d@example.com')
78       end
79       
80       # corrected in http://www.rfc-editor.org/errata_search.php?rfc=3696
81       context 'invalid email from http://tools.ietf.org/html/rfc3696, page 5' do
82         should_not_allow_values(klass,
83           'Fred\ Bloggs_@example.com',
84           'Abc\@def+@example.com',
85           'Joe.\\Blow@example.com')
86       end
87
88       context 'invalid email exceeding length limits' do
89         should_not_allow_values(klass,
90           "#{'a' * 65}@example.com",
91           "test@#{'a'*252}.com")
92       end
93     end
94   end
95 end
96
97 Test::Unit::TestCase.extend(ValidatesEmailFormatOf::Shoulda)