Skip to content

Commit

Permalink
update tests to support Elixir 1.16
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanRublev committed Apr 29, 2024
1 parent b3fd539 commit 4af0037
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 70 deletions.
78 changes: 39 additions & 39 deletions lib/domo/type_ensurer_factory/generator/match_fun_registry/lists.ex
Original file line number Diff line number Diff line change
Expand Up @@ -152,45 +152,6 @@ defmodule Domo.TypeEnsurerFactory.Generator.MatchFunRegistry.Lists do
{[match_spec_quoted, match_list_elements_quoted], [element_spec_precond]}
end

defp match_el_quoted(type_spec_atom, head_attributes, tail_attributes) do
{head_spec_atom, head_precond_atom, head_spec_string} = head_attributes
{tail_spec_atom, tail_precond_atom, tail_spec_string} = tail_attributes

quote do
def do_match_list_elements(unquote(type_spec_atom), [head | tail], idx, opts) do
case do_match_spec({unquote(head_spec_atom), unquote(head_precond_atom)}, head, unquote(head_spec_string), opts) do
:ok ->
do_match_list_elements(unquote(type_spec_atom), tail, idx + 1, opts)

{:error, element_value, messages} ->
{:element_error,
[
{"The element at index %{idx} has value %{element_value} that is invalid.", [idx: idx, element_value: inspect(element_value)]}
| messages
]}
end
end

def do_match_list_elements(unquote(type_spec_atom), [], idx, _opts) do
{:proper, idx}
end

def do_match_list_elements(unquote(type_spec_atom), tail, idx, opts) do
case do_match_spec({unquote(tail_spec_atom), unquote(tail_precond_atom)}, tail, unquote(tail_spec_string), opts) do
:ok ->
{:improper, idx}

{:error, element_value, messages} ->
{:element_error,
[
{"The tail element has value %{element_value} that is invalid.", [element_value: inspect(element_value)]}
| messages
]}
end
end
end
end

defp match_list_function_quoted(:maybe_improper_list, type_spec, precond) do
{:maybe_improper_list, [], [head_spec_precond, tail_spec_precond]} = type_spec
type_spec_atom = TypeSpec.to_atom(type_spec)
Expand Down Expand Up @@ -277,4 +238,43 @@ defmodule Domo.TypeEnsurerFactory.Generator.MatchFunRegistry.Lists do

{[match_spec_quoted, match_list_elements_quoted], [head_spec_precond, tail_spec_precond]}
end

defp match_el_quoted(type_spec_atom, head_attributes, tail_attributes) do
{head_spec_atom, head_precond_atom, head_spec_string} = head_attributes
{tail_spec_atom, tail_precond_atom, tail_spec_string} = tail_attributes

quote do
def do_match_list_elements(unquote(type_spec_atom), [head | tail], idx, opts) do
case do_match_spec({unquote(head_spec_atom), unquote(head_precond_atom)}, head, unquote(head_spec_string), opts) do
:ok ->
do_match_list_elements(unquote(type_spec_atom), tail, idx + 1, opts)

{:error, element_value, messages} ->
{:element_error,
[
{"The element at index %{idx} has value %{element_value} that is invalid.", [idx: idx, element_value: inspect(element_value)]}
| messages
]}
end
end

def do_match_list_elements(unquote(type_spec_atom), [], idx, _opts) do
{:proper, idx}
end

def do_match_list_elements(unquote(type_spec_atom), tail, idx, opts) do
case do_match_spec({unquote(tail_spec_atom), unquote(tail_precond_atom)}, tail, unquote(tail_spec_string), opts) do
:ok ->
{:improper, idx}

{:error, element_value, messages} ->
{:element_error,
[
{"The tail element has value %{element_value} that is invalid.", [element_value: inspect(element_value)]}
| messages
]}
end
end
end
end
end
58 changes: 29 additions & 29 deletions lib/domo/type_ensurer_factory/resolver/fields.ex
Original file line number Diff line number Diff line change
Expand Up @@ -133,35 +133,6 @@ defmodule Domo.TypeEnsurerFactory.Resolver.Fields do
do_resolve_ecto_schema(:many, many_type, type, module, precond, resolving_context, acc)
end

defp do_resolve_ecto_schema(schema_kind, schema_type, type, module, precond, resolving_context, {types, errs, deps} = acc) do
type_to_resolve =
case schema_kind do
:one -> quote(do: unquote(type) | Ecto.Association.NotLoaded.t())
:many -> quote(do: [unquote(type)] | Ecto.Association.NotLoaded.t())
end

if is_nil(precond) do
{types, errors, deps} =
resolve_type(
type_to_resolve,
module,
precond,
resolving_context,
acc
)

{types, errors, deps}
else
error =
{:error,
"""
Precondition for value of Ecto.Schema.#{Atom.to_string(schema_type)}(t) type is not allowed.\
"""}

{types, [error | errs], deps}
end
end

# Remote Types

defp resolve_type({{:., _, [rem_module, rem_type]}, _, _}, _module, precond, resolving_context, {types, errs, deps}) do
Expand Down Expand Up @@ -749,6 +720,35 @@ defmodule Domo.TypeEnsurerFactory.Resolver.Fields do
{[joint_type | types], errs, deps}
end

defp do_resolve_ecto_schema(schema_kind, schema_type, type, module, precond, resolving_context, {types, errs, deps} = acc) do
type_to_resolve =
case schema_kind do
:one -> quote(do: unquote(type) | Ecto.Association.NotLoaded.t())
:many -> quote(do: [unquote(type)] | Ecto.Association.NotLoaded.t())
end

if is_nil(precond) do
{types, errors, deps} =
resolve_type(
type_to_resolve,
module,
precond,
resolving_context,
acc
)

{types, errors, deps}
else
error =
{:error,
"""
Precondition for value of Ecto.Schema.#{Atom.to_string(schema_type)}(t) type is not allowed.\
"""}

{types, [error | errs], deps}
end
end

# this one is for internal cases when we have several arguments to be resolved, f.e. from {a, b, c}
# it resolves each element independently so f.e. :any as one resolved element doesn't affect others
defp parallel_resolve_type([_ | _] = list, module, nil, resolving_context, acc) do
Expand Down
12 changes: 10 additions & 2 deletions test/domo/type_ensurer_factory/module_inspector_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,16 @@ defmodule Domo.TypeEnsurerFactory.ModuleInspectorTest do
end

test "return hash of module types giving loadable module" do
assert <<165, 63, 215, 58, 173, 14, 220, 157, 192, 81, 20, 19, 68, 90, 147, 171>> ==
ModuleInspector.beam_types_hash(EmptyStruct)
module_hash =
case ElixirVersion.version() do
[1, minor, _] when minor < 16 ->
<<165, 63, 215, 58, 173, 14, 220, 157, 192, 81, 20, 19, 68, 90, 147, 171>>

[1, minor, _] when minor >= 16 ->
<<210, 153, 0, 251, 50, 73, 13, 33, 58, 87, 29, 116, 203, 250, 237, 148>>
end

assert module_hash == ModuleInspector.beam_types_hash(EmptyStruct)
end

test "return nil as hash of module types giving unloadable module" do
Expand Down

0 comments on commit 4af0037

Please sign in to comment.