ruby_regex 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -38,6 +38,7 @@ If you want to contribute send me a pull request with new regular expressions (a
38
38
 
39
39
  - Lleïr Borràs
40
40
  - Jaime Iniesta
41
+ - Fernando Guillen
41
42
 
42
43
  ---
43
44
 
data/lib/ruby_regex.rb CHANGED
@@ -45,29 +45,8 @@ module RubyRegex
45
45
  # Del.icio.us username
46
46
  DeliciousUsername = /^([a-z0-9\_\-])+$/ix
47
47
 
48
- # RFC822 Email Address Regex
49
- #
50
- # Originally written by Cal Henderson
51
- # c.f. http://iamcal.com/publish/articles/php/parsing_email/
52
- #
53
- # Translated to Ruby by Tim Fletcher, with changes suggested by Dan Kubb.
54
- #
55
- # Licensed under a Creative Commons Attribution-ShareAlike 2.5 License
56
- # http://creativecommons.org/licenses/by-sa/2.5/
57
- Email = begin
58
- qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
59
- dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'
60
- atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-' +
61
- '\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'
62
- quoted_pair = '\\x5c[\\x00-\\x7f]'
63
- domain_literal = "\\x5b(?:#{dtext}|#{quoted_pair})*\\x5d"
64
- quoted_string = "\\x22(?:#{qtext}|#{quoted_pair})*\\x22"
65
- domain_ref = atom
66
- sub_domain = "(?:#{domain_ref}|#{domain_literal})"
67
- word = "(?:#{atom}|#{quoted_string})"
68
- domain = "#{sub_domain}(?:\\x2e#{sub_domain})*"
69
- local_part = "#{word}(?:\\x2e#{word})*"
70
- addr_spec = "#{local_part}\\x40#{domain}"
71
- pattern = /\A#{addr_spec}\z/
72
- end
48
+ # Email
49
+ # From the email regex research: http://fightingforalostcause.net/misc/2006/compare-email-regex.php
50
+ # Authors: James Watts and Francisco Jose Martin Moreno
51
+ Email = /^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i
73
52
  end
@@ -0,0 +1,39 @@
1
+ valid:
2
+ - "l3tt3rsAndNumb3rs@domain.com"
3
+ - "has-dash@domain.com"
4
+ - "hasApostrophe.o'leary@domain.org"
5
+ - "uncommonTLD@domain.museum"
6
+ - "uncommonTLD@domain.travel"
7
+ - "uncommonTLD@domain.mobi"
8
+ - "countryCodeTLD@domain.uk"
9
+ - "countryCodeTLD@domain.rw"
10
+ - "lettersInDomain@911.com"
11
+ - "underscore_inLocal@domain.net"
12
+ - "subdomain@sub.domain.com"
13
+ - "local@dash-inDomain.com"
14
+ - "dot.inLocal@foo.com"
15
+ - "a@singleLetterLocal.org"
16
+ - "singleLetterDomain@x.org"
17
+ - "&*=?^+{}'~@validCharsInLocal.net"
18
+ - "foor@bar.newTLD"
19
+
20
+
21
+ invalid:
22
+ - "@missingLocal.org"
23
+ - "someone-else@127.0.0.1.26"
24
+ - "missingdomain@.com"
25
+ - "missingatSign.net"
26
+ - "missingDot@com"
27
+ - "two@@signs.com"
28
+ - "colonButNoPort@127.0.0.1:"
29
+ - ""
30
+ - ".localStartsWithDot@domain.com"
31
+ - "localEndsWithDot.@domain.com"
32
+ - "two..consecutiveDots@domain.com"
33
+ - "domainStartsWithDash@-domain.com"
34
+ - "domainEndsWithDash@domain-.com"
35
+ - "numbersInTLD@domain.c0m"
36
+ - "missingTLD@domain."
37
+ - "! #$%(),/;<>[]`|@invalidCharsInLocal.org"
38
+ - "invalidCharsInDomain@! #$%(),/;<>_[]`|.org"
39
+ - "local@SecondLevelDomainNamesAreInvalidIfTheyAreLongerThan64Charactersss.org"
@@ -2,230 +2,143 @@ require 'test/unit'
2
2
  require 'rubygems'
3
3
  require 'active_support'
4
4
  require 'active_support/test_case'
5
- require 'ruby_regex'
5
+ require File.join( File.dirname(__FILE__), '../lib/ruby_regex' )
6
6
 
7
- class RubyRegexTest < ActiveSupport::TestCase
7
+ class RubyRegexTest < ActiveSupport::TestCase
8
+
8
9
  #Username
9
10
  def test_valid_usernames
10
- usernames = ['test', 'test_test', 'test1', 'test_1']
11
- usernames.each do |username|
12
- message = build_message(message, '<?> do not pass the test', username)
13
- assert(username =~ RubyRegex::Username, username)
14
- end
11
+ check_valid_regex RubyRegex::Username, ['test', 'test_test', 'test1', 'test_1']
15
12
  end
16
13
 
17
14
  def test_invalid_usernames
18
- usernames = ['test-test', 'test.test', 'test/test', 'test@test']
19
- usernames.each do |username|
20
- message = build_message(message, '<?> do not pass the test', username)
21
- assert(username !~ RubyRegex::Username, username)
22
- end
15
+ check_invalid_regex RubyRegex::Username, ['test-test', 'test.test', 'test/test', 'test@test']
23
16
  end
24
17
 
25
18
  # DNI
26
19
  def test_valid_dnis
27
- dnis = ['40990889J', '99888777h']
28
- dnis.each do |dni|
29
- message = build_message(message, '<?> do not pass the test', dni)
30
- assert(dni =~ RubyRegex::Username, dni)
31
- end
20
+ check_valid_regex RubyRegex::Dni, ['40990889J', '99888777h']
32
21
  end
33
22
 
34
23
  def test_invalid_dnis
35
- dnis = ['90.900.900V', '90900900-V']
36
- dnis.each do |dni|
37
- message = build_message(message, '<?> do not pass the test', dni)
38
- assert(dni !~ RubyRegex::Username, dni)
39
- end
24
+ check_invalid_regex RubyRegex::Dni, ['90.900.900V', '90900900-V']
40
25
  end
41
26
 
42
27
  # Email
43
28
  def test_valid_emails
44
- emails = ['test@test.com', 'test@test.co.uk', 'test@test.es', 'test@test.info']
45
- emails.each do |email|
46
- message = build_message(message, '<?> do not pass the test', email)
47
- assert(email =~ RubyRegex::Email, email)
48
- end
29
+ check_valid_regex RubyRegex::Email, load_fixture('emails')['valid']
49
30
  end
50
31
 
51
- #TODO: 'test@test' is a valid domain, fix!!!
52
32
  def test_invalid_emails
53
- emails = ['test/test.com', 'test', 'test-test.com', 'test.test.com']
54
- emails.each do |email|
55
- message = build_message(message, '<?> do not pass the test', email)
56
- assert(email !~ RubyRegex::Email, message)
57
- end
33
+ check_invalid_regex RubyRegex::Email, load_fixture('emails')["invalid"]
58
34
  end
59
35
 
60
36
  # Domains
61
37
  def test_valid_domains
62
- domains = [ 'test.com', 'www.test.com', 'test.es', 'www.test.es', 'test.co.uk', 'www.test.co.uk', 'test.info', 'www.test.info', 'test.com.es', 'www.test.com.es']
63
- domains.each do |domain|
64
- message = build_message(message, '<?> do not pass the test', domain)
65
- assert(domain =~ RubyRegex::Domain, message)
66
- end
38
+ check_valid_regex RubyRegex::Domain, ['test.com', 'www.test.com', 'test.es', 'www.test.es', 'test.co.uk', 'www.test.co.uk', 'test.info', 'www.test.info', 'test.com.es', 'www.test.com.es', 'test-test.com']
67
39
  end
68
40
 
69
41
  def test_invalid_domains
70
- domains = [ 'test.', 'www.test.e', 'www.test.', 'test.e', '!test.com', 'test/test.com']
71
- domains.each do |domain|
72
- message = build_message(message, '<?> do not pass the test', domain)
73
- assert(domain !~ RubyRegex::Domain, message)
74
- end
42
+ check_invalid_regex RubyRegex::Domain, ['test.', 'www.test.e', 'www.test.', 'test.e', '!test.com', 'test/test.com', 'test_test.com']
75
43
  end
76
44
 
77
45
  # Url
78
46
  def test_valid_url
79
- urls = [ 'http://test.com', 'http://www.test.com', 'http://test.es/index', 'http://www.test.es/index.html',
80
- 'https://test.co.uk', 'http://www.test.co.uk/index.html?id=34&name=username']
81
- urls.each do |url|
82
- message = build_message('<?> do not pass the test', url)
83
- assert(url =~ RubyRegex::URL, message)
84
- end
47
+ check_valid_regex RubyRegex::URL, ['http://test.com', 'http://www.test.com', 'http://test.es/index', 'http://www.test.es/index.html', 'https://test.co.uk', 'http://www.test.co.uk/index.html?id=34&name=username']
85
48
  end
86
49
 
87
50
  def test_invalid_url
88
- urls = [ 'test.com', 'www.test.com', 'http://test.es-index', 'http://www.test.es?index.html']
89
- urls.each do |url|
90
- message = build_message(message, '<?> do not pass the test', url)
91
- assert(url !~ RubyRegex::URL, message)
92
- end
51
+ check_invalid_regex RubyRegex::URL, ['test.com', 'www.test.com', 'http://test.es-index', 'http://www.test.es?index.html']
93
52
  end
94
53
 
95
54
  # CreditCard
96
55
  def test_valid_credit_cards
97
- credit_cards = [ '1234123412341234', '1234 1234 1234 1234', '1234-1234-1234-1234']
98
- credit_cards.each do |credit_card|
99
- message = build_message(message, '<?> do not pass the test', credit_card)
100
- assert(credit_card =~ RubyRegex::CreditCard, message)
101
- end
56
+ check_valid_regex RubyRegex::CreditCard, [ '1234123412341234', '1234 1234 1234 1234', '1234-1234-1234-1234']
102
57
  end
103
58
 
104
59
  def test_invalid_credit_cards
105
- credit_cards = [ '1234_1234_1234_1234', '1234', '12341234', '123412341234',
106
- '1234 1234 1234 1234', '1234-1234 12341234', '123a-1234-1234-1234']
107
- credit_cards.each do |credit_card|
108
- message = build_message(message, '<?> do not pass the test', credit_card)
109
- assert(credit_card !~ RubyRegex::Domain, message)
110
- end
60
+ check_invalid_regex RubyRegex::CreditCard, ['1234_1234_1234_1234', '1234', '12341234', '123412341234', '1234 1234 1234 1234', '1234-1234 12341234', '123a-1234-1234-1234']
111
61
  end
112
62
 
113
63
  # US Social Security
114
64
  def test_valid_usss_numbers
115
- usss_numbers = [ '123-12-1234']
116
- usss_numbers.each do |usss_number|
117
- message = build_message(message, '<?> do not pass the test', usss_number)
118
- assert(usss_number =~ RubyRegex::USSocialSecurity, message)
119
- end
65
+ check_valid_regex RubyRegex::USSocialSecurity, ['123-12-1234']
120
66
  end
121
67
 
122
68
  def test_invalid_usss_numbers
123
- usss_numbers = [ '1234_1234_1234_1234', '1234', '123121234', '123_12_1234', '123 12 1234']
124
- usss_numbers.each do |usss_number|
125
- message = build_message(message, '<?> do not pass the test', usss_number)
126
- assert(usss_number !~ RubyRegex::USSocialSecurity, message)
127
- end
69
+ check_invalid_regex RubyRegex::USSocialSecurity, [ '1234_1234_1234_1234', '1234', '123121234', '123_12_1234', '123 12 1234']
128
70
  end
129
71
 
130
72
  # General Postal Code
131
73
  def test_valid_postal_codes
132
- postal_codes = [ '12345']
133
- postal_codes.each do |postal_code|
134
- message = build_message(message, '<?> do not pass the test', postal_code)
135
- assert(postal_code =~ RubyRegex::GeneralPostalCode, message)
136
- end
74
+ check_valid_regex RubyRegex::GeneralPostalCode, ['12345']
137
75
  end
138
76
 
139
77
  def test_invalid_postal_codes
140
- postal_codes = [ '1', '12', '123', '1234', '123456']
141
- postal_codes.each do |postal_code|
142
- message = build_message(message, '<?> do not pass the test', postal_code)
143
- assert(postal_code !~ RubyRegex::GeneralPostalCode, message)
144
- end
78
+ check_invalid_regex RubyRegex::GeneralPostalCode, [ '1', '12', '123', '1234', '123456']
145
79
  end
146
80
 
147
81
  # ZIP Code
148
82
  def test_valid_zip_codes
149
- zip_codes = [ '12345', '12345-1234']
150
- zip_codes.each do |zip_code|
151
- message = build_message(message, '<?> do not pass the test', zip_code)
152
- assert(zip_code =~ RubyRegex::ZIPCode, message)
153
- end
83
+ check_valid_regex RubyRegex::ZIPCode, [ '12345', '12345-1234']
154
84
  end
155
85
 
156
86
  def test_invalid_zip_codes
157
- zip_codes = [ '1', '12', '123', '1234', '123456', '12345_1234', '12345 1234', '1234-1234']
158
- zip_codes.each do |zip_code|
159
- message = build_message(message, '<?> do not pass the test', zip_code)
160
- assert(zip_code !~ RubyRegex::ZIPCode, message)
161
- end
87
+ check_invalid_regex RubyRegex::ZIPCode, [ '1', '12', '123', '1234', '123456', '12345_1234', '12345 1234', '1234-1234']
162
88
  end
163
89
 
164
90
  # Twitter usernames
165
91
  def test_valid_twitter_usernames
166
- twitter_usernames = ['ji', 'nickel84', 'sepa_rate']
167
- twitter_usernames.each do |twitter_username|
168
- message = build_message(message, '<?> does not pass the test', twitter_username)
169
- assert(twitter_username =~ RubyRegex::TwitterUsername, message)
170
- end
92
+ check_valid_regex RubyRegex::TwitterUsername, ['ji', 'nickel84', 'sepa_rate']
171
93
  end
172
94
 
173
95
  def test_invalid_twitter_usernames
174
- twitter_usernames = ['nickel 83', 'h.ppywebcoder']
175
- twitter_usernames.each do |twitter_username|
176
- message = build_message(message, '<?> does not pass the test', twitter_username)
177
- assert(twitter_username !~ RubyRegex::TwitterUsername, message)
178
- end
96
+ check_invalid_regex RubyRegex::TwitterUsername, ['nickel 83', 'h.ppywebcoder']
179
97
  end
180
98
 
181
99
  # Github usernames
182
100
  def test_valid_github_usernames
183
- github_usernames = ['ji', 'nickel84', 'sepa_rate', 'ernesto-jimenez']
184
- github_usernames.each do |github_username|
185
- message = build_message(message, '<?> does not pass the test', github_username)
186
- assert(github_username =~ RubyRegex::GithubUsername, message)
187
- end
101
+ check_valid_regex RubyRegex::GithubUsername, ['ji', 'nickel84', 'sepa_rate', 'ernesto-jimenez']
188
102
  end
189
103
 
190
104
  def test_invalid_github_usernames
191
- github_usernames = ['nickel 84', 'h.ppywebcoder']
192
- github_usernames.each do |github_username|
193
- message = build_message(message, '<?> does not pass the test', github_username)
194
- assert(github_username !~ RubyRegex::GithubUsername, message)
195
- end
105
+ check_invalid_regex RubyRegex::GithubUsername, ['nickel 84', 'h.ppywebcoder']
196
106
  end
197
107
 
198
108
  # Slideshare usernames
199
109
  def test_valid_slideshare_usernames
200
- slideshare_usernames = ['ji', 'nickel84']
201
- slideshare_usernames.each do |slideshare_username|
202
- message = build_message(message, '<?> does not pass the test', slideshare_username)
203
- assert(slideshare_username =~ RubyRegex::SlideshareUsername, message)
204
- end
110
+ check_valid_regex RubyRegex::SlideshareUsername, ['ji', 'nickel84']
205
111
  end
206
112
 
207
113
  def test_invalid_slideshare_usernames
208
- slideshare_usernames = ['nickel 84', 'h.ppywebcoder', 'sepa_rate', 'ernesto-jimenez']
209
- slideshare_usernames.each do |slideshare_username|
210
- message = build_message(message, '<?> does not pass the test', slideshare_username)
211
- assert(slideshare_username !~ RubyRegex::SlideshareUsername, message)
212
- end
114
+ check_invalid_regex RubyRegex::SlideshareUsername, ['nickel 84', 'h.ppywebcoder', 'sepa_rate', 'ernesto-jimenez']
213
115
  end
214
116
 
215
117
  # Del.icio.us usernames
216
118
  def test_valid_delicious_usernames
217
- delicious_usernames = ['ji', 'nickel84', 'sepa_rate', 'ernesto-jimenez']
218
- delicious_usernames.each do |delicious_username|
219
- message = build_message(message, '<?> does not pass the test', delicious_username)
220
- assert(delicious_username =~ RubyRegex::DeliciousUsername, message)
221
- end
119
+ check_valid_regex RubyRegex::DeliciousUsername, ['ji', 'nickel84', 'sepa_rate', 'ernesto-jimenez']
222
120
  end
223
121
 
224
122
  def test_invalid_delicious_usernames
225
- delicious_usernames = ['nickel 84', 'h.ppywebcoder']
226
- delicious_usernames.each do |delicious_username|
227
- message = build_message(message, '<?> does not pass the test', delicious_username)
228
- assert(delicious_username !~ RubyRegex::DeliciousUsername, message)
229
- end
123
+ check_invalid_regex RubyRegex::DeliciousUsername, ['nickel 84', 'h.ppywebcoder']
230
124
  end
125
+
126
+ private
127
+ def load_fixture( name )
128
+ YAML.load( File.read( File.join( File.dirname(__FILE__), 'fixtures', "#{name}.yml" ) ) )
129
+ end
130
+
131
+ def check_valid_regex(regexp, strings)
132
+ strings.each do |str|
133
+ message = build_message(message, '<?> does not pass the test', str)
134
+ assert(str =~ regexp, message)
135
+ end
136
+ end
137
+
138
+ def check_invalid_regex(regexp, strings)
139
+ strings.each do |str|
140
+ message = build_message(message, '<?> does not pass the test', str)
141
+ assert(str !~ regexp, message)
142
+ end
143
+ end
231
144
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_regex
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 19
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
8
  - 0
8
- - 5
9
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
10
11
  platform: ruby
11
12
  authors:
12
13
  - Emili Parreno
@@ -14,7 +15,7 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-05-18 00:00:00 +02:00
18
+ date: 2010-08-26 00:00:00 +02:00
18
19
  default_executable:
19
20
  dependencies: []
20
21
 
@@ -31,6 +32,8 @@ files:
31
32
  - README.rdoc
32
33
  - LICENSE
33
34
  - lib/ruby_regex.rb
35
+ - test/ruby_regex_test.rb
36
+ - test/fixtures/emails.yml
34
37
  has_rdoc: true
35
38
  homepage: http://github.com/eparreno/ruby_regex
36
39
  licenses: []
@@ -42,25 +45,30 @@ rdoc_options:
42
45
  require_paths:
43
46
  - lib
44
47
  required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
45
49
  requirements:
46
50
  - - ">="
47
51
  - !ruby/object:Gem::Version
52
+ hash: 3
48
53
  segments:
49
54
  - 0
50
55
  version: "0"
51
56
  required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
52
58
  requirements:
53
59
  - - ">="
54
60
  - !ruby/object:Gem::Version
61
+ hash: 3
55
62
  segments:
56
63
  - 0
57
64
  version: "0"
58
65
  requirements: []
59
66
 
60
67
  rubyforge_project:
61
- rubygems_version: 1.3.6
68
+ rubygems_version: 1.3.7
62
69
  signing_key:
63
70
  specification_version: 3
64
71
  summary: none
65
72
  test_files:
66
73
  - test/ruby_regex_test.rb
74
+ - test/fixtures/emails.yml