Skip to content

Commit

Permalink
refactors a bunch of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ityonemo committed May 31, 2024
1 parent 31f9faf commit 7e92d61
Show file tree
Hide file tree
Showing 27 changed files with 254 additions and 701 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ zigler-*.tar
# erlang module zig files
.test_*.zig
# other compiled files
*.a
*.a

/test/code
27 changes: 1 addition & 26 deletions lib/zig/_nif.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule Zig.Nif do

@enforce_keys ~w[name export concurrency file module_code_path zig_code_path]a

defstruct @enforce_keys ++ ~w[line signature params return leak_check alias doc spec raw]a
defstruct @enforce_keys ++ ~w[line signature params return leak_check alias doc raw]a

alias Zig.Nif.Concurrency
alias Zig.Nif.DirtyCpu
Expand Down Expand Up @@ -41,7 +41,6 @@ defmodule Zig.Nif do
leak_check: boolean(),
alias: nil | atom,
doc: nil | String.t(),
spec: Macro.t(),
raw: :term | :erl_nif_term
}

Expand All @@ -59,7 +58,6 @@ defmodule Zig.Nif do
leak_check: boolean(),
alias: nil | atom,
doc: nil | String.t(),
spec: Macro.t(),
raw: nil
}

Expand All @@ -77,7 +75,6 @@ defmodule Zig.Nif do
| {:return, Return.opts()}
| {:alias, atom()}
| {:doc, String.t()}
| {:spec, Macro.t()}

@type opts() :: [defaultable_opts | individual_opts]

Expand Down Expand Up @@ -127,32 +124,10 @@ defmodule Zig.Nif do
end
end

typespec =
case nif.spec do
false ->
quote do
end

_ ->
nif.spec
|> List.wrap()
|> Enum.map(fn spec ->
nil
end)

{_, list} when is_list(list) ->
Enum.map(list, fn spec ->
quote do
@spec unquote(spec)
end
end)
end

functions = concurrency.render_elixir(nif)

quote context: Elixir do
unquote(doc)
unquote(typespec)
unquote(functions)
end
end
Expand Down
10 changes: 8 additions & 2 deletions lib/zig/compile_error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ defmodule Zig.CompileError do
|> Enum.reduce({[], nil}, fn
error_line, {so_far, nil} ->
{maybe_line, fileline} = revise_line(error_line, so_far, zig_code_path, manifest_module)
[_, _ | rest] = List.flatten(maybe_line)
{[IO.iodata_to_binary(rest)], fileline}

case List.flatten(maybe_line) do
[str1, str2 | rest] when is_binary(str1) and is_binary(str2) ->
{[IO.iodata_to_binary(rest)], fileline}

_ ->
{maybe_line, fileline}
end

error_line, {so_far, fileline} ->
{next, _} = revise_line(error_line, so_far, zig_code_path, manifest_module)
Expand Down
6 changes: 5 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ defmodule Zigler.MixProject do
preferred_cli_env: [dialyzer: :dev],
source_url: "https://github.com/E-xyza/zigler/",
docs: docs(),
aliases: [docs: "zig_doc"]
aliases: [docs: "zig_doc"],
test_elixirc_options: [
debug_info: true,
docs: true
]
]
end

Expand Down
27 changes: 27 additions & 0 deletions test/_support/compiler.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule ZiglerTest.Compiler do

@root_dir "test/code"

def init do
if File.dir?(@root_dir) do
File.rm_rf!(@root_dir)
end

File.mkdir_p!(@root_dir)

:code.add_pathz(~c'#{@root_dir}')
end

defmacro compile(file) do
quote do
[{mod, bin}] =
__DIR__
|> Path.join(unquote(file))
|> Code.compile_file()

beamfile = Path.join(unquote(@root_dir), "#{mod}.beam")

File.write!(beamfile, bin)
end
end
end
19 changes: 0 additions & 19 deletions test/_support/tests/documentation.ex

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
defmodule ZiglerTest.OverrideTypespec do
@moduledoc false
use Zig, otp_app: :zigler, nifs: [do_something: [spec: (integer -> integer)]]
defmodule ZiglerTest.Documentation do
use Zig, otp_app: :zigler

~Z"""
const beam = @import("beam");
/// This is a function that does something
pub fn do_something(term: beam.term) beam.term {
const value = beam.get(i32, term, .{}) catch unreachable;
return beam.make(value + 47, .{});
}
"""
end
end
15 changes: 15 additions & 0 deletions test/integration/_typespec_override.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule ZiglerTest.TypespecOverride do
@compile :debug_info

use Zig, otp_app: :zigler, nifs: [do_something: [specs: false]]

@spec do_something(integer) :: integer

~Z"""
const beam = @import("beam");
pub fn do_something(term: beam.term) beam.term {
const value = beam.get(i32, term, .{}) catch unreachable;
return beam.make(value + 47, .{});
}
"""
end
39 changes: 39 additions & 0 deletions test/integration/callbacks/on_load_automatic_get_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
defmodule ZiglerTest.Callbacks.OnLoadAutomaticGetTest do
# this is a test of the "automatic" on_load function. This means that the
# beam.context.env variable is set, and the term value is typed.
# the return value is also allowed to be an enum value
#
# the magic __on_load__ function is also tested here.

use ZiglerTest.IntegrationCase, async: true

use Zig, otp_app: :zigler, callbacks: [on_load: :automatic]

~Z"""
const beam = @import("beam");
var stored_mode: beam.ContextMode = undefined;
var stored_number: u32 = undefined;
pub fn automatic(_: [*c]?*anyopaque, number: u32) void {
stored_mode = beam.context.mode;
stored_number = number;
}
pub fn success() beam.term {
return beam.make(.{stored_mode, beam.context.mode, stored_number}, .{});
}
"""

defp __on_load__, do: 47

test "on_load can use automatic mode" do
assert {:callback, :synchronous, 47} = success()
end

test "the on_load function is not exported" do
refute :functions
|> __MODULE__.__info__()
|> Keyword.has_key?(:automatic)
end
end
2 changes: 1 addition & 1 deletion test/integration/callbacks/on_load_erroring_enum_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule ZiglerTest.Callbacks.OnLoadErroringEnumTest do
use ZiglerTest.IntegrationCase, async: true
import ExUnit.CaptureLog

test "compiler error when on_load function errors out" do
test "error when on_load function errors out" do
log =
capture_log(fn ->
Code.compile_quoted(
Expand Down
30 changes: 30 additions & 0 deletions test/integration/callbacks/on_load_get_error_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule ZiglerTest.Callbacks.OnLoadGetErrorTest do
# this is a test of the "automatic" on_load function.

use ZiglerTest.IntegrationCase, async: true
import ExUnit.CaptureLog

test "error when on_load function is passed the wrong type" do
log =
capture_log(fn ->
Code.compile_quoted(
quote do
defmodule ZiglerTest.OnLoadGetError do
use Zig, otp_app: :zigler, callbacks: [on_load: :foo], dir: unquote(__DIR__)

def __on_load__, do: "not_an_integer"

~Z"""
const beam = @import("beam");
pub fn foo(_: [*c]?*anyopaque, _: i32) void {
}
"""
end
end
)
end)

assert log =~ "[error] loading module Elixir.ZiglerTest.OnLoadGetError"
assert log =~ "(42)"
end
end
4 changes: 4 additions & 0 deletions test/integration/callbacks/on_load_malformed_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ defmodule ZiglerTest.Callbacks.OnLoadMalformedTest do
)
end
end

test "uses a non-pub struct"

test "uses a type that isn't approved by beam.get"
end

describe "compiler error when on_load arity 3" do
Expand Down
47 changes: 47 additions & 0 deletions test/integration/callbacks/on_upgrade_automatic_get_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
defmodule ZiglerTest.Callbacks.OnUpgradeAutomaticGetTest do
# this is a test of the "automatic" on_upgrade function. This means that the
# beam.context.env variable is set, and the term value is set to beam.term.
# the return value is also allowed to be a void value

use ZiglerTest.IntegrationCase, async: true

import ExUnit.CaptureIO

def build_module(opts) do
Code.compile_quoted(
quote do
defmodule ZiglerTest.OnUpgradeAutomaticGet do
use Zig, otp_app: :zigler, callbacks: [on_upgrade: :on_upgrade], dir: unquote(__DIR__)

defp __on_load__, do: unquote(opts)

~z"""
const beam = @import("beam");
const S = struct{ pid: beam.pid, value: i32};
pub fn on_upgrade(_: [*c]?*anyopaque, _: [*c]?*anyopaque, config: S) void {
_ = beam.send(config.pid, .{.result, config.value}, .{}) catch unreachable;
}
pub fn bar() u8 { return #{unquote(opts[:value])}; }
"""
end
end
)
end

alias ZiglerTest.OnUpgradeAutomaticGet

test "on_upgrade generally works" do
this = self()
build_module(pid: this, value: 0)
assert 0 = apply(OnUpgradeAutomaticGet, :bar, [])

redefine_warn =
capture_io(:stderr, fn ->
build_module(pid: self(), value: 42)
assert_receive {:result, 42}
end)

assert redefine_warn =~ "redefining module ZiglerTest.OnUpgradeAutomaticGet"
end
end
4 changes: 4 additions & 0 deletions test/integration/callbacks/on_upgrade_malformed_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ defmodule ZiglerTest.Callbacks.OnUpgradeMalformedTest do
)
end
end

test "uses a an non-pub struct"

test "uses an unapproved type"
end

describe "compiler error when on_upgrade arity 4" do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
defmodule ZiglerTest.Unit.Typespec.DocCommentTest do
defmodule ZiglerTest.Unit.Typespec.Documentation do
use ExUnit.Case, async: true

@moduletag :documentation

require ZiglerTest.Compiler

setup_all do
ZiglerTest.Compiler.compile("_documentation.ex")
end

test "it is possible to write a doc comment" do
assert {:docs_v1, _, _, _, _, _, funcs} = Code.fetch_docs(ZiglerTest.Documentation)

Expand Down
5 changes: 5 additions & 0 deletions test/integration/raw/_error_raw_must_have_arity_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule ZiglerTest.Integration.Raw.ErrorRawMustHaveArityTest do
use ExUnit.Case, async: true
@tag :skip
test "restore"
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule ZiglerTest.Integration.Raw.ErrorRawMustHaveCorrectParamsTest do
use ExUnit.Case, async: true
@tag :skip
test "restore"
end
11 changes: 11 additions & 0 deletions test/integration/raw/_typespec.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule ZiglerTest.Integration.Raw.Typespec do
@compile :debug_info
use Zig, otp_app: :zigler, nifs: [raw: [arity: 1]]

~Z"""
const beam = @import("beam");
pub fn raw(_: beam.env, _: c_int, terms: [*]const beam.term) beam.term {
return terms[0];
}
"""
end
Loading

0 comments on commit 7e92d61

Please sign in to comment.