|
| 1 | +module main |
| 2 | + |
| 3 | +fn test_cleans_the_number() { |
| 4 | + phrase := '(223) 456-7890' |
| 5 | + assert clean(phrase)! == '2234567890' |
| 6 | +} |
| 7 | + |
| 8 | +fn test_cleans_numbers_with_dots() { |
| 9 | + phrase := '223.456.7890' |
| 10 | + assert clean(phrase)! == '2234567890' |
| 11 | +} |
| 12 | + |
| 13 | +fn test_cleans_numbers_with_multiple_spaces() { |
| 14 | + phrase := '223 456 7890 ' |
| 15 | + assert clean(phrase)! == '2234567890' |
| 16 | +} |
| 17 | + |
| 18 | +fn test_invalid_when_9_digits() { |
| 19 | + phrase := '123456789' |
| 20 | + if res := clean(phrase) { |
| 21 | + assert false, 'invalid when 9 digits should return an error' |
| 22 | + } else { |
| 23 | + assert err.msg() == 'must not be fewer than 10 digits' |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +fn test_invalid_when_11_digits_does_not_start_with_a_1() { |
| 28 | + phrase := '22234567890' |
| 29 | + if res := clean(phrase) { |
| 30 | + assert false, 'invalid when 11 digits does not start with a 1 should return an error' |
| 31 | + } else { |
| 32 | + assert err.msg() == '11 digits must start with 1' |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +fn test_valid_when_11_digits_and_starting_with_1() { |
| 37 | + phrase := '12234567890' |
| 38 | + assert clean(phrase)! == '2234567890' |
| 39 | +} |
| 40 | + |
| 41 | +fn test_valid_when_11_digits_and_starting_with_1_even_with_punctuation() { |
| 42 | + phrase := '+1 (223) 456-7890' |
| 43 | + assert clean(phrase)! == '2234567890' |
| 44 | +} |
| 45 | + |
| 46 | +fn test_invalid_when_more_than_11_digits() { |
| 47 | + phrase := '321234567890' |
| 48 | + if res := clean(phrase) { |
| 49 | + assert false, 'invalid when more than 11 digits should return an error' |
| 50 | + } else { |
| 51 | + assert err.msg() == 'must not be greater than 11 digits' |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +fn test_invalid_with_letters() { |
| 56 | + phrase := '523-abc-7890' |
| 57 | + if res := clean(phrase) { |
| 58 | + assert false, 'invalid with letters should return an error' |
| 59 | + } else { |
| 60 | + assert err.msg() == 'letters not permitted' |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +fn test_invalid_with_punctuations() { |
| 65 | + phrase := '523-@:!-7890' |
| 66 | + if res := clean(phrase) { |
| 67 | + assert false, 'invalid with punctuations should return an error' |
| 68 | + } else { |
| 69 | + assert err.msg() == 'punctuations not permitted' |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +fn test_invalid_if_area_code_starts_with_0() { |
| 74 | + phrase := '(023) 456-7890' |
| 75 | + if res := clean(phrase) { |
| 76 | + assert false, 'invalid if area code starts with 0 should return an error' |
| 77 | + } else { |
| 78 | + assert err.msg() == 'area code cannot start with zero' |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +fn test_invalid_if_area_code_starts_with_1() { |
| 83 | + phrase := '(123) 456-7890' |
| 84 | + if res := clean(phrase) { |
| 85 | + assert false, 'invalid if area code starts with 1 should return an error' |
| 86 | + } else { |
| 87 | + assert err.msg() == 'area code cannot start with one' |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +fn test_invalid_if_exchange_code_starts_with_0() { |
| 92 | + phrase := '(223) 056-7890' |
| 93 | + if res := clean(phrase) { |
| 94 | + assert false, 'invalid if exchange code starts with 0 should return an error' |
| 95 | + } else { |
| 96 | + assert err.msg() == 'exchange code cannot start with zero' |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +fn test_invalid_if_exchange_code_starts_with_1() { |
| 101 | + phrase := '(223) 156-7890' |
| 102 | + if res := clean(phrase) { |
| 103 | + assert false, 'invalid if exchange code starts with 1 should return an error' |
| 104 | + } else { |
| 105 | + assert err.msg() == 'exchange code cannot start with one' |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +fn test_invalid_if_area_code_starts_with_0_on_valid_11_digit_number() { |
| 110 | + phrase := '1 (023) 456-7890' |
| 111 | + if res := clean(phrase) { |
| 112 | + assert false, 'invalid if area code starts with 0 on valid 11-digit number should return an error' |
| 113 | + } else { |
| 114 | + assert err.msg() == 'area code cannot start with zero' |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +fn test_invalid_if_area_code_starts_with_1_on_valid_11_digit_number() { |
| 119 | + phrase := '1 (123) 456-7890' |
| 120 | + if res := clean(phrase) { |
| 121 | + assert false, 'invalid if area code starts with 1 on valid 11-digit number should return an error' |
| 122 | + } else { |
| 123 | + assert err.msg() == 'area code cannot start with one' |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +fn test_invalid_if_exchange_code_starts_with_0_on_valid_11_digit_number() { |
| 128 | + phrase := '1 (223) 056-7890' |
| 129 | + if res := clean(phrase) { |
| 130 | + assert false, 'invalid if exchange code starts with 0 on valid 11-digit number should return an error' |
| 131 | + } else { |
| 132 | + assert err.msg() == 'exchange code cannot start with zero' |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +fn test_invalid_if_exchange_code_starts_with_1_on_valid_11_digit_number() { |
| 137 | + phrase := '1 (223) 156-7890' |
| 138 | + if res := clean(phrase) { |
| 139 | + assert false, 'invalid if exchange code starts with 1 on valid 11-digit number should return an error' |
| 140 | + } else { |
| 141 | + assert err.msg() == 'exchange code cannot start with one' |
| 142 | + } |
| 143 | +} |
0 commit comments