Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a test for external functions with callbacks #55

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ defmodule Lua.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:luerl, "~> 1.2"},
{:luerl, path: "../../oss/luerl"},
{:ex_doc, "~> 0.31", only: :dev, runtime: false}
]
end
Expand Down
30 changes: 30 additions & 0 deletions test/lua_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,36 @@ defmodule LuaTest do
assert lua |> Lua.decode!(table) |> Lua.Table.as_map() == my_table
end

test "it can register functions that take callbacks that modify state" do
require Logger

lua = ~LUA"""
state = {}

function assignFoo()
state["foo"] = "bar"
end

function assignBar()
state["bar"] = "foo"
end

assignBar()
run(assignFoo)

return state
"""

assert {[ret], _lua} =
Lua.new()
|> Lua.set!([:run], fn [callback], lua ->
Lua.call_function!(lua, callback, [])
end)
|> Lua.eval!(lua)

assert Lua.Table.as_map(ret) == %{"foo" => "bar", "bar" => "foo"}
end

test "it can evaluate chunks" do
assert %Lua.Chunk{} = chunk = ~LUA[return 2 + 2]c

Expand Down
Loading