Something went wrong. Try again.
forked from nix-community/nixvim
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790# For shorter test iterations run the following in the root of the repo:# `echo ':b checks.${builtins.currentSystem}.lib-tests' | nix repl .`{ helpers, lib, pkgs, runCommandLocal, writeText,}:let luaNames = { # Keywords in lua 5.1 keywords = [ "and" "break" "do" "else" "elseif" "end" "false" "for" "function" "if" "in" "local" "nil" "not" "or" "repeat" "return" "then" "true" "until" "while" ]; identifiers = [ "validIdentifier" "valid_identifier" "_also_valid_" "_weirdNameFMT" ]; other = [ "1_starts_with_digit" "01234" "12340" "kebab-case" ]; };
drv = writeText "example-derivation" "hello, world!";
optionalSetupInlinePlugin = { lib, ... }: lib.nixvim.plugins.mkNeovimPlugin { name = "fake"; moduleName = "fake"; package = [ "vimPlugins" "vim-repeat" ]; maintainers = [ ]; callSetup = "optional";
settingsOptions = { foo = lib.mkOption { type = lib.types.bool; default = false; }; }; };
alwaysSetupInlinePlugin = { lib, ... }: lib.nixvim.plugins.mkNeovimPlugin { name = "fake"; moduleName = "fake"; package = [ "vimPlugins" "vim-repeat" ]; maintainers = [ ]; callSetup = true;
settingsOptions = { foo = lib.mkOption { type = lib.types.bool; default = false; }; }; };
neverSetupInlinePlugin = { lib, ... }: lib.nixvim.plugins.mkNeovimPlugin { name = "fake"; moduleName = "fake"; package = [ "vimPlugins" "vim-repeat" ]; maintainers = [ ]; callSetup = false;
settingsOptions = { foo = lib.mkOption { type = lib.types.bool; default = false; }; }; };
noLuaConfigInlinePlugin = { lib, ... }: lib.nixvim.plugins.mkNeovimPlugin { name = "fake"; package = [ "vimPlugins" "vim-repeat" ]; maintainers = [ ]; callSetup = false; hasLuaConfig = false; hasSettings = false; };
inlinePluginEval = plugin: module: lib.nixvim.modules.evalNixvim { modules = [ { _module.args.pkgs = lib.mkForce pkgs; } plugin module ]; };
inlinePluginConfig = plugin: module: (inlinePluginEval plugin module).config.content;
optionalSetupConfig = inlinePluginConfig optionalSetupInlinePlugin; alwaysSetupConfig = inlinePluginConfig alwaysSetupInlinePlugin; neverSetupConfig = inlinePluginConfig neverSetupInlinePlugin;
results = lib.runTests { testToLuaObject = { expr = lib.nixvim.toLuaObject { foo = "bar"; qux = [ 1 2 3 ]; }; expected = ''{ foo = "bar", qux = { 1, 2, 3 } }''; };
testApplyPrefixToAttrs = { expr = lib.nixvim.applyPrefixToAttrs "prefix_" { foo = 1; bar = 2; }; expected = { prefix_foo = 1; prefix_bar = 2; }; };
testToLuaObjectRawLua = { expr = lib.nixvim.toLuaObject { __raw = "<lua code>"; }; expected = "<lua code>"; };
testToLuaObjectInlineLua = { expr = lib.nixvim.toLuaObject (lib.generators.mkLuaInline "<lua code>"); expected = "(<lua code>)"; };
testToLuaObjectInlineLuaNested = { expr = lib.nixvim.toLuaObject { lua = lib.generators.mkLuaInline "<lua code>"; }; expected = "{ lua = (<lua code>) }"; };
testToLuaObjectLuaTableMixingList = { expr = lib.nixvim.toLuaObject { "__unkeyed...." = "foo"; bar = "baz"; }; expected = ''{ "foo", bar = "baz" }''; };
testToLuaObjectNestedAttrs = { expr = lib.nixvim.toLuaObject { a = { b = 1; c = 2; d = { e = 3; }; }; }; expected = "{ a = { b = 1, c = 2, d = { e = 3 } } }"; };
testToLuaObjectNestedList = { expr = lib.nixvim.toLuaObject [ 1 2 [ 3 4 [ 5 6 ] ] 7 ]; expected = "{ 1, 2, { 3, 4, { 5, 6 } }, 7 }"; };
testToLuaObjectNonStringPrims = { expr = lib.nixvim.toLuaObject { a = 1.0; b = 2; c = true; d = false; e = null; }; expected = "{ a = 1.0, b = 2, c = true, d = false }"; };
testToLuaObjectNilPrim = { expr = lib.nixvim.toLuaObject null; expected = "nil"; };
testToLuaObjectStringPrim = { expr = lib.nixvim.toLuaObject '' foo\bar baz''; expected = ''"foo\\bar\nbaz"''; };
testToLuaObjectDerivation = { expr = lib.nixvim.toLuaObject drv; expected = ''"${drv}"''; };
testToLuaObjectDerivationNested = { expr = lib.nixvim.toLuaObject { a = drv; b = { c = drv; }; d = [ drv ]; e = [ { f = drv; } ]; }; expected = ''{ a = "${drv}", b = { c = "${drv}" }, d = { "${drv}" }, e = { { f = "${drv}" } } }''; };
testToLuaObjectFilters = { expr = lib.nixvim.toLuaObject { a = null; b = { }; c = [ ]; d = { e = null; f = { }; }; }; expected = "{ }"; };
testToLuaObjectAttrListFilters = { expr = lib.nixvim.toLuaObject { a = null; b = { }; c = [ ]; d = { e = null; f = { }; }; g = [ { x = null; y = null; z = null; } { x = { }; y = [ ]; z = null; } ]; __unkeyed-1 = [ { x = null; y = null; z = null; } { x = { }; y = [ ]; z = null; } [ { x = null; y = null; z = null; } { x = { }; y = [ ]; z = null; } ] ]; }; expected = "{ { { }, { }, { { }, { } } }, g = { { }, { } } }"; };
testToLuaObjectEmptyTable = { expr = lib.nixvim.toLuaObject { a = null; b = { }; c = { __empty = null; }; d = { e = null; f = { }; g = helpers.emptyTable; }; }; expected = "{ c = { }, d = { g = { } } }"; };
testToLuaObjectEmptyListEntries = { expr = helpers.lua.toLua' { removeEmptyListEntries = true; } [ { } [ ] [ { } ] null 1 2 [ null 3 4 { } [ ] [ { } ] ] 5 ]; expected = "{ nil, 1, 2, { nil, 3, 4 }, 5 }"; };
testToLuaObjectNullListEntries = { expr = helpers.lua.toLua' { removeNullListEntries = true; } [ null 1 2 [ null 3 4 [ null 5 6 ] ] 7 ]; expected = "{ 1, 2, { 3, 4, { 5, 6 } }, 7 }"; };
testIsLuaKeyword = { expr = builtins.mapAttrs (_: builtins.filter helpers.lua.isKeyword) luaNames; expected = { keywords = [ "and" "break" "do" "else" "elseif" "end" "false" "for" "function" "if" "in" "local" "nil" "not" "or" "repeat" "return" "then" "true" "until" "while" ]; identifiers = [ ]; other = [ ]; }; };
testIsLuaIdentifier = { expr = builtins.mapAttrs (_: builtins.filter helpers.lua.isIdentifier) luaNames; expected = { keywords = [ ]; identifiers = [ "validIdentifier" "valid_identifier" "_also_valid_" "_weirdNameFMT" ]; other = [ ]; }; };
testLiteralLua = { expr = builtins.mapAttrs (_: helpers.literalLua) { print = "print('hi')"; nil = "nil"; table = "{}"; }; expected = builtins.mapAttrs (_: lib.literalExpression) { print = ''lib.nixvim.mkRaw "print('hi')"''; nil = ''lib.nixvim.mkRaw "nil"''; table = ''lib.nixvim.mkRaw "{}"''; }; };
# Integration test for nestedLiteral and renderOptionValue testNestedLiteral_withRenderOptionValue = { expr = builtins.mapAttrs ( _: v: (lib.options.renderOptionValue { literal = helpers.nestedLiteral v; }).text ) { empty = ""; sum = "1 + 1"; print = ''lib.mkRaw "print('hi')"''; }; expected = builtins.mapAttrs (_: literal: '' { literal = ${literal}; }'') { empty = ""; sum = "1 + 1"; print = ''lib.mkRaw "print('hi')"''; }; };
testUpperFirstChar = { expr = map helpers.upperFirstChar [ "foo" " foo" "foo bar" "UPPER" "mIxEd" ]; expected = [ "Foo" " foo" "Foo bar" "UPPER" "MIxEd" ]; };
testGroupListBySize = { expr = { empty = helpers.groupListBySize 5 [ ]; "5/5" = helpers.groupListBySize 5 (lib.genList lib.id 5); "13/5" = helpers.groupListBySize 5 (lib.genList lib.id 13); }; expected = { empty = [ ]; "5/5" = [ [ 0 1 2 3 4 ] ]; "13/5" = [ [ 0 1 2 3 4 ] [ 5 6 7 8 9 ] [ 10 11 12 ] ]; }; };
testEscapeStringForLua = { expr = lib.mapAttrs (_: helpers.utils.toLuaLongLiteral) { simple = "simple"; depth-one = " ]] "; depth-two = " ]] ]=] "; }; expected = { simple = "[[simple]]"; depth-one = "[=[ ]] ]=]"; depth-two = "[==[ ]] ]=] ]==]"; }; };
testEscapeStringForVimscript = { expr = lib.mapAttrs (_: helpers.utils.toVimscriptHeredoc) { simple = "simple"; depth-one = "EOF"; depth-two = "EOF EOFF"; }; expected = { simple = '' << EOF simple EOF''; depth-one = '' << EOFF EOF EOFF''; depth-two = '' << EOFFF EOF EOFF EOFFF''; }; };
testMkLuaConfig = { expr = lib.mapAttrs (_: loc: helpers.plugins.utils.mkConfigAt loc "Hello!") { "simple string" = "foo"; "simple list" = [ "foo" "bar" ]; "mkBefore string" = lib.mkBefore "foo"; "mkBefore list" = lib.mkBefore [ "foo" "bar" ]; "mkAfter string" = lib.mkAfter "foo"; "mkAfter list" = lib.mkAfter [ "foo" "bar" ]; }; expected = { "simple string".foo = "Hello!"; "simple list".foo.bar = "Hello!"; "mkBefore string".foo = lib.mkBefore "Hello!"; "mkBefore list".foo.bar = lib.mkBefore "Hello!"; "mkAfter string".foo = lib.mkAfter "Hello!"; "mkAfter list".foo.bar = lib.mkAfter "Hello!"; }; };
testMkAssertions = { expr = (lib.nixvim.mkAssertions "foo-bar" [ { assertion = true; message = "This is the message"; } { assertion = false; message = "Another assertion"; } ]) ++ (lib.nixvim.mkAssertions "single-element") { assertion = true; message = " Trailing whitespaces "; }; expected = [ { assertion = true; message = "Nixvim (foo-bar): This is the message"; } { assertion = false; message = "Nixvim (foo-bar): Another assertion"; } { assertion = true; message = "Nixvim (single-element): Trailing whitespaces"; } ]; };
testMkWarnings = { expr = (lib.nixvim.mkWarnings "foo-bar" [ { when = true; message = "This is the message"; } { when = false; message = "This should not be included"; } "This is an unconditional warning" ]) ++ (lib.nixvim.mkWarnings "single-element") { when = true; message = " Trailing whitespaces "; }; expected = [ "Nixvim (foo-bar): This is the message" "Nixvim (foo-bar): This is an unconditional warning" "Nixvim (single-element): Trailing whitespaces" ]; };
buildNixvimWith_hasExpectedArgs = { expr = lib.functionArgs lib.nixvim.modules.buildNixvimWith; expected = lib.functionArgs lib.nixvim.modules.evalNixvim; };
testNixvimWith_hasExpectedArgs = { expr = lib.functionArgs lib.nixvim.modules.testNixvimWith; expected = lib.functionArgs lib.nixvim.modules.evalNixvim; };
testMkNeovimPluginOptionalSetupSkipsImplicitDefaults = { expr = lib.hasInfix "require('fake').setup(" (optionalSetupConfig { plugins.fake.enable = true; }); expected = false; };
testMkNeovimPluginCallSetupOptionForcesOptionalSetup = { expr = let content = optionalSetupConfig { plugins.fake = { enable = true; callSetup = true; }; }; in lib.hasInfix "require('fake').setup(" content; expected = true; };
testMkNeovimPluginCallSetupOptionDisablesDefaultSetup = { expr = let content = alwaysSetupConfig { plugins.fake = { enable = true; callSetup = false; }; }; in lib.hasInfix "require('fake').setup(" content; expected = false; };
testMkNeovimPluginCallSetupOptionForcesDisabledSetup = { expr = let content = neverSetupConfig { plugins.fake = { enable = true; callSetup = true; }; }; in lib.hasInfix "require('fake').setup(" content; expected = true; };
testMkNeovimPluginCallSetupOptionPreservesDisabledSetup = { expr = let content = neverSetupConfig { plugins.fake = { enable = true; callSetup = false; }; }; in lib.hasInfix "require('fake').setup(" content; expected = false; };
testMkNeovimPluginCallSetupOptionDefaultsToDisabledSetup = { expr = let content = neverSetupConfig { plugins.fake.enable = true; }; in lib.hasInfix "require('fake').setup(" content; expected = false; };
testMkNeovimPluginWithoutLuaConfigOmitsCallSetupOption = { expr = lib.hasAttrByPath [ "plugins" "fake" "callSetup" ] (inlinePluginEval noLuaConfigInlinePlugin { }).options; expected = false; };
testMkNeovimPluginOptionalSetupRunsForExplicitEmptySettings = { expr = let content = optionalSetupConfig { plugins.fake.enable = true; plugins.fake.settings = { }; }; in { hasSetup = lib.hasInfix "require('fake').setup(" content; hasDefaults = lib.hasInfix "foo = false" content; }; expected = { hasSetup = true; hasDefaults = true; }; };
testMkNeovimPluginOptionalSetupRunsForExplicitSettingsValues = { expr = let content = optionalSetupConfig { plugins.fake.enable = true; plugins.fake.settings.foo = true; }; in { hasSetup = lib.hasInfix "require('fake').setup(" content; hasValue = lib.hasInfix "foo = true" content; }; expected = { hasSetup = true; hasValue = true; }; }; };inif results == [ ] then runCommandLocal "lib-tests-success" { } "touch $out"else runCommandLocal "lib-tests-failure" { results = lib.concatStringsSep "\n" ( map (result: '' ${result.name}: expected: ${lib.generators.toPretty { } result.expected} result: ${lib.generators.toPretty { } result.result} '') results ); } '' echo -e "Tests failed:\\n\\n$results" >&2 exit 1 ''