From 618fbef5341c8054eac4da45b488b7a22be59a0d Mon Sep 17 00:00:00 2001 From: Pedro Gaspar Date: Fri, 5 Oct 2018 13:36:46 +0100 Subject: [PATCH 1/2] phone-number: Regenerate Tests Update generator to use the standard error indicator. Update solution to pass updated tests. --- .../.meta/generator/phone_number_case.rb | 12 ++++- .../.meta/solutions/phone_number.rb | 3 +- exercises/phone-number/phone_number_test.rb | 54 ++++++++++++++----- 3 files changed, 53 insertions(+), 16 deletions(-) diff --git a/exercises/phone-number/.meta/generator/phone_number_case.rb b/exercises/phone-number/.meta/generator/phone_number_case.rb index c42599349d..b14c160c40 100644 --- a/exercises/phone-number/.meta/generator/phone_number_case.rb +++ b/exercises/phone-number/.meta/generator/phone_number_case.rb @@ -2,6 +2,16 @@ class PhoneNumberCase < Generator::ExerciseCase def workload - assert_equal(expected, "PhoneNumber.clean(#{phrase.inspect})") + if error_expected? + assert_raises(ArgumentError, subject_of_test) + else + assert_equal(expected, subject_of_test) + end + end + + private + + def subject_of_test + "PhoneNumber.clean(#{phrase.inspect})" end end diff --git a/exercises/phone-number/.meta/solutions/phone_number.rb b/exercises/phone-number/.meta/solutions/phone_number.rb index df8493bb0a..56808bc242 100644 --- a/exercises/phone-number/.meta/solutions/phone_number.rb +++ b/exercises/phone-number/.meta/solutions/phone_number.rb @@ -11,7 +11,8 @@ module PhoneNumber def self.clean(number) sanitized = digits_only(number) sections = nanp_parse(sanitized) - format '%s%s%s', sections if sections + raise ArgumentError unless sections + format '%s%s%s', sections end def self.digits_only(number) diff --git a/exercises/phone-number/phone_number_test.rb b/exercises/phone-number/phone_number_test.rb index 3167daa11b..e97f28839f 100644 --- a/exercises/phone-number/phone_number_test.rb +++ b/exercises/phone-number/phone_number_test.rb @@ -1,7 +1,7 @@ require 'minitest/autorun' require_relative 'phone_number' -# Common test data version: 1.5.0 e8a5119 +# Common test data version: 1.6.0 a317aa4 class PhoneNumberTest < Minitest::Test def test_cleans_the_number # skip @@ -20,12 +20,16 @@ def test_cleans_numbers_with_multiple_spaces def test_invalid_when_9_digits skip - assert_nil PhoneNumber.clean("123456789") + assert_raises(ArgumentError) do + PhoneNumber.clean("123456789") + end end def test_invalid_when_11_digits_does_not_start_with_a_1 skip - assert_nil PhoneNumber.clean("22234567890") + assert_raises(ArgumentError) do + PhoneNumber.clean("22234567890") + end end def test_valid_when_11_digits_and_starting_with_1 @@ -40,56 +44,78 @@ def test_valid_when_11_digits_and_starting_with_1_even_with_punctuation def test_invalid_when_more_than_11_digits skip - assert_nil PhoneNumber.clean("321234567890") + assert_raises(ArgumentError) do + PhoneNumber.clean("321234567890") + end end def test_invalid_with_letters skip - assert_nil PhoneNumber.clean("123-abc-7890") + assert_raises(ArgumentError) do + PhoneNumber.clean("123-abc-7890") + end end def test_invalid_with_punctuations skip - assert_nil PhoneNumber.clean("123-@:!-7890") + assert_raises(ArgumentError) do + PhoneNumber.clean("123-@:!-7890") + end end def test_invalid_if_area_code_starts_with_0 skip - assert_nil PhoneNumber.clean("(023) 456-7890") + assert_raises(ArgumentError) do + PhoneNumber.clean("(023) 456-7890") + end end def test_invalid_if_area_code_starts_with_1 skip - assert_nil PhoneNumber.clean("(123) 456-7890") + assert_raises(ArgumentError) do + PhoneNumber.clean("(123) 456-7890") + end end def test_invalid_if_exchange_code_starts_with_0 skip - assert_nil PhoneNumber.clean("(223) 056-7890") + assert_raises(ArgumentError) do + PhoneNumber.clean("(223) 056-7890") + end end def test_invalid_if_exchange_code_starts_with_1 skip - assert_nil PhoneNumber.clean("(223) 156-7890") + assert_raises(ArgumentError) do + PhoneNumber.clean("(223) 156-7890") + end end def test_invalid_if_area_code_starts_with_0_on_valid_11_digit_number skip - assert_nil PhoneNumber.clean("1 (023) 456-7890") + assert_raises(ArgumentError) do + PhoneNumber.clean("1 (023) 456-7890") + end end def test_invalid_if_area_code_starts_with_1_on_valid_11_digit_number skip - assert_nil PhoneNumber.clean("1 (123) 456-7890") + assert_raises(ArgumentError) do + PhoneNumber.clean("1 (123) 456-7890") + end end def test_invalid_if_exchange_code_starts_with_0_on_valid_11_digit_number skip - assert_nil PhoneNumber.clean("1 (223) 056-7890") + assert_raises(ArgumentError) do + PhoneNumber.clean("1 (223) 056-7890") + end end def test_invalid_if_exchange_code_starts_with_1_on_valid_11_digit_number skip - assert_nil PhoneNumber.clean("1 (223) 156-7890") + assert_raises(ArgumentError) do + PhoneNumber.clean("1 (223) 156-7890") + end end end From 1ec8fcd36230ce62989615b4d9b5c1e532a82a0d Mon Sep 17 00:00:00 2001 From: Pedro Gaspar Date: Tue, 16 Oct 2018 20:12:16 +0100 Subject: [PATCH 2/2] phone-number: Expect nil on invalid input Relevant discussion: https://github.com/exercism/ruby/pull/870/files#r223096387 --- .../.meta/generator/phone_number_case.rb | 2 +- .../.meta/solutions/phone_number.rb | 3 +- exercises/phone-number/phone_number_test.rb | 52 +++++-------------- 3 files changed, 15 insertions(+), 42 deletions(-) diff --git a/exercises/phone-number/.meta/generator/phone_number_case.rb b/exercises/phone-number/.meta/generator/phone_number_case.rb index b14c160c40..5edbe99a2d 100644 --- a/exercises/phone-number/.meta/generator/phone_number_case.rb +++ b/exercises/phone-number/.meta/generator/phone_number_case.rb @@ -3,7 +3,7 @@ class PhoneNumberCase < Generator::ExerciseCase def workload if error_expected? - assert_raises(ArgumentError, subject_of_test) + assert_equal(nil, subject_of_test) else assert_equal(expected, subject_of_test) end diff --git a/exercises/phone-number/.meta/solutions/phone_number.rb b/exercises/phone-number/.meta/solutions/phone_number.rb index 56808bc242..df8493bb0a 100644 --- a/exercises/phone-number/.meta/solutions/phone_number.rb +++ b/exercises/phone-number/.meta/solutions/phone_number.rb @@ -11,8 +11,7 @@ module PhoneNumber def self.clean(number) sanitized = digits_only(number) sections = nanp_parse(sanitized) - raise ArgumentError unless sections - format '%s%s%s', sections + format '%s%s%s', sections if sections end def self.digits_only(number) diff --git a/exercises/phone-number/phone_number_test.rb b/exercises/phone-number/phone_number_test.rb index e97f28839f..4716d75756 100644 --- a/exercises/phone-number/phone_number_test.rb +++ b/exercises/phone-number/phone_number_test.rb @@ -20,16 +20,12 @@ def test_cleans_numbers_with_multiple_spaces def test_invalid_when_9_digits skip - assert_raises(ArgumentError) do - PhoneNumber.clean("123456789") - end + assert_nil PhoneNumber.clean("123456789") end def test_invalid_when_11_digits_does_not_start_with_a_1 skip - assert_raises(ArgumentError) do - PhoneNumber.clean("22234567890") - end + assert_nil PhoneNumber.clean("22234567890") end def test_valid_when_11_digits_and_starting_with_1 @@ -44,78 +40,56 @@ def test_valid_when_11_digits_and_starting_with_1_even_with_punctuation def test_invalid_when_more_than_11_digits skip - assert_raises(ArgumentError) do - PhoneNumber.clean("321234567890") - end + assert_nil PhoneNumber.clean("321234567890") end def test_invalid_with_letters skip - assert_raises(ArgumentError) do - PhoneNumber.clean("123-abc-7890") - end + assert_nil PhoneNumber.clean("123-abc-7890") end def test_invalid_with_punctuations skip - assert_raises(ArgumentError) do - PhoneNumber.clean("123-@:!-7890") - end + assert_nil PhoneNumber.clean("123-@:!-7890") end def test_invalid_if_area_code_starts_with_0 skip - assert_raises(ArgumentError) do - PhoneNumber.clean("(023) 456-7890") - end + assert_nil PhoneNumber.clean("(023) 456-7890") end def test_invalid_if_area_code_starts_with_1 skip - assert_raises(ArgumentError) do - PhoneNumber.clean("(123) 456-7890") - end + assert_nil PhoneNumber.clean("(123) 456-7890") end def test_invalid_if_exchange_code_starts_with_0 skip - assert_raises(ArgumentError) do - PhoneNumber.clean("(223) 056-7890") - end + assert_nil PhoneNumber.clean("(223) 056-7890") end def test_invalid_if_exchange_code_starts_with_1 skip - assert_raises(ArgumentError) do - PhoneNumber.clean("(223) 156-7890") - end + assert_nil PhoneNumber.clean("(223) 156-7890") end def test_invalid_if_area_code_starts_with_0_on_valid_11_digit_number skip - assert_raises(ArgumentError) do - PhoneNumber.clean("1 (023) 456-7890") - end + assert_nil PhoneNumber.clean("1 (023) 456-7890") end def test_invalid_if_area_code_starts_with_1_on_valid_11_digit_number skip - assert_raises(ArgumentError) do - PhoneNumber.clean("1 (123) 456-7890") - end + assert_nil PhoneNumber.clean("1 (123) 456-7890") end def test_invalid_if_exchange_code_starts_with_0_on_valid_11_digit_number skip - assert_raises(ArgumentError) do - PhoneNumber.clean("1 (223) 056-7890") - end + assert_nil PhoneNumber.clean("1 (223) 056-7890") end def test_invalid_if_exchange_code_starts_with_1_on_valid_11_digit_number skip - assert_raises(ArgumentError) do - PhoneNumber.clean("1 (223) 156-7890") - end + assert_nil PhoneNumber.clean("1 (223) 156-7890") end end