lazy.nvim.nix/tests/luaUtils.nix

181 lines
4.5 KiB
Nix

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