Skip to content

Commit

Permalink
Merge e291a1a into 63c347a
Browse files Browse the repository at this point in the history
  • Loading branch information
fcevado committed Apr 23, 2017
2 parents 63c347a + e291a1a commit 67517b4
Show file tree
Hide file tree
Showing 9 changed files with 349 additions and 397 deletions.
19 changes: 19 additions & 0 deletions .ebert.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This configuration was used Ebert to review the fcevado/phone repository
# on 135f7621e5521278abc6862b4ed989b88aa6b2c4.
# You can make this the default configuration for future reviews by moving this
# file to your repository as `.ebert.yml` and pushing it to GitHub, and tweak
# it as you wish - To know more on how to change this file to better review your
# repository you can go to https://ebertapp.io/docs/config and see the configuration
# details.
---
styleguide: plataformatec/linters
engines:
credo:
enabled: true
fixme:
enabled: true
remark-lint:
enabled: true
exclude_paths:
- test/**

3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ otp_release:
- 19.0

script:
- MIX_ENV=test mix compile
- MIX_ENV=test mix test --cover
- MIX_ENV=test mix test --force --cover

after_script:
- MIX_ENV=docs mix deps.get
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ defmodule Phone.Mixfile do
defp coverage do
[
tool: Coverex.Task,
ignore_modules: [Helper.Area, Helper.Country, Helper.Parser],
coveralls: true
]
end
Expand Down
68 changes: 68 additions & 0 deletions test/phone/br_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
defmodule Phone.BRTest do
use ExUnit.Case, async: true

main_module = Phone.BR

all_modules = [
Phone.BR.AC, Phone.BR.AL, Phone.BR.AM, Phone.BR.AP, Phone.BR.BA,
Phone.BR.CE, Phone.BR.DF, Phone.BR.ES, Phone.BR.GO, Phone.BR.MA,
Phone.BR.MT, Phone.BR.MS, Phone.BR.MG, Phone.BR.PA, Phone.BR.PB,
Phone.BR.PE, Phone.BR.PI, Phone.BR.PR, Phone.BR.RJ, Phone.BR.RN,
Phone.BR.RO, Phone.BR.RR, Phone.BR.RS, Phone.BR.SC, Phone.BR.SE,
Phone.BR.SP, Phone.BR.TO
]

Enum.map(main_module.codes,
fn code ->
test "#{inspect main_module} parses area code #{code}" do
assert Phone.valid?(unquote("#{code}55555555"))
assert {:ok, parsed} = Phone.parse(unquote("#{code}55555555"))

assert parsed.country == unquote(main_module).country
assert parsed.a2 == unquote(main_module).a2
assert parsed.a3 == unquote(main_module).a3
end

test "#{inspect main_module} cant parse wrong number with code #{code}" do
refute Phone.valid?(unquote("#{code}5555555"))
assert {:error, _} = Phone.parse(unquote("#{code}555555"))

refute unquote(main_module).match?(unquote("#{code}5555555"))
assert {:error, _} = unquote(main_module).build(unquote("#{code}555555"))

assert_raise ArgumentError,
"Not a valid phone number.",
fn -> unquote(main_module).build!(unquote("#{code}555555")) end
end
end)

Enum.map(all_modules,
fn module ->
Enum.map(module.codes,
fn code ->
test "#{inspect module} parses area code #{code}" do
assert Phone.valid?(unquote("#{code}55555555"))
assert {:ok, parsed} = Phone.parse(unquote("#{code}55555555"))

assert parsed.country == unquote(main_module).country
assert parsed.a2 == unquote(main_module).a2
assert parsed.a3 == unquote(main_module).a3
assert parsed.area_type == unquote(module).area_type
assert parsed.area_name == unquote(module).area_name
assert parsed.area_abbreviation == unquote(module).area_abbreviation
end

test "#{inspect module} cant parse wrong number with code #{code}" do
refute Phone.valid?(unquote("#{code}555555"))
assert {:error, _} = Phone.parse(unquote("#{code}555555"))

refute unquote(module).match?(unquote("#{code}555555"))
assert {:error, _} = unquote(module).build(unquote("#{code}555555"))

assert_raise ArgumentError,
"Not a valid phone number.",
fn -> unquote(module).build!(unquote("#{code}555555")) end
end
end)
end)
end
66 changes: 66 additions & 0 deletions test/phone/ca_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
defmodule Phone.CATest do
use ExUnit.Case, async: true

main_module = Phone.NANP.CA

all_modules = [
Phone.NANP.CA.AB, Phone.NANP.CA.BC, Phone.NANP.CA.MB,
Phone.NANP.CA.NB, Phone.NANP.CA.NL, Phone.NANP.CA.NSAndPE,
Phone.NANP.CA.ON, Phone.NANP.CA.QC, Phone.NANP.CA.SK,
Phone.NANP.CA.Territory
]

Enum.map(main_module.codes,
fn code ->
test "#{inspect main_module} parses area code #{code}" do
assert Phone.valid?(unquote("#{code}5551234"))
assert {:ok, parsed} = Phone.parse(unquote("#{code}5551234"))

assert parsed.country == unquote(main_module).country
assert parsed.a2 == unquote(main_module).a2
assert parsed.a3 == unquote(main_module).a3
end

test "#{inspect main_module} cant parse wrong number with code #{code}" do
refute Phone.valid?(unquote("#{code}555123"))
assert {:error, _} = Phone.parse(unquote("#{code}555123"))

refute unquote(main_module).match?(unquote("#{code}555123"))
assert {:error, _} = unquote(main_module).build(unquote("#{code}555123"))

assert_raise ArgumentError,
"Not a valid phone number.",
fn -> unquote(main_module).build!(unquote("#{code}555123")) end
end
end)

Enum.map(all_modules,
fn module ->
Enum.map(module.codes,
fn code ->
test "#{inspect module} parses area code #{code}" do
assert Phone.valid?(unquote("#{code}5551234"))
assert {:ok, parsed} = Phone.parse(unquote("#{code}5551234"))

assert parsed.country == unquote(main_module).country
assert parsed.a2 == unquote(main_module).a2
assert parsed.a3 == unquote(main_module).a3
assert parsed.area_type == unquote(module).area_type
assert parsed.area_name == unquote(module).area_name
assert parsed.area_abbreviation == unquote(module).area_abbreviation
end

test "#{inspect module} cant parse wrong number with code #{code}" do
refute Phone.valid?(unquote("#{code}555123"))
assert {:error, _} = Phone.parse(unquote("#{code}555123"))

refute unquote(module).match?(unquote("#{code}555123"))
assert {:error, _} = unquote(module).build(unquote("#{code}555123"))

assert_raise ArgumentError,
"Not a valid phone number.",
fn -> unquote(module).build!(unquote("#{code}555123")) end
end
end)
end)
end
72 changes: 72 additions & 0 deletions test/phone/countries_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
defmodule Phone.CountriesTest do
use ExUnit.Case, async: true

main_module = Phone.Countries
all_modules = [Phone.AD, Phone.AE, Phone.AF, Phone.AL, Phone.AM, Phone.AO,
Phone.AR, Phone.AT, Phone.AU, Phone.AW, Phone.AZ, Phone.BA,
Phone.BD, Phone.BE, Phone.BG, Phone.BH, Phone.BI, Phone.BJ,
Phone.BN, Phone.BO, Phone.BR, Phone.BT, Phone.BW, Phone.BY,
Phone.BZ, Phone.CD, Phone.CF, Phone.CG, Phone.CH, Phone.CI,
Phone.CK, Phone.CL, Phone.CM, Phone.CN, Phone.CO, Phone.CR,
Phone.CU, Phone.CV, Phone.CW, Phone.CY, Phone.CZ, Phone.DE,
Phone.DJ, Phone.DK, Phone.DZ, Phone.EC, Phone.EE, Phone.EG,
Phone.ER, Phone.ES, Phone.ET, Phone.FI, Phone.FJ, Phone.FM,
Phone.FO, Phone.FR, Phone.GA, Phone.GB, Phone.GE, Phone.GF,
Phone.GH, Phone.GI, Phone.GL, Phone.GM, Phone.GN, Phone.GQ,
Phone.GR, Phone.GT, Phone.GW, Phone.GY, Phone.HK, Phone.HN,
Phone.HR, Phone.HT, Phone.HU, Phone.ID, Phone.IE, Phone.IL,
Phone.IN, Phone.IO, Phone.IQ, Phone.IR, Phone.IS, Phone.IT,
Phone.JO, Phone.JP, Phone.KE, Phone.KG, Phone.KH, Phone.KI,
Phone.KM, Phone.KP, Phone.KR, Phone.KW, Phone.KZ, Phone.LA,
Phone.LB, Phone.LI, Phone.LK, Phone.LR, Phone.LS, Phone.LT,
Phone.LU, Phone.LV, Phone.LY, Phone.MA, Phone.MC, Phone.MD,
Phone.ME, Phone.MG, Phone.MH, Phone.MK, Phone.ML, Phone.MM,
Phone.MN, Phone.MO, Phone.MQ, Phone.MR, Phone.MT, Phone.MU,
Phone.MV, Phone.MW, Phone.MX, Phone.MY, Phone.MZ, Phone.NA,
Phone.NC, Phone.NE, Phone.NG, Phone.NI, Phone.NL, Phone.NO,
Phone.NP, Phone.NR, Phone.NU, Phone.NZ, Phone.OM, Phone.PA,
Phone.PE, Phone.PF, Phone.PG, Phone.PH, Phone.PK, Phone.PL,
Phone.PM, Phone.PS, Phone.PT, Phone.PW, Phone.PY, Phone.QA,
Phone.RO, Phone.RS, Phone.RU, Phone.RW, Phone.SA, Phone.SB,
Phone.SC, Phone.SD, Phone.SE, Phone.SG, Phone.SH, Phone.SI,
Phone.SK, Phone.SL, Phone.SM, Phone.SN, Phone.SO, Phone.SR,
Phone.SS, Phone.ST, Phone.SV, Phone.SY, Phone.SZ, Phone.TD,
Phone.TG, Phone.TH, Phone.TJ, Phone.TK, Phone.TL, Phone.TM,
Phone.TN, Phone.TO, Phone.TR, Phone.TV, Phone.TW, Phone.TZ,
Phone.UA, Phone.UG, Phone.UY, Phone.UZ, Phone.VE, Phone.VN,
Phone.VU, Phone.WF, Phone.WS, Phone.YE, Phone.ZA, Phone.ZM,
Phone.ZW, Phone.NANP]

Enum.map(main_module.codes,
fn code ->
test "#{inspect main_module} cant parse wrong number with code #{code}" do
refute Phone.valid?(unquote("#{code}"))
assert {:error, _} = Phone.parse(unquote("#{code}"))

refute unquote(main_module).match?(unquote("#{code}"))
assert {:error, _} = unquote(main_module).build(unquote("#{code}"))

assert_raise ArgumentError,
"Not a valid phone number.",
fn -> unquote(main_module).build!(unquote("#{code}")) end
end
end)

Enum.map(all_modules,
fn module ->
Enum.map(module.codes,
fn code ->
test "#{inspect module} cant parse wrong number with code #{code}" do
refute Phone.valid?(unquote("#{code}"))
assert {:error, _} = Phone.parse(unquote("#{code}"))

refute unquote(module).match?(unquote("#{code}"))
assert {:error, _} = unquote(module).build(unquote("#{code}"))

assert_raise ArgumentError,
"Not a valid phone number.",
fn -> unquote(module).build!(unquote("#{code}")) end
end
end)
end)
end
62 changes: 49 additions & 13 deletions test/phone/nanp_test.exs
Original file line number Diff line number Diff line change
@@ -1,20 +1,56 @@
defmodule Phone.NanpTest do
defmodule Phone.NANPTest do
use ExUnit.Case, async: true

@external_resource area_codes_path = Path.join([__DIR__, "..", "support", "nanp-area-codes.csv"])
main_module = Phone.NANP
all_modules = [
Phone.NANP.AS, Phone.NANP.AI, Phone.NANP.AG, Phone.NANP.BS,
Phone.NANP.BB, Phone.NANP.BM, Phone.NANP.CA, Phone.NANP.DM,
Phone.NANP.DO, Phone.NANP.GD, Phone.NANP.GU, Phone.NANP.JM,
Phone.NANP.KN, Phone.NANP.KY, Phone.NANP.LC, Phone.NANP.MP,
Phone.NANP.MS, Phone.NANP.PR, Phone.NANP.SX, Phone.NANP.TC,
Phone.NANP.TT, Phone.NANP.US, Phone.NANP.VC, Phone.NANP.VG,
Phone.NANP.VI, Phone.NANP.TollFree
]

for line <- File.stream!(area_codes_path, [], :line) do
[area_code, state_abbreviation] = line |> String.strip |> String.split(",")
Enum.map(main_module.codes,
fn code ->
test "#{inspect main_module} cant parse wrong number with code #{code}" do
refute Phone.valid?(unquote("#{code}555123"))
assert {:error, _} = Phone.parse(unquote("#{code}555123"))

test "correctly parses NANP area code #{area_code} (#{state_abbreviation})" do
assert {:ok, result} = Phone.parse(unquote("1#{area_code}5551234"))
refute unquote(main_module).match?(unquote("#{code}555123"))
assert {:error, _} = unquote(main_module).build(unquote("#{code}555123"))

case result do
%{a2: "US"} ->
assert result.area_abbreviation == unquote(state_abbreviation)
_ ->
true
assert_raise ArgumentError,
"Not a valid phone number.",
fn -> unquote(main_module).build!(unquote("#{code}555123")) end
end
end
end
end)

Enum.map(all_modules,
fn module ->
Enum.map(module.codes,
fn code ->
test "#{inspect module} parses area code #{code}" do
assert Phone.valid?(unquote("#{code}5551234"))
assert {:ok, parsed} = Phone.parse(unquote("#{code}5551234"))

assert parsed.country == unquote(module).country
assert parsed.a2 == unquote(module).a2
assert parsed.a3 == unquote(module).a3
end

test "#{inspect module} cant parse wrong number with code #{code}" do
refute Phone.valid?(unquote("#{code}555123"))
assert {:error, _} = Phone.parse(unquote("#{code}555123"))

refute unquote(module).match?(unquote("#{code}555123"))
assert {:error, _} = unquote(module).build(unquote("#{code}555123"))

assert_raise ArgumentError,
"Not a valid phone number.",
fn -> unquote(module).build!(unquote("#{code}555123")) end
end
end)
end)
end
73 changes: 73 additions & 0 deletions test/phone/us_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
defmodule Phone.USTest do
use ExUnit.Case, async: true

main_module = Phone.NANP.US

all_modules = [Phone.NANP.US.AK, Phone.NANP.US.AL, Phone.NANP.US.AR, Phone.NANP.US.AZ,
Phone.NANP.US.CA, Phone.NANP.US.CO, Phone.NANP.US.CT, Phone.NANP.US.DC,
Phone.NANP.US.DE, Phone.NANP.US.FL, Phone.NANP.US.GA, Phone.NANP.US.HI,
Phone.NANP.US.IA, Phone.NANP.US.ID, Phone.NANP.US.IL, Phone.NANP.US.IN,
Phone.NANP.US.KS, Phone.NANP.US.KY, Phone.NANP.US.LA, Phone.NANP.US.MA,
Phone.NANP.US.MD, Phone.NANP.US.ME, Phone.NANP.US.MI, Phone.NANP.US.MN,
Phone.NANP.US.MO, Phone.NANP.US.MS, Phone.NANP.US.MT, Phone.NANP.US.NC,
Phone.NANP.US.ND, Phone.NANP.US.NE, Phone.NANP.US.NH, Phone.NANP.US.NJ,
Phone.NANP.US.NM, Phone.NANP.US.NV, Phone.NANP.US.NY, Phone.NANP.US.OH,
Phone.NANP.US.OK, Phone.NANP.US.OR, Phone.NANP.US.PA, Phone.NANP.US.RI,
Phone.NANP.US.SC, Phone.NANP.US.SD, Phone.NANP.US.TN, Phone.NANP.US.TX,
Phone.NANP.US.UT, Phone.NANP.US.VT, Phone.NANP.US.VA, Phone.NANP.US.WA,
Phone.NANP.US.WI, Phone.NANP.US.WV, Phone.NANP.US.WY]

Enum.map(main_module.codes,
fn code ->
test "#{inspect main_module} parses area code #{code}" do
assert Phone.valid?(unquote("#{code}5551234"))
assert {:ok, parsed} = Phone.parse(unquote("#{code}5551234"))

assert parsed.country == unquote(main_module).country
assert parsed.a2 == unquote(main_module).a2
assert parsed.a3 == unquote(main_module).a3
end

test "#{inspect main_module} cant parse wrong number with code #{code}" do
refute Phone.valid?(unquote("#{code}555123"))
assert {:error, _} = Phone.parse(unquote("#{code}555123"))

refute unquote(main_module).match?(unquote("#{code}555123"))
assert {:error, _} = unquote(main_module).build(unquote("#{code}555123"))

assert_raise ArgumentError,
"Not a valid phone number.",
fn -> unquote(main_module).build!(unquote("#{code}555123")) end
end
end)

Enum.map(all_modules,
fn module ->
Enum.map(module.codes,
fn code ->
test "#{inspect module} parses area code #{code}" do
assert Phone.valid?(unquote("#{code}5551234"))
assert {:ok, parsed} = Phone.parse(unquote("#{code}5551234"))

assert parsed.country == unquote(main_module).country
assert parsed.a2 == unquote(main_module).a2
assert parsed.a3 == unquote(main_module).a3
assert parsed.area_type == unquote(module).area_type
assert parsed.area_name == unquote(module).area_name
assert parsed.area_abbreviation == unquote(module).area_abbreviation
end

test "#{inspect module} cant parse wrong number with code #{code}" do
refute Phone.valid?(unquote("#{code}555123"))
assert {:error, _} = Phone.parse(unquote("#{code}555123"))

refute unquote(module).match?(unquote("#{code}555123"))
assert {:error, _} = unquote(module).build(unquote("#{code}555123"))

assert_raise ArgumentError,
"Not a valid phone number.",
fn -> unquote(module).build!(unquote("#{code}555123")) end
end
end)
end)
end

0 comments on commit 67517b4

Please sign in to comment.