diff options
author | Robert Hensing <robert@roberthensing.nl> | 2023-12-08 23:15:53 +0100 |
---|---|---|
committer | github-actions[bot] <github-actions[bot]@users.noreply.github.com> | 2023-12-11 12:26:16 +0000 |
commit | cd19e8941c2d71757d4176662fbd72b0c630d582 (patch) | |
tree | 22b1cc98c78ba1362e6a9971fd4996120192465c | |
parent | aa42f176f60e8fbd2ad6c17f817ae4edd9688f97 (diff) |
lib.attrsets.hasAttrByPath: Document law and laziness, and test it
(cherry picked from commit 7d993b9521aa8a229648a1c33e730df8b630670f)
-rw-r--r-- | lib/attrsets.nix | 7 | ||||
-rw-r--r-- | lib/tests/misc.nix | 5 |
2 files changed, 12 insertions, 0 deletions
diff --git a/lib/attrsets.nix b/lib/attrsets.nix index 798a53646d811..56a685ddcb447 100644 --- a/lib/attrsets.nix +++ b/lib/attrsets.nix @@ -43,12 +43,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 b97f080cca3a8..2884e880e13aa 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 = [ ]; |