2022-11-19 04:46:37 +03:00
|
|
|
/**
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2022-11-18 02:36:55 +03:00
|
|
|
{ pkgs ? import <nixpkgs> { } }:
|
|
|
|
|
|
|
|
let
|
|
|
|
nix2lua = import ./lib.nix;
|
2022-11-19 03:43:29 +03:00
|
|
|
inherit (builtins) tryEval;
|
|
|
|
|
|
|
|
failed = { success = false; value = false; };
|
2022-11-18 02:36:55 +03:00
|
|
|
in
|
2024-04-17 10:56:41 +03:00
|
|
|
with nix2lua; pkgs.lib.runTests {
|
2022-11-18 12:40:18 +03:00
|
|
|
"test returns null" = {
|
2022-11-18 02:36:55 +03:00
|
|
|
expr = toLua null;
|
2022-11-18 12:40:18 +03:00
|
|
|
expected = null;
|
|
|
|
};
|
|
|
|
"test returns nil" = {
|
2024-02-28 12:50:08 +03:00
|
|
|
expr = toLua LuaNil;
|
2022-11-18 12:40:18 +03:00
|
|
|
expected = "nil";
|
2022-11-18 02:36:55 +03:00
|
|
|
};
|
|
|
|
"test returns a lua string" = {
|
|
|
|
expr = toLua "hello world";
|
2024-04-20 19:11:24 +03:00
|
|
|
expected = ''"hello world"'';
|
2022-11-18 02:36:55 +03:00
|
|
|
};
|
2024-05-06 02:16:02 +03:00
|
|
|
"test returns an escaped lua string" = {
|
|
|
|
expr = toLua ''\hello world'';
|
|
|
|
expected = ''"\\hello world"'';
|
|
|
|
};
|
2024-04-20 23:43:21 +03:00
|
|
|
"test returns a multiline lua string" = {
|
|
|
|
expr = toLua ''
|
|
|
|
hello
|
|
|
|
world
|
|
|
|
'';
|
|
|
|
expected = ''
|
|
|
|
[[
|
|
|
|
hello
|
|
|
|
world
|
|
|
|
]]'';
|
|
|
|
};
|
2024-02-28 12:50:08 +03:00
|
|
|
"test returns a path as a lua string" = {
|
|
|
|
expr = toLua /hello/world;
|
2024-04-20 19:11:24 +03:00
|
|
|
expected = ''"/hello/world"'';
|
2024-02-28 12:50:08 +03:00
|
|
|
};
|
2022-11-18 02:36:55 +03:00
|
|
|
"test returns an integer number" = {
|
|
|
|
expr = toLua 10;
|
|
|
|
expected = "10";
|
|
|
|
};
|
2022-11-18 12:40:18 +03:00
|
|
|
"test returns a negative integer number" = {
|
|
|
|
expr = toLua (-10);
|
|
|
|
expected = "-10";
|
|
|
|
};
|
2022-11-18 02:36:55 +03:00
|
|
|
"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";
|
|
|
|
};
|
2022-11-19 03:43:29 +03:00
|
|
|
"test returns table with all primitive types" = {
|
2022-11-18 02:36:55 +03:00
|
|
|
expr = toLua [ "hello" 10 10.1 true ];
|
2024-04-20 19:11:24 +03:00
|
|
|
expected = ''{ "hello", 10, 10.100000, true }'';
|
2022-11-18 12:40:18 +03:00
|
|
|
};
|
2022-11-19 03:43:29 +03:00
|
|
|
"test returns table without null values" = {
|
2022-11-18 12:40:18 +03:00
|
|
|
expr = toLua [ null "hello" null 10 null 10.1 null true null ];
|
2024-04-20 19:11:24 +03:00
|
|
|
expected = ''{ "hello", 10, 10.100000, true }'';
|
2022-11-18 12:40:18 +03:00
|
|
|
};
|
2022-11-19 03:43:29 +03:00
|
|
|
"test returns named table" = {
|
2022-11-18 12:40:18 +03:00
|
|
|
expr = toLua {
|
|
|
|
foo = "hello";
|
|
|
|
int = 10;
|
|
|
|
float = 10.1;
|
|
|
|
success = true;
|
|
|
|
fail = false;
|
2024-04-24 18:41:46 +03:00
|
|
|
quoted-var = true;
|
2022-11-18 12:40:18 +03:00
|
|
|
};
|
2024-04-24 18:41:46 +03:00
|
|
|
expected = ''{ fail = false, float = 10.100000, foo = "hello", int = 10, ["quoted-var"] = true, success = true }'';
|
2022-11-18 12:40:18 +03:00
|
|
|
};
|
2022-11-19 03:43:29 +03:00
|
|
|
"test returns named table without nullable items" = {
|
2022-11-18 12:40:18 +03:00
|
|
|
expr = toLua { foo = "hello"; bar = null; };
|
2024-04-24 18:41:46 +03:00
|
|
|
expected = ''{ foo = "hello" }'';
|
2022-11-18 12:40:18 +03:00
|
|
|
};
|
2022-11-19 03:43:29 +03:00
|
|
|
"test returns recursive named table" = {
|
2022-11-18 12:40:18 +03:00
|
|
|
expr = toLua {
|
|
|
|
first = {
|
|
|
|
second = {
|
|
|
|
last = "hello";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2024-04-24 18:41:46 +03:00
|
|
|
expected = ''{ first = { second = { last = "hello" } } }'';
|
2022-11-18 02:36:55 +03:00
|
|
|
};
|
2022-11-19 03:43:29 +03:00
|
|
|
"test return recursive table" = {
|
|
|
|
expr = toLua [ [ [ "foo" ] "bar" ] ];
|
2024-04-20 19:11:24 +03:00
|
|
|
expected = ''{ { { "foo" }, "bar" } }'';
|
2022-11-19 03:43:29 +03:00
|
|
|
};
|
|
|
|
"test returns table with one named field" = {
|
2022-11-18 22:46:17 +03:00
|
|
|
expr = toLua [
|
|
|
|
"foo"
|
2024-04-17 10:56:41 +03:00
|
|
|
(namedField "foo" "hello")
|
2022-11-18 22:46:17 +03:00
|
|
|
10
|
|
|
|
];
|
2024-04-24 18:41:46 +03:00
|
|
|
expected = ''{ "foo", foo = "hello", 10 }'';
|
2022-11-18 22:46:17 +03:00
|
|
|
};
|
2024-05-16 23:56:33 +03:00
|
|
|
"test returns table with skipped nullable named field" = {
|
|
|
|
expr = toLua [
|
|
|
|
"foo"
|
|
|
|
(namedField "foo" null)
|
|
|
|
10
|
|
|
|
];
|
|
|
|
expected = ''{ "foo", 10 }'';
|
|
|
|
};
|
2024-05-20 11:52:42 +03:00
|
|
|
"test returns named field with a raw in a key" = {
|
|
|
|
expr = toLua [
|
|
|
|
(namedField (raw "foo") 1)
|
|
|
|
(namedField "foo" 2)
|
|
|
|
];
|
|
|
|
expected = ''{ [foo] = 1, foo = 2 }'';
|
|
|
|
};
|
2022-11-19 00:25:30 +03:00
|
|
|
"test returns raw string" = {
|
2024-04-17 10:56:41 +03:00
|
|
|
expr = toLua (raw "hello");
|
2022-11-19 00:25:30 +03:00
|
|
|
expected = "hello";
|
|
|
|
};
|
2024-02-28 15:25:21 +03:00
|
|
|
"test returns deep raw string" = {
|
2024-04-17 10:56:41 +03:00
|
|
|
expr = toLua (raw (raw (raw "hello")));
|
2024-02-28 15:25:21 +03:00
|
|
|
expected = "hello";
|
|
|
|
};
|
2022-11-19 04:41:15 +03:00
|
|
|
"test returns path as string" = {
|
|
|
|
expr = toLua /foo/bar;
|
2024-04-20 19:11:24 +03:00
|
|
|
expected = ''"/foo/bar"'';
|
2022-11-19 04:41:15 +03:00
|
|
|
};
|
2022-11-19 03:43:29 +03:00
|
|
|
"test throws an error when you try to use named field withoun table" = {
|
2024-04-17 10:56:41 +03:00
|
|
|
expr = tryEval (toLua (namedField "foo" "bar"));
|
2022-11-19 03:43:29 +03:00
|
|
|
expected = failed;
|
|
|
|
};
|
2024-02-28 16:54:16 +03:00
|
|
|
"test returns call function with arguments" = {
|
2024-04-17 10:56:41 +03:00
|
|
|
expr = toLua (call "root_pattern" [ "deno.json" "deno.jsonc" ]);
|
2024-04-20 19:11:24 +03:00
|
|
|
expected = ''root_pattern("deno.json", "deno.jsonc")'';
|
2024-02-28 16:54:16 +03:00
|
|
|
};
|
2024-04-20 19:11:24 +03:00
|
|
|
"test returns concated expressions" = {
|
|
|
|
expr = toLua (spaceBetween [
|
2024-04-17 10:56:41 +03:00
|
|
|
(call "foo" [ 1 2 ])
|
|
|
|
(call "bar" [ "baz" "biz" ])
|
|
|
|
]);
|
2024-04-20 19:11:24 +03:00
|
|
|
expected = ''foo(1, 2) bar("baz", "biz")'';
|
2024-04-17 10:56:41 +03:00
|
|
|
};
|
2024-04-17 12:12:50 +03:00
|
|
|
"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()";
|
|
|
|
};
|
2024-05-10 15:28:03 +03:00
|
|
|
"test returns a pipe that convert string to var" = {
|
|
|
|
expr = toLua (pipe [
|
|
|
|
(require "nvim-tree.api")
|
|
|
|
"config"
|
|
|
|
"mappings"
|
|
|
|
(call "default_on_attach" (var "bufnr"))
|
|
|
|
]);
|
|
|
|
expected = "require(\"nvim-tree.api\").config.mappings.default_on_attach(bufnr)";
|
|
|
|
};
|
2024-05-20 13:31:10 +03:00
|
|
|
"test returns a simple var definition" = {
|
2024-04-17 12:12:50 +03:00
|
|
|
expr = toLua (set "parser_config.d2" { });
|
|
|
|
expected = "parser_config.d2 = { }";
|
|
|
|
};
|
2024-05-20 13:31:10 +03:00
|
|
|
"test returns a var definition with pipe in the name part" = {
|
|
|
|
expr = toLua (set (pipe1 "parser_config" "d2") { });
|
|
|
|
expected = "parser_config.d2 = { }";
|
|
|
|
};
|
|
|
|
"test returns a local var definition" = {
|
2024-04-17 14:55:12 +03:00
|
|
|
expr = toLua (local (set "parser_config.d2" { }));
|
2024-04-17 12:12:50 +03:00
|
|
|
expected = "local parser_config.d2 = { }";
|
|
|
|
};
|
2024-05-20 13:31:10 +03:00
|
|
|
"test returns a short variant of local var definitions" = {
|
2024-05-20 00:08:38 +03:00
|
|
|
expr = toLua (lset "parser_config.d2" { });
|
|
|
|
expected = "local parser_config.d2 = { }";
|
|
|
|
};
|
2024-04-17 14:22:15 +03:00
|
|
|
"test returns all operations" = {
|
2024-04-20 19:11:24 +03:00
|
|
|
expr = toLua (spaceBetween [
|
|
|
|
(set "a" (eq (mul (add 1 2) (sub 2 1)) 3))
|
|
|
|
(set "b" (not (eq (mul (add 1 2) (sub 2 1)) 3)))
|
|
|
|
(set "c" (not 10))
|
|
|
|
(set "d" (gt 10 5))
|
2024-04-17 14:22:15 +03:00
|
|
|
]);
|
2024-04-20 19:11:24 +03:00
|
|
|
expected = ''a = (((1 + 2) * (2 - 1)) == 3) b = not (((1 + 2) * (2 - 1)) == 3) c = not 10 d = (10 > 5)'';
|
2024-04-17 14:22:15 +03:00
|
|
|
};
|
2024-04-17 18:27:55 +03:00
|
|
|
"test returns rendered condition" = {
|
|
|
|
expr = toLua (set "name"
|
|
|
|
(and (gte (var "a") 1) (lte (var "a") 10))
|
|
|
|
);
|
|
|
|
expected = "name = ((a >= 1) and (a <= 10))";
|
|
|
|
};
|
2024-04-20 19:11:24 +03:00
|
|
|
"test returns defined function" = {
|
2024-04-17 14:55:12 +03:00
|
|
|
expr = toLua (func "hello" [ "a" "b" ] [
|
2024-04-20 19:11:24 +03:00
|
|
|
(set "a" (eq (mul (add 1 2) (sub 2 1)) 3))
|
|
|
|
(set "b" (not (eq (mul (add 1 2) (sub 2 1)) 3)))
|
|
|
|
(set "c" (not 10))
|
|
|
|
(set "d" (gt 10 5))
|
2024-04-17 14:55:12 +03:00
|
|
|
]);
|
2024-04-20 19:11:24 +03:00
|
|
|
expected = ''function hello(a, b) a = (((1 + 2) * (2 - 1)) == 3) b = not (((1 + 2) * (2 - 1)) == 3) c = not 10 d = (10 > 5) end'';
|
2024-04-17 14:55:12 +03:00
|
|
|
};
|
2024-04-20 17:36:34 +03:00
|
|
|
"test returns defined singleline function" = {
|
|
|
|
expr = toLua (func "hello" [ "a" ] (call1 "h.world" (var "a")));
|
|
|
|
expected = "function hello(a) h.world(a) end";
|
|
|
|
};
|
2024-04-20 20:12:52 +03:00
|
|
|
"test returns defined lambda" = {
|
2024-04-20 22:27:27 +03:00
|
|
|
expr = toLua (lambda [ "a" ] (call "h.world" (var "a")));
|
2024-04-20 20:12:52 +03:00
|
|
|
expected = "function(a) h.world(a) end";
|
|
|
|
};
|
2024-04-17 17:05:36 +03:00
|
|
|
"test returns if statement" = {
|
2024-04-20 19:11:24 +03:00
|
|
|
expr = toLua (if' (eq 10 10) (call "print" [ 10 ]));
|
|
|
|
expected = ''if (10 == 10) then print(10) end'';
|
2024-04-17 17:05:36 +03:00
|
|
|
};
|
|
|
|
"test returns if else statement" = {
|
|
|
|
expr = toLua (ifelse (eq 10 10) [
|
2024-04-20 19:11:24 +03:00
|
|
|
(call "print" "10 == 10")
|
|
|
|
(call "print" "10 == 10")
|
2024-04-17 17:05:36 +03:00
|
|
|
] [
|
2024-04-20 19:11:24 +03:00
|
|
|
(call "print" "10 != 10")
|
|
|
|
(call "print" "10 != 10")
|
2024-04-17 17:05:36 +03:00
|
|
|
]);
|
2024-04-20 19:11:24 +03:00
|
|
|
expected = ''if (10 == 10) then print("10 == 10") print("10 == 10") else print("10 != 10") print("10 != 10") end'';
|
2024-04-17 17:05:36 +03:00
|
|
|
};
|
|
|
|
"test returns a deep if else statement" = {
|
2024-04-20 19:11:24 +03:00
|
|
|
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'';
|
2024-04-17 17:05:36 +03:00
|
|
|
};
|
|
|
|
"test returns a returns keyword" = {
|
2024-04-20 19:11:24 +03:00
|
|
|
expr = toLua (ifelse (eq 10 10) return_void (return 10));
|
|
|
|
expected = ''if (10 == 10) then return else return 10 end'';
|
2024-04-17 17:05:36 +03:00
|
|
|
};
|
2024-05-20 00:08:38 +03:00
|
|
|
"test returns a definition of a variable with require function" = {
|
|
|
|
expr = toLua (requireTo "ls" "luasnip");
|
|
|
|
expected = ''local ls = require("luasnip")'';
|
|
|
|
};
|
2022-11-18 02:36:55 +03:00
|
|
|
}
|