Add unit tests for luaUtils and some for lazyUtils.

This commit is contained in:
Kevin Baensch 2024-05-18 11:36:22 +02:00
parent 8dd643ceeb
commit d82f7a033d
Signed by: derped
GPG key ID: C0F1D326C7626543
6 changed files with 317 additions and 3 deletions

38
tests/default.nix Normal file
View file

@ -0,0 +1,38 @@
{
lib ? (import <nixpkgs> { }).lib,
}:
let
# pkgs stub to mock calls to pkgs
pkgs = rec {
writeTextFile =
{
name,
text,
executable ? false,
destination ? "",
checkPhase ? "",
meta ? { },
allowSubstitutes ? false,
preferLocalBuild ? true,
derivationArgs ? { },
}@set:
set;
writeTextDir =
path: text:
writeTextFile {
inherit text;
name = builtins.baseNameOf path;
destination = "/${path}";
};
symlinkJoin = { name, paths }@set: set;
stylua = "";
vimPlugins.lazy-nvim = "";
};
luaUtils = import ../pkgs/luaUtils.nix { inherit lib pkgs; };
lazyUtils = import ../pkgs/lazyUtils.nix { inherit lib pkgs luaUtils; };
in
{
luaUtils = import ./luaUtils.nix { inherit luaUtils; };
lazyUtils = import ./lazyUtils.nix { inherit lazyUtils; };
}

0
tests/empty.txt Normal file
View file

68
tests/lazyUtils.nix Normal file
View file

