ruby_regex 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/ruby_regex.rb +11 -1
  2. data/test/ruby_regex_test.rb +63 -38
  3. metadata +4 -3
data/lib/ruby_regex.rb CHANGED
@@ -52,5 +52,15 @@ module RubyRegex
52
52
 
53
53
  # UUID
54
54
  # Validates a UUID as defined: http://en.wikipedia.org/wiki/Universally_unique_identifier
55
- UUID = /\A(\h{32}|\h{8}-\h{4}-\h{4}-\h{4}-\h{12})\z/
55
+ UUID = /\A([0-9a-fA-F]{32}|[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})\z/
56
+
57
+ # Date DB format YYYY-MM-DD
58
+ # I know it will validate 2001-02-31 but is I think we are focusing in formats more than in parsing
59
+ DBDate = /\A\d{4}-(#{("01".."12").to_a.join("|")})-(#{("01".."31").to_a.join("|")})\z/
60
+
61
+ # Date Time DB format YYYY-MM-DD hh:mm:ss
62
+ DBDateTime = /\A\d{4}-(#{("01".."12").to_a.join("|")})-(#{("01".."31").to_a.join("|")})\s(#{("00".."23").to_a.join("|")}):(#{("00".."59").to_a.join("|")}):(#{("00".."59").to_a.join("|")})\z/
63
+
64
+ # SpanishBankAccountNumber
65
+ SpanishBankAccountNumber = /\A\d{4}[ -]?\d{4}[ -]?\d{2}[ -]?\d{10}\z/
56
66
  end
@@ -1,29 +1,27 @@
1
1
  require 'test/unit'
2
- require 'rubygems'
3
- require 'active_support'
4
- require 'active_support/test_case'
2
+ require 'yaml'
5
3
  require File.join( File.dirname(__FILE__), '../lib/ruby_regex' )
6
4
 
7
- class RubyRegexTest < ActiveSupport::TestCase
8
-
5
+ class RubyRegexTest < Test::Unit::TestCase
6
+
9
7
  #Username
10
8
  def test_valid_usernames
11
9
  check_valid_regex RubyRegex::Username, ['test', 'test_test', 'test1', 'test_1']
12
10
  end
13
-
11
+
14
12
  def test_invalid_usernames
15
13
  check_invalid_regex RubyRegex::Username, ['test-test', 'test.test', 'test/test', 'test@test']
16
14
  end
17
-
15
+
18
16
  # DNI
19
17
  def test_valid_dnis
20
18
  check_valid_regex RubyRegex::Dni, ['40990889J', '99888777h']
21
19
  end
22
-
20
+
23
21
  def test_invalid_dnis
24
22
  check_invalid_regex RubyRegex::Dni, ['90.900.900V', '90900900-V']
25
23
  end
26
-
24
+
27
25
  # Email
28
26
  def test_valid_emails
29
27
  check_valid_regex RubyRegex::Email, load_fixture('emails')['valid']
@@ -32,120 +30,147 @@ class RubyRegexTest < ActiveSupport::TestCase
32
30
  def test_invalid_emails
33
31
  check_invalid_regex RubyRegex::Email, load_fixture('emails')["invalid"]
34
32
  end
35
-
33
+
36
34
  # Domains
37
35
  def test_valid_domains
38
36
  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']
39
37
  end
40
-
38
+
41
39
  def test_invalid_domains
42
40
  check_invalid_regex RubyRegex::Domain, ['test.', 'www.test.e', 'www.test.', 'test.e', '!test.com', 'test/test.com', 'test_test.com']
43
41
  end
44
-
42
+
45
43
  # Url
46
44
  def test_valid_url
47
45
  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']
48
46
  end
49
-
47
+
50
48
  def test_invalid_url
51
49
  check_invalid_regex RubyRegex::URL, ['test.com', 'www.test.com', 'http://test.es-index', 'http://www.test.es?index.html']
52
50
  end
53
-
51
+
54
52
  # CreditCard
55
53
  def test_valid_credit_cards
56
54
  check_valid_regex RubyRegex::CreditCard, [ '1234123412341234', '1234 1234 1234 1234', '1234-1234-1234-1234']
57
55
  end
58
-
56
+
59
57
  def test_invalid_credit_cards
60
58
  check_invalid_regex RubyRegex::CreditCard, ['1234_1234_1234_1234', '1234', '12341234', '123412341234', '1234 1234 1234 1234', '1234-1234 12341234', '123a-1234-1234-1234']
61
59
  end
62
-
60
+
63
61
  # US Social Security
64
62
  def test_valid_usss_numbers
65
63
  check_valid_regex RubyRegex::USSocialSecurity, ['123-12-1234']
66
64
  end
67
-
65
+
68
66
  def test_invalid_usss_numbers
69
67
  check_invalid_regex RubyRegex::USSocialSecurity, [ '1234_1234_1234_1234', '1234', '123121234', '123_12_1234', '123 12 1234']
70
68
  end
71
-
69
+
72
70
  # General Postal Code
73
71
  def test_valid_postal_codes
74
72
  check_valid_regex RubyRegex::GeneralPostalCode, ['12345']
75
73
  end
76
-
74
+
77
75
  def test_invalid_postal_codes
78
76
  check_invalid_regex RubyRegex::GeneralPostalCode, [ '1', '12', '123', '1234', '123456']
79
77
  end
80
-
78
+
81
79
  # ZIP Code
82
80
  def test_valid_zip_codes
83
81
  check_valid_regex RubyRegex::ZIPCode, [ '12345', '12345-1234']
84
82
  end
85
-
83
+
86
84
  def test_invalid_zip_codes
87
85
  check_invalid_regex RubyRegex::ZIPCode, [ '1', '12', '123', '1234', '123456', '12345_1234', '12345 1234', '1234-1234']
88
86
  end
89
-
87
+
90
88
  # Twitter usernames
91
89
  def test_valid_twitter_usernames
92
90
  check_valid_regex RubyRegex::TwitterUsername, ['ji', 'nickel84', 'sepa_rate']
93
91
  end
94
-
92
+
95
93
  def test_invalid_twitter_usernames
96
94
  check_invalid_regex RubyRegex::TwitterUsername, ['nickel 83', 'h.ppywebcoder']
97
- end
98
-
95
+ end
96
+
99
97
  # Github usernames
100
98
  def test_valid_github_usernames
101
99
  check_valid_regex RubyRegex::GithubUsername, ['ji', 'nickel84', 'sepa_rate', 'ernesto-jimenez']
102
100
  end
103
-
101
+
104
102
  def test_invalid_github_usernames
105
103
  check_invalid_regex RubyRegex::GithubUsername, ['nickel 84', 'h.ppywebcoder']
106
104
  end
107
-
105
+
108
106
  # Slideshare usernames
109
107
  def test_valid_slideshare_usernames
110
108
  check_valid_regex RubyRegex::SlideshareUsername, ['ji', 'nickel84']
111
109
  end
112
-
110
+
113
111
  def test_invalid_slideshare_usernames
114
112
  check_invalid_regex RubyRegex::SlideshareUsername, ['nickel 84', 'h.ppywebcoder', 'sepa_rate', 'ernesto-jimenez']
115
- end
116
-
113
+ end
114
+
117
115
  # Del.icio.us usernames
118
116
  def test_valid_delicious_usernames
119
117
  check_valid_regex RubyRegex::DeliciousUsername, ['ji', 'nickel84', 'sepa_rate', 'ernesto-jimenez']
120
118
  end
121
-
119
+
122
120
  def test_invalid_delicious_usernames
123
121
  check_invalid_regex RubyRegex::DeliciousUsername, ['nickel 84', 'h.ppywebcoder']
124
122
  end
125
-
123
+
126
124
  def test_valid_uuids
127
125
  check_valid_regex RubyRegex::UUID, ['550e8400e29b41d4a716446655440000', '550e8400-e29b-41d4-a716-446655440000', '6ba7b8109dad11d180b400c04fd430c8', '6ba7b810-9dad-11d1-80b4-00c04fd430c8']
128
126
  end
129
-
127
+
130
128
  def test_invalid_uuids
131
129
  check_invalid_regex RubyRegex::UUID, ['6ba7b810-9dad-11d180b400c04fd430c8', 'zba7b810-9dad-11d1-80b4-00c04fd430c8', '6ba7b81-9ad-1d1-0b4-00c04fd430c8', '1234', 'asdf', '555-555-5555', 'abcd@qwerty.com']
132
130
  end
133
-
131
+
132
+ # DBDate
133
+ def test_valid_db_dates
134
+ check_valid_regex RubyRegex::DBDate, load_fixture('db_dates')['valid']
135
+ end
136
+
137
+ def test_invalid_db_dates
138
+ check_invalid_regex RubyRegex::DBDate, load_fixture('db_dates')['invalid']
139
+ end
140
+
141
+ # DBDateTime
142
+ def test_valid_db_date_times
143
+ check_valid_regex RubyRegex::DBDateTime, load_fixture('db_date_times')['valid']
144
+ end
145
+
146
+ def test_invalid_db_date_times
147
+ check_invalid_regex RubyRegex::DBDateTime, load_fixture('db_date_times')['invalid']
148
+ end
149
+
150
+ # SpanishBankAccountNumber
151
+ def test_valid_spanish_bank_account_numbers
152
+ check_valid_regex RubyRegex::SpanishBankAccountNumber, load_fixture('spanish_bank_account_numbers')['valid']
153
+ end
154
+
155
+ def test_invalid_spanish_bank_account_numbers
156
+ check_invalid_regex RubyRegex::SpanishBankAccountNumber, load_fixture('spanish_bank_account_numbers')['invalid']
157
+ end
158
+
134
159
  private
135
160
  def load_fixture( name )
136
161
  YAML.load( File.read( File.join( File.dirname(__FILE__), 'fixtures', "#{name}.yml" ) ) )
137
162
  end
138
-
163
+
139
164
  def check_valid_regex(regexp, strings)
140
165
  strings.each do |str|
141
- message = build_message(message, '<?> does not pass the test', str)
166
+ message = build_message(message, '<?> should be valid but it is not', str)
142
167
  assert(str =~ regexp, message)
143
168
  end
144
169
  end
145
-
170
+
146
171
  def check_invalid_regex(regexp, strings)
147
172
  strings.each do |str|
148
- message = build_message(message, '<?> does not pass the test', str)
173
+ message = build_message(message, '<?> should be invalid but it is not', str)
149
174
  assert(str !~ regexp, message)
150
175
  end
151
176
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_regex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-30 00:00:00.000000000Z
12
+ date: 2012-09-24 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Ruby regular expressions library
15
15
  email: emili@eparreno.com
@@ -45,10 +45,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
45
45
  version: '0'
46
46
  requirements: []
47
47
  rubyforge_project:
48
- rubygems_version: 1.8.6
48
+ rubygems_version: 1.8.24
49
49
  signing_key:
50
50
  specification_version: 3
51
51
  summary: none
52
52
  test_files:
53
53
  - test/ruby_regex_test.rb
54
54
  - test/fixtures/emails.yml
55
+ has_rdoc: true