simple_phone_number 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
data.tar.gz.sig CHANGED
Binary file
data/Rakefile CHANGED
@@ -1,8 +1,20 @@
1
1
  require 'rubygems'
2
2
  require 'rake'
3
+ require 'rake/testtask'
3
4
  require 'echoe'
4
5
 
5
- Echoe.new('simple_phone_number', '0.1.8') do |p|
6
+ desc 'Default: run unit tests.'
7
+ task :default => :test
8
+
9
+ desc 'Test the simple_phone_number plugin.'
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'lib'
12
+ t.libs << 'test'
13
+ t.pattern = 'test/**/*_test.rb'
14
+ t.verbose = true
15
+ end
16
+
17
+ Echoe.new('simple_phone_number', '0.1.9') do |p|
6
18
  p.description = "Validate and set phone number format"
7
19
  p.url = "http://github.com/akennedy/simple_phone_number"
8
20
  p.author = 'Andrew Kennedy'
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{simple_phone_number}
5
- s.version = "0.1.8"
5
+ s.version = "0.1.9"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Andrew Kennedy"]
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
11
11
  s.description = %q{Validate and set phone number format}
12
12
  s.email = %q{andrew@jackrussellsoftware.com}
13
13
  s.extra_rdoc_files = ["README.md", "lib/simple_phone_number.rb"]
14
- s.files = ["Manifest", "README.md", "Rakefile", "init.rb", "lib/simple_phone_number.rb", "simple_phone_number.gemspec"]
14
+ s.files = ["Manifest", "README.md", "Rakefile", "init.rb", "lib/simple_phone_number.rb", "simple_phone_number.gemspec", "test/attributes_test.rb", "test/test_helper.rb", "test/validations_test.rb"]
15
15
  s.homepage = %q{http://github.com/akennedy/simple_phone_number}
16
16
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Simple_phone_number", "--main", "README.md"]
17
17
  s.require_paths = ["lib"]
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.rubygems_version = %q{1.3.6}
20
20
  s.signing_key = %q{/Users/andrew/gems/gem-private_key.pem}
21
21
  s.summary = %q{Validate and set phone number format}
22
+ s.test_files = ["test/attributes_test.rb", "test/test_helper.rb", "test/validations_test.rb"]
22
23
 
23
24
  if s.respond_to? :specification_version then
24
25
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -0,0 +1,71 @@
1
+ require 'test_helper'
2
+
3
+ class AttributesTest < Test::Unit::TestCase
4
+ context 'Setting simple_phone_number' do
5
+ setup do
6
+ @attribute_names = %w(phone_number telephone phone_not_validated phone_with_validation_options phone_validated_elsewhere)
7
+ @user = User.new(:name => 'Test User')
8
+
9
+ # generates a phone number in the form: nnn-nnn-nnnn
10
+ def generate_phone_number
11
+ "#{(x = ('000'..'999').to_a)[rand(x.length - 1)]}-#{x[rand(x.length - 1)]}-#{(y = ('0000'..'9999').to_a)[rand(y.length - 1)]}"
12
+ end
13
+ end
14
+
15
+ should 'add attributes names to the phone_number_attributes array' do
16
+ assert_same_elements @user.class.phone_number_attributes.map {|a| a.to_s}, @attribute_names
17
+ end
18
+
19
+ should 'automatically use :phone_number if no attribute names are given' do
20
+ %w(phone_number).each do |name|
21
+ assert_contains @user.class.phone_number_attributes, name.to_sym
22
+ assert_respond_to @user, "#{name}="
23
+ assert_respond_to @user, name
24
+ assert_nothing_raised do
25
+ @user.send(name)
26
+ end
27
+ end
28
+ end
29
+
30
+ should 'create a setter method' do
31
+ @attribute_names.each do |name|
32
+ assert_respond_to @user, "#{name}="
33
+ end
34
+ end
35
+
36
+ should 'create a getter method' do
37
+ @attribute_names.each do |name|
38
+ assert_respond_to @user, name
39
+ end
40
+ end
41
+
42
+ should 'properly set a phone number using setter method' do
43
+ @attribute_names.each do |name|
44
+ assert_send [@user, "#{name}=", number = generate_phone_number]
45
+ assert_equal number.gsub(/^(?:\+?1[-. ]?)?\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/, '\1-\2-\3'), @user.send("#{name}")
46
+ end
47
+ end
48
+
49
+ should 'properly return a phone number using getter method' do
50
+ # need to run the setters again…
51
+ @attribute_names.each do |name|
52
+ assert_send [@user, "#{name}=", generate_phone_number]
53
+ end
54
+
55
+ @attribute_names.each do |name|
56
+ assert_match /^([0-9]{3})[-]([0-9]{3})[-]([0-9]{4})$/, @user.send(name)
57
+ end
58
+ end
59
+
60
+ should 'return the "database stored" value if argument is passed to getter' do
61
+ # need to run the setters again…
62
+ @attribute_names.each do |name|
63
+ assert_send [@user, "#{name}=", generate_phone_number]
64
+ end
65
+
66
+ @attribute_names.each do |name|
67
+ assert_match /^([0-9]{3})[-]([0-9]{3})[-]([0-9]{4})$/, @user.send(name)
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,11 @@
1
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
2
+ $: << File.dirname(__FILE__)
3
+
4
+ require 'rubygems'
5
+ require 'test/unit'
6
+ require 'active_record'
7
+ require 'shoulda'
8
+ require 'action_view'
9
+ require 'simple_phone_number'
10
+ require 'schema'
11
+ begin; require 'redgreen'; rescue LoadError; end
@@ -0,0 +1,92 @@
1
+ require 'test_helper'
2
+
3
+ class ValidationsTest < Test::Unit::TestCase
4
+ context 'Setting simple_phone_number or simple_fax_number' do
5
+ setup do
6
+ @attribute_names = %w(phone_number telephone phone_not_validated phone_with_validation_options phone_validated_elsewhere)
7
+ @not_validated = %w(phone_not_validated)
8
+ @validated_on_update = %w(phone_with_validation_options)
9
+ @auto_validated = @attribute_names - @not_validated - @validated_on_update
10
+
11
+ @user = User.new(:name => 'Test User')
12
+
13
+ # generates a phone number in the form: nnn-nnn-nnnn
14
+ def generate_phone_number
15
+ "#{(x = ('000'..'999').to_a)[rand(x.length - 1)]}-#{x[rand(x.length - 1)]}-#{(y = ('0000'..'9999').to_a)[rand(y.length - 1)]}"
16
+ end
17
+
18
+ def set_fields(fields)
19
+ fields.each do |name|
20
+ @user.send("#{name}=", generate_phone_number)
21
+ end
22
+ end
23
+ end
24
+
25
+ should 'automatically add a validation unless told not to' do
26
+ @auto_validated.each do |name|
27
+ assert_equal false, @user.valid?
28
+ assert @user.errors[name.to_s].any?
29
+
30
+ @user.send("#{name}=", 'gibberish.')
31
+ assert_equal false, @user.valid?
32
+ assert @user.errors[name.to_s].any?
33
+
34
+ @user.send("#{name}=", generate_phone_number)
35
+ end
36
+ assert @user.save
37
+ end
38
+
39
+ should 'not validate if told not to' do
40
+ set_fields @auto_validated
41
+
42
+ @not_validated.each do |name|
43
+ assert @user.valid?
44
+ assert @user.errors.empty?
45
+
46
+ @user.send("#{name}=", 'gibberish.')
47
+ @user.valid?
48
+ assert @user.errors.empty?
49
+ end
50
+ assert @user.save
51
+ end
52
+
53
+ should 'apply and adhere to validation-specific options set for the default validation' do
54
+ set_fields @auto_validated + ['fax_validated_elsewhere']
55
+
56
+ # test that validation fails on :update
57
+ assert @user.save
58
+ assert_equal false, @user.valid?
59
+ assert @user.errors[:phone_with_validation_options]
60
+
61
+ # test invalid data
62
+ @user.phone_with_validation_options = 'gibberish.'
63
+ assert_equal false, @user.valid?
64
+ assert @user.errors[:phone_with_validation_options].any?
65
+
66
+ # test our custom error message
67
+ assert_equal ['is so not a valid phone number!'], @user.errors[:phone_with_validation_options]
68
+
69
+ # test our custom error :if option
70
+ @user.name = nil
71
+ assert @user.valid?
72
+
73
+ # test saving valid record
74
+ @user.name = 'Test User'
75
+ @user.phone_with_validation_options = generate_phone_number
76
+ assert @user.save
77
+ end
78
+
79
+ should 'respond to validates_simple_phone_number and adhere to specified options' do
80
+ set_fields @auto_validated - ['phone_validated_elsewhere']
81
+
82
+ # test our custom error message
83
+ assert_equal false, @user.valid?
84
+ assert @user.errors[:phone_validated_elsewhere].any?
85
+ assert_equal ["didn't pass validations elsewhere"], @user.errors[:phone_validated_elsewhere]
86
+
87
+ # test saving valid record
88
+ @user.phone_validated_elsewhere = generate_phone_number
89
+ assert @user.save
90
+ end
91
+ end
92
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 8
9
- version: 0.1.8
8
+ - 9
9
+ version: 0.1.9
10
10
  platform: ruby
11
11
  authors:
12
12
  - Andrew Kennedy
@@ -91,5 +91,7 @@ rubygems_version: 1.3.6
91
91
  signing_key:
92
92
  specification_version: 3
93
93
  summary: Validate and set phone number format
94
- test_files: []
95
-
94
+ test_files:
95
+ - test/attributes_test.rb
96
+ - test/test_helper.rb
97
+ - test/validations_test.rb
metadata.gz.sig CHANGED
Binary file