Skip to content

Commit

Permalink
Some tests of the SizedBinary checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
Qqwy committed Oct 9, 2021
1 parent f8a4da6 commit dc10efc
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/type_check/builtin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ defmodule TypeCheck.Builtin do
if_recompiling? do
@spec! sized_bitstring(prefix_size :: non_neg_integer(), unit_size :: nil | 1..256) :: TypeCheck.Builtin.SizedBitstring.t()
end
def sized_bitstring(prefix_size, unit_size) do
def sized_bitstring(prefix_size, unit_size \\ nil) do
build_struct(TypeCheck.Builtin.SizedBitstring)
|> Map.put(:prefix_size, prefix_size)
|> Map.put(:unit_size, unit_size)
Expand Down
2 changes: 1 addition & 1 deletion lib/type_check/type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ defmodule TypeCheck.Type do

type_ast
|> build_unescaped(__CALLER__, options)
|> Macro.escape()
|> Macro.escape(unquote: true)
end

@doc false
Expand Down
60 changes: 60 additions & 0 deletions test/type_check/builtin/sized_binary_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
defmodule TypeCheck.Builtin.SizedBitstringTest do
use ExUnit.Case, async: true
use ExUnitProperties
import StreamData, only: []
use TypeCheck

test "Recognizes empty <<>> as literal empty binary" do
t = TypeCheck.Type.build(<<>>)
assert %TypeCheck.Builtin.Literal{value: <<>>} = t
end

property "Handles any sized bitstring (with only a prefix size) correctly" do
check all prefix_size <- StreamData.positive_integer(),
str <- StreamData.bitstring(length: prefix_size),
wrong_str <- StreamData.bitstring(length: prefix_size + 1) do
t = TypeCheck.Builtin.sized_bitstring(prefix_size)
assert %TypeCheck.Builtin.SizedBitstring{prefix_size: ^prefix_size} = t

assert {:ok, ^str} = TypeCheck.dynamic_conforms(str, t)
assert {:error, %{raw: {_, :no_match, _, _}}} = TypeCheck.dynamic_conforms(42, t)
assert {:error, %{raw: {_, :wrong_size, _, _}}} = TypeCheck.dynamic_conforms(wrong_str, t)
end
end

property "Handles any sized bitstring (with only a unit size) correctly" do
check all unit_size <- StreamData.integer(1..256),
repetitions <- StreamData.positive_integer(),
str <- StreamData.bitstring(length: repetitions * unit_size),
wrong_str <- StreamData.bitstring(length: repetitions * unit_size + 1) do
t = TypeCheck.Builtin.sized_bitstring(0, unit_size)
assert %TypeCheck.Builtin.SizedBitstring{prefix_size: 0, unit_size: ^unit_size} = t

assert {:ok, ^str} = TypeCheck.dynamic_conforms(str, t)
assert {:error, %{raw: {_, :no_match, _, _}}} = TypeCheck.dynamic_conforms(42, t)
if unit_size > 1 do
assert {:error, %{raw: {_, :wrong_size, _, _}}} = TypeCheck.dynamic_conforms(wrong_str, t)
end
end
end

property "Handles any sized bitstring (with both a prefix size and a unit size) correctly" do
check all prefix_size <- StreamData.positive_integer(),
unit_size <- StreamData.integer(1..256),
repetitions <- StreamData.positive_integer(),
proper_length = prefix_size + repetitions * unit_size,
wrong_length = proper_length + 1,
str <- StreamData.bitstring(length: proper_length),
wrong_str <- StreamData.bitstring(length: wrong_length) do

t = TypeCheck.Builtin.sized_bitstring(prefix_size, unit_size)
assert %TypeCheck.Builtin.SizedBitstring{prefix_size: ^prefix_size, unit_size: ^unit_size} = t

assert {:ok, ^str} = TypeCheck.dynamic_conforms(str, t)
assert {:error, %{raw: {_, :no_match, _, _}}} = TypeCheck.dynamic_conforms(42, t)
if unit_size > 1 do
assert {:error, %{raw: {_, :wrong_size, _, _}}} = TypeCheck.dynamic_conforms(wrong_str, t)
end
end
end
end
3 changes: 3 additions & 0 deletions test/type_check/builtin_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ defmodule TypeCheck.BuiltinTest do
quote do
impl(Enumerable)
end => TypeCheck.Builtin.ImplementsProtocol,
quote do
<<_ :: 4 >>
end => TypeCheck.Builtin.SizedBitstring,
}

for {type, module} <- possibilities do
Expand Down

0 comments on commit dc10efc

Please sign in to comment.