245 lines
6.3 KiB
Nix
245 lines
6.3 KiB
Nix
/**
|
|
* Copyright (C) 2022, Dmitriy Pleshevskiy <dmitriy@pleshevski.ru>
|
|
*
|
|
* nix2lua is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* nix2lua is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with nix2lua. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
{ pkgs ? import <nixpkgs> { } }:
|
|
|
|
let
|
|
nix2lua = import ./lib.nix;
|
|
inherit (builtins) tryEval;
|
|
|
|
failed = { success = false; value = false; };
|
|
in
|
|
with nix2lua; pkgs.lib.runTests {
|
|
"test returns null" = {
|
|
expr = toLua null;
|
|
expected = null;
|
|
};
|
|
"test returns nil" = {
|
|
expr = toLua LuaNil;
|
|
expected = "nil";
|
|
};
|
|
"test returns a lua string" = {
|
|
expr = toLua "hello world";
|
|
expected = "\"hello world\"";
|
|
};
|
|
"test returns a path as a lua string" = {
|
|
expr = toLua /hello/world;
|
|
expected = "\"/hello/world\"";
|
|
};
|
|
"test returns an integer number" = {
|
|
expr = toLua 10;
|
|
expected = "10";
|
|
};
|
|
"test returns a negative integer number" = {
|
|
expr = toLua (-10);
|
|
expected = "-10";
|
|
};
|
|
"test returns a float number" = {
|
|
expr = toLua 10.1;
|
|
expected = "10.100000";
|
|
};
|
|
"test returns true" = {
|
|
expr = toLua true;
|
|
expected = "true";
|
|
};
|
|
"test returns false" = {
|
|
expr = toLua false;
|
|
expected = "false";
|
|
};
|
|
"test returns table with all primitive types" = {
|
|
expr = toLua [ "hello" 10 10.1 true ];
|
|
expected = "{ \"hello\", 10, 10.100000, true }";
|
|
};
|
|
"test returns table without null values" = {
|
|
expr = toLua [ null "hello" null 10 null 10.1 null true null ];
|
|
expected = "{ \"hello\", 10, 10.100000, true }";
|
|
};
|
|
"test returns named table" = {
|
|
expr = toLua {
|
|
foo = "hello";
|
|
int = 10;
|
|
float = 10.1;
|
|
success = true;
|
|
fail = false;
|
|
};
|
|
expected = "{ [\"fail\"] = false, [\"float\"] = 10.100000, [\"foo\"] = \"hello\", [\"int\"] = 10, [\"success\"] = true }";
|
|
};
|
|
"test returns named table without nullable items" = {
|
|
expr = toLua { foo = "hello"; bar = null; };
|
|
expected = "{ [\"foo\"] = \"hello\" }";
|
|
};
|
|
"test returns recursive named table" = {
|
|
expr = toLua {
|
|
first = {
|
|
second = {
|
|
last = "hello";
|
|
};
|
|
};
|
|
};
|
|
expected = "{ [\"first\"] = { [\"second\"] = { [\"last\"] = \"hello\" } } }";
|
|
};
|
|
"test return recursive table" = {
|
|
expr = toLua [ [ [ "foo" ] "bar" ] ];
|
|
expected = "{ { { \"foo\" }, \"bar\" } }";
|
|
};
|
|
"test returns table with one named field" = {
|
|
expr = toLua [
|
|
"foo"
|
|
(namedField "foo" "hello")
|
|
10
|
|
];
|
|
expected = "{ \"foo\", [\"foo\"] = \"hello\", 10 }";
|
|
};
|
|
"test returns raw string" = {
|
|
expr = toLua (raw "hello");
|
|
expected = "hello";
|
|
};
|
|
"test returns deep raw string" = {
|
|
expr = toLua (raw (raw (raw "hello")));
|
|
expected = "hello";
|
|
};
|
|
"test returns path as string" = {
|
|
expr = toLua /foo/bar;
|
|
expected = "\"/foo/bar\"";
|
|
};
|
|
"test throws an error when you try to use named field withoun table" = {
|
|
expr = tryEval (toLua (namedField "foo" "bar"));
|
|
expected = failed;
|
|
};
|
|
"test returns call function with arguments" = {
|
|
expr = toLua (call "root_pattern" [ "deno.json" "deno.jsonc" ]);
|
|
expected = "root_pattern(\"deno.json\", \"deno.jsonc\")";
|
|
};
|
|
"test returns concated lines" = {
|
|
expr = toLua (concatLines [
|
|
(call "foo" [ 1 2 ])
|
|
(call "bar" [ "baz" "biz" ])
|
|
]);
|
|
expected = ''
|
|
foo(1, 2)
|
|
bar("baz", "biz")
|
|
'';
|
|
};
|
|
"test returns a pipe with many function call" = {
|
|
expr = toLua (pipe [
|
|
(require "nvim-treesitter.parsers")
|
|
(call "get_parser_configs" [ ])
|
|
]);
|
|
expected = "require(\"nvim-treesitter.parsers\").get_parser_configs()";
|
|
};
|
|
"test returns lua with setting value to the variable" = {
|
|
expr = toLua (set "parser_config.d2" { });
|
|
expected = "parser_config.d2 = { }";
|
|
};
|
|
"test returns lua with setting value to the local variable" = {
|
|
expr = toLua (local (set "parser_config.d2" { }));
|
|
expected = "local parser_config.d2 = { }";
|
|
};
|
|
"test returns all operations" = {
|
|
expr = toLua (concatLines [
|
|
(eq (mul (add 1 2) (sub 2 1)) 3)
|
|
(not (eq (mul (add 1 2) (sub 2 1)) 3))
|
|
(not 10)
|
|
(gt 10 5)
|
|
]);
|
|
expected = ''
|
|
(((1 + 2) * (2 - 1)) == 3)
|
|
not (((1 + 2) * (2 - 1)) == 3)
|
|
not 10
|
|
(10 > 5)
|
|
'';
|
|
};
|
|
"test returns rendered condition" = {
|
|
expr = toLua (set "name"
|
|
(and (gte (var "a") 1) (lte (var "a") 10))
|
|
);
|
|
expected = "name = ((a >= 1) and (a <= 10))";
|
|
};
|
|
"test returns defined function" = {
|
|
expr = toLua (func "hello" [ "a" "b" ] [
|
|
(eq (mul (add (var "a") 2) (sub 2 1)) 3)
|
|
(not (eq (mul (add (var "b") 2) (sub 2 1)) 3))
|
|
(not 10)
|
|
(gt 10 5)
|
|
]);
|
|
expected = ''
|
|
function hello(a, b)
|
|
(((a + 2) * (2 - 1)) == 3)
|
|
not (((b + 2) * (2 - 1)) == 3)
|
|
not 10
|
|
(10 > 5)
|
|
end
|
|
'';
|
|
};
|
|
"test returns if statement" = {
|
|
expr = toLua (if' (eq 10 10) [
|
|
(call "print" [ 10 ])
|
|
]);
|
|
expected = ''
|
|
if (10 == 10) then
|
|
print(10)
|
|
end
|
|
'';
|
|
};
|
|
"test returns if else statement" = {
|
|
expr = toLua (ifelse (eq 10 10) [
|
|
(call "print" [ "10 == 10" ])
|
|
] [
|
|
(call "print" [ "10 != 10" ])
|
|
]);
|
|
expected = ''
|
|
if (10 == 10) then
|
|
print("10 == 10")
|
|
else
|
|
print("10 != 10")
|
|
end
|
|
'';
|
|
};
|
|
"test returns a deep if else statement" = {
|
|
expr = toLua (ifelse (eq 10 10) [
|
|
(if' true [ (call "print" [ "yes" ]) ])
|
|
] [
|
|
(if' true [ (call "print" [ "no" ]) ])
|
|
]);
|
|
expected = ''
|
|
if (10 == 10) then
|
|
if true then
|
|
print("yes")
|
|
end
|
|
|
|
else
|
|
if true then
|
|
print("no")
|
|
end
|
|
|
|
end
|
|
'';
|
|
};
|
|
"test returns a returns keyword" = {
|
|
expr = toLua (ifelse (eq 10 10) [
|
|
return_void
|
|
] [
|
|
(return 10)
|
|
]);
|
|
expected = ''
|
|
if (10 == 10) then
|
|
return
|
|
else
|
|
return 10
|
|
end
|
|
'';
|
|
};
|
|
}
|