Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eparreno committed Mar 2, 2010
1 parent de5ece2 commit dcf7b0b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
32 changes: 32 additions & 0 deletions ruby_regex.rb
@@ -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
57 changes: 57 additions & 0 deletions ruby_regex_test.rb
@@ -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

0 comments on commit dcf7b0b

Please sign in to comment.