test/phonelib_test.rb in phonelib-0.1.1 vs test/phonelib_test.rb in phonelib-0.1.2

- old
+ new

@@ -1,105 +1,175 @@ require 'test_helper' -class PhonelibTest < ActiveSupport::TestCase - test "truth" do +class PhonelibTest < Test::Unit::TestCase + + should 'be a Module' do assert_kind_of Module, Phonelib end - test "returns phone object" do - assert Phonelib.parse('972541234567').is_a? Phonelib::Phone - end + context '.parse' do + setup { @phone = Phonelib.parse '9721234567' } - test "valid? with malformed phone number" do - assert !Phonelib.valid?('sdffsd') - end + should 'return a Phone object' do + assert @phone.is_a? Phonelib::Phone #instance_of? + end - test "invalid? with malformed phone number" do - assert Phonelib.invalid?('sdffsd') + should 'be possible but not valid phone number' do + assert !@phone.valid? + assert @phone.possible? + end end - test "valid? with valid phone number" do - assert Phonelib.valid? '972541234567' - end + context '.valid?' do + context 'with malformed phone number' do + should 'not be valid' do + assert !Phonelib.valid?('sdffsd') + end + end - test "invalid? with valid phone number" do - assert !Phonelib.invalid?('972541234567') - end + context 'with valid phone number' do + should 'be valid' do + assert Phonelib.valid?('972541234567') + end + end - test "possible? with valid phone number" do - assert Phonelib.possible? '972541234567' + context 'with invalid phone number' do + should 'not be valid' do + assert !Phonelib.valid?('97254123') + end + end end - test "impossible? with valid phone number" do - assert !Phonelib.impossible?('972541234567') - end + context '.invalid?' do + context 'with malformed phone number' do + should 'be valid' do + assert Phonelib.invalid?('sdffsd') + end + end - test "valid? with invalid phone number" do - assert !Phonelib.valid?('97254123') - end + context 'with valid phone number' do + should 'not be valid' do + assert !Phonelib.invalid?('972541234567') + end + end - test "invalid? with invalid phone number" do - assert Phonelib.invalid?('97254123') + context 'with invalid phone number' do + should 'be valid' do + assert Phonelib.invalid?('97254123') + end + end end - test "possible? with invalid phone number" do - assert !Phonelib.possible?('97254123') - end + context '.possible?' do + context 'with valid phone number' do + should 'be valid' do + assert Phonelib.possible? '972541234567' + end + end - test "impossible? with invalid phone number" do - assert Phonelib.impossible?('97254123') + context 'with invalid phone number' do + should 'not be valid' do + assert !Phonelib.possible?('97254123') + end + end end - test "possible but not valid phone number" do - phone = Phonelib.parse('9721234567') - assert !phone.valid? - assert phone.possible? - end + context '.impossible?' do + context 'with valid phone number' do + should 'not be valid' do + assert !Phonelib.impossible?('972541234567') + end + end - test "valid_for_country? with correct data" do - assert Phonelib.valid_for_country?('972541234567', 'IL') + context 'with invalid phone number' do + should 'be valid' do + assert Phonelib.impossible?('97254123') + end + end end - - test "valid_for_country? with correct data and national number" do - assert Phonelib.valid_for_country?('0541234567', 'IL') - end - - test "valid_for_country? with correct data and without prefix" do - assert Phonelib.valid_for_country?('541234567', 'IL') - end - - test "valid_for_country? with fake data and without prefix" do - assert !Phonelib.valid_for_country?('541234567', 'US') - end - test "invalid_for_country? with correct data" do - assert !Phonelib.invalid_for_country?('972541234567', 'IL') - end + context 'valid_for_country?' do + context 'with correct data' do + ['IL', :il].each do |country| + context "with #{country} as country" do + should 'be valid' do + assert Phonelib.valid_for_country?('972541234567', country) + end - test "invalid_for_country? with incorrect data" do - assert Phonelib.invalid_for_country?('972541234567', 'US') - end + context 'and national number' do + should 'be valid' do + assert Phonelib.valid_for_country?('0541234567', country) + end + end - test "valid_for_country? with incorrect data" do - assert !Phonelib.valid_for_country?('972541234567', 'US') - end + context 'and without prefix' do + should 'be valid' do + assert Phonelib.valid_for_country?('541234567', country) + end + end + end + end + end - test "international method returns with right formatting" do - phone = Phonelib.parse('972541234567') - assert phone.international == '+972 54-123-4567' + ['US', :us].each do |country| + context "with #{country} as country" do + context 'with incorrect data' do + should 'not be valid' do + assert !Phonelib.valid_for_country?('972541234567', country) + end + + context 'and without prefix' do + should 'not be valid' do + assert !Phonelib.valid_for_country?('541234567', country) + end + end + end + end + end end - test "national method returns right formatting" do - phone = Phonelib.parse('972541234567') - assert phone.national == '054-123-4567' + context '.invalid_for_country?' do + context 'with correct data' do + ['IL', :il].each do |country| + context "with #{country} as country" do + should 'not be invalid' do + assert !Phonelib.invalid_for_country?('972541234567', country) + end + end + end + end + + context 'with incorrect data' do + ['US', :us].each do |country| + context "with #{country} as country" do + should 'be invalid' do + assert Phonelib.invalid_for_country?('972541234567', country) + end + end + end + end end - test "international method returns sanitized when number invalid but possible" do - phone = Phonelib.parse('9721234567') - assert phone.international == '+9721234567' + context '#international' do + should 'return right formatting' do + phone = Phonelib.parse('972541234567') + assert_equal '+972 54-123-4567', phone.international + end + + should 'return sanitized when number invalid but possible' do + phone = Phonelib.parse('9721234567') + assert_equal '+9721234567', phone.international + end end - test "national method returns sanitized national when number invalid but possible" do - phone = Phonelib.parse('9721234567') - assert phone.national == '1234567' + context '#national' do + should 'return right formatting' do + phone = Phonelib.parse('972541234567') + assert_equal '054-123-4567', phone.national + end + + should 'return sanitized national when number invalid but possible' do + phone = Phonelib.parse('9721234567') + assert_equal '1234567', phone.national + end end end