Skip to content

Commit

Permalink
feat: more test coverage (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
doomspork committed Jan 9, 2024
1 parent 931b24d commit ced718b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 56 deletions.
12 changes: 7 additions & 5 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@ defmodule Luau.MixProject do
def project do
[
app: :luau,
version: "0.1.0",
deps: deps(),
elixir: "~> 1.15",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
deps: deps()
version: "0.1.0"
]
end

# Path.join([:code.priv_dir(:sidecar), "lua", name])
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end

# Run "mix help deps" to learn about dependencies.
defp elixirc_paths(:test), do: ["lib", "test/support", "priv/scripts"]
defp elixirc_paths(:dev), do: ["lib", "priv/scripts"]
defp elixirc_paths(_), do: ["lib"]

defp deps do
[
{:luerl, "~> 1.1"},
Expand Down
85 changes: 34 additions & 51 deletions test/luau_test.exs
Original file line number Diff line number Diff line change
@@ -1,35 +1,11 @@
defmodule LuauTest do
use ExUnit.Case

defmodule AdderLibrary do
use Luau.Library, scope: "Adder"

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 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
setup do
{:ok, luau: Luau.init()}
end

describe "set_variable/3" do
setup do
{:ok, luau: Luau.init()}
end

test "sets a variable", %{luau: luau} do
name = ["name"]
value = "Robert"
Expand All @@ -44,29 +20,12 @@ defmodule LuauTest do
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)
test "load a Luau.Library to the luau", %{luau: luau} do
assert %Luau{modules: [Math]} = Luau.load_module!(luau, Math)
end
end

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")
Expand All @@ -75,16 +34,21 @@ defmodule LuauTest do

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)
assert %Luau{lua_files: [^path]} = Luau.load_lua!(luau, path)
end
end

script = """
return hello("Robert")
"""
describe "run/2" do
test "evaluates lua script and returns the result", %{luau: luau} do
path = Path.join([__DIR__, "support", "hello.lua"])

assert {:ok, ["Hello Robert!"], _luau} = Luau.run(loaded_luau, script)
assert {:ok, ["Hello Robert!"], _luau} =
luau
|> Luau.load_lua!(path)
|> Luau.run("return hello('Robert')")
end

test "loads lua module into luau", %{luau: luau} do
test "supports multiple return values", %{luau: luau} do
path = Path.join([__DIR__, "support", "enum.lua"])

assert {:ok, %Luau{lua_files: [^path]} = loaded_luau} =
Expand All @@ -101,5 +65,24 @@ defmodule LuauTest do

assert {:ok, [false, true], _luau} = Luau.run(loaded_luau, script)
end

test "supports running with state modified by another run", %{luau: luau} do
{:ok, luau} =
luau
|> Luau.load_module!(Math)
|> Luau.set_variable("numbers", [1, 2, 3])

# We don't use `local` within these tests intentionally
{:ok, _, luau} = Luau.run(luau, "size = #numbers")

script = """
double_size = Math.add(size, size)
return double_size
"""

{:ok, [6], luau} = Luau.run(luau, script)

{:ok, [5], _luau} = Luau.run(luau, "return Math.sub(double_size, 1)")
end
end
end
11 changes: 11 additions & 0 deletions test/support/math.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule Math do
use Luau.Library, scope: "Math"

deflua add(a, b) do
a + b
end

deflua sub(a, b) do
a - b
end
end

0 comments on commit ced718b

Please sign in to comment.