Skip to content

Commit

Permalink
Sync Phone Number tests with canonical specs (#1362)
Browse files Browse the repository at this point in the history
* chore(phone-number): sync tests with canonical specs

* refactor(phone-number): prefer cond to case for readability

* style(phone-number): remove useless whitespaces

Pass `mix format --check-formatted`
  • Loading branch information
antoine-duchenet authored Sep 16, 2023
1 parent 54aed74 commit 2e25d77
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
10 changes: 6 additions & 4 deletions exercises/practice/phone-number/.meta/example.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ defmodule PhoneNumber do
end

defp validate_length(number) do
if String.length(number) in 10..11 do
{:ok, number}
else
{:error, "incorrect number of digits"}
length = String.length(number)

cond do
length < 10 -> {:error, "must not be fewer than 10 digits"}
length > 11 -> {:error, "must not be greater than 11 digits"}
true -> {:ok, number}
end
end

Expand Down
10 changes: 10 additions & 0 deletions exercises/practice/phone-number/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ description = "cleans numbers with multiple spaces"

[598d8432-0659-4019-a78b-1c6a73691d21]
description = "invalid when 9 digits"
include = false

[2de74156-f646-42b5-8638-0ef1d8b58bc2]
description = "invalid when 9 digits"
reimplements = "598d8432-0659-4019-a78b-1c6a73691d21"

[57061c72-07b5-431f-9766-d97da7c4399d]
description = "invalid when 11 digits does not start with a 1"
Expand All @@ -32,6 +37,11 @@ description = "valid when 11 digits and starting with 1 even with punctuation"

[c6a5f007-895a-4fc5-90bc-a7e70f9b5cad]
description = "invalid when more than 11 digits"
include = false

[4a1509b7-8953-4eec-981b-c483358ff531]
description = "invalid when more than 11 digits"
reimplements = "c6a5f007-895a-4fc5-90bc-a7e70f9b5cad"

[63f38f37-53f6-4a5f-bd86-e9b404f10a60]
description = "invalid with letters"
Expand Down
4 changes: 2 additions & 2 deletions exercises/practice/phone-number/test/phone_number_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule PhoneNumberTest do

@tag :pending
test "invalid when 9 digits" do
assert PhoneNumber.clean("212555010") == {:error, "incorrect number of digits"}
assert PhoneNumber.clean("123456789") == {:error, "must not be fewer than 10 digits"}
end

@tag :pending
Expand All @@ -38,7 +38,7 @@ defmodule PhoneNumberTest do

@tag :pending
test "invalid when more than 11 digits" do
assert PhoneNumber.clean("321234567890") == {:error, "incorrect number of digits"}
assert PhoneNumber.clean("321234567890") == {:error, "must not be greater than 11 digits"}
end

@tag :pending
Expand Down

0 comments on commit 2e25d77

Please sign in to comment.