Skip to content

Commit

Permalink
tests/lib: Add basic unit tests for helpers.toLuaObject (nix-commun…
Browse files Browse the repository at this point in the history
  • Loading branch information
rummik authored Mar 26, 2023
1 parent 1c6efc5 commit 7f50b54
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
3 changes: 3 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
makeNixvim = self.legacyPackages."${system}".makeNixvim;
})
// {
lib-tests = import ./tests/lib-tests.nix {
inherit (pkgs) pkgs lib;
};
pre-commit-check = pre-commit-hooks.lib.${system}.run {
src = ./.;
hooks = {
Expand Down
100 changes: 100 additions & 0 deletions tests/lib-tests.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# For shorter test iterations run the following in the root of the repo:
# `echo ':b checks.${builtins.currentSystem}.lib-tests' | nix repl .`
{
pkgs,
lib,
}: let
helpers = import ../lib/helpers.nix {inherit pkgs lib;};
results = pkgs.lib.runTests {
testToLuaObject = {
expr = helpers.toLuaObject {
foo = "bar";
qux = [1 2 3];
};
expected = ''{["foo"] = "bar",["qux"] = {1,2,3}}'';
};

testToLuaObjectRawLua = {
expr = helpers.toLuaObject {
__raw = "<lua code>";
};
expected = "<lua code>";
};

testToLuaObjectLuaTableMixingList = {
expr = helpers.toLuaObject {
"@...." = "foo";
bar = "baz";
};
expected = ''{"foo",["bar"] = "baz"}'';
};

testToLuaObjectNestedAttrs = {
expr = helpers.toLuaObject {
a = {
b = 1;
c = 2;
d = {e = 3;};
};
};
expected = ''{["a"] = {["b"] = 1,["c"] = 2,["d"] = {["e"] = 3}}}'';
};

testToLuaObjectNestedList = {
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"'';
};

testToLuaObjectShouldFilterNullAttrs = {
expr = helpers.toLuaObject {
a = null;
b = {};
c = [];
d = {
e = null;
f = {};
};
};
expected = ''{["b"] = {},["c"] = {},["d"] = {["f"] = {}}}'';
};
};
in
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}
'')
results
);
} ''
echo -e "Tests failed:\\n\\n$results" >&2
exit 1
''

0 comments on commit 7f50b54

Please sign in to comment.