nix-community.nixvim/tests/lib-tests.nix

135 lines
2.7 KiB
Nix
Raw Permalink Normal View History

# For shorter test iterations run the following in the root of the repo:
# `echo ':b checks.${builtins.currentSystem}.lib-tests' | nix repl .`
{
lib,
pkgs,
helpers,
2024-05-05 19:39:35 +02:00
}:
let
results = pkgs.lib.runTests {
testToLuaObject = {
expr = helpers.toLuaObject {
foo = "bar";
2024-05-05 19:39:35 +02:00
qux = [
1
2
3
];
};
expected = ''{["foo"] = "bar",["qux"] = {1,2,3}}'';
};
testToLuaObjectRawLua = {
2024-05-05 19:39:35 +02:00
expr = helpers.toLuaObject { __raw = "<lua code>"; };
expected = "<lua code>";
};
testToLuaObjectLuaTableMixingList = {
expr = helpers.toLuaObject {
"__unkeyed...." = "foo";
bar = "baz";
};
expected = ''{"foo",["bar"] = "baz"}'';
};
testToLuaObjectNestedAttrs = {
expr = helpers.toLuaObject {
a = {
b = 1;
c = 2;
2024-05-05 19:39:35 +02:00
d = {
e = 3;
};
};
};
expected = ''{["a"] = {["b"] = 1,["c"] = 2,["d"] = {["e"] = 3}}}'';
};
testToLuaObjectNestedList = {
2024-05-05 19:39:35 +02:00
expr = helpers.toLuaObject [
1
2
[
3
4
[
5
6
]
]
7
];
expected = "{1,2,{3,4,{5,6}},7}";
};
testToLuaObjectNonStringPrims = {
expr = helpers.toLuaObject {
a = 1.0;
b = 2;
c = true;
d = false;
e = null;
};
expected = ''{["a"] = 1.000000,["b"] = 2,["c"] = true,["d"] = false}'';
};
testToLuaObjectNilPrim = {
expr = helpers.toLuaObject null;
expected = "nil";
};
testToLuaObjectStringPrim = {
expr = helpers.toLuaObject ''
foo\bar
baz'';
expected = ''"foo\\bar\nbaz"'';
};
testToLuaObjectFilters = {
expr = helpers.toLuaObject {
a = null;
2024-05-05 19:39:35 +02:00
b = { };
c = [ ];
d = {
e = null;
2024-05-05 19:39:35 +02:00
f = { };
};
};
expected = ''{}'';
};
testToLuaObjectEmptyTable = {
expr = helpers.toLuaObject {
a = null;
2024-05-05 19:39:35 +02:00
b = { };
c = {
__empty = null;
};
d = {
e = null;
2024-05-05 19:39:35 +02:00
f = { };
g = helpers.emptyTable;
};
};
expected = ''{["c"] = { },["d"] = {["g"] = { }}}'';
};
};
in
2024-05-05 19:39:35 +02:00
if results == [ ] then
pkgs.runCommand "lib-tests-success" { } "touch $out"
else
pkgs.runCommand "lib-tests-failure"
{
results = pkgs.lib.concatStringsSep "\n" (
builtins.map (result: ''
${result.name}:
expected: ${result.expected}
result: ${result.result}
2024-05-05 19:39:35 +02:00
'') results
);
2024-05-05 19:39:35 +02:00
}
''
echo -e "Tests failed:\\n\\n$results" >&2
exit 1
''