nix2lua/lib.test.nix

81 lines
2 KiB
Nix
Raw Normal View History

2022-11-18 02:36:55 +03:00
{ pkgs ? import <nixpkgs> { } }:
let
nix2lua = import ./lib.nix;
2022-11-18 22:46:17 +03:00
inherit (nix2lua) toLua mkLuaNil mkDictItem;
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" = {
expr = toLua mkLuaNil;
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
};
"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";
};
"test returns array with all primitive types" = {
expr = toLua [ "hello" 10 10.1 true ];
2022-11-18 12:40:18 +03:00
expected = "{ \"hello\", 10, 10.100000, true }";
};
"test returns array without null values" = {
expr = toLua [ null "hello" null 10 null 10.1 null true null ];
expected = "{ \"hello\", 10, 10.100000, true }";
};
"test returns dict" = {
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 dict without nullable items" = {
expr = toLua { foo = "hello"; bar = null; };
expected = "{ [\"foo\"] = \"hello\" }";
};
"test returns recursive dict" = {
expr = toLua {
first = {
second = {
last = "hello";
};
};
};
expected = "{ [\"first\"] = { [\"second\"] = { [\"last\"] = \"hello\" } } }";
2022-11-18 02:36:55 +03:00
};
2022-11-18 22:46:17 +03:00
"test returns array with dict items" = {
expr = toLua [
"foo"
(mkDictItem "foo" "hello")
10
];
expected = "{ \"foo\", [\"foo\"] = \"hello\", 10 }";
};
2022-11-18 02:36:55 +03:00
}