Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 279 lines (268 sloc) 10.778 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
# encoding: utf-8
#
require 'spec_helper'

describe Phony do
  
  describe 'nil cases' do
    it "should raise on normalize nil" do
      expect {
        Phony.normalize(nil)
      }.to raise_error(ArgumentError, "Phone number cannot be nil. Use e.g. number && Phony.normalize(number).")
    end
    it "should raise on format nil" do
      expect {
        Phony.format(nil)
      }.to raise_error(ArgumentError, "Phone number cannot be nil. Use e.g. number && Phony.format(number).")
    end
    it "should raise on split nil" do
      expect {
        Phony.split(nil)
      }.to raise_error(ArgumentError, "Phone number cannot be nil. Use e.g. number && Phony.split(number).")
    end
  end
  
  describe "normalize" do
    describe "some examples" do
      it "should normalize an already normalized number" do
        Phony.normalize('41443643533').should == '41443643533'
      end
      it "should normalize a format number" do
        Phony.normalize('+41 44 364 35 33').should == '41443643533'
      end
      it "should normalize a format number" do
        Phony.normalize('+41 44 364 35 33').should == '41443643533'
      end
      it "should normalize a service number" do
        Phony.normalize('+41 800 11 22 33').should == '41800112233'
      end
      it "should remove characters from the number" do
        Phony.normalize('John: +41 44 364 35 33').should == '41443643533'
      end
      it "should normalize one of these crazy american numbers" do
        Phony.normalize('1 (703) 451-5115').should == '17034515115'
      end
      it "should normalize another one of these crazy american numbers" do
        Phony.normalize('1-888-407-4747').should == '18884074747'
      end
      it "should normalize a number with colons" do
        Phony.normalize('1.906.387.1698').should == '19063871698'
      end
      it "should normalize a number with optional ndc" do
        Phony.normalize('+41 (044) 364 35 33').should == '41443643533'
      end
      it "should normalize a number with erroneous zero inside" do
        Phony.normalize('+410443643533').should == '41443643533'
      end
      it "should not normalize a number with a correct zero inside" do
        Phony.normalize('+390909709511').should == '390909709511'
      end
    end
  end
  
  describe 'formatted' do
    it 'is an alias of format' do
      Phony.formatted('41443643532').should == '+41 44 364 35 32'
    end
  end
  
  describe "format" do
    describe "default" do
      it "should format swiss numbers" do
        Phony.format('41443643532').should == '+41 44 364 35 32'
      end
      # TODO
      #
      it "should format swiss service numbers" do
        Phony.format('41800112233').should == '+41 800 112 233'
      end
      it "should format austrian numbers" do
        Phony.format('43198110').should == '+43 1 98110'
      end
      it "should format american numbers" do
        Phony.format('18705551122').should == '+1 870 555 1122'
      end
    end
    describe "international" do
      it "should format north american numbers" do
        Phony.format('18091231234', :format => :international).should == '+1 809 123 1234'
      end
      it "should format austrian numbers" do
        Phony.format('43198110', :format => :international).should == '+43 1 98110'
      end
      it "should format austrian numbers" do
        Phony.format('43198110', :format => :international_absolute).should == '+43 1 98110'
      end
      it "should format french numbers" do
        Phony.format('33142278186', :format => :+).should == '+33 1 42 27 81 86'
      end
      it "should format austrian numbers" do
        Phony.format('43198110', :format => :international_relative).should == '0043 1 98110'
      end
      it 'should format liechtensteiner numbers' do
        Phony.format('4233841148', :format => :international_relative).should == '00423 384 11 48'
      end
      context 'with no spaces' do
        it "should format north american numbers" do
          Phony.format('18091231234', :format => :international, :spaces => '').should == '+18091231234'
        end
        it "should format austrian numbers" do
          Phony.format('43198110', :format => :international, :spaces => '').should == '+43198110'
        end
        it "should format austrian numbers" do
          Phony.format('43198110', :format => :international_absolute, :spaces => '').should == '+43198110'
        end
        it "should format french numbers" do
          Phony.format('33142278186', :format => :+, :spaces => '').should == '+33142278186'
        end
        it "should format austrian numbers" do
          Phony.format('43198110', :format => :international_relative, :spaces => '').should == '0043198110'
        end
        it 'should format liechtensteiner numbers' do
          Phony.format('4233841148', :format => :international_relative, :spaces => '').should == '004233841148'
        end
      end
      context 'with special spaces' do
        it "should format swiss numbers" do
          Phony.format('41443643532', :format => :international).should == '+41 44 364 35 32'
        end
        it "should format north american numbers" do
          Phony.format('18091231234', :format => :international, :spaces => :-).should == '+1-809-123-1234'
        end
        it "should format austrian numbers" do
          Phony.format('43198110', :format => :international, :spaces => :-).should == '+43-1-98110'
        end
        it "should format austrian numbers" do
          Phony.format('43198110', :format => :international_absolute, :spaces => :-).should == '+43-1-98110'
        end
        it "should format french numbers" do
          Phony.format('33142278186', :format => :+, :spaces => :-).should == '+33-1-42-27-81-86'
        end
        it "should format austrian numbers" do
          Phony.format('43198110', :format => :international_relative, :spaces => :-).should == '0043-1-98110'
        end
        it 'should format liechtensteiner numbers' do
          Phony.format('4233841148', :format => :international_relative, :spaces => :-).should == '00423-384-11-48'
        end
      end
      describe '"unsupported" countries' do
        it 'should format as a single block' do
          Phony.format('88032155605220').should == '+880 32155605220'
        end
        it 'should format as a single block, regardless of format' do
          Phony.format('8801819372205', :format => :international).should == '+880 1819372205'
        end
        it 'should format as a single block, respecting custom spaces' do
          Phony.format('8801819372205', :spaces => :-).should == '+880-1819372205'
        end
        it 'should format as a single block, even without spaces' do
          Phony.format('8801819372205', :spaces => '').should == '+8801819372205'
        end
      end
    end
    describe "national" do
      it "should format swiss numbers" do
        Phony.format('41443643532', :format => :national).should == '044 364 35 32'
      end
      # TODO
      #
      it "should format swiss service numbers" do
        Phony.format('41800112233', :format => :national).should == '0800 112 233'
      end
      it "should format austrian numbers" do
        Phony.format('43198110', :format => :national).should == '01 98110'
      end
    end
    describe "local" do
      it "should format swiss numbers" do
        Phony.format('41443643532', :format => :local).should == '364 35 32'
      end
      it "should format german numbers" do
        Phony.format('493038625454', :format => :local).should == '386 25454'
      end
    end
  end
  
  context 'minimal cases' do
    context 'normalizing' do
      it 'handles completely crazy "numbers"' do
        Phony.normalize('Hello, I am Cora, the 41th parrot, and 044 is my 364 times 35 funky number. 32.').should == '41443643532'
      end
    end
    context 'splitting' do
      it 'handles completely missing numbers well enough' do
        Phony.split('4144').should == ['41', '44', '']
      end
      it 'handles a missing number part' do
        Phony.split('4144364').should == ['41', '44', '364']
      end
      it 'handles a missing number part' do
        Phony.split('414436435').should == ['41', '44', '364', '35']
      end
    end
    context 'formatting' do
      it 'handles completely missing numbers well enough' do
        Phony.format('4144').should == '+41 44 '
      end
      it 'handles a missing number part' do
        Phony.format('4144364').should == '+41 44 364'
      end
      it 'handles a missing number part' do
        Phony.format('414436435').should == '+41 44 364 35'
      end
    end
    context '"unsupported" countries' do
      it 'handles normalizing, then formatting' do
        Phony.format(Phony.normalize('+880-321 5560 5220')).should == '+880 32155605220'
      end
    end
  end
  
  context "speed" do
    before(:each) do
      @phone_numbers = [
        '41443643532',
        '18091231234',
        '43198110',
        '33142278186',
        '4233841148'
      ]
    end
    describe 'split' do
      it 'is fast' do
        number = @phone_numbers.first
        performance_of { Phony.split(number) }.should < 0.00005
      end
      it 'is fast' do
        performance_of { @phone_numbers.each { |number| Phony.split(number) } }.should < 0.00015
      end
    end
    describe 'normalize' do
      it 'is fast' do
        number = @phone_numbers.first
        performance_of { Phony.normalize(number) }.should < 0.0001
      end
      it 'is fast' do
        performance_of { @phone_numbers.each { |number| Phony.normalize(number) } }.should < 0.00016
      end
    end
    describe 'format' do
      it 'is fast' do
        number = @phone_numbers.first
        performance_of { Phony.format(number) }.should < 0.000075
      end
      it 'is fast' do
        performance_of { @phone_numbers.each { |number| Phony.format(number) } }.should < 0.00016
      end
    end
  end
  
  describe 'vanity' do
    describe 'vanity_number?' do
      it {Phony.vanity?('41800 WEGGLI').should be_true}
      it {Phony.vanity?('41800WEGGLI').should be_true}
      it {Phony.vanity?('41848 SUCCESSMATCH').should be_true}
      it {Phony.vanity?('4180 NO NO NO').should be_false}
      it {Phony.vanity?('41900 KURZ').should be_false}
      it {Phony.vanity?('41 44 364 35 32').should be_false}
    end
    
    describe 'vanity_to_number' do
      it {Phony.vanity_to_number('41800WEGGLI').should == '41800934454'}
      it {Phony.vanity_to_number('41800weggli').should == '41800934454'}
      it {Phony.vanity_to_number('41800SUCCESSMATCH').should == '41800782237'} # Cut off according to the swiss norms.
      it {Phony.vanity_to_number('4180BLA').should == '4180252'} # Does not check for validity of number.
    end
  end

end
Something went wrong with that request. Please try again.