From 7d993b9521aa8a229648a1c33e730df8b630670f Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 8 Dec 2023 23:15:53 +0100 Subject: [PATCH] lib.attrsets.hasAttrByPath: Document law and laziness, and test it --- lib/attrsets.nix | 7 +++++++ lib/tests/misc.nix | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/lib/attrsets.nix b/lib/attrsets.nix index 4a10728da02f..9b4a8684f9b7 100644 --- a/lib/attrsets.nix +++ b/lib/attrsets.nix @@ -51,12 +51,19 @@ rec { /* Return if an attribute from nested attribute set exists. + **Laws**: + 1. ```nix + hasAttrByPath [] x == true + ``` + Example: x = { a = { b = 3; }; } hasAttrByPath ["a" "b"] x => true hasAttrByPath ["z" "z"] x => false + hasAttrByPath [] (throw "no need") + => true Type: hasAttrByPath :: [String] -> AttrSet -> Bool diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index b97f080cca3a..2884e880e13a 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -697,6 +697,11 @@ runTests { expected = false; }; + testHasAttrByPathNonStrict = { + expr = hasAttrByPath [] (throw "do not use"); + expected = true; + }; + testLongestValidPathPrefix_empty_empty = { expr = attrsets.longestValidPathPrefix [ ] { }; expected = [ ];