Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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