From ae78e3600b3283990d9b05b4554c94946a1aa92d Mon Sep 17 00:00:00 2001 From: ZapAnton Date: Tue, 12 Nov 2019 14:53:36 +0300 Subject: [PATCH 1/2] luhn: Updated the exercise to the version 1.6.1 Relevant PRs: - https://github.com/exercism/problem-specifications/pull/1420 - https://github.com/exercism/problem-specifications/pull/1480 - https://github.com/exercism/problem-specifications/pull/1500 - https://github.com/exercism/problem-specifications/pull/1523 --- exercises/luhn/Cargo.toml | 2 +- exercises/luhn/tests/luhn.rs | 91 +++++++++++++++++++++++++----------- 2 files changed, 64 insertions(+), 29 deletions(-) diff --git a/exercises/luhn/Cargo.toml b/exercises/luhn/Cargo.toml index af784656b..6b54be8b0 100644 --- a/exercises/luhn/Cargo.toml +++ b/exercises/luhn/Cargo.toml @@ -1,4 +1,4 @@ [package] edition = "2018" name = "luhn" -version = "1.3.0" +version = "1.6.1" diff --git a/exercises/luhn/tests/luhn.rs b/exercises/luhn/tests/luhn.rs index 0915cdf73..cd2a0ad16 100644 --- a/exercises/luhn/tests/luhn.rs +++ b/exercises/luhn/tests/luhn.rs @@ -1,90 +1,125 @@ use luhn::*; +fn process_valid_case(number: &str, is_luhn_expected: bool) { + assert_eq!(is_valid(number), is_luhn_expected); +} + #[test] -fn single_digit_string_is_invalid() { - assert!(!is_valid("1")); +fn test_single_digit_strings_can_not_be_valid() { + process_valid_case("1", false); } #[test] #[ignore] -fn single_zero_string_is_invalid() { - assert!(!is_valid("0")); +fn test_a_single_zero_is_invalid() { + process_valid_case("0", false); } #[test] #[ignore] -fn simple_valid_sin_that_remains_valid_if_reversed() { - assert!(is_valid("059")); +fn test_a_simple_valid_sin_that_remains_valid_if_reversed() { + process_valid_case("059", true); } #[test] #[ignore] -fn simple_valid_sin_that_becomes_invalid_if_reversed() { - assert!(is_valid("59")); +fn test_a_simple_valid_sin_that_becomes_invalid_if_reversed() { + process_valid_case("59", true); } #[test] #[ignore] -fn valid_canadian_sin_is_valid() { - assert!(is_valid("055 444 285")); +fn test_a_valid_canadian_sin() { + process_valid_case("055 444 285", true); } #[test] #[ignore] -fn invalid_canadian_sin_is_invalid() { - assert!(!is_valid("055 444 286")); +fn test_invalid_canadian_sin() { + process_valid_case("055 444 286", false); } #[test] #[ignore] -fn invalid_credit_card_is_invalid() { - assert!(!is_valid("8273 1232 7352 0569")); +fn test_invalid_credit_card() { + process_valid_case("8273 1232 7352 0569", false); } #[test] #[ignore] -fn valid_number_with_an_even_number_of_digits() { - assert!(is_valid("095 245 88")); +fn test_valid_number_with_an_even_number_of_digits() { + process_valid_case("095 245 88", true); } #[test] #[ignore] fn strings_that_contain_non_digits_are_invalid() { - assert!(!is_valid("055a 444 285")); + process_valid_case("055a 444 285", false); } #[test] #[ignore] -fn punctuation_is_invalid() { - assert!(!is_valid("055-444-285")); +fn test_valid_strings_with_punctuation_included_become_invalid() { + process_valid_case("055-444-285", false); } #[test] #[ignore] fn symbols_are_invalid() { - assert!(!is_valid("055£ 444$ 285")); + process_valid_case("055£ 444$ 285", false); +} + +#[test] +#[ignore] +fn test_single_zero_with_space_is_invalid() { + process_valid_case(" 0", false); +} + +#[test] +#[ignore] +fn test_more_than_a_single_zero_is_valid() { + process_valid_case("0000 0", true); +} + +#[test] +#[ignore] +fn test_input_digit_9_is_correctly_converted_to_output_digit_9() { + process_valid_case("091", true); +} + +#[test] +#[ignore] +/// using ascii value for doubled non-digit isn't allowed/// "Convert non-digits to their ascii values and then offset them by 48 sometimes accidentally declare an invalid string to be valid." +/// "This test is designed to avoid that solution." +fn test_using_ascii_value_for_doubled_nondigit_isnt_allowed() { + process_valid_case(":9", false); } #[test] #[ignore] -fn single_digit_with_space_is_invalid() { - assert!(!is_valid(" 0")); +/// valid strings with a non-digit added at the end become invalid +fn test_valid_strings_with_a_nondigit_added_at_the_end_become_invalid() { + process_valid_case("059a", false); } #[test] #[ignore] -fn more_than_a_single_zero_is_valid() { - assert!(is_valid("0000 0")); +/// valid strings with symbols included become invalid +fn test_valid_strings_with_symbols_included_become_invalid() { + process_valid_case("055# 444$ 285", false); } #[test] #[ignore] -fn input_digit_9_is_correctly_converted_to_output_digit_9() { - assert!(is_valid("091")); +/// using ascii value for non-doubled non-digit isn't allowed/// "Convert non-digits to their ascii values and then offset them by 48 sometimes accidentally declare an invalid string to be valid." +/// "This test is designed to avoid that solution." +fn test_using_ascii_value_for_nondoubled_nondigit_isnt_allowed() { + process_valid_case("055b 444 285", false); } #[test] #[ignore] -fn non_digit_isnt_converted_to_digit_by_ascii_value() { - assert!(!is_valid(":9")); +/// valid number with an odd number of spaces +fn test_valid_number_with_an_odd_number_of_spaces() { + process_valid_case("234 567 891 234", true); } From 8cb297bf0f9feea7c033203a956e7a96a056dcb5 Mon Sep 17 00:00:00 2001 From: ZapAnton Date: Tue, 12 Nov 2019 16:15:22 +0300 Subject: [PATCH 2/2] luhn: Fixed the 'exercise' util generated comments in the test suite --- exercises/luhn/tests/luhn.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/exercises/luhn/tests/luhn.rs b/exercises/luhn/tests/luhn.rs index cd2a0ad16..0f72f2638 100644 --- a/exercises/luhn/tests/luhn.rs +++ b/exercises/luhn/tests/luhn.rs @@ -89,8 +89,9 @@ fn test_input_digit_9_is_correctly_converted_to_output_digit_9() { #[test] #[ignore] -/// using ascii value for doubled non-digit isn't allowed/// "Convert non-digits to their ascii values and then offset them by 48 sometimes accidentally declare an invalid string to be valid." -/// "This test is designed to avoid that solution." +/// using ascii value for doubled non-digit isn't allowed +/// Convert non-digits to their ascii values and then offset them by 48 sometimes accidentally declare an invalid string to be valid. +/// This test is designed to avoid that solution. fn test_using_ascii_value_for_doubled_nondigit_isnt_allowed() { process_valid_case(":9", false); } @@ -111,8 +112,9 @@ fn test_valid_strings_with_symbols_included_become_invalid() { #[test] #[ignore] -/// using ascii value for non-doubled non-digit isn't allowed/// "Convert non-digits to their ascii values and then offset them by 48 sometimes accidentally declare an invalid string to be valid." -/// "This test is designed to avoid that solution." +/// using ascii value for non-doubled non-digit isn't allowed +/// Convert non-digits to their ascii values and then offset them by 48 sometimes accidentally declare an invalid string to be valid. +/// This test is designed to avoid that solution. fn test_using_ascii_value_for_nondoubled_nondigit_isnt_allowed() { process_valid_case("055b 444 285", false); }