diff options
Diffstat (limited to 'lib/tests')
-rw-r--r-- | lib/tests/misc.nix | 59 | ||||
-rwxr-xr-x | lib/tests/modules.sh | 5 | ||||
-rw-r--r-- | lib/tests/modules/gvariant.nix | 93 | ||||
-rw-r--r-- | lib/tests/modules/options-type-error-configuration.nix | 6 | ||||
-rw-r--r-- | lib/tests/modules/options-type-error-typical-nested.nix | 5 | ||||
-rw-r--r-- | lib/tests/modules/options-type-error-typical.nix | 5 |
6 files changed, 173 insertions, 0 deletions
diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index 887ea39a080df..6d55ae684771e 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -349,6 +349,27 @@ runTests { expected = true; }; + testRemovePrefixExample1 = { + expr = removePrefix "foo." "foo.bar.baz"; + expected = "bar.baz"; + }; + testRemovePrefixExample2 = { + expr = removePrefix "xxx" "foo.bar.baz"; + expected = "foo.bar.baz"; + }; + testRemovePrefixEmptyPrefix = { + expr = removePrefix "" "foo"; + expected = "foo"; + }; + testRemovePrefixEmptyString = { + expr = removePrefix "foo" ""; + expected = ""; + }; + testRemovePrefixEmptyBoth = { + expr = removePrefix "" ""; + expected = ""; + }; + testNormalizePath = { expr = strings.normalizePath "//a/b//c////d/"; expected = "/a/b/c/d/"; @@ -492,6 +513,44 @@ runTests { ([ 1 2 3 ] == (take 4 [ 1 2 3 ])) ]; + testListHasPrefixExample1 = { + expr = lists.hasPrefix [ 1 2 ] [ 1 2 3 4 ]; + expected = true; + }; + testListHasPrefixExample2 = { + expr = lists.hasPrefix [ 0 1 ] [ 1 2 3 4 ]; + expected = false; + }; + testListHasPrefixLazy = { + expr = lists.hasPrefix [ 1 ] [ 1 (abort "lib.lists.hasPrefix is not lazy") ]; + expected = true; + }; + testListHasPrefixEmptyPrefix = { + expr = lists.hasPrefix [ ] [ 1 2 ]; + expected = true; + }; + testListHasPrefixEmptyList = { + expr = lists.hasPrefix [ 1 2 ] [ ]; + expected = false; + }; + + testListRemovePrefixExample1 = { + expr = lists.removePrefix [ 1 2 ] [ 1 2 3 4 ]; + expected = [ 3 4 ]; + }; + testListRemovePrefixExample2 = { + expr = (builtins.tryEval (lists.removePrefix [ 0 1 ] [ 1 2 3 4 ])).success; + expected = false; + }; + testListRemovePrefixEmptyPrefix = { + expr = lists.removePrefix [ ] [ 1 2 ]; + expected = [ 1 2 ]; + }; + testListRemovePrefixEmptyList = { + expr = (builtins.tryEval (lists.removePrefix [ 1 2 ] [ ])).success; + expected = false; + }; + testFoldAttrs = { expr = foldAttrs (n: a: [n] ++ a) [] [ { a = 2; b = 7; } diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh index b933a24a57a12..2c5e4cdbcec10 100755 --- a/lib/tests/modules.sh +++ b/lib/tests/modules.sh @@ -393,6 +393,11 @@ checkConfigError \ config.set \ ./declare-set.nix ./declare-enable-nested.nix +# Options: accidental use of an option-type instead of option (or other tagged type; unlikely) +checkConfigError 'In module .*/options-type-error-typical.nix: expected an option declaration at option path .result. but got an attribute set with type option-type' config.result ./options-type-error-typical.nix +checkConfigError 'In module .*/options-type-error-typical-nested.nix: expected an option declaration at option path .result.here. but got an attribute set with type option-type' config.result.here ./options-type-error-typical-nested.nix +checkConfigError 'In module .*/options-type-error-configuration.nix: expected an option declaration at option path .result. but got an attribute set with type configuration' config.result ./options-type-error-configuration.nix + # Check that that merging of option collisions doesn't depend on type being set checkConfigError 'The option .group..*would be a parent of the following options, but its type .<no description>. does not support nested options.\n\s*- option.s. with prefix .group.enable..*' config.group.enable ./merge-typeless-option.nix diff --git a/lib/tests/modules/gvariant.nix b/lib/tests/modules/gvariant.nix new file mode 100644 index 0000000000000..a792ebf85b743 --- /dev/null +++ b/lib/tests/modules/gvariant.nix @@ -0,0 +1,93 @@ +{ config, lib, ... }: + +let inherit (lib) concatStringsSep mapAttrsToList mkMerge mkOption types gvariant; +in { + options.examples = mkOption { type = types.attrsOf gvariant; }; + + config = { + examples = with gvariant; + mkMerge [ + { bool = true; } + { bool = true; } + + { float = 3.14; } + + { int32 = mkInt32 (- 42); } + { int32 = mkInt32 (- 42); } + + { uint32 = mkUint32 42; } + { uint32 = mkUint32 42; } + + { int16 = mkInt16 (-42); } + { int16 = mkInt16 (-42); } + + { uint16 = mkUint16 42; } + { uint16 = mkUint16 42; } + + { int64 = mkInt64 (-42); } + { int64 = mkInt64 (-42); } + + { uint64 = mkUint64 42; } + { uint64 = mkUint64 42; } + + { array1 = [ "one" ]; } + { array1 = mkArray [ "two" ]; } + { array2 = mkArray [ (mkInt32 1) ]; } + { array2 = mkArray [ (nkUint32 2) ]; } + + { emptyArray1 = [ ]; } + { emptyArray2 = mkEmptyArray type.uint32; } + + { string = "foo"; } + { string = "foo"; } + { + escapedString = '' + '\ + ''; + } + + { tuple = mkTuple [ (mkInt32 1) [ "foo" ] ]; } + + { maybe1 = mkNothing type.string; } + { maybe2 = mkJust (mkUint32 4); } + + { variant1 = mkVariant "foo"; } + { variant2 = mkVariant 42; } + + { dictionaryEntry = mkDictionaryEntry (mkInt32 1) [ "foo" ]; } + ]; + + assertions = [ + { + assertion = ( + let + mkLine = n: v: "${n} = ${toString (gvariant.mkValue v)}"; + result = concatStringsSep "\n" (mapAttrsToList mkLine config.examples); + in + result + "\n" + ) == '' + array1 = @as ['one','two'] + array2 = @au [1,2] + bool = true + dictionaryEntry = @{ias} {1,@as ['foo']} + emptyArray1 = @as [] + emptyArray2 = @au [] + escapedString = '\'\\\n' + float = 3.140000 + int = -42 + int16 = @n -42 + int64 = @x -42 + maybe1 = @ms nothing + maybe2 = just @u 4 + string = 'foo' + tuple = @(ias) (1,@as ['foo']) + uint16 = @q 42 + uint32 = @u 42 + uint64 = @t 42 + variant1 = @v <'foo'> + variant2 = @v <42> + ''; + } + ]; + }; +} diff --git a/lib/tests/modules/options-type-error-configuration.nix b/lib/tests/modules/options-type-error-configuration.nix new file mode 100644 index 0000000000000..bcd6db89487a3 --- /dev/null +++ b/lib/tests/modules/options-type-error-configuration.nix @@ -0,0 +1,6 @@ +{ lib, ... }: { + options = { + # unlikely mistake, but we can catch any attrset with _type + result = lib.evalModules { modules = []; }; + }; +} diff --git a/lib/tests/modules/options-type-error-typical-nested.nix b/lib/tests/modules/options-type-error-typical-nested.nix new file mode 100644 index 0000000000000..2c07e19fb8ae0 --- /dev/null +++ b/lib/tests/modules/options-type-error-typical-nested.nix @@ -0,0 +1,5 @@ +{ lib, ... }: { + options = { + result.here = lib.types.str; + }; +} diff --git a/lib/tests/modules/options-type-error-typical.nix b/lib/tests/modules/options-type-error-typical.nix new file mode 100644 index 0000000000000..416f436e0ad70 --- /dev/null +++ b/lib/tests/modules/options-type-error-typical.nix @@ -0,0 +1,5 @@ +{ lib, ... }: { + options = { + result = lib.types.str; + }; +} |