mirror of
https://github.com/sloanelybutsurely/typeid-elixir.git
synced 2024-11-28 01:42:54 -05:00
modify to catch invalid specs
This commit is contained in:
parent
6d4a555585
commit
db5e754d86
3 changed files with 26 additions and 5 deletions
|
@ -147,11 +147,14 @@ defmodule TypeID do
|
|||
@spec from_string!(String.t()) :: t() | no_return()
|
||||
def from_string!(str) do
|
||||
case String.split(str, "_") do
|
||||
[prefix, suffix] ->
|
||||
[prefix, suffix] when prefix != "" ->
|
||||
from!(prefix, suffix)
|
||||
|
||||
[suffix] ->
|
||||
from!("", suffix)
|
||||
|
||||
_ ->
|
||||
raise ArgumentError, "invalid TypeID"
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -20,6 +20,8 @@ defmodule TypeID.Base32 do
|
|||
do_encode(c23)::8, do_encode(c24)::8, do_encode(c25)::8, do_encode(c26)::8>>
|
||||
end
|
||||
|
||||
def encode(_), do: :error
|
||||
|
||||
@spec decode(binary()) :: {:ok, binary()} | :error
|
||||
def decode(string) when is_binary(string) do
|
||||
{:ok, decode!(string)}
|
||||
|
@ -40,6 +42,10 @@ defmodule TypeID.Base32 do
|
|||
do_decode(c23)::5, do_decode(c24)::5, do_decode(c25)::5, do_decode(c26)::5>>
|
||||
end
|
||||
|
||||
def decode!(_) do
|
||||
raise ArgumentError, "invalid base 32 suffix"
|
||||
end
|
||||
|
||||
@compile {:inline, [do_encode: 1]}
|
||||
defp do_encode(byte) do
|
||||
elem({unquote_splicing(crockford_alphabet)}, byte)
|
||||
|
|
|
@ -1,8 +1,15 @@
|
|||
defmodule TypeID.SpecTest do
|
||||
use ExUnit.Case
|
||||
|
||||
@valid_specs :code.priv_dir(:typeid_elixir)
|
||||
|> Path.join("spec/valid.yml")
|
||||
specs_path = :code.priv_dir(:typeid_elixir)
|
||||
|> Path.join("/spec")
|
||||
|
||||
@valid_specs specs_path
|
||||
|> Path.join("/valid.yml")
|
||||
|> YamlElixir.read_from_file!()
|
||||
|
||||
@invalid_specs specs_path
|
||||
|> Path.join("/invalid.yml")
|
||||
|> YamlElixir.read_from_file!()
|
||||
|
||||
describe "valid" do
|
||||
|
@ -15,6 +22,11 @@ defmodule TypeID.SpecTest do
|
|||
end
|
||||
end
|
||||
|
||||
# describe "invalid" do
|
||||
# end
|
||||
describe "invalid" do
|
||||
for %{"name" => name, "typeid" => typeid, "description" => desc } <- @invalid_specs do
|
||||
test "#{name}: #{desc}" do
|
||||
assert :error = TypeID.from_string(unquote(typeid))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue