From b0078528579a279dcfa5a2b521c6f4b15277e39f Mon Sep 17 00:00:00 2001 From: ZapAnton Date: Tue, 20 Nov 2018 14:36:25 +0300 Subject: [PATCH 1/2] phone-number: Updated the exercise to the 1.6.1 version Relevant PRs: - https://github.com/exercism/problem-specifications/pull/772 - https://github.com/exercism/problem-specifications/pull/1090 - https://github.com/exercism/problem-specifications/pull/1120 - https://github.com/exercism/problem-specifications/pull/1327 - https://github.com/exercism/problem-specifications/pull/1337 - https://github.com/exercism/problem-specifications/pull/1361 --- exercises/phone-number/Cargo.toml | 2 +- exercises/phone-number/example.rs | 50 ++++++++---- exercises/phone-number/tests/phone-number.rs | 85 ++++++++++++++------ 3 files changed, 98 insertions(+), 39 deletions(-) diff --git a/exercises/phone-number/Cargo.toml b/exercises/phone-number/Cargo.toml index 9ea7a8b59..a2c51c10a 100644 --- a/exercises/phone-number/Cargo.toml +++ b/exercises/phone-number/Cargo.toml @@ -1,3 +1,3 @@ [package] name = "phone-number" -version = "1.2.0" +version = "1.6.1" diff --git a/exercises/phone-number/example.rs b/exercises/phone-number/example.rs index fe177d727..aa9023b93 100644 --- a/exercises/phone-number/example.rs +++ b/exercises/phone-number/example.rs @@ -1,17 +1,37 @@ -pub fn number(s: &str) -> Option { - let digits: String = s.chars().filter(|&c| c.is_digit(10)).collect(); - match digits.len() { - 10 => match (digits.chars().nth(0), digits.chars().nth(3)) { - (Some('0'), _) => None, - (Some('1'), _) => None, - (_, Some('0')) => None, - (_, Some('1')) => None, - _ => Some(digits), - }, - 11 => match digits.chars().nth(0) { - Some('1') => Some(digits[1..].to_string()), - _ => None, - }, - _ => None, +pub fn number(user_number: &str) -> Option { + let mut filtered_number: String = user_number.chars().filter(|ch| ch.is_digit(10)).collect(); + + let number_len = filtered_number.len(); + + if number_len < 10 + || number_len > 11 + || (filtered_number.len() == 11 + && filtered_number.chars().nth(0).unwrap() != '1') + { + return None; } + + if number_len == 11 { + filtered_number = filtered_number.chars().skip(1).collect(); + } + + if filtered_number + .chars() + .nth(0) + .unwrap() + .to_digit(10) + .unwrap() + < 2 + || filtered_number + .chars() + .nth(3) + .unwrap() + .to_digit(10) + .unwrap() + < 2 + { + return None; + } + + Some(filtered_number) } diff --git a/exercises/phone-number/tests/phone-number.rs b/exercises/phone-number/tests/phone-number.rs index a903d310b..16e5d84e6 100644 --- a/exercises/phone-number/tests/phone-number.rs +++ b/exercises/phone-number/tests/phone-number.rs @@ -1,85 +1,124 @@ extern crate phone_number as phone; -fn to_some_string(s: &str) -> Option { - Some(s.to_string()) +fn process_clean_case(number: &str, expected: Option<&str>) { + assert_eq!(phone::number(number), expected.map(|x| x.to_string())); } #[test] fn test_cleans_the_number() { - assert_eq!( - phone::number("(223) 456-7890"), - to_some_string("2234567890") - ); + process_clean_case("(223) 456-7890", Some("2234567890")); } #[test] #[ignore] fn test_cleans_numbers_with_dots() { - assert_eq!(phone::number("223.456.7890"), to_some_string("2234567890")); + process_clean_case("223.456.7890", Some("2234567890")); } #[test] #[ignore] fn test_cleans_numbers_with_multiple_spaces() { - assert_eq!( - phone::number("223 456 7890 "), - to_some_string("2234567890") - ); + process_clean_case("223 456 7890 ", Some("2234567890")); } #[test] #[ignore] fn test_invalid_when_9_digits() { - assert_eq!(phone::number("123456789"), None); + process_clean_case("123456789", None); } #[test] #[ignore] fn test_invalid_when_11_digits_does_not_start_with_a_1() { - assert_eq!(phone::number("22234567890"), None); + process_clean_case("22234567890", None); } #[test] #[ignore] fn test_valid_when_11_digits_and_starting_with_1() { - assert_eq!(phone::number("12234567890"), to_some_string("2234567890")); + process_clean_case("12234567890", Some("2234567890")); } #[test] #[ignore] fn test_valid_when_11_digits_and_starting_with_1_even_with_punctuation() { - assert_eq!( - phone::number("+1 (223) 456-7890"), - to_some_string("2234567890") - ); + process_clean_case("+1 (223) 456-7890", Some("2234567890")); } #[test] #[ignore] fn test_invalid_when_more_than_11_digits() { - assert_eq!(phone::number("321234567890"), None); + process_clean_case("321234567890", None); } #[test] #[ignore] fn test_invalid_with_letters() { - assert_eq!(phone::number("123-abc-7890"), None); + process_clean_case("123-abc-7890", None); } #[test] #[ignore] fn test_invalid_with_punctuations() { - assert_eq!(phone::number("123-@:!-7890"), None); + process_clean_case("123-@:!-7890", None); } #[test] #[ignore] fn test_invalid_if_area_code_does_not_start_with_2_9() { - assert_eq!(phone::number("(123) 456-7890"), None); + process_clean_case("(123) 456-7890", None); } #[test] #[ignore] fn test_invalid_if_exchange_code_does_not_start_with_2_9() { - assert_eq!(phone::number("(223) 056-7890"), None); + process_clean_case("(223) 056-7890", None); +} + +#[test] +#[ignore] +fn test_invalid_if_area_code_starts_with_1_on_valid_11digit_number() { + process_clean_case("1 (123) 456-7890", None); +} + +#[test] +#[ignore] +fn test_invalid_if_area_code_starts_with_0_on_valid_11digit_number() { + process_clean_case("1 (023) 456-7890", None); +} + +#[test] +#[ignore] +fn test_invalid_if_area_code_starts_with_1() { + process_clean_case("(123) 456-7890", None); +} + +#[test] +#[ignore] +fn test_invalid_if_exchange_code_starts_with_1() { + process_clean_case("(223) 156-7890", None); +} + +#[test] +#[ignore] +fn test_invalid_if_exchange_code_starts_with_0() { + process_clean_case("(223) 056-7890", None); +} + +#[test] +#[ignore] +fn test_invalid_if_exchange_code_starts_with_1_on_valid_11digit_number() { + process_clean_case("1 (223) 156-7890", None); +} + +#[test] +#[ignore] +fn test_invalid_if_exchange_code_starts_with_0_on_valid_11digit_number() { + process_clean_case("1 (223) 056-7890", None); +} + +#[test] +#[ignore] +fn test_invalid_if_area_code_starts_with_0() { + process_clean_case("(023) 456-7890", None); } From 7f57e92d98a678c72974fb9a343cbbc4c2085240 Mon Sep 17 00:00:00 2001 From: ZapAnton Date: Wed, 21 Nov 2018 09:14:09 +0300 Subject: [PATCH 2/2] phone-number: Removed the duplicate test cases --- exercises/phone-number/tests/phone-number.rs | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/exercises/phone-number/tests/phone-number.rs b/exercises/phone-number/tests/phone-number.rs index 16e5d84e6..6c0fc6bdf 100644 --- a/exercises/phone-number/tests/phone-number.rs +++ b/exercises/phone-number/tests/phone-number.rs @@ -63,18 +63,6 @@ fn test_invalid_with_punctuations() { process_clean_case("123-@:!-7890", None); } -#[test] -#[ignore] -fn test_invalid_if_area_code_does_not_start_with_2_9() { - process_clean_case("(123) 456-7890", None); -} - -#[test] -#[ignore] -fn test_invalid_if_exchange_code_does_not_start_with_2_9() { - process_clean_case("(223) 056-7890", None); -} - #[test] #[ignore] fn test_invalid_if_area_code_starts_with_1_on_valid_11digit_number() {