phony 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe Phony::NDC::Austria do
4
+
5
+ attr_reader :splitter
6
+ before(:each) do
7
+ @splitter = Phony::NDC::Austria
8
+ end
9
+
10
+ describe "split" do
11
+ it "should handle Vienna" do
12
+ splitter.split('198110').should == ['1', '98110']
13
+ end
14
+ it "should handle some mobile services" do
15
+ splitter.split('66914093902').should == ['669', '14093902']
16
+ end
17
+ it "should handle Graz" do
18
+ splitter.split('3161234567891').should == ['316', '1234567891']
19
+ end
20
+ it "should handle Rohrau" do
21
+ splitter.split('2164123456789').should == ['2164', '123456789']
22
+ end
23
+ end
24
+
25
+ describe "formatted" do
26
+ it "should format Vienna" do
27
+ splitter.formatted('198110').should == '1 98110'
28
+ end
29
+ it "should format some mobile services" do
30
+ splitter.formatted('671234567891').should == '67 1234567891'
31
+ end
32
+ it "should format Graz" do
33
+ splitter.formatted('316123456789').should == '316 123456789'
34
+ end
35
+ it "should format Rohrau" do
36
+ splitter.formatted('216412345678').should == '2164 12345678'
37
+ end
38
+ end
39
+
40
+ end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ describe Phony::NDC::FixedSize do
4
+
5
+ attr_reader :splitter
6
+
7
+ # describe 'size 0' do
8
+ # before(:each) do
9
+ # @splitter = Phony::NDC.fixed 0
10
+ # end
11
+ #
12
+ # describe "formatted" do
13
+ # it "should format correctly" do
14
+ # # TODO not perfect, but does this case make sense?
15
+ # splitter.formatted('1234567').should == '123 45 67 '
16
+ # end
17
+ # end
18
+ # end
19
+
20
+ describe "size 1" do
21
+ before(:each) do
22
+ @splitter = Phony::NDC.fixed 1
23
+ end
24
+
25
+ describe "formatted" do
26
+ it "should format correctly" do
27
+ splitter.formatted('12345678').should == '1 234 56 78'
28
+ end
29
+ end
30
+ end
31
+ describe "size 2" do
32
+ before(:each) do
33
+ @splitter = Phony::NDC.fixed 2
34
+ end
35
+
36
+ describe "formatted" do
37
+ it "should format correctly" do
38
+ splitter.formatted('123456789').should == '12 345 67 89'
39
+ end
40
+ end
41
+ end
42
+ describe "size 3" do
43
+ before(:each) do
44
+ @splitter = Phony::NDC.fixed 3
45
+ end
46
+
47
+ describe "formatted" do
48
+ it "should format correctly" do
49
+ splitter.formatted('1234567890').should == '123 456 78 90'
50
+ end
51
+ end
52
+ end
53
+ describe "size 4" do
54
+ before(:each) do
55
+ @splitter = Phony::NDC.fixed 4
56
+ end
57
+
58
+ describe "formatted" do
59
+ it "should format correctly" do
60
+ splitter.formatted('12345678901').should == '1234 567 89 01'
61
+ end
62
+ end
63
+ end
64
+
65
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe Phony::NDC::Germany do
4
+
5
+ attr_reader :splitter
6
+ before(:each) do
7
+ @splitter = Phony::NDC::Germany
8
+ end
9
+
10
+ describe "split" do
11
+ it "should handle Berlin" do
12
+ splitter.split('3038625454').should == ['30', '386', '25454']
13
+ end
14
+ it "should handle Cologne" do
15
+ splitter.split('22137683323').should == ['221', '376', '83323']
16
+ end
17
+ it "should handle Freiburg im Breisgau" do
18
+ splitter.split('7614767676').should == ['761', '476', '7676']
19
+ end
20
+ it "should handle Nettetal-Lobberich" do
21
+ splitter.split('21535100').should == ['2153', '510', '0']
22
+ end
23
+ end
24
+
25
+ describe "formatted" do
26
+ it "should format Berlin" do
27
+ splitter.formatted('3038625454').should == '30 386 25454'
28
+ end
29
+ it "should format Cologne" do
30
+ splitter.formatted('22137683323').should == '221 376 83323'
31
+ end
32
+ it "should format Freiburg im Breisgau" do
33
+ splitter.formatted('7614767676').should == '761 476 7676'
34
+ end
35
+ it "should format Nettetal-Lobberich" do
36
+ splitter.formatted('21535100').should == '2153 510 0'
37
+ end
38
+ end
39
+
40
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe Phony::NDC::Prefix do
4
+
5
+ attr_reader :prefix
6
+ before(:each) do
7
+ @prefix = Phony::NDC::Prefix
8
+ end
9
+
10
+ describe "optimize" do
11
+ it "should convert an array into a hash" do
12
+ prefix.optimize(Array.new).should be_kind_of(Hash)
13
+ end
14
+ it "should convert an empty array into an empty thing" do
15
+ prefix.optimize([]).should be_empty
16
+ end
17
+ it "should convert an empty array into an empty hash" do
18
+ prefix.optimize([]).should == {}
19
+ end
20
+ it "should convert a various string length array into a hash(size=>array(entries))" do
21
+ prefix.optimize(['1', '22', '333', '4444']).should == { 1 => ['1'], 2 => ['22'], 3 => ['333'], 4 => ['4444'] }
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe Phony::NDC::Splitter do
4
+
5
+ attr_reader :splitter
6
+
7
+ describe "DSL" do
8
+ before(:each) do
9
+ @splitter = Phony::NDC::Splitter
10
+ end
11
+ describe "local" do
12
+ it "should set @split_sizes" do
13
+ splitter.local 3,2,2
14
+ splitter.instance_variable_get(:@split_sizes).should == [3,2,2]
15
+ end
16
+ it "should call format" do
17
+ splitter.should_receive(:format).once.with('%s%s%s%s%s')
18
+ splitter.local 3,2,2
19
+ end
20
+ end
21
+ describe "format" do
22
+ it "should set @format" do
23
+ splitter.format 'any format'
24
+ splitter.instance_variable_get(:@format).should == 'any format'
25
+ end
26
+ it "should set @format_with_ndc" do
27
+ splitter.format 'any format'
28
+ splitter.instance_variable_get(:@format_with_ndc).should == '%s%sany format'
29
+ end
30
+ end
31
+ end
32
+
33
+ describe "split_local" do
34
+ describe "with local" do
35
+ before(:each) do
36
+ @splitter = Phony::NDC.fixed(2, :local => [3, 2, 2])
37
+ end
38
+ it "should split it according to the local format" do
39
+ splitter.split_local('3643533').should == ['364', '35', '33']
40
+ end
41
+ end
42
+ end
43
+
44
+ describe 'formatted_with_spaces' do
45
+ before(:each) do
46
+ @splitter = Phony::NDC.fixed(2, :local => [3, 2, 2])
47
+ end
48
+ it 'should format with spaces' do
49
+ splitter.formatted_with_spaces('%s%s%s%s%s', ['364', '35', '33'], ' ').should == '364 35 33'
50
+ end
51
+ it 'should format with spaces' do
52
+ splitter.formatted_with_spaces('%s%s%s%s%s', ['364', '35', '33'], '').should == '3643533'
53
+ end
54
+ it 'should format with dashes' do
55
+ splitter.formatted_with_spaces('%s%s%s%s%s', ['364', '35', '33'], :-).should == '364-35-33'
56
+ end
57
+ end
58
+
59
+ end
@@ -0,0 +1,380 @@
1
+ # encoding: utf-8
2
+ #
3
+ require 'spec_helper'
4
+
5
+ describe Phony do
6
+
7
+ describe "normalize" do
8
+ describe "some examples" do
9
+ it "should normalize an already normalized number" do
10
+ Phony.normalize('41443643533').should == '41443643533'
11
+ end
12
+ it "should normalize a formatted number" do
13
+ Phony.normalize('+41 44 364 35 33').should == '41443643533'
14
+ end
15
+ it "should normalize a formatted number" do
16
+ Phony.normalize('+41 44 364 35 33').should == '41443643533'
17
+ end
18
+ it "should normalize a special service number" do
19
+ Phony.normalize('+41 800 11 22 33').should == '41800112233'
20
+ end
21
+ it "should remove characters from the number" do
22
+ Phony.normalize('John: +41 44 364 35 33').should == '41443643533'
23
+ end
24
+ it "should normalize one of these crazy american numbers" do
25
+ Phony.normalize('1 (703) 451-5115').should == '17034515115'
26
+ end
27
+ it "should normalize another one of these crazy american numbers" do
28
+ Phony.normalize('1-888-407-4747').should == '18884074747'
29
+ end
30
+ it "should normalize a number with colons" do
31
+ Phony.normalize('1.906.387.1698').should == '19063871698'
32
+ end
33
+ it "should normalize a number with optional ndc" do
34
+ Phony.normalize('+41 (044) 364 35 33').should == '41443643533'
35
+ end
36
+ end
37
+ end
38
+
39
+ describe "remove_relative_zeros" do
40
+ it "should remove an ndc zero from an almost normalized number and return it" do
41
+ in_the Phony do
42
+ remove_relative_zeros!('410443643533').should == '41443643533'
43
+ end
44
+ end
45
+ end
46
+
47
+ describe "formatted_cc_ndc" do
48
+ describe "international" do
49
+ it "should return an internationally formatted cc-ndc combo" do
50
+ in_the Phony do
51
+ formatted_cc_ndc('41', '44', nil).should == '+41 44 '
52
+ end
53
+ end
54
+ it "should return an internationally formatted cc-ndc combo" do
55
+ in_the Phony do
56
+ formatted_cc_ndc('41', '44', :international_absolute).should == '+41 44 '
57
+ end
58
+ end
59
+ it "should return an internationally formatted cc-ndc combo" do
60
+ in_the Phony do
61
+ formatted_cc_ndc('41', '44', :international).should == '+41 44 '
62
+ end
63
+ end
64
+ it "should return an internationally formatted cc-ndc combo" do
65
+ in_the Phony do
66
+ formatted_cc_ndc('41', '44', :+).should == '+41 44 '
67
+ end
68
+ end
69
+ it "should return an relative internationally formatted cc-ndc combo" do
70
+ in_the Phony do
71
+ formatted_cc_ndc('41', '44', :international_relative).should == '0041 44 '
72
+ end
73
+ end
74
+ it "should return an internationally formatted cc-ndc combo (for special service number)" do
75
+ in_the Phony do
76
+ formatted_cc_ndc('41', '800', :international_absolute).should == '+41 800 '
77
+ end
78
+ end
79
+ context 'with a different space character' do
80
+ it "should return an internationally formatted cc-ndc combo (for special service number), with special space" do
81
+ in_the Phony do
82
+ formatted_cc_ndc('41', '800', :international_absolute, :-).should == '+41-800-'
83
+ end
84
+ end
85
+ end
86
+ context 'if the ndc is blank' do
87
+ it "should have only one space at the end (not two) / international" do
88
+ in_the Phony do
89
+ formatted_cc_ndc('423', '', :international).should == '+423 '
90
+ end
91
+ end
92
+ it "should have only one space at the end (not two) / international relative" do
93
+ in_the Phony do
94
+ formatted_cc_ndc('423', '', :international_relative).should == '00423 '
95
+ end
96
+ end
97
+ it "should have only one space at the end (not two) / national" do
98
+ in_the Phony do
99
+ formatted_cc_ndc('423', '', :national).should == ''
100
+ end
101
+ end
102
+
103
+ end
104
+ end
105
+ describe "national" do
106
+ it "should return a nationally formatted cc-ndc combo" do
107
+ in_the Phony do
108
+ formatted_cc_ndc('41', '44', :national).should == '044 '
109
+ end
110
+ end
111
+ it "should return a nationally formatted cc-ndc combo (for special service number)" do
112
+ in_the Phony do
113
+ formatted_cc_ndc('41', '800', :national).should == '0800 '
114
+ end
115
+ end
116
+ end
117
+ describe "local" do
118
+ it "should return a locally formatted cc-ndc combo" do
119
+ in_the Phony do
120
+ formatted_cc_ndc('41', '44', :local).should == ''
121
+ end
122
+ end
123
+ end
124
+ end
125
+
126
+ describe "split" do
127
+ it "should handle austrian numbers" do
128
+ Phony.split('43198110').should == ['43', '1', '98110']
129
+ end
130
+ it "should handle french numbers" do
131
+ Phony.split('33112345678').should == ['33', '1', '12','34','56','78']
132
+ end
133
+ it "should handle german numbers" do
134
+ Phony.split('4976112345').should == ['49', '761', '123', '45']
135
+ end
136
+ it "should handle italian numbers opinionatedly" do
137
+ Phony.split('3928061371').should == ['39', '280', '613', '71']
138
+ end
139
+ it "should handle swiss numbers" do
140
+ Phony.split('41443643532').should == ['41', '44', '364', '35', '32']
141
+ end
142
+ it "should handle US numbers" do
143
+ Phony.split('15551115511').should == ['1', '555', '111', '5511']
144
+ end
145
+ it "should handle new zealand numbers" do
146
+ Phony.split('6491234567').should == ['64', '9', '123', '4567']
147
+ end
148
+ it "should handle swiss special services numbers" do
149
+ Phony.split('41800334455').should == ['41', '800', '33', '44', '55']
150
+ end
151
+ end
152
+
153
+ describe "split_cc" do
154
+ it "should handle partial numbers" do
155
+ Phony.split_cc('4').should == ['', '']
156
+ end
157
+ it "should handle partial numbers" do
158
+ Phony.split_cc('43').should == ['43', '']
159
+ end
160
+ it "should handle partial numbers" do
161
+ Phony.split_cc('431').should == ['43', '1']
162
+ end
163
+ it "should handle partial numbers" do
164
+ Phony.split_cc('4319').should == ['43', '19']
165
+ end
166
+ it "should handle partial numbers" do
167
+ Phony.split_cc('43198').should == ['43', '198']
168
+ end
169
+ it "should handle partial numbers" do
170
+ Phony.split_cc('431981').should == ['43', '1981']
171
+ end
172
+ it "should handle partial numbers" do
173
+ Phony.split_cc('4319811').should == ['43', '19811']
174
+ end
175
+
176
+ it "should handle austrian numbers" do
177
+ Phony.split_cc('43198110').should == ['43', '198110']
178
+ end
179
+ it "should handle french numbers" do
180
+ Phony.split_cc('33112345678').should == ['33', '112345678']
181
+ end
182
+ it "should handle german numbers" do
183
+ Phony.split_cc('4976112345').should == ['49', '76112345']
184
+ end
185
+ it "should handle italian numbers opinionatedly" do
186
+ Phony.split_cc('3928061371').should == ['39', '28061371']
187
+ end
188
+ it "should handle swiss numbers" do
189
+ Phony.split_cc('41443643532').should == ['41', '443643532']
190
+ end
191
+ it "should handle swiss special services numbers" do
192
+ Phony.split_cc('41800223344').should == ['41', '800223344']
193
+ end
194
+ it "should handle US numbers" do
195
+ Phony.split_cc('15551115511').should == ['1', '5551115511']
196
+ end
197
+ it "should handle new zealand numbers" do
198
+ Phony.split_cc('6491234567').should == ['64', '91234567']
199
+ end
200
+ it 'should handle Liechtenstein' do
201
+ Phony.split_cc('4233841148').should == ['423', '3841148']
202
+ end
203
+ end
204
+
205
+ describe "split_cc_ndc" do
206
+ it "should handle partial numbers" do
207
+ Phony.split_cc_ndc('4').should == ['', '', '']
208
+ end
209
+ it "should handle partial numbers" do
210
+ Phony.split_cc_ndc('43').should == ['43', '', '']
211
+ end
212
+ it "should handle partial numbers" do
213
+ Phony.split_cc_ndc('431').should == ['43', '1', '']
214
+ end
215
+ it "should handle partial numbers" do
216
+ Phony.split_cc_ndc('4319').should == ['43', '1', '9']
217
+ end
218
+ it "should handle partial numbers" do
219
+ Phony.split_cc_ndc('43198').should == ['43', '1', '98']
220
+ end
221
+ it "should handle partial numbers" do
222
+ Phony.split_cc_ndc('431981').should == ['43', '1', '981']
223
+ end
224
+ it "should handle partial numbers" do
225
+ Phony.split_cc_ndc('4319811').should == ['43', '1', '9811']
226
+ end
227
+
228
+ it "should handle austrian numbers" do
229
+ Phony.split_cc_ndc('43198110').should == ['43', '1', '98110']
230
+ end
231
+ it "should handle french numbers" do
232
+ Phony.split_cc_ndc('33112345678').should == ['33', '1', '12345678']
233
+ end
234
+ it "should handle german numbers" do
235
+ Phony.split_cc_ndc('4976112345').should == ['49', '761', '12345']
236
+ end
237
+ it "should handle italian numbers opinionatedly" do
238
+ Phony.split_cc_ndc('3928061371').should == ['39', '280', '61371']
239
+ end
240
+ it "should handle swiss numbers" do
241
+ Phony.split_cc_ndc('41443643532').should == ['41', '44', '3643532']
242
+ end
243
+ it "should handle swiss special services numbers" do
244
+ Phony.split_cc_ndc('41800112233').should == ['41', '800', '112233']
245
+ end
246
+ it "should handle US numbers" do
247
+ Phony.split_cc_ndc('15551115511').should == ['1', '555', '1115511']
248
+ end
249
+ it "should handle new zealand numbers" do
250
+ Phony.split_cc_ndc('6491234567').should == ['64', '9', '1234567']
251
+ end
252
+ end
253
+
254
+ describe "formatted" do
255
+ describe "default" do
256
+ it "should format swiss numbers" do
257
+ Phony.formatted('41443643532').should == '+41 44 364 35 32'
258
+ end
259
+ it "should format swiss special services numbers" do
260
+ Phony.formatted('41800112233').should == '+41 800 11 22 33'
261
+ end
262
+ it "should format austrian numbers" do
263
+ Phony.formatted('43198110').should == '+43 1 98110'
264
+ end
265
+ it "should format american numbers" do
266
+ Phony.formatted('18705551122').should == '+1 870 555 1122'
267
+ end
268
+ end
269
+ describe "international" do
270
+ it "should format north american numbers" do
271
+ Phony.formatted('18091231234', :format => :international).should == '+1 809 123 1234'
272
+ end
273
+ it "should format austrian numbers" do
274
+ Phony.formatted('43198110', :format => :international).should == '+43 1 98110'
275
+ end
276
+ it "should format austrian numbers" do
277
+ Phony.formatted('43198110', :format => :international_absolute).should == '+43 1 98110'
278
+ end
279
+ it "should format french numbers" do
280
+ Phony.formatted('33142278186', :format => :+).should == '+33 1 42 27 81 86'
281
+ end
282
+ it "should format austrian numbers" do
283
+ Phony.formatted('43198110', :format => :international_relative).should == '0043 1 98110'
284
+ end
285
+ it 'should format liechtensteiner numbers' do
286
+ Phony.formatted('4233841148', :format => :international_relative).should == '00423 384 11 48'
287
+ end
288
+ context 'with no spaces' do
289
+ it "should format north american numbers" do
290
+ Phony.formatted('18091231234', :format => :international, :spaces => '').should == '+18091231234'
291
+ end
292
+ it "should format austrian numbers" do
293
+ Phony.formatted('43198110', :format => :international, :spaces => '').should == '+43198110'
294
+ end
295
+ it "should format austrian numbers" do
296
+ Phony.formatted('43198110', :format => :international_absolute, :spaces => '').should == '+43198110'
297
+ end
298
+ it "should format french numbers" do
299
+ Phony.formatted('33142278186', :format => :+, :spaces => '').should == '+33142278186'
300
+ end
301
+ it "should format austrian numbers" do
302
+ Phony.formatted('43198110', :format => :international_relative, :spaces => '').should == '0043198110'
303
+ end
304
+ it 'should format liechtensteiner numbers' do
305
+ Phony.formatted('4233841148', :format => :international_relative, :spaces => '').should == '004233841148'
306
+ end
307
+ end
308
+ context 'with special spaces' do
309
+ it "should format swiss numbers" do
310
+ Phony.formatted('41443643532', :format => :international).should == '+41 44 364 35 32'
311
+ end
312
+ it "should format north american numbers" do
313
+ Phony.formatted('18091231234', :format => :international, :spaces => :-).should == '+1-809-123-1234'
314
+ end
315
+ it "should format austrian numbers" do
316
+ Phony.formatted('43198110', :format => :international, :spaces => :-).should == '+43-1-98110'
317
+ end
318
+ it "should format austrian numbers" do
319
+ Phony.formatted('43198110', :format => :international_absolute, :spaces => :-).should == '+43-1-98110'
320
+ end
321
+ it "should format french numbers" do
322
+ Phony.formatted('33142278186', :format => :+, :spaces => :-).should == '+33-1-42-27-81-86'
323
+ end
324
+ it "should format austrian numbers" do
325
+ Phony.formatted('43198110', :format => :international_relative, :spaces => :-).should == '0043-1-98110'
326
+ end
327
+ it 'should format liechtensteiner numbers' do
328
+ Phony.formatted('4233841148', :format => :international_relative, :spaces => :-).should == '00423-384-11-48'
329
+ end
330
+ end
331
+ end
332
+ describe "national" do
333
+ it "should format swiss numbers" do
334
+ Phony.formatted('41443643532', :format => :national).should == '044 364 35 32'
335
+ end
336
+ it "should format swiss special services numbers" do
337
+ Phony.formatted('41800112233', :format => :national).should == '0800 11 22 33'
338
+ end
339
+ it "should format austrian numbers" do
340
+ Phony.formatted('43198110', :format => :national).should == '01 98110'
341
+ end
342
+ end
343
+ describe "local" do
344
+ it "should format swiss numbers" do
345
+ Phony.formatted('41443643532', :format => :local).should == '364 35 32'
346
+ end
347
+ it "should format german numbers" do
348
+ Phony.formatted('493038625454', :format => :local).should == '386 25454'
349
+ end
350
+ end
351
+ end
352
+
353
+ describe 'regarding vanity' do
354
+ describe 'vanity_number?' do
355
+ it {Phony.vanity_number?('0800 WEGGLI').should be_true}
356
+ it {Phony.vanity_number?('0800WEGGLI').should be_true}
357
+ it {Phony.vanity_number?('0848 SUCCESSMATCH').should be_true}
358
+ it {Phony.vanity_number?('080 NO NO NO').should be_false}
359
+ it {Phony.vanity_number?('0900 KURZ').should be_false}
360
+ end
361
+
362
+ describe 'vanity_to_number' do
363
+ before(:each) do
364
+ @number = stub(:number)
365
+ end
366
+ it "should delegate to Phony::Vanity.replace" do
367
+ Phony::Vanity.should_receive(:replace).with(@number)
368
+ Phony.vanity_to_number @number
369
+ end
370
+ end
371
+
372
+ describe 'replace_vanity' do
373
+ it {Phony::Vanity.replace('0800 WEGGLI').should == '0800 934454'}
374
+ it {Phony::Vanity.replace('0800weggli').should == '0800934454'}
375
+ it {Phony::Vanity.replace('0800SUCCESSMATCH').should == '0800782237762824'}
376
+ it {Phony::Vanity.replace('080BLA').should == '080252'} #replace_vanity does not check for validity of number
377
+ end
378
+ end
379
+
380
+ end