-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #112 from Qqwy/port_and_ref
Support for the Port and Reference built-in types.
- Loading branch information
Showing
9 changed files
with
172 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
defmodule TypeCheck.Builtin.Port do | ||
@moduledoc """ | ||
Type to check whether the given input is any port. | ||
Check `Port` for more information on ports. | ||
NOTE: The property testing generator will generate ports of the `cat` binary | ||
which is a sensible default as it will send back any binaries sent to it exactly. | ||
However, note that generated ports are not automatically closed. | ||
""" | ||
defstruct [] | ||
|
||
use TypeCheck | ||
@type! t :: %__MODULE__{} | ||
@type! problem_tuple :: {t(), :no_match, %{}, any()} | ||
|
||
defimpl TypeCheck.Protocols.ToCheck do | ||
def to_check(s, param) do | ||
quote generated: true, location: :keep do | ||
case unquote(param) do | ||
x when is_port(x) -> | ||
{:ok, [], x} | ||
_ -> | ||
{:error, {unquote(Macro.escape(s)), :no_match, %{}, unquote(param)}} | ||
end | ||
end | ||
end | ||
end | ||
|
||
defimpl TypeCheck.Protocols.Inspect do | ||
def inspect(_, opts) do | ||
"port()" | ||
|> Inspect.Algebra.color(:builtin_type, opts) | ||
end | ||
end | ||
|
||
if Code.ensure_loaded?(StreamData) do | ||
defimpl TypeCheck.Protocols.ToStreamData do | ||
def to_gen(_s) do | ||
# Ensure every iteration we create a _different_ port | ||
{} | ||
|> StreamData.constant() | ||
|> StreamData.map(fn _ -> | ||
Port.open({:spawn, "cat"}, [:binary]) | ||
end) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
defmodule TypeCheck.Builtin.Reference do | ||
@moduledoc """ | ||
Type to check whether the given input is a reference. | ||
Elixir/Erlang uses references for two use-cases: | ||
1. As unique identifiers. | ||
2. To refer to resources created and returned by NIFs (to be passed to other NIFs of the same NIF module). | ||
The property testing generator will generate arbitrary references using `Kernel.make_ref()`. | ||
To property-test the second kind of data, you should create your own kind of generator | ||
that calls the appropriate NIF. | ||
""" | ||
defstruct [] | ||
|
||
use TypeCheck | ||
@type! t :: %__MODULE__{} | ||
@type! problem_tuple :: {t(), :no_match, %{}, any()} | ||
|
||
defimpl TypeCheck.Protocols.ToCheck do | ||
def to_check(s, param) do | ||
quote generated: :true, location: :keep do | ||
case unquote(param) do | ||
x when is_reference(x) -> | ||
{:ok, [], x} | ||
_ -> | ||
{:error, {unquote(Macro.escape(s)), :no_match, %{}, unquote(param)}} | ||
end | ||
end | ||
end | ||
end | ||
|
||
defimpl TypeCheck.Protocols.Inspect do | ||
def inspect(_, opts) do | ||
"reference()" | ||
|> Inspect.Algebra.color(:builtin_type, opts) | ||
end | ||
end | ||
|
||
if Code.ensure_loaded?(StreamData) do | ||
defimpl TypeCheck.Protocols.ToStreamData do | ||
def to_gen(_s) do | ||
# Ensure every iteration we create a _different_ reference | ||
StreamData.constant({}) | ||
|> StreamData.map(fn _ -> make_ref() end) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters