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;
|
2024-02-28 16:54:16 +03:00
|
|
|
inherit (nix2lua) toLua LuaNil mkLuaRaw mkNamedField mkCall;
|
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
|
|
|
|
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"
|
2022-11-19 03:43:29 +03:00
|
|
|
(mkNamedField "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 (mkLuaRaw "hello");
|
|
|
|
expected = "hello";
|
|
|
|
};
|
2024-02-28 15:25:21 +03:00
|
|
|
"test returns deep raw string" = {
|
|
|
|
expr = toLua (mkLuaRaw (mkLuaRaw (mkLuaRaw "hello")));
|
|
|
|
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 (mkNamedField "foo" "bar"));
|
|
|
|
expected = failed;
|
|
|
|
};
|
2024-02-28 16:54:16 +03:00
|
|
|
"test returns call function with arguments" = {
|
|
|
|
expr = toLua (mkCall "root_pattern" [ "deno.json" "deno.jsonc" ]);
|
|
|
|
expected = "root_pattern(\"deno.json\", \"deno.jsonc\")";
|
|
|
|
};
|
2022-11-18 02:36:55 +03:00
|
|
|
}
|