nix2lua/lib.test.nix

166 lines
4.7 KiB
Nix
Raw Normal View History

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
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";
2022-11-18 12:40:18 +03:00
expected = "\"hello world\"";
2022-11-18 02:36:55 +03:00
};
2024-02-28 12:50:08 +03:00
"test returns a path as a lua string" = {
expr = toLua /hello/world;
expected = "\"/hello/world\"";
};
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 ];
2022-11-18 12:40:18 +03:00
expected = "{ \"hello\", 10, 10.100000, true }";
};
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 ];
expected = "{ \"hello\", 10, 10.100000, true }";
};
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;
};
expected = "{ [\"fail\"] = false, [\"float\"] = 10.100000, [\"foo\"] = \"hello\", [\"int\"] = 10, [\"success\"] = true }";
};
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; };
expected = "{ [\"foo\"] = \"hello\" }";
};
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";
};
};
};
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" ] ];
expected = "{ { { \"foo\" }, \"bar\" } }";
};
"test returns table with one named field" = {
2022-11-18 22:46:17 +03:00
expr = toLua [
"foo"
(namedField "foo" "hello")
2022-11-18 22:46:17 +03:00
10
];
expected = "{ \"foo\", [\"foo\"] = \"hello\", 10 }";
};
2022-11-19 00:25:30 +03:00
"test returns raw string" = {
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" = {
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;
expected = "\"/foo/bar\"";
};
2022-11-19 03:43:29 +03:00
"test throws an error when you try to use named field withoun table" = {
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" = {
expr = toLua (call "root_pattern" [ "deno.json" "deno.jsonc" ]);
2024-02-28 16:54:16 +03:00
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")
'';
};
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()";
};
"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 (lset "parser_config.d2" { });
expected = "local parser_config.d2 = { }";
};
2024-04-17 14:22:15 +03:00
"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)
'';
};
2022-11-18 02:36:55 +03:00
}