Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 351 lines (341 sloc) 14.808 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
0ab6b39 @floere + refactoring e164
authored
6
41cbfce @floere + initial commit
authored
7 describe "split" do
3d82bb2 @floere + Salaam, Afghanistan!, . various edits
authored
8 it 'handles afghan numbers' do
9 Phony.split('93201234567').should == ['93', '20', '1234567'] # Kabul
10 end
5f017c5 @floere + belgian numbers in global spec
authored
11 it "handles austrian numbers" do
cedc4c2 @floere + phony rename
authored
12 Phony.split('43198110').should == ['43', '1', '98110']
41cbfce @floere + initial commit
authored
13 end
5f017c5 @floere + belgian numbers in global spec
authored
14 it 'handles belgian numbers' do
15 Phony.split('3235551212').should == ['32', '3', '555', '1212'] # Antwerpen
16 Phony.split('32505551212').should == ['32', '50', '555', '1212'] # Brugge
17 Phony.split('3225551212').should == ['32', '2', '555', '1212'] # Brussels
18 Phony.split('3295551914').should == ['32', '9', '555', '1914'] # Gent
19 Phony.split('3245551414').should == ['32', '4', '555', '1414'] # Liège
20 Phony.split('32475279584').should == ['32', '475', '279584'] # mobile
21 end
28f5cab @floere + Oiii Brasil!
authored
22 it 'handles brazilian numbers' do
23 Phony.split('551112341234').should == ['55', '11', '1234', '1234']
24 end
67a03da @floere + buen día Chile!
authored
25 it 'handles chilean numbers' do
26 Phony.split('5621234567').should == ['56', '2', '1234567'] # Santiago
3d82bb2 @floere + Salaam, Afghanistan!, . various edits
authored
27 Phony.split('5675123456').should == ['56', '75', '123456'] # Curico
67a03da @floere + buen día Chile!
authored
28 Phony.split('56912345678').should == ['56', '9', '12345678'] # mobile
29 end
885869c @floere + Ni hao, China.
authored
30 it 'handles chinese numbers' do
31 Phony.split('862112345678').should == ['86', '21', '1234', '5678'] # Shanghai
271a3c3 @floere ! Shenzen spec
authored
32 Phony.split('8675582193447').should == ['86', '755', '8219', '3447'] # Shenzhen
885869c @floere + Ni hao, China.
authored
33 end
9c99b67 @floere + Hej Danmark!
authored
34 it 'handles danish numbers' do
35 Phony.split('4532121212').should == ['45', '32', '12', '12', '12']
36 end
28f5cab @floere + Oiii Brasil!
authored
37 it 'handles dutch numbers' do
da33c53 @floere + sweden, yay, + russia, sweden specs
authored
38 Phony.split('31612345678').should == ['31', '6', '12345678'] # mobile
f01d580 @floere + first draft for the netherlands
authored
39 Phony.split('31201234567').should == ['31', '20', '1234567']
da33c53 @floere + sweden, yay, + russia, sweden specs
authored
40 Phony.split('31222123456').should == ['31', '222', '123456']
f01d580 @floere + first draft for the netherlands
authored
41 end
5f017c5 @floere + belgian numbers in global spec
authored
42 it "handles egyptian numbers" do
03f78b6 @floere + egyptian specs
authored
43 Phony.split('20212345678').should == ['20', '2', '12345678']
44 Phony.split('20951234567').should == ['20', '95', '1234567']
332d1bd @floere + egyptian splitting
authored
45 end
5f017c5 @floere + belgian numbers in global spec
authored
46 it "handles french numbers" do
cedc4c2 @floere + phony rename
authored
47 Phony.split('33112345678').should == ['33', '1', '12','34','56','78']
41cbfce @floere + initial commit
authored
48 end
5f017c5 @floere + belgian numbers in global spec
authored
49 it "handles german numbers" do
7284642 @floere + German 4-, and 5-digit NDCs (thanks to ledermann!)
authored
50 Phony.split('4930123456').should == ['49', '30', '123', '456']
51 Phony.split('4976112345').should == ['49', '761', '123', '45']
52 Phony.split('492041123456').should == ['49', '2041', '123', '456']
53 Phony.split('493434112345').should == ['49', '34341', '123', '45'] # Geithain
41cbfce @floere + initial commit
authored
54 end
29ea6bb @floere + greece
authored
55 it "should handle greek numbers" do
56 Phony.split('3022631234').should == ['30', '2263', '1234']
57 end
7a40c1c @floere + hungarian numbers, swiss specs
authored
58 it "should handle hungarian numbers" do
59 Phony.split('3612345678').should == ['36', '1', '234', '5678']
60 end
0fa7caa @floere + Polish numbers :)
authored
61 it "should handle italian numbers" do
cee3d9a @floere + italy, including mobile phones. Unsure about formatting.
authored
62 Phony.split('3934869528').should == ['39', '3486', '952', '8'] # Mobile
63 Phony.split('39068546705').should == ['39', '06', '854', '6705'] # Roma
64 Phony.split('390909709511').should == ['39', '090', '970', '9511'] # Barcellona
41cbfce @floere + initial commit
authored
65 end
0fb354a @floere + 1.2.4, Malaysia
authored
66 it 'handles malay numbers' do
67 Phony.split('6082123456').should == ['60', '82', '123456'] # Kuching
68 Phony.split('60312345678').should == ['60', '3', '12345678'] # Kuala Lumpur
a42da4a @floere + Singapore
authored
69 Phony.split('60212345678').should == ['60', '2', '12345678'] # Singapore
0fb354a @floere + 1.2.4, Malaysia
authored
70 end
0c193a4 @floere + Hola Peru!
authored
71 it 'handles peruvian numbers' do
72 Phony.split('51112341234').should == ['51', '1', '1234', '1234'] # Lima
73 Phony.split('51912341234').should == ['51', '9', '1234', '1234'] # mobile
74 Phony.split('51841234123').should == ['51', '84', '1234', '123'] # Cuzco, best effort
75 end
0fa7caa @floere + Polish numbers :)
authored
76 it "should handle polish numbers" do
85ca955 @floere + polish numbers
authored
77 Phony.split('48123123123').should == ['48', '123', '123', '123']
0fa7caa @floere + Polish numbers :)
authored
78 end
f26323e @floere + Oláá Portugal!
authored
79 it 'handles portuguese numbers' do
5f86885 @floere + description
authored
80 Phony.split('351211231234').should == ['351', '21', '123', '1234'] # Lisboa
81 Phony.split('351241123123').should == ['351', '241', '123', '123'] # Abrantes
82 Phony.split('351931231234').should == ['351', '93', '123', '1234'] # mobile
f26323e @floere + Oláá Portugal!
authored
83 end
e32f33e @floere + romania, best effort, + 1.1.3
authored
84 it 'handles romanian numbers' do
0c193a4 @floere + Hola Peru!
authored
85 Phony.split('40211231234').should == ['40', '21', '123', '1234'] # Bucureşti
86 Phony.split('40721231234').should == ['40', '72', '123', '1234'] # mobile
87 Phony.split('40249123123').should == ['40', '249', '123', '123'] # Olt
88 end
89 it 'handles russian numbers' do
90 Phony.split('78122345678').should == ['7', '812', '234', '56', '78']
e32f33e @floere + romania, best effort, + 1.1.3
authored
91 end
5fd6965 @floere + Annyeonhaseyo, South Korea!
authored
92 it 'handles south korean numbers' do
93 Phony.split('82212345678').should == ['82', '2', '1234', '5678'] # Seoul
94 Phony.split('825112345678').should == ['82', '51', '1234', '5678'] # Busan
95 Phony.split('821027975588').should == ['82', '10', '2797', '5588'] # mobile
96 end
da33c53 @floere + sweden, yay, + russia, sweden specs
authored
97 it "should handle swedish numbers" do
98 Phony.split('46812345678').should == ['46', '8', '12345678']
99 Phony.split('46111234567').should == ['46', '11', '1234567']
100 Phony.split('46125123456').should == ['46', '125', '123456']
101 end
41cbfce @floere + initial commit
authored
102 it "should handle swiss numbers" do
cedc4c2 @floere + phony rename
authored
103 Phony.split('41443643532').should == ['41', '44', '364', '35', '32']
41cbfce @floere + initial commit
authored
104 end
394fe10 @floere + Merhaba, Turkey!
authored
105 it 'handles turkish numbers' do
106 Phony.split('903121234567').should == ['90', '312', '123', '4567'] # Ankara
107 end
41cbfce @floere + initial commit
authored
108 it "should handle US numbers" do
cedc4c2 @floere + phony rename
authored
109 Phony.split('15551115511').should == ['1', '555', '111', '5511']
41cbfce @floere + initial commit
authored
110 end
74333d3 @floere + Venezuela, Olaaaa!, + specs
authored
111 it "should handle venezuelan numbers" do
112 Phony.split('582121234567').should == ["58", "212", "1234567"]
113 end
41cbfce @floere + initial commit
authored
114 it "should handle new zealand numbers" do
cedc4c2 @floere + phony rename
authored
115 Phony.split('6491234567').should == ['64', '9', '123', '4567']
41cbfce @floere + initial commit
authored
116 end
7a40c1c @floere + hungarian numbers, swiss specs
authored
117
a6fb758 @floere + service numbers use the same local format as the rest of the number…
authored
118 it "should handle french service numbers" do
119 Phony.split('33812345678').should == ['33', '8', '12','34','56','78']
120 end
83d2d66 @floere + local splitter
authored
121 it "should handle swiss service numbers" do
59f0936 @floere + specs all run
authored
122 Phony.split('41800334455').should == ['41', '800', '334', '455']
83d2d66 @floere + local splitter
authored
123 end
98cc263 @floere + vanity, normalize
authored
124 end
125
126 describe "normalize" do
127 describe "some examples" do
128 it "should normalize an already normalized number" do
129 Phony.normalize('41443643533').should == '41443643533'
130 end
131 it "should normalize a formatted number" do
132 Phony.normalize('+41 44 364 35 33').should == '41443643533'
133 end
134 it "should normalize a formatted number" do
135 Phony.normalize('+41 44 364 35 33').should == '41443643533'
136 end
137 it "should normalize a service number" do
138 Phony.normalize('+41 800 11 22 33').should == '41800112233'
139 end
140 it "should remove characters from the number" do
141 Phony.normalize('John: +41 44 364 35 33').should == '41443643533'
142 end
143 it "should normalize one of these crazy american numbers" do
144 Phony.normalize('1 (703) 451-5115').should == '17034515115'
145 end
146 it "should normalize another one of these crazy american numbers" do
147 Phony.normalize('1-888-407-4747').should == '18884074747'
148 end
149 it "should normalize a number with colons" do
150 Phony.normalize('1.906.387.1698').should == '19063871698'
151 end
152 it "should normalize a number with optional ndc" do
153 Phony.normalize('+41 (044) 364 35 33').should == '41443643533'
154 end
155 it "should normalize a number with erroneous zero inside" do
156 Phony.normalize('+410443643533').should == '41443643533'
157 end
147ea5f @andi + Strategy Pattern for FixedSplitter to handle Special Services numbers
andi authored
158 end
41cbfce @floere + initial commit
authored
159 end
c0a9f79 @floere + doc, + lots of specs
authored
160
b742c4f @floere + cleanup
authored
161 describe "formatted" do
162 describe "default" do
163 it "should format swiss numbers" do
164 Phony.formatted('41443643532').should == '+41 44 364 35 32'
165 end
83d2d66 @floere + local splitter
authored
166 # TODO
167 #
b742c4f @floere + cleanup
authored
168 it "should format swiss service numbers" do
59f0936 @floere + specs all run
authored
169 Phony.formatted('41800112233').should == '+41 800 112 233'
b742c4f @floere + cleanup
authored
170 end
171 it "should format austrian numbers" do
172 Phony.formatted('43198110').should == '+43 1 98110'
173 end
174 it "should format american numbers" do
175 Phony.formatted('18705551122').should == '+1 870 555 1122'
176 end
177 end
178 describe "international" do
179 it "should format north american numbers" do
180 Phony.formatted('18091231234', :format => :international).should == '+1 809 123 1234'
181 end
182 it "should format austrian numbers" do
183 Phony.formatted('43198110', :format => :international).should == '+43 1 98110'
184 end
185 it "should format austrian numbers" do
186 Phony.formatted('43198110', :format => :international_absolute).should == '+43 1 98110'
187 end
188 it "should format french numbers" do
189 Phony.formatted('33142278186', :format => :+).should == '+33 1 42 27 81 86'
190 end
191 it "should format austrian numbers" do
192 Phony.formatted('43198110', :format => :international_relative).should == '0043 1 98110'
193 end
194 it 'should format liechtensteiner numbers' do
195 Phony.formatted('4233841148', :format => :international_relative).should == '00423 384 11 48'
196 end
197 context 'with no spaces' do
198 it "should format north american numbers" do
199 Phony.formatted('18091231234', :format => :international, :spaces => '').should == '+18091231234'
200 end
201 it "should format austrian numbers" do
202 Phony.formatted('43198110', :format => :international, :spaces => '').should == '+43198110'
203 end
204 it "should format austrian numbers" do
205 Phony.formatted('43198110', :format => :international_absolute, :spaces => '').should == '+43198110'
206 end
207 it "should format french numbers" do
208 Phony.formatted('33142278186', :format => :+, :spaces => '').should == '+33142278186'
209 end
210 it "should format austrian numbers" do
211 Phony.formatted('43198110', :format => :international_relative, :spaces => '').should == '0043198110'
212 end
213 it 'should format liechtensteiner numbers' do
214 Phony.formatted('4233841148', :format => :international_relative, :spaces => '').should == '004233841148'
215 end
216 end
217 context 'with special spaces' do
218 it "should format swiss numbers" do
219 Phony.formatted('41443643532', :format => :international).should == '+41 44 364 35 32'
220 end
221 it "should format north american numbers" do
222 Phony.formatted('18091231234', :format => :international, :spaces => :-).should == '+1-809-123-1234'
223 end
224 it "should format austrian numbers" do
225 Phony.formatted('43198110', :format => :international, :spaces => :-).should == '+43-1-98110'
226 end
227 it "should format austrian numbers" do
228 Phony.formatted('43198110', :format => :international_absolute, :spaces => :-).should == '+43-1-98110'
229 end
230 it "should format french numbers" do
231 Phony.formatted('33142278186', :format => :+, :spaces => :-).should == '+33-1-42-27-81-86'
232 end
233 it "should format austrian numbers" do
234 Phony.formatted('43198110', :format => :international_relative, :spaces => :-).should == '0043-1-98110'
235 end
236 it 'should format liechtensteiner numbers' do
237 Phony.formatted('4233841148', :format => :international_relative, :spaces => :-).should == '00423-384-11-48'
238 end
239 end
240 end
241 describe "national" do
242 it "should format swiss numbers" do
243 Phony.formatted('41443643532', :format => :national).should == '044 364 35 32'
244 end
83d2d66 @floere + local splitter
authored
245 # TODO
246 #
b742c4f @floere + cleanup
authored
247 it "should format swiss service numbers" do
40e77a8 @floere + intermediate commit
authored
248 Phony.formatted('41800112233', :format => :national).should == '0800 112 233'
b742c4f @floere + cleanup
authored
249 end
250 it "should format austrian numbers" do
251 Phony.formatted('43198110', :format => :national).should == '01 98110'
252 end
253 end
254 describe "local" do
255 it "should format swiss numbers" do
256 Phony.formatted('41443643532', :format => :local).should == '364 35 32'
257 end
258 it "should format german numbers" do
259 Phony.formatted('493038625454', :format => :local).should == '386 25454'
260 end
261 end
262 end
98cc263 @floere + vanity, normalize
authored
263
c25956c @floere + minimal test cases
authored
264 context 'minimal cases' do
265 context 'normalizing' do
266 it 'handles completely crazy "numbers"' do
267 Phony.normalize('Hello, I am Cora, the 41th parrot, and 044 is my 364 times 35 funky number. 32.').should == '41443643532'
268 end
269 end
270 context 'splitting' do
271 it 'handles completely missing numbers well enough' do
272 Phony.split('4144').should == ['41', '44', '']
273 end
274 it 'handles a missing number part' do
275 Phony.split('4144364').should == ['41', '44', '364']
276 end
277 it 'handles a missing number part' do
278 Phony.split('414436435').should == ['41', '44', '364', '35']
279 end
280 end
281 context 'formatting' do
282 it 'handles completely missing numbers well enough' do
283 Phony.formatted('4144').should == '+41 44 '
284 end
285 it 'handles a missing number part' do
286 Phony.formatted('4144364').should == '+41 44 364'
287 end
288 it 'handles a missing number part' do
289 Phony.formatted('414436435').should == '+41 44 364 35'
290 end
291 end
292 end
293
eda887f @floere + more speed specs
authored
294 context "speed" do
a050691 @floere + speed specs
authored
295 before(:each) do
296 @phone_numbers = [
297 '41443643532',
298 '18091231234',
299 '43198110',
300 '33142278186',
301 '4233841148'
302 ]
303 end
2b9ed71 @kbingman added speed tests back
kbingman authored
304 describe 'split' do
305 it 'is fast' do
306 number = @phone_numbers.first
307 performance_of { Phony.split(number) }.should < 0.00005
308 end
309 it 'is fast' do
310 performance_of { @phone_numbers.each { |number| Phony.split(number) } }.should < 0.00015
311 end
312 end
313 describe 'normalize' do
314 it 'is fast' do
315 number = @phone_numbers.first
316 performance_of { Phony.normalize(number) }.should < 0.0001
317 end
318 it 'is fast' do
319 performance_of { @phone_numbers.each { |number| Phony.normalize(number) } }.should < 0.00016
320 end
321 end
322 describe 'formatted' do
323 it 'is fast' do
324 number = @phone_numbers.first
325 performance_of { Phony.formatted(number) }.should < 0.000075
326 end
327 it 'is fast' do
328 performance_of { @phone_numbers.each { |number| Phony.formatted(number) } }.should < 0.00016
329 end
330 end
a050691 @floere + speed specs
authored
331 end
332
525e5f5 @floere + vanity number handling
authored
333 describe 'vanity' do
334 describe 'vanity_number?' do
335 it {Phony.vanity?('41800 WEGGLI').should be_true}
336 it {Phony.vanity?('41800WEGGLI').should be_true}
337 it {Phony.vanity?('41848 SUCCESSMATCH').should be_true}
338 it {Phony.vanity?('4180 NO NO NO').should be_false}
339 it {Phony.vanity?('41900 KURZ').should be_false}
340 it {Phony.vanity?('41 44 364 35 32').should be_false}
341 end
342
343 describe 'vanity_to_number' do
344 it {Phony.vanity_to_number('41800WEGGLI').should == '41800934454'}
345 it {Phony.vanity_to_number('41800weggli').should == '41800934454'}
346 it {Phony.vanity_to_number('41800SUCCESSMATCH').should == '41800782237'} # Cut off according to the swiss norms.
347 it {Phony.vanity_to_number('4180BLA').should == '4180252'} # Does not check for validity of number.
348 end
349 end
41cbfce @floere + initial commit
authored
350
351 end
Something went wrong with that request. Please try again.