Skip to content

Commit

Permalink
Merge 73ccdab into 63c347a
Browse files Browse the repository at this point in the history
  • Loading branch information
fcevado committed Apr 23, 2017
2 parents 63c347a + 73ccdab commit afde714
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 398 deletions.
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
48 changes: 34 additions & 14 deletions test/phone/nanp_test.exs
Original file line number Diff line number Diff line change
@@ -1,20 +1,40 @@
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"])
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(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"))

test "correctly parses NANP area code #{area_code} (#{state_abbreviation})" do
assert {:ok, result} = Phone.parse(unquote("1#{area_code}5551234"))
assert parsed.country == unquote(module).country
assert parsed.a2 == unquote(module).a2
assert parsed.a3 == unquote(module).a3
end

case result do
%{a2: "US"} ->
assert result.area_abbreviation == unquote(state_abbreviation)
_ ->
true
end
end
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
75 changes: 75 additions & 0 deletions test/phone/us_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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
Loading

0 comments on commit afde714

Please sign in to comment.