-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Collapse Runtime into Luau (#7)
- Loading branch information
Showing
8 changed files
with
158 additions
and
165 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 |
---|---|---|
|
@@ -24,3 +24,5 @@ luau-*.tar | |
|
||
# Temporary files, for example, from tests. | ||
/tmp/ | ||
|
||
.DS_Store |
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,6 +1,6 @@ | ||
# Lūʻau | ||
|
||
Welcome to the Lua Lūʻau! | ||
Welcome to the Lua Lūʻau! | ||
|
||
## What's in a name? | ||
|
||
|
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 was deleted.
Oops, something went wrong.
Empty file.
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,27 +1,105 @@ | ||
defmodule LuauTest do | ||
use ExUnit.Case | ||
|
||
defmodule TestLibrary do | ||
@moduledoc """ | ||
A test library for Luau. | ||
""" | ||
use Luau.Library, scope: "test" | ||
defmodule AdderLibrary do | ||
use Luau.Library, scope: "Adder" | ||
|
||
deflua hello(name) do | ||
"Hello, #{name}!" | ||
deflua add(a, b) do | ||
a + b | ||
end | ||
end | ||
|
||
defmodule EvenLibrary do | ||
use Luau.Library, scope: "Even" | ||
|
||
deflua is_even(value) do | ||
rem(value, 2) == 0 | ||
end | ||
end | ||
|
||
describe "init/0" do | ||
test "returns a runtime" do | ||
assert %Luau.Runtime{} = Luau.init() | ||
test "returns a luau containing the Lua state and unique id" do | ||
assert %Luau{id: id, state: state} = Luau.init() | ||
assert is_binary(id) | ||
refute is_nil(state) | ||
end | ||
end | ||
|
||
describe "set_variable/3" do | ||
setup do | ||
{:ok, luau: Luau.init()} | ||
end | ||
|
||
test "sets a variable", %{luau: luau} do | ||
name = ["name"] | ||
value = "Robert" | ||
assert {:ok, %Luau{variables: %{^name => ^value}}} = Luau.set_variable(luau, name, value) | ||
end | ||
|
||
test "wraps variable path as necessary", %{luau: luau} do | ||
name = "name" | ||
value = "Robert" | ||
assert {:ok, %Luau{variables: %{[^name] => ^value}}} = Luau.set_variable(luau, name, value) | ||
end | ||
end | ||
|
||
describe "load_module!/2" do | ||
setup do | ||
{:ok, luau: Luau.init()} | ||
end | ||
|
||
test "load a Luau.Library to the lua luau", %{luau: luau} do | ||
assert %Luau{modules: [EvenLibrary, AdderLibrary]} = | ||
loaded_luau = luau |> Luau.load_module!(AdderLibrary) |> Luau.load_module!(EvenLibrary) | ||
|
||
script = """ | ||
local a = Adder.add(3,3) | ||
local b = Even.is_even(a) | ||
return b | ||
""" | ||
|
||
assert {:ok, [true], _luau} = Luau.run(loaded_luau, script) | ||
end | ||
end | ||
|
||
describe "execute/2" do | ||
test "evaluates Lua" do | ||
# runtime = Luau.initialize(libraries: [TestLibrary], variables: %{name: "Robert"}) | ||
# assert {"Hello, Robert!", %Luau.Runtime{}} = Luau.execute(runtime, ~s/test.hello(name)/) | ||
describe "load_lua!/2" do | ||
setup do | ||
{:ok, luau: Luau.init()} | ||
end | ||
|
||
test "raises an error for missing files", %{luau: luau} do | ||
assert_raise RuntimeError, fn -> | ||
Luau.load_lua!(luau, "missing.lua") | ||
end | ||
end | ||
|
||
test "loads simple lua script into luau", %{luau: luau} do | ||
path = Path.join([__DIR__, "support", "hello.lua"]) | ||
assert %Luau{lua_files: [^path]} = loaded_luau = Luau.load_lua!(luau, path) | ||
|
||
script = """ | ||
return hello("Robert") | ||
""" | ||
|
||
assert {:ok, ["Hello Robert!"], _luau} = Luau.run(loaded_luau, script) | ||
end | ||
|
||
test "loads lua module into luau", %{luau: luau} do | ||
path = Path.join([__DIR__, "support", "enum.lua"]) | ||
|
||
assert {:ok, %Luau{lua_files: [^path]} = loaded_luau} = | ||
luau | ||
|> Luau.load_lua!(path) | ||
|> Luau.set_variable("numbers", [1, 2, 3, 4]) | ||
|
||
script = """ | ||
local all = Enum.all(numbers, function(n) return n % 2 == 0 end) | ||
local any = Enum.any(numbers, function(n) return n % 2 == 0 end) | ||
return all, any | ||
""" | ||
|
||
assert {:ok, [false, true], _luau} = Luau.run(loaded_luau, script) | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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