Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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/luhn/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[package]
edition = "2018"
name = "luhn"
version = "1.3.0"
version = "1.6.1"
93 changes: 65 additions & 28 deletions exercises/luhn/tests/luhn.rs
Original file line number Diff line number Diff line change
@@ -1,90 +1,127 @@
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);
}