@ -0,0 +1,68 @@
{ lazyUtils }:
let
nullPlugin = {
name = null;
short = null;
dir = null;
};
miniDrv = builtins.derivation {
name = "test";
pname = "test";
builder = "test";
system = "test";
};
in
{
helpers = {
getName = {
testName = {
expr = lazyUtils.helpers.getName (nullPlugin // { name = "nameTest"; });
expected = "nameTest";
};
testShort = {
expr = lazyUtils.helpers.getName (nullPlugin // { short = "shortTest"; });
expected = "shortTest";
};
testDir = {
expr = lazyUtils.helpers.getName (nullPlugin // { dir = ./empty.txt; });
expected = "empty.txt";
};
testDrv = {
expr = lazyUtils.helpers.getName (nullPlugin // { dir = miniDrv; });
expected = "test";
};
testInvalidInput = {
expr = lazyUtils.helpers.getName { };
expectedError = {
type = "ThrownError";
# For some reason there is whitespace after the newline
msg = "Could not determine a plugin name.\n +\\{\\}";
};
};
};
getKey = { };
pluginWrapper = {
testShort = {
expr = lazyUtils.helpers.pluginWrapper "test";
expected = {
short = "test";
};
};
testDrv = {
expr = lazyUtils.helpers.pluginWrapper miniDrv;
expected = {
dir = miniDrv;
};
};
testSet = {
expr = lazyUtils.helpers.pluginWrapper { };
expected = { };
};
testInvalidInput = {
expr = lazyUtils.helpers.pluginWrapper null;
expectedError.type = "AssertionError";
};
};
};
}

181
tests/luaUtils.nix Normal file
View file

@ -0,0 +1,181 @@
{ luaUtils }:
let
miniDrv = builtins.derivation {
name = "test";
builder = "test";
system = "test";
};
in
{
nullToLua = {
testNull = {
expr = luaUtils.nullToLua null;
expected = "nil";
};
testInvalidInput = {
expr = luaUtils.nullToLua { };
expectedError.type = "AssertionError";
};
};
boolToLua = {
testTrue = {
expr = luaUtils.boolToLua true;
expected = "true";
};
testFalse = {
expr = luaUtils.boolToLua false;
expected = "false";
};
testInvalidInput = {
expr = luaUtils.boolToLua { };
expectedError.type = "AssertionError";
};
};
stringToLua = {
testEmptyString = {
expr = luaUtils.stringToLua "";
expected = ''""'';
};
testSimpleString = {
expr = luaUtils.stringToLua "test";
expected = ''"test"'';
};
testPath = {
expr = luaUtils.stringToLua ./empty.txt;
expected = ''"/nix/store/sfmk8wid8fzwnvzcwvw8sgblj2xz4n58-empty.txt"'';
};
testInvalidInput = {
expr = luaUtils.stringToLua { };
expectedError.type = "AssertionError";
};
};
functionToLua = {
testEmptyString = {
expr = luaUtils.functionToLua (_: "");
expected = "";
};
testSimpleString = {
expr = luaUtils.functionToLua (_: "test");
expected = "test";
};
testFunctionArgs = {
expr = luaUtils.functionToLua (lua: luaUtils == lua.luaUtils);
expected = true;
};
testInvalidInput = {
expr = luaUtils.functionToLua { };
expectedError.type = "AssertionError";
};
};
numberToLua = {
testInt = {
expr = luaUtils.numberToLua 42;
expected = "42";
};
testNegativeInt = {
expr = luaUtils.numberToLua (-1);
expected = "-1";
};
testFloat = {
expr = luaUtils.numberToLua 3.141;
expected = "3.141";
};
testNegativeFloat = {
expr = luaUtils.numberToLua (-3.141);
expected = "-3.141";
};
# Nix cannot properly represent double precision floats
# builtins.toJSON cutoff point seems to be longer than lua rounding precision
# TODO: Check for smallest unrounded 64bit float.
# (nix) builtins.toJSON 3.1415926535897932 => "3.141592653589793"
# (lua) print(3.1415926535897932) => 3.1415926535898
# https://github.com/NixOS/nix/issues/5733
testFloatCutoff = {
expr = luaUtils.numberToLua 3.141592653589793;
expected = "3.141592653589793";
};
testInvalidInput = {
expr = luaUtils.numberToLua { };
expectedError.type = "AssertionError";
};
};
listToLua = {
testEmptyList = {
expr = luaUtils.listToLua [ ];
expected = "{}";
};
testEmptyUnpackedList = {
expr = luaUtils.listToLua [ "__unpack" ];
expected = "";
};
};
setToLua = {
testEmptySet = {
expr = luaUtils.setToLua { };
expected = "{}";
};
testEmptyUnpackedSet = {
expr = luaUtils.setToLua { __unpack = true; };
expected = "";
};
testDerivation = {
expr = luaUtils.setToLua miniDrv;
expected = ''"/nix/store/x9sny7pilv8rdsvqlkf0skfqy30xpp2l-test"'';
};
};
toLua = {
testNull = {
expr = luaUtils.toLua null;
expected = "nil";
};
testBool = {
expr = luaUtils.toLua true;
expected = "true";
};
testString = {
expr = luaUtils.toLua "";
expected = ''""'';
};
testFunction = {
expr = luaUtils.toLua (_: "");
expected = "";
};
testPath = {
expr = luaUtils.toLua ./empty.txt;
expected = ''"/nix/store/sfmk8wid8fzwnvzcwvw8sgblj2xz4n58-empty.txt"'';
};
testInt = {
expr = luaUtils.toLua 42;
expected = "42";
};
testFloat = {
expr = luaUtils.toLua 3.141;
expected = "3.141";
};
testList = {
expr = luaUtils.toLua [ ];
expected = "{}";
};
testSet = {
expr = luaUtils.toLua { };
expected = "{}";
};
testDerivation = {
expr = luaUtils.setToLua miniDrv;
expected = ''"/nix/store/x9sny7pilv8rdsvqlkf0skfqy30xpp2l-test"'';
};
};
testWriteLuaFileDir = {
expr = luaUtils.writeLuaFileDir "lua/lazyWrapper/plugins/my_plugin" "return {}";
expected = {
name = "my_plugin";
destination = "/lua/lazyWrapper/plugins/my_plugin.lua";
text = "return {}";
# TODO: Check writeText function and remove duplicate '/' either in destination or checkPhase
checkPhase = ''
/bin/stylua $out//lua/lazyWrapper/plugins/my_plugin.lua
'';
};
};
}