Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module RubyRegex | ||
URL = /(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$)/ix | ||
Domain = /(^$)|(^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?)?$)/ix | ||
|
||
# | ||
# RFC822 Email Address Regex | ||
# -------------------------- | ||
# | ||
# Originally written by Cal Henderson | ||
# c.f. http://iamcal.com/publish/articles/php/parsing_email/ | ||
# | ||
# Translated to Ruby by Tim Fletcher, with changes suggested by Dan Kubb. | ||
# | ||
# Licensed under a Creative Commons Attribution-ShareAlike 2.5 License | ||
# http://creativecommons.org/licenses/by-sa/2.5/ | ||
Email = begin | ||
qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]' | ||
dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]' | ||
atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-' + | ||
'\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+' | ||
quoted_pair = '\\x5c[\\x00-\\x7f]' | ||
domain_literal = "\\x5b(?:#{dtext}|#{quoted_pair})*\\x5d" | ||
quoted_string = "\\x22(?:#{qtext}|#{quoted_pair})*\\x22" | ||
domain_ref = atom | ||
sub_domain = "(?:#{domain_ref}|#{domain_literal})" | ||
word = "(?:#{atom}|#{quoted_string})" | ||
domain = "#{sub_domain}(?:\\x2e#{sub_domain})*" | ||
local_part = "#{word}(?:\\x2e#{word})*" | ||
addr_spec = "#{local_part}\\x40#{domain}" | ||
pattern = /\A#{addr_spec}\z/ | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
require 'test/unit' | ||
require 'rubygems' | ||
require 'active_support' | ||
require 'active_support/test_case' | ||
require 'ruby_regex' | ||
|
||
class RubyRegexTest < ActiveSupport::TestCase | ||
def test_valid_emails | ||
emails = ['test@test.com', 'test@test.co.uk', 'test@test.es', 'test@test.info'] | ||
emails.each do |email| | ||
message = build_message(message, '<?> do not pass the test', email) | ||
assert(email =~ RubyRegex::Email, email) | ||
end | ||
end | ||
|
||
#TODO: 'test@test' is a valid domain, fix!!! | ||
def test_invalid_emails | ||
emails = ['test/test.com', 'test', 'test-test.com', 'test.test.com'] | ||
emails.each do |email| | ||
message = build_message(message, '<?> do not pass the test', email) | ||
assert(email !~ RubyRegex::Email, message) | ||
end | ||
end | ||
|
||
def test_valid_domains | ||
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'] | ||
domains.each do |domain| | ||
message = build_message(message, '<?> do not pass the test', domain) | ||
assert(domain =~ RubyRegex::Domain, message) | ||
end | ||
end | ||
|
||
def test_invalid_domains | ||
domains = [ 'test.', 'www.test.e', 'www.test.', 'test.e', '!test.com', 'test/test.com'] | ||
domains.each do |domain| | ||
message = build_message(message, '<?> do not pass the test', domain) | ||
assert(domain !~ RubyRegex::Domain, message) | ||
end | ||
end | ||
|
||
def test_valid_url | ||
urls = [ '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'] | ||
urls.each do |url| | ||
message = build_message('<?> do not pass the test', url) | ||
assert(url =~ RubyRegex::URL, message) | ||
end | ||
end | ||
|
||
def test_invalid_url | ||
urls = [ 'test.com', 'www.test.com', 'http://test.es-index', 'http://www.test.es?index.html'] | ||
urls.each do |url| | ||
message = build_message(message, '<?> do not pass the test', url) | ||
assert(url !~ RubyRegex::URL, message) | ||
end | ||
end | ||
end |