lib.gvariant: make the tests really work

This commit is contained in:
linsui
2023-09-23 02:40:03 +08:00
committed by Jan Tojnar
parent 8fe5ccd12e
commit a50cea84be
2 changed files with 57 additions and 86 deletions

View File

@@ -91,6 +91,9 @@ checkConfigOutput '^true$' config.result ./test-mergeAttrDefinitionsWithPrio.nix
# is the option. # is the option.
checkConfigOutput '^true$' config.result ./module-argument-default.nix checkConfigOutput '^true$' config.result ./module-argument-default.nix
# gvariant
checkConfigOutput '^true$' config.assertion ./gvariant.nix
# types.pathInStore # types.pathInStore
checkConfigOutput '".*/store/0lz9p8xhf89kb1c1kk6jxrzskaiygnlh-bash-5.2-p15.drv"' config.pathInStore.ok1 ./types.nix checkConfigOutput '".*/store/0lz9p8xhf89kb1c1kk6jxrzskaiygnlh-bash-5.2-p15.drv"' config.pathInStore.ok1 ./types.nix
checkConfigOutput '".*/store/0fb3ykw9r5hpayd05sr0cizwadzq1d8q-bash-5.2-p15"' config.pathInStore.ok2 ./types.nix checkConfigOutput '".*/store/0fb3ykw9r5hpayd05sr0cizwadzq1d8q-bash-5.2-p15"' config.pathInStore.ok2 ./types.nix

View File

@@ -1,93 +1,61 @@
{ config, lib, ... }: { config, lib, ... }:
let inherit (lib) concatStringsSep mapAttrsToList mkMerge mkOption types gvariant; {
in { options = {
options.examples = mkOption { type = types.attrsOf gvariant; }; examples = lib.mkOption { type = lib.types.attrs; };
assertion = lib.mkOption { type = lib.types.bool; };
};
config = { config = {
examples = with gvariant; examples = with lib.gvariant; {
mkMerge [ bool = true;
{ bool = true; } float = 3.14;
{ bool = true; } int32 = mkInt32 (- 42);
uint32 = mkUint32 42;
int16 = mkInt16 (-42);
uint16 = mkUint16 42;
int64 = mkInt64 (-42);
uint64 = mkUint64 42;
array1 = [ "one" ];
array2 = mkArray [ (mkInt32 1) ];
array3 = mkArray [ (mkUint32 2) ];
emptyArray = mkEmptyArray type.uint32;
string = "foo";
escapedString = ''
'\
'';
tuple = mkTuple [ (mkInt32 1) [ "foo" ] ];
maybe1 = mkNothing type.string;
maybe2 = mkJust (mkUint32 4);
variant = mkVariant "foo";
dictionaryEntry = mkDictionaryEntry (mkInt32 1) [ "foo" ];
};
{ float = 3.14; } assertion =
let
{ int32 = mkInt32 (- 42); } mkLine = n: v: "${n} = ${toString (lib.gvariant.mkValue v)}";
{ int32 = mkInt32 (- 42); } result = lib.concatStringsSep "\n" (lib.mapAttrsToList mkLine config.examples);
in
{ uint32 = mkUint32 42; } (result + "\n") == ''
{ uint32 = mkUint32 42; } array1 = @as ['one']
array2 = @ai [1]
{ int16 = mkInt16 (-42); } array3 = @au [@u 2]
{ int16 = mkInt16 (-42); } bool = true
dictionaryEntry = @{ias} {1,@as ['foo']}
{ uint16 = mkUint16 42; } emptyArray = @au []
{ uint16 = mkUint16 42; } escapedString = '\'\\\n'
float = 3.140000
{ int64 = mkInt64 (-42); } int16 = @n -42
{ int64 = mkInt64 (-42); } int32 = -42
int64 = @x -42
{ uint64 = mkUint64 42; } maybe1 = @ms nothing
{ uint64 = mkUint64 42; } maybe2 = just @u 4
string = 'foo'
{ array1 = [ "one" ]; } tuple = @(ias) (1,@as ['foo'])
{ array1 = mkArray [ "two" ]; } uint16 = @q 42
{ array2 = mkArray [ (mkInt32 1) ]; } uint32 = @u 42
{ array2 = mkArray [ (nkUint32 2) ]; } uint64 = @t 42
variant = <'foo'>
{ 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>
'';
}
];
}; };
} }