From 4af0037946681634ee6d95d78d753e6d1f4a721a Mon Sep 17 00:00:00 2001 From: Ivan Rublev Date: Mon, 29 Apr 2024 22:56:15 +0200 Subject: [PATCH] update tests to support Elixir 1.16 --- .../generator/match_fun_registry/lists.ex | 78 +++++++++---------- .../type_ensurer_factory/resolver/fields.ex | 58 +++++++------- .../module_inspector_test.exs | 12 ++- 3 files changed, 78 insertions(+), 70 deletions(-) diff --git a/lib/domo/type_ensurer_factory/generator/match_fun_registry/lists.ex b/lib/domo/type_ensurer_factory/generator/match_fun_registry/lists.ex index 1ba491c6..da59accf 100644 --- a/lib/domo/type_ensurer_factory/generator/match_fun_registry/lists.ex +++ b/lib/domo/type_ensurer_factory/generator/match_fun_registry/lists.ex @@ -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) @@ -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 diff --git a/lib/domo/type_ensurer_factory/resolver/fields.ex b/lib/domo/type_ensurer_factory/resolver/fields.ex index 00d90bc1..a37b66f0 100644 --- a/lib/domo/type_ensurer_factory/resolver/fields.ex +++ b/lib/domo/type_ensurer_factory/resolver/fields.ex @@ -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 @@ -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 diff --git a/test/domo/type_ensurer_factory/module_inspector_test.exs b/test/domo/type_ensurer_factory/module_inspector_test.exs index 0174e284..ea6588db 100644 --- a/test/domo/type_ensurer_factory/module_inspector_test.exs +++ b/test/domo/type_ensurer_factory/module_inspector_test.exs @@ -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