Skip to content

Commit dae7d47

Browse files
committed
support all type kinds
Fixes #41. Hammox blows up when encountering `@typep` and `@opaque`. For now we will treat them like any other type. Further work on opaque types in #43.
1 parent df66c8a commit dae7d47

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

lib/hammox/type_engine.ex

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ defmodule Hammox.TypeEngine do
33

44
alias Hammox.Utils
55

6+
@type_kinds [:type, :typep, :opaque]
7+
68
def match_type(value, {:type, _, :union, union_types} = union) when is_list(union_types) do
79
results =
810
Enum.reduce_while(union_types, [], fn type, reason_stacks ->
@@ -653,7 +655,8 @@ defmodule Hammox.TypeEngine do
653655
)
654656
when is_atom(module_name) and is_atom(type_name) and is_list(args) do
655657
with {:ok, types} <- fetch_types(module_name),
656-
{:ok, {:type, {_name, type, vars}}} <- get_type(types, type_name, length(args)) do
658+
{:ok, {type_kind, {_name, type, vars}}} when type_kind in @type_kinds <-
659+
get_type(types, type_name, length(args)) do
657660
resolved_type =
658661
args
659662
|> Enum.zip(vars)
@@ -688,7 +691,8 @@ defmodule Hammox.TypeEngine do
688691
end
689692

690693
defp get_type(type_list, type_name, arity) do
691-
case Enum.find(type_list, fn {:type, {name, _type, params}} ->
694+
case Enum.find(type_list, fn {type_kind, {name, _type, params}}
695+
when type_kind in @type_kinds ->
692696
name == type_name and length(params) == arity
693697
end) do
694698
nil -> {:error, {:type_not_found, {type_name, arity}}}

test/hammox_test.exs

+20
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,26 @@ defmodule HammoxTest do
11631163
end
11641164
end
11651165

1166+
describe "private types" do
1167+
test "pass" do
1168+
assert_pass(:foo_private_type, :private_value)
1169+
end
1170+
1171+
test "fail" do
1172+
assert_fail(:foo_private_type, :other)
1173+
end
1174+
end
1175+
1176+
describe "opaque types" do
1177+
test "pass" do
1178+
assert_pass(:foo_opaque_type, :opaque_value)
1179+
end
1180+
1181+
test "fail" do
1182+
assert_fail(:foo_opaque_type, :other)
1183+
end
1184+
end
1185+
11661186
defp assert_pass(function_name, value) do
11671187
TestMock |> expect(function_name, fn -> value end)
11681188
assert value == apply(TestMock, function_name, [])

test/support/behaviour.ex

+7
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,11 @@ defmodule Hammox.Test.Behaviour do
118118
value: param
119119
}
120120
@callback foo_multiline_param_type() :: multiline_param_type(:arg)
121+
122+
@typep private_type :: :private_value
123+
@type type_including_private_type :: private_type()
124+
@callback foo_private_type() :: type_including_private_type()
125+
126+
@opaque opaque_type :: :opaque_value
127+
@callback foo_opaque_type() :: opaque_type
121128
end

0 commit comments

Comments
 (0)