# tests for the nixos options to jsonschema converter # run these tests via `nix-unit ./test.nix` { lib ? (import { }).lib, slib ? (import ./. { inherit lib; } { }), }: { testParseOptions = { expr = slib.parseModule ./example-interface.nix; expected = builtins.fromJSON (builtins.readFile ./example-schema.json); }; testParseNestedOptions = let evaled = lib.evalModules { modules = [ { options.foo.bar = lib.mkOption { type = lib.types.bool; }; options.foo.baz = lib.mkOption { type = lib.types.bool; default = false; }; } ]; }; in { expr = slib.parseOptions evaled.options { }; expected = { "$schema" = "http://json-schema.org/draft-07/schema#"; "$exportedModuleInfo" = { path = [ ]; }; additionalProperties = false; properties = { foo = { "$exportedModuleInfo" = { path = [ "foo" ]; }; additionalProperties = false; properties = { bar = { "$exportedModuleInfo" = { path = [ "foo" "bar" ]; }; type = "boolean"; }; baz = { "$exportedModuleInfo" = { path = [ "foo" "baz" ]; }; type = "boolean"; default = false; }; }; required = [ "bar" ]; type = "object"; }; }; type = "object"; required = [ "foo" ]; }; }; 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#"; "$exportedModuleInfo" = { path = [ ]; }; additionalProperties = { "$exportedModuleInfo" = { path = [ ]; }; type = "integer"; }; properties = { enable = { "$exportedModuleInfo" = { path = [ "enable" ]; }; default = false; description = "Whether to enable enable this."; examples = [ true ]; type = "boolean"; }; }; type = "object"; }; }; }