From c33c5f8b19ddf2be589aa9ed06b1cab8340975ad Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Fri, 8 Nov 2024 13:22:58 +0100 Subject: [PATCH] Jsonschema: clean up tests --- lib/jsonschema/test_parseOption.nix | 470 +++++++++++++-------------- lib/jsonschema/test_parseOptions.nix | 37 +++ 2 files changed, 254 insertions(+), 253 deletions(-) diff --git a/lib/jsonschema/test_parseOption.nix b/lib/jsonschema/test_parseOption.nix index bf1b62ef0..896d47d89 100644 --- a/lib/jsonschema/test_parseOption.nix +++ b/lib/jsonschema/test_parseOption.nix @@ -209,42 +209,6 @@ in }; }; - testFreeFormOfInt = - let - default = { - foo = 1; - bar = 2; - }; - in - { - expr = slib.parseOptions (lib.evalModules { - modules = [ - { - freeformType = with lib.types; attrsOf int; - options = { - enable = lib.mkEnableOption "enable this"; - }; - } - default - ]; - }).options { }; - expected = { - "$schema" = "http://json-schema.org/draft-07/schema#"; - additionalProperties = { - type = "integer"; - }; - properties = { - enable = { - default = false; - description = "Whether to enable enable this."; - examples = [ true ]; - type = "boolean"; - }; - }; - type = "object"; - }; - }; - testLazyAttrsOfInt = let default = { @@ -264,230 +228,230 @@ in }; }; - # testNullOrBool = - # let - # default = null; # null is a valid value for this type - # in - # { - # expr = slib.parseOption (evalType (lib.types.nullOr lib.types.bool) default); - # expected = { - # oneOf = [ - # { type = "null"; } - # { type = "boolean"; } - # ]; - # inherit default description; - # }; - # }; + testNullOrBool = + let + default = null; # null is a valid value for this type + in + { + expr = slib.parseOption (evalType (lib.types.nullOr lib.types.bool) default); + expected = { + oneOf = [ + { type = "null"; } + { type = "boolean"; } + ]; + inherit default description; + }; + }; - # testNullOrNullOr = - # let - # default = null; # null is a valid value for this type - # in - # { - # expr = slib.parseOption (evalType (lib.types.nullOr (lib.types.nullOr lib.types.bool)) default); - # expected = { - # oneOf = [ - # { type = "null"; } - # { - # oneOf = [ - # { type = "null"; } - # { type = "boolean"; } - # ]; - # } - # ]; - # inherit default description; - # }; - # }; + testNullOrNullOr = + let + default = null; # null is a valid value for this type + in + { + expr = slib.parseOption (evalType (lib.types.nullOr (lib.types.nullOr lib.types.bool)) default); + expected = { + oneOf = [ + { type = "null"; } + { + oneOf = [ + { type = "null"; } + { type = "boolean"; } + ]; + } + ]; + inherit default description; + }; + }; - # testSubmoduleOption = - # let - # subModule = { - # options.opt = lib.mkOption { - # type = lib.types.bool; - # default = true; - # inherit description; - # }; - # }; - # in - # { - # expr = slib.parseOption (evalType (lib.types.submodule subModule) { }); - # expected = { - # type = "object"; - # additionalProperties = false; - # description = "Test Description"; - # properties = { - # opt = { - # type = "boolean"; - # default = true; - # inherit description; - # }; - # }; - # }; - # }; + testSubmoduleOption = + let + subModule = { + options.opt = lib.mkOption { + type = lib.types.bool; + default = true; + inherit description; + }; + }; + in + { + expr = slib.parseOption (evalType (lib.types.submodule subModule) { }); + expected = { + type = "object"; + additionalProperties = false; + description = "Test Description"; + properties = { + opt = { + type = "boolean"; + default = true; + inherit description; + }; + }; + }; + }; - # testSubmoduleOptionWithoutDefault = - # let - # subModule = { - # options.opt = lib.mkOption { - # type = lib.types.bool; - # inherit description; - # }; - # }; - # in - # { - # expr = slib.parseOption (evalType (lib.types.submodule subModule) { }); - # expected = { - # type = "object"; - # additionalProperties = false; - # description = "Test Description"; - # properties = { - # opt = { - # type = "boolean"; - # inherit description; - # }; - # }; - # required = [ "opt" ]; - # }; - # }; + testSubmoduleOptionWithoutDefault = + let + subModule = { + options.opt = lib.mkOption { + type = lib.types.bool; + inherit description; + }; + }; + in + { + expr = slib.parseOption (evalType (lib.types.submodule subModule) { }); + expected = { + type = "object"; + additionalProperties = false; + description = "Test Description"; + properties = { + opt = { + type = "boolean"; + inherit description; + }; + }; + required = [ "opt" ]; + }; + }; - # testAttrsOfSubmodule = - # let - # subModule = { - # options.opt = lib.mkOption { - # type = lib.types.bool; - # default = true; - # inherit description; - # }; - # }; - # default = { - # foo.opt = false; - # bar.opt = true; - # }; - # in - # { - # expr = slib.parseOption (evalType (lib.types.attrsOf (lib.types.submodule subModule)) default); - # expected = { - # type = "object"; - # additionalProperties = { - # type = "object"; - # additionalProperties = false; - # properties = { - # opt = { - # type = "boolean"; - # default = true; - # inherit description; - # }; - # }; - # }; - # inherit default description; - # }; - # }; + testAttrsOfSubmodule = + let + subModule = { + options.opt = lib.mkOption { + type = lib.types.bool; + default = true; + inherit description; + }; + }; + default = { + foo.opt = false; + bar.opt = true; + }; + in + { + expr = slib.parseOption (evalType (lib.types.attrsOf (lib.types.submodule subModule)) default); + expected = { + type = "object"; + additionalProperties = { + type = "object"; + additionalProperties = false; + properties = { + opt = { + type = "boolean"; + default = true; + inherit description; + }; + }; + }; + inherit default description; + }; + }; - # testListOfSubmodule = - # let - # subModule = { - # options.opt = lib.mkOption { - # type = lib.types.bool; - # default = true; - # inherit description; - # }; - # }; - # default = [ - # { opt = false; } - # { opt = true; } - # ]; - # in - # { - # expr = slib.parseOption (evalType (lib.types.listOf (lib.types.submodule subModule)) default); - # expected = { - # type = "array"; - # items = { - # type = "object"; - # additionalProperties = false; - # properties = { - # opt = { - # type = "boolean"; - # default = true; - # inherit description; - # }; - # }; - # }; - # inherit default description; - # }; - # }; + testListOfSubmodule = + let + subModule = { + options.opt = lib.mkOption { + type = lib.types.bool; + default = true; + inherit description; + }; + }; + default = [ + { opt = false; } + { opt = true; } + ]; + in + { + expr = slib.parseOption (evalType (lib.types.listOf (lib.types.submodule subModule)) default); + expected = { + type = "array"; + items = { + type = "object"; + additionalProperties = false; + properties = { + opt = { + type = "boolean"; + default = true; + inherit description; + }; + }; + }; + inherit default description; + }; + }; - # testEither = - # let - # default = "foo"; - # in - # { - # expr = slib.parseOption (evalType (lib.types.either lib.types.bool lib.types.str) default); - # expected = { - # oneOf = [ - # { type = "boolean"; } - # { type = "string"; } - # ]; - # inherit default description; - # }; - # }; + testEither = + let + default = "foo"; + in + { + expr = slib.parseOption (evalType (lib.types.either lib.types.bool lib.types.str) default); + expected = { + oneOf = [ + { type = "boolean"; } + { type = "string"; } + ]; + inherit default description; + }; + }; - # testAnything = - # let - # default = "foo"; - # in - # { - # expr = slib.parseOption (evalType lib.types.anything default); - # expected = { - # inherit default description; - # type = [ - # "boolean" - # "integer" - # "number" - # "string" - # "array" - # "object" - # "null" - # ]; - # }; - # }; + testAnything = + let + default = "foo"; + in + { + expr = slib.parseOption (evalType lib.types.anything default); + expected = { + inherit default description; + type = [ + "boolean" + "integer" + "number" + "string" + "array" + "object" + "null" + ]; + }; + }; - # testUnspecified = - # let - # default = "foo"; - # in - # { - # expr = slib.parseOption (evalType lib.types.unspecified default); - # expected = { - # inherit default description; - # type = [ - # "boolean" - # "integer" - # "number" - # "string" - # "array" - # "object" - # "null" - # ]; - # }; - # }; + testUnspecified = + let + default = "foo"; + in + { + expr = slib.parseOption (evalType lib.types.unspecified default); + expected = { + inherit default description; + type = [ + "boolean" + "integer" + "number" + "string" + "array" + "object" + "null" + ]; + }; + }; - # testRaw = - # let - # default = "foo"; - # in - # { - # expr = slib.parseOption (evalType lib.types.raw default); - # expected = { - # inherit default description; - # type = [ - # "boolean" - # "integer" - # "number" - # "string" - # "array" - # "object" - # "null" - # ]; - # }; - # }; + testRaw = + let + default = "foo"; + in + { + expr = slib.parseOption (evalType lib.types.raw default); + expected = { + inherit default description; + type = [ + "boolean" + "integer" + "number" + "string" + "array" + "object" + "null" + ]; + }; + }; } diff --git a/lib/jsonschema/test_parseOptions.nix b/lib/jsonschema/test_parseOptions.nix index f637999a3..fe5d2f169 100644 --- a/lib/jsonschema/test_parseOptions.nix +++ b/lib/jsonschema/test_parseOptions.nix @@ -36,4 +36,41 @@ type = "object"; }; }; + + testFreeFormOfInt = + let + default = { + foo = 1; + bar = 2; + }; + in + { + expr = slib.parseOptions (lib.evalModules { + modules = [ + { + freeformType = with lib.types; attrsOf int; + options = { + enable = lib.mkEnableOption "enable this"; + }; + } + default + ]; + }).options { }; + expected = { + "$schema" = "http://json-schema.org/draft-07/schema#"; + additionalProperties = { + type = "integer"; + }; + properties = { + enable = { + default = false; + description = "Whether to enable enable this."; + examples = [ true ]; + type = "boolean"; + }; + }; + type = "object"; + }; + }; + }