Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 286 lines (275 sloc) 11.121 kB
b010a78 @floere + e164 spec
authored
1 # encoding: utf-8
2 #
3 require 'spec_helper'
41cbfce @floere + initial commit
authored
4
cedc4c2 @floere + phony rename
authored
5 describe Phony do
b46b9c9 @floere + 1.6.1, ! 00 normalization
authored
6
c05b476 @floere + 1.5.0, + cleanly raise on nil number in interface methods
authored
7 describe 'nil cases' do
8 it "should raise on normalize nil" do
9 expect {
10 Phony.normalize(nil)
11 }.to raise_error(ArgumentError, "Phone number cannot be nil. Use e.g. number && Phony.normalize(number).")
12 end
13 it "should raise on format nil" do
14 expect {
15 Phony.format(nil)
16 }.to raise_error(ArgumentError, "Phone number cannot be nil. Use e.g. number && Phony.format(number).")
17 end
18 it "should raise on split nil" do
19 expect {
20 Phony.split(nil)
21 }.to raise_error(ArgumentError, "Phone number cannot be nil. Use e.g. number && Phony.split(number).")
22 end
23 end
b46b9c9 @floere + 1.6.1, ! 00 normalization
authored
24
98cc263 @floere + vanity, normalize
authored
25 describe "normalize" do
26 describe "some examples" do
27 it "should normalize an already normalized number" do
28 Phony.normalize('41443643533').should == '41443643533'
29 end
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
30 it "should normalize a format number" do
98cc263 @floere + vanity, normalize
authored
31 Phony.normalize('+41 44 364 35 33').should == '41443643533'
32 end
b46b9c9 @floere + 1.6.1, ! 00 normalization
authored
33 it "should normalize a 00 number" do
34 Phony.normalize('0041 44 364 35 33').should == '41443643533'
98cc263 @floere + vanity, normalize
authored
35 end
36 it "should normalize a service number" do
37 Phony.normalize('+41 800 11 22 33').should == '41800112233'
38 end
39 it "should remove characters from the number" do
40 Phony.normalize('John: +41 44 364 35 33').should == '41443643533'
41 end
42 it "should normalize one of these crazy american numbers" do
43 Phony.normalize('1 (703) 451-5115').should == '17034515115'
44 end
45 it "should normalize another one of these crazy american numbers" do
46 Phony.normalize('1-888-407-4747').should == '18884074747'
47 end
48 it "should normalize a number with colons" do
49 Phony.normalize('1.906.387.1698').should == '19063871698'
50 end
51 it "should normalize a number with optional ndc" do
52 Phony.normalize('+41 (044) 364 35 33').should == '41443643533'
53 end
54 it "should normalize a number with erroneous zero inside" do
55 Phony.normalize('+410443643533').should == '41443643533'
56 end
6bbb572 @floere + Italian numbers are correctly normalized (i.e. zero before area cod…
authored
57 it "should not normalize a number with a correct zero inside" do
58 Phony.normalize('+390909709511').should == '390909709511'
59 end
147ea5f @andi + Strategy Pattern for FixedSplitter to handle Special Services numbers
andi authored
60 end
41cbfce @floere + initial commit
authored
61 end
b46b9c9 @floere + 1.6.1, ! 00 normalization
authored
62
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
63 describe 'formatted' do
64 it 'is an alias of format' do
65 Phony.formatted('41443643532').should == '+41 44 364 35 32'
66 end
67 end
b46b9c9 @floere + 1.6.1, ! 00 normalization
authored
68
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
69 describe "format" do
b742c4f @floere + cleanup
authored
70 describe "default" do
71 it "should format swiss numbers" do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
72 Phony.format('41443643532').should == '+41 44 364 35 32'
b742c4f @floere + cleanup
authored
73 end
83d2d66 @floere + local splitter
authored
74 # TODO
75 #
b742c4f @floere + cleanup
authored
76 it "should format swiss service numbers" do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
77 Phony.format('41800112233').should == '+41 800 112 233'
b742c4f @floere + cleanup
authored
78 end
79 it "should format austrian numbers" do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
80 Phony.format('43198110').should == '+43 1 98110'
b742c4f @floere + cleanup
authored
81 end
82 it "should format american numbers" do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
83 Phony.format('18705551122').should == '+1 870 555 1122'
b742c4f @floere + cleanup
authored
84 end
85 end
86 describe "international" do
87 it "should format north american numbers" do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
88 Phony.format('18091231234', :format => :international).should == '+1 809 123 1234'
b742c4f @floere + cleanup
authored
89 end
90 it "should format austrian numbers" do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
91 Phony.format('43198110', :format => :international).should == '+43 1 98110'
b742c4f @floere + cleanup
authored
92 end
93 it "should format austrian numbers" do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
94 Phony.format('43198110', :format => :international_absolute).should == '+43 1 98110'
b742c4f @floere + cleanup
authored
95 end
96 it "should format french numbers" do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
97 Phony.format('33142278186', :format => :+).should == '+33 1 42 27 81 86'
b742c4f @floere + cleanup
authored
98 end
99 it "should format austrian numbers" do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
100 Phony.format('43198110', :format => :international_relative).should == '0043 1 98110'
b742c4f @floere + cleanup
authored
101 end
102 it 'should format liechtensteiner numbers' do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
103 Phony.format('4233841148', :format => :international_relative).should == '00423 384 11 48'
b742c4f @floere + cleanup
authored
104 end
105 context 'with no spaces' do
106 it "should format north american numbers" do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
107 Phony.format('18091231234', :format => :international, :spaces => '').should == '+18091231234'
b742c4f @floere + cleanup
authored
108 end
109 it "should format austrian numbers" do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
110 Phony.format('43198110', :format => :international, :spaces => '').should == '+43198110'
b742c4f @floere + cleanup
authored
111 end
112 it "should format austrian numbers" do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
113 Phony.format('43198110', :format => :international_absolute, :spaces => '').should == '+43198110'
b742c4f @floere + cleanup
authored
114 end
115 it "should format french numbers" do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
116 Phony.format('33142278186', :format => :+, :spaces => '').should == '+33142278186'
b742c4f @floere + cleanup
authored
117 end
118 it "should format austrian numbers" do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
119 Phony.format('43198110', :format => :international_relative, :spaces => '').should == '0043198110'
b742c4f @floere + cleanup
authored
120 end
121 it 'should format liechtensteiner numbers' do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
122 Phony.format('4233841148', :format => :international_relative, :spaces => '').should == '004233841148'
b742c4f @floere + cleanup
authored
123 end
124 end
125 context 'with special spaces' do
126 it "should format swiss numbers" do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
127 Phony.format('41443643532', :format => :international).should == '+41 44 364 35 32'
b742c4f @floere + cleanup
authored
128 end
129 it "should format north american numbers" do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
130 Phony.format('18091231234', :format => :international, :spaces => :-).should == '+1-809-123-1234'
b742c4f @floere + cleanup
authored
131 end
132 it "should format austrian numbers" do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
133 Phony.format('43198110', :format => :international, :spaces => :-).should == '+43-1-98110'
b742c4f @floere + cleanup
authored
134 end
135 it "should format austrian numbers" do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
136 Phony.format('43198110', :format => :international_absolute, :spaces => :-).should == '+43-1-98110'
b742c4f @floere + cleanup
authored
137 end
138 it "should format french numbers" do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
139 Phony.format('33142278186', :format => :+, :spaces => :-).should == '+33-1-42-27-81-86'
b742c4f @floere + cleanup
authored
140 end
141 it "should format austrian numbers" do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
142 Phony.format('43198110', :format => :international_relative, :spaces => :-).should == '0043-1-98110'
b742c4f @floere + cleanup
authored
143 end
144 it 'should format liechtensteiner numbers' do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
145 Phony.format('4233841148', :format => :international_relative, :spaces => :-).should == '00423-384-11-48'
b742c4f @floere + cleanup
authored
146 end
147 end
3851473 @mrchucho Allow unsupported countries to use a more conservative default.
mrchucho authored
148 describe '"unsupported" countries' do
149 it 'should format as a single block' do
150 Phony.format('88032155605220').should == '+880 32155605220'
151 end
152 it 'should format as a single block, regardless of format' do
153 Phony.format('8801819372205', :format => :international).should == '+880 1819372205'
154 end
155 it 'should format as a single block, respecting custom spaces' do
156 Phony.format('8801819372205', :spaces => :-).should == '+880-1819372205'
157 end
d0d9a83 @mrchucho Respect :spaces when removing trailing space(s)
mrchucho authored
158 it 'should format as a single block, even without spaces' do
159 Phony.format('8801819372205', :spaces => '').should == '+8801819372205'
160 end
3851473 @mrchucho Allow unsupported countries to use a more conservative default.
mrchucho authored
161 end
b742c4f @floere + cleanup
authored
162 end
163 describe "national" do
164 it "should format swiss numbers" do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
165 Phony.format('41443643532', :format => :national).should == '044 364 35 32'
b742c4f @floere + cleanup
authored
166 end
83d2d66 @floere + local splitter
authored
167 # TODO
168 #
b742c4f @floere + cleanup
authored
169 it "should format swiss service numbers" do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
170 Phony.format('41800112233', :format => :national).should == '0800 112 233'
b742c4f @floere + cleanup
authored
171 end
172 it "should format austrian numbers" do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
173 Phony.format('43198110', :format => :national).should == '01 98110'
b742c4f @floere + cleanup
authored
174 end
09dee51 @floere + for now, do not change the outer API – change it in the next major …
authored
175 it "should format US numbers without a leading zero" do
176 Phony.format('14159224711', :format => :national).should == '415 922 4711'
177 end
b742c4f @floere + cleanup
authored
178 end
179 describe "local" do
180 it "should format swiss numbers" do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
181 Phony.format('41443643532', :format => :local).should == '364 35 32'
b742c4f @floere + cleanup
authored
182 end
183 it "should format german numbers" do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
184 Phony.format('493038625454', :format => :local).should == '386 25454'
b742c4f @floere + cleanup
authored
185 end
186 end
187 end
b46b9c9 @floere + 1.6.1, ! 00 normalization
authored
188
c25956c @floere + minimal test cases
authored
189 context 'minimal cases' do
190 context 'normalizing' do
191 it 'handles completely crazy "numbers"' do
192 Phony.normalize('Hello, I am Cora, the 41th parrot, and 044 is my 364 times 35 funky number. 32.').should == '41443643532'
193 end
194 end
195 context 'splitting' do
196 it 'handles completely missing numbers well enough' do
197 Phony.split('4144').should == ['41', '44', '']
198 end
199 it 'handles a missing number part' do
200 Phony.split('4144364').should == ['41', '44', '364']
201 end
202 it 'handles a missing number part' do
203 Phony.split('414436435').should == ['41', '44', '364', '35']
204 end
205 end
206 context 'formatting' do
207 it 'handles completely missing numbers well enough' do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
208 Phony.format('4144').should == '+41 44 '
c25956c @floere + minimal test cases
authored
209 end
210 it 'handles a missing number part' do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
211 Phony.format('4144364').should == '+41 44 364'
c25956c @floere + minimal test cases
authored
212 end
213 it 'handles a missing number part' do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
214 Phony.format('414436435').should == '+41 44 364 35'
c25956c @floere + minimal test cases
authored
215 end
216 end
47d7409 @mrchucho Modified "default" so it can be used alone.
mrchucho authored
217 context '"unsupported" countries' do
09dee51 @floere + for now, do not change the outer API – change it in the next major …
authored
218 it 'handles formatting' do
219 Phony.format('88032155605220').should == '+880 32155605220'
220 end
221 it 'handles normalizing' do
222 Phony.normalize('+880-321 5560 5220').should == '88032155605220'
47d7409 @mrchucho Modified "default" so it can be used alone.
mrchucho authored
223 end
224 end
c25956c @floere + minimal test cases
authored
225 end
b46b9c9 @floere + 1.6.1, ! 00 normalization
authored
226
cf54d16 @kbingman speed tests
kbingman authored
227 context "speed" do
228 before(:each) do
229 @phone_numbers = [
230 '41443643532',
231 '18091231234',
232 '43198110',
233 '33142278186',
234 '4233841148'
235 ]
236 end
237 describe 'split' do
238 it 'is fast' do
239 number = @phone_numbers.first
240 performance_of { Phony.split(number) }.should < 0.00005
241 end
242 it 'is fast' do
243 performance_of { @phone_numbers.each { |number| Phony.split(number) } }.should < 0.00015
244 end
245 end
246 describe 'normalize' do
247 it 'is fast' do
248 number = @phone_numbers.first
249 performance_of { Phony.normalize(number) }.should < 0.0001
250 end
251 it 'is fast' do
252 performance_of { @phone_numbers.each { |number| Phony.normalize(number) } }.should < 0.00016
253 end
254 end
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
255 describe 'format' do
cf54d16 @kbingman speed tests
kbingman authored
256 it 'is fast' do
257 number = @phone_numbers.first
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
258 performance_of { Phony.format(number) }.should < 0.000075
cf54d16 @kbingman speed tests
kbingman authored
259 end
260 it 'is fast' do
24c56de @floere + Version 1.4.0. Changed Phony.formatted to Phony.format. Old method …
authored
261 performance_of { @phone_numbers.each { |number| Phony.format(number) } }.should < 0.00016
cf54d16 @kbingman speed tests
kbingman authored
262 end
263 end
264 end
b46b9c9 @floere + 1.6.1, ! 00 normalization
authored
265
525e5f5 @floere + vanity number handling
authored
266 describe 'vanity' do
267 describe 'vanity_number?' do
268 it {Phony.vanity?('41800 WEGGLI').should be_true}
269 it {Phony.vanity?('41800WEGGLI').should be_true}
270 it {Phony.vanity?('41848 SUCCESSMATCH').should be_true}
271 it {Phony.vanity?('4180 NO NO NO').should be_false}
272 it {Phony.vanity?('41900 KURZ').should be_false}
273 it {Phony.vanity?('41 44 364 35 32').should be_false}
274 end
b46b9c9 @floere + 1.6.1, ! 00 normalization
authored
275
525e5f5 @floere + vanity number handling
authored
276 describe 'vanity_to_number' do
277 it {Phony.vanity_to_number('41800WEGGLI').should == '41800934454'}
278 it {Phony.vanity_to_number('41800weggli').should == '41800934454'}
4270bcd @floere + 1.6.0, more lenient splitting, adds an arbitrary number (+10) to th…
authored
279 # it {Phony.vanity_to_number('41800SUCCESSMATCH').should == '41800782237'} # Cut off according to the swiss norms.
280 it {Phony.vanity_to_number('41800SUCCESSMATCH').should == '41800782237762824'} # Allow for call-through numbers (>= 1.6.0)
525e5f5 @floere + vanity number handling
authored
281 it {Phony.vanity_to_number('4180BLA').should == '4180252'} # Does not check for validity of number.
282 end
283 end
41cbfce @floere + initial commit
authored
284
3851473 @mrchucho Allow unsupported countries to use a more conservative default.
mrchucho authored
285 end
Something went wrong with that request. Please try again.