diff options
author | Oliver Schmidt <os@flyingcircus.io> | 2023-09-10 17:56:29 +0200 |
---|---|---|
committer | Oliver Schmidt <os@flyingcircus.io> | 2023-10-10 16:11:01 +0200 |
commit | d70633f91cb27d9314940d3a6e9385f89bf7f007 (patch) | |
tree | 7cefb07ef73c3912105e7cb1d2bdf8d96446156a /lib/tests/misc.nix | |
parent | 27eedb56010219cd7f779e87b9a00c7504f76125 (diff) | |
download | nixlib-d70633f91cb27d9314940d3a6e9385f89bf7f007.tar nixlib-d70633f91cb27d9314940d3a6e9385f89bf7f007.tar.gz nixlib-d70633f91cb27d9314940d3a6e9385f89bf7f007.tar.bz2 nixlib-d70633f91cb27d9314940d3a6e9385f89bf7f007.tar.lz nixlib-d70633f91cb27d9314940d3a6e9385f89bf7f007.tar.xz nixlib-d70633f91cb27d9314940d3a6e9385f89bf7f007.tar.zst nixlib-d70633f91cb27d9314940d3a6e9385f89bf7f007.zip |
lib.attrsets.attrsToList: add function
For transforming back between lists and attrsets, it makes sense to have a quasi-inverse of `builtins.listToAttrs` available as a library function. Co-authored-by: Silvan Mosberger <github@infinisil.com> Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
Diffstat (limited to 'lib/tests/misc.nix')
-rw-r--r-- | lib/tests/misc.nix | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index 6d55ae684771..702be9529b4e 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -20,6 +20,10 @@ let expr = (builtins.tryEval (builtins.seq expr "didn't throw")); expected = { success = false; value = false; }; }; + testingEval = expr: { + expr = (builtins.tryEval expr).success; + expected = true; + }; testingDeepThrow = expr: testingThrow (builtins.deepSeq expr expr); testSanitizeDerivationName = { name, expected }: @@ -784,6 +788,26 @@ runTests { expected = { a = 1; b = 2; }; }; + testListAttrsReverse = let + exampleAttrs = {foo=1; bar="asdf"; baz = [1 3 3 7]; fnord=null;}; + exampleSingletonList = [{name="foo"; value=1;}]; + in { + expr = { + isReverseToListToAttrs = builtins.listToAttrs (attrsToList exampleAttrs) == exampleAttrs; + isReverseToAttrsToList = attrsToList (builtins.listToAttrs exampleSingletonList) == exampleSingletonList; + testDuplicatePruningBehaviour = attrsToList (builtins.listToAttrs [{name="a"; value=2;} {name="a"; value=1;}]); + }; + expected = { + isReverseToAttrsToList = true; + isReverseToListToAttrs = true; + testDuplicatePruningBehaviour = [{name="a"; value=2;}]; + }; + }; + + testAttrsToListsCanDealWithFunctions = testingEval ( + attrsToList { someFunc= a: a + 1;} + ); + # GENERATORS # these tests assume attributes are converted to lists # in alphabetical order |