-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
254 additions
and
701 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,4 +48,6 @@ zigler-*.tar | |
# erlang module zig files | ||
.test_*.zig | ||
# other compiled files | ||
*.a | ||
*.a | ||
|
||
/test/code |
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,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 |
This file was deleted.
Oops, something went wrong.
8 changes: 4 additions & 4 deletions
8
test/_support/tests/override_typespec.ex → test/integration/_documentation.ex
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 |
---|---|---|
@@ -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 |
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,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 |
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,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 |
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,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 |
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
47 changes: 47 additions & 0 deletions
47
test/integration/callbacks/on_upgrade_automatic_get_test.exs
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,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 |
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
10 changes: 9 additions & 1 deletion
10
test/integration/doc/doc_comment_test.exs → test/integration/documentation_test.exs
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,5 @@ | ||
defmodule ZiglerTest.Integration.Raw.ErrorRawMustHaveArityTest do | ||
use ExUnit.Case, async: true | ||
@tag :skip | ||
test "restore" | ||
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,5 @@ | ||
defmodule ZiglerTest.Integration.Raw.ErrorRawMustHaveCorrectParamsTest do | ||
use ExUnit.Case, async: true | ||
@tag :skip | ||
test "restore" | ||
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,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 |
Oops, something went wrong.