lib.attrsets.hasAttrByPath: Document law and laziness, and test it

This commit is contained in:
Robert Hensing 2023-12-08 23:15:53 +01:00
parent 72bd4bbb58
commit 7d993b9521
2 changed files with 12 additions and 0 deletions

View File

@ -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

View File

@ -697,6 +697,11 @@ runTests {
expected = false;
};
testHasAttrByPathNonStrict = {
expr = hasAttrByPath [] (throw "do not use");
expected = true;
};
testLongestValidPathPrefix_empty_empty = {
expr = attrsets.longestValidPathPrefix [ ] { };
expected = [ ];