Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #310 Conform phone number unit test to NANP and updated track to 1.2.0 #359

Merged
merged 2 commits into from
Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion exercises/phone-number/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion exercises/phone-number/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[package]
name = "phone-number"
version = "0.0.0"
version = "1.2.0"
43 changes: 27 additions & 16 deletions exercises/phone-number/example.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
pub fn number(s: &str) -> Option<String> {
let digits: String = s
.chars()
.filter(|&c| c.is_digit(10))
.collect();
let digits: String = s.chars().filter(|&c| c.is_digit(10)).collect();
match digits.len() {
10 => Some(digits),
11 => match digits.chars().nth(0) {
Some('1') => Some(digits[1..].to_string()),
_ => None
},
_ => None
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,
}
}

Expand All @@ -18,10 +25,14 @@ pub fn area_code(s: &str) -> Option<String> {
}

pub fn pretty_print(s: &str) -> String {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since pretty_print is no longer being tested, you can and should now remove this function

You also can/should remove area_code

number(s).map(|n|
format!("({area}) {prefix}-{exchange}",
area=&n[..3],
prefix=&n[3..6],
exchange=&n[6..])
).unwrap_or("invalid".to_string())
number(s)
.map(|n| {
format!(
"({area}) {prefix}-{exchange}",
area = &n[..3],
prefix = &n[3..6],
exchange = &n[6..]
)
})
.unwrap_or("invalid".to_string())
}
82 changes: 31 additions & 51 deletions exercises/phone-number/tests/phone-number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,90 +5,70 @@ fn to_some_string(s: &str) -> Option<String> {
}

#[test]
fn test_number_cleans() {
assert_eq!(phone::number("(123) 456-7890"), to_some_string("1234567890"));
fn test_cleans_the_number() {
assert_eq!(
phone::number("(223) 456-7890"),
to_some_string("2234567890")
);
}

#[test]
#[ignore]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please leave the ignores in place, as discussed in #279 and https://github.com/exercism/docs/blob/master/language-tracks/exercises/anatomy/test-suites.md (where it says "In order to provide a better user experience, some language tracks only leave the first test in an exercise test suite active.")

fn test_number_cleans_with_dots() {
assert_eq!(phone::number("123.456.7890"), to_some_string("1234567890"));
fn test_cleans_numbers_with_dots() {
assert_eq!(phone::number("223.456.7890"), to_some_string("2234567890"));
}

#[test]
#[ignore]
fn test_valid_when_11_digits_and_first_is_1() {
assert_eq!(phone::number("11234567890"), to_some_string("1234567890"));
fn test_cleans_numbers_with_multiple_spaces() {
assert_eq!(
phone::number("223 456 7890 "),
to_some_string("2234567890")
);
}

#[test]
#[ignore]
fn test_invalid_when_11_digits() {
assert_eq!(phone::number("21234567890"), None);
}

#[test]
#[ignore]
fn test_invalid_when_9_digits() {
assert_eq!(phone::number("123456789"), None);
}

#[test]
#[ignore]
fn test_invalid_when_empty() {
assert_eq!(phone::number(""), None);
}

#[test]
#[ignore]
fn test_invalid_when_no_digits_present() {
assert_eq!(phone::number(" (-) "), None);
}

#[test]
#[ignore]
fn test_valid_with_leading_characters() {
assert_eq!(phone::number("my number is 123 456 7890"), to_some_string("1234567890"));
fn test_invalid_when_11_digits_does_not_start_with_a_1() {
assert_eq!(phone::number("22234567890"), None);
}

#[test]
#[ignore]
fn test_valid_with_trailing_characters() {
assert_eq!(phone::number("123 456 7890 - bob"), to_some_string("1234567890"));
fn test_valid_when_11_digits_and_starting_with_1() {
assert_eq!(phone::number("12234567890"), to_some_string("2234567890"));
}

#[test]
#[ignore]
fn test_area_code() {
assert_eq!(phone::area_code("1234567890"), to_some_string("123"));
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")
);
}

#[test]
#[ignore]
fn test_area_code_with_full_us_phone_number() {
assert_eq!(phone::area_code("18234567890"), to_some_string("823"));
fn test_invalid_when_more_than_11_digits() {
assert_eq!(phone::number("321234567890"), None);
}

#[test]
#[ignore]
fn test_area_code_with_invalid() {
assert_eq!(phone::area_code("1234161567890"), None);
fn test_invalid_with_letters() {
assert_eq!(phone::number("123-abc-7890"), None);
}

#[test]
#[ignore]
fn test_pretty_print() {
assert_eq!(phone::pretty_print("1234567890"), "(123) 456-7890");
fn test_invalid_with_punctuations() {
assert_eq!(phone::number("123-@:!-7890"), None);
}

#[test]
#[ignore]
fn test_pretty_print_with_full_us_phone_number() {
assert_eq!(phone::pretty_print("11234567890"), "(123) 456-7890");
fn test_invalid_if_area_code_does_not_start_with_2_9() {
assert_eq!(phone::number("(123) 456-7890"), None);
}

#[test]
#[ignore]
fn test_pretty_print_with_invalid() {
assert_eq!(phone::pretty_print("1186234567890"), "invalid");
fn test_invalid_if_exchange_code_does_not_start_with_2_9() {
assert_eq!(phone::number("(223) 056-7890"), None);
}