diff --git a/.gitignore b/.gitignore index 4c1287f..0a02885 100644 --- a/.gitignore +++ b/.gitignore @@ -197,4 +197,5 @@ docsrc/tools/FSharp.Formatting.svclog dist /tests/**/js /tests/**/py -/.venv \ No newline at end of file +/.venv +/node_modules diff --git a/DynamicObj.sln b/DynamicObj.sln index ec973df..fe490ab 100644 --- a/DynamicObj.sln +++ b/DynamicObj.sln @@ -53,6 +53,8 @@ Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "DynamicObject.Tests", "test EndProject Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "DynamicObject.Immutable.Tests", "tests\DynamicObject.Immutable.Tests\DynamicObject.Immutable.Tests.fsproj", "{0F6A539F-82D2-4BDC-8BF0-F2D261873B92}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E0867002-4410-4E5F-BE71-46ABBE93ED07}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -88,8 +90,10 @@ Global HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution + {B8BF1554-AAC3-434E-9502-FC83B43F3704} = {E0867002-4410-4E5F-BE71-46ABBE93ED07} {D62D0901-DB69-4C64-AC63-FBBBDCF6BC7D} = {988D804A-3A42-4E46-B233-B64F5C22524B} {C73AB951-91F2-4668-B2E0-B58298E5F664} = {39AA72A1-A628-481B-A2B5-94E2BD163061} + {5E7DAC28-7752-4209-B3BB-6DCE54C28AD8} = {E0867002-4410-4E5F-BE71-46ABBE93ED07} {39192F2D-164B-4905-A7D7-5C5B0FFCD2BB} = {988D804A-3A42-4E46-B233-B64F5C22524B} {0F6A539F-82D2-4BDC-8BF0-F2D261873B92} = {988D804A-3A42-4E46-B233-B64F5C22524B} EndGlobalSection diff --git a/build.cmd b/build.cmd index 57dd9f0..dffea34 100644 --- a/build.cmd +++ b/build.cmd @@ -1,5 +1,4 @@ @echo off -cls set PYTHONIOENCODING=utf-8 diff --git a/build/TestTasks.fs b/build/TestTasks.fs index 360f9fb..719b9ec 100644 --- a/build/TestTasks.fs +++ b/build/TestTasks.fs @@ -51,9 +51,17 @@ module RunTests = |> Seq.iter dotnetRun } -let runTests = BuildTask.create "RunTests" [clean; build; RunTests.runTestsJs; (*RunTests.runTestsJsNative; *)RunTests.runTestsPy;(*RunTests.runTestsPyNative; *)RunTests.runTestsDotnet] { - () -} +let runTests = BuildTask.createEmpty "RunTests" [ + clean; + build; + RunTests.runTestsPy; + (*RunTests.runTestsPyNative; *) + RunTests.runTestsJs; + (*RunTests.runTestsJsNative; *) + RunTests.runTestsDotnet + + ] + // to do: use this once we have actual tests diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..bea53d7 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,877 @@ +{ + "name": "DynamicObj", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "fable-library": "^1.1.1" + }, + "devDependencies": { + "mkdirp": "3.0.1", + "mocha": "^10.2.0" + } + }, + "node_modules/ansi-colors": { + "version": "4.1.3", + "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-4.1.3.tgz", + "integrity": "sha512-/6w/C21Pm1A7aZitlI5Ni/2J6FFQN8i1Cvz3kHABAAbw93v/NlvKdVOqz7CCWz/3iv/JplRSEEZ83XION15ovw==", + "dev": true, + "engines": { + "node": ">=6" + } + }, + "node_modules/ansi-regex": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz", + "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==", + "dev": true, + "engines": { + "node": ">=8" + } + }, + "node_modules/ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dev": true, + "dependencies": { + "color-convert": "^2.0.1" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, + "node_modules/anymatch": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.3.tgz", + "integrity": "sha512-KMReFUr0B4t+D+OBkjR3KYqvocp2XaSzO55UcB6mgQMd3KbcE+mWTyvVV7D/zsdEbNnV6acZUutkiHQXvTr1Rw==", + "dev": true, + "dependencies": { + "normalize-path": "^3.0.0", + "picomatch": "^2.0.4" + }, + "engines": { + "node": ">= 8" + } + }, + "node_modules/argparse": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", + "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==", + "dev": true + }, + "node_modules/balanced-match": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", + "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", + "dev": true + }, + "node_modules/binary-extensions": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.3.0.tgz", + "integrity": "sha512-Ceh+7ox5qe7LJuLHoY0feh3pHuUDHAcRUeyL2VYghZwfpkNIy/+8Ocg0a3UuSoYzavmylwuLWQOf3hl0jjMMIw==", + "dev": true, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/brace-expansion": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", + "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==", + "dev": true, + "dependencies": { + "balanced-match": "^1.0.0" + } + }, + "node_modules/braces": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz", + "integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==", + "dev": true, + "dependencies": { + "fill-range": "^7.1.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/browser-stdout": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.1.tgz", + "integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==", + "dev": true + }, + "node_modules/camelcase": { + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-6.3.0.tgz", + "integrity": "sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==", + "dev": true, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dev": true, + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, + "node_modules/chalk/node_modules/supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "dev": true, + "dependencies": { + "has-flag": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/chokidar": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.6.0.tgz", + "integrity": "sha512-7VT13fmjotKpGipCW9JEQAusEPE+Ei8nl6/g4FBAmIm0GOOLMua9NDDo/DWp0ZAxCr3cPq5ZpBqmPAQgDda2Pw==", + "dev": true, + "dependencies": { + "anymatch": "~3.1.2", + "braces": "~3.0.2", + "glob-parent": "~5.1.2", + "is-binary-path": "~2.1.0", + "is-glob": "~4.0.1", + "normalize-path": "~3.0.0", + "readdirp": "~3.6.0" + }, + "engines": { + "node": ">= 8.10.0" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + }, + "optionalDependencies": { + "fsevents": "~2.3.2" + } + }, + "node_modules/cliui": { + "version": "7.0.4", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-7.0.4.tgz", + "integrity": "sha512-OcRE68cOsVMXp1Yvonl/fzkQOyjLSu/8bhPDfQt0e0/Eb283TKP20Fs2MqoPsr9SwA595rRCA+QMzYc9nBP+JQ==", + "dev": true, + "dependencies": { + "string-width": "^4.2.0", + "strip-ansi": "^6.0.0", + "wrap-ansi": "^7.0.0" + } + }, + "node_modules/color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dev": true, + "dependencies": { + "color-name": "~1.1.4" + }, + "engines": { + "node": ">=7.0.0" + } + }, + "node_modules/color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", + "dev": true + }, + "node_modules/debug": { + "version": "4.3.7", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.7.tgz", + "integrity": "sha512-Er2nc/H7RrMXZBFCEim6TCmMk02Z8vLC2Rbi1KEBggpo0fS6l0S1nnapwmIi3yW/+GOJap1Krg4w0Hg80oCqgQ==", + "dev": true, + "dependencies": { + "ms": "^2.1.3" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/decamelize": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-4.0.0.tgz", + "integrity": "sha512-9iE1PgSik9HeIIw2JO94IidnE3eBoQrFJ3w7sFuzSX4DpmZ3v5sZpUiV5Swcf6mQEF+Y0ru8Neo+p+nyh2J+hQ==", + "dev": true, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/diff": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-5.2.0.tgz", + "integrity": "sha512-uIFDxqpRZGZ6ThOk84hEfqWoHx2devRFvpTZcTHur85vImfaxUbTW9Ryh4CpCuDnToOP1CEtXKIgytHBPVff5A==", + "dev": true, + "engines": { + "node": ">=0.3.1" + } + }, + "node_modules/emoji-regex": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", + "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==", + "dev": true + }, + "node_modules/escalade": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.2.0.tgz", + "integrity": "sha512-WUj2qlxaQtO4g6Pq5c29GTcWGDyd8itL8zTlipgECz3JesAiiOKotd8JU6otB3PACgG6xkJUyVhboMS+bje/jA==", + "dev": true, + "engines": { + "node": ">=6" + } + }, + "node_modules/escape-string-regexp": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz", + "integrity": "sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==", + "dev": true, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/fable-library": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/fable-library/-/fable-library-1.1.1.tgz", + "integrity": "sha512-tGJqNcPMDfDps2rklbzN9PDOSpzbDXqjHcT7FdP6LEOX2Fe0dj7UWnCzpBWdSCstd3HLPwFVC7DYvReiWtobIQ==" + }, + "node_modules/fill-range": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", + "integrity": "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==", + "dev": true, + "dependencies": { + "to-regex-range": "^5.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/find-up": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/find-up/-/find-up-5.0.0.tgz", + "integrity": "sha512-78/PXT1wlLLDgTzDs7sjq9hzz0vXD+zn+7wypEe4fXQxCmdmqfGsEPQxmiCSQI3ajFV91bVSsvNtrJRiW6nGng==", + "dev": true, + "dependencies": { + "locate-path": "^6.0.0", + "path-exists": "^4.0.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/flat": { + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/flat/-/flat-5.0.2.tgz", + "integrity": "sha512-b6suED+5/3rTpUBdG1gupIl8MPFCAMA0QXwmljLhvCUKcUvdE4gWky9zpuGCcXHOsz4J9wPGNWq6OKpmIzz3hQ==", + "dev": true, + "bin": { + "flat": "cli.js" + } + }, + "node_modules/fs.realpath": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", + "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==", + "dev": true + }, + "node_modules/fsevents": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", + "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", + "dev": true, + "hasInstallScript": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, + "node_modules/get-caller-file": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz", + "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==", + "dev": true, + "engines": { + "node": "6.* || 8.* || >= 10.*" + } + }, + "node_modules/glob": { + "version": "8.1.0", + "resolved": "https://registry.npmjs.org/glob/-/glob-8.1.0.tgz", + "integrity": "sha512-r8hpEjiQEYlF2QU0df3dS+nxxSIreXQS1qRhMJM0Q5NDdR386C7jb7Hwwod8Fgiuex+k0GFjgft18yvxm5XoCQ==", + "deprecated": "Glob versions prior to v9 are no longer supported", + "dev": true, + "dependencies": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^5.0.1", + "once": "^1.3.0" + }, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/glob-parent": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", + "integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==", + "dev": true, + "dependencies": { + "is-glob": "^4.0.1" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "dev": true, + "engines": { + "node": ">=8" + } + }, + "node_modules/he": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz", + "integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==", + "dev": true, + "bin": { + "he": "bin/he" + } + }, + "node_modules/inflight": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", + "integrity": "sha512-k92I/b08q4wvFscXCLvqfsHCrjrF7yiXsQuIVvVE7N82W3+aqpzuUdBbfhWcy/FZR3/4IgflMgKLOsvPDrGCJA==", + "deprecated": "This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.", + "dev": true, + "dependencies": { + "once": "^1.3.0", + "wrappy": "1" + } + }, + "node_modules/inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "dev": true + }, + "node_modules/is-binary-path": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz", + "integrity": "sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==", + "dev": true, + "dependencies": { + "binary-extensions": "^2.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/is-extglob": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", + "integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/is-fullwidth-code-point": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz", + "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==", + "dev": true, + "engines": { + "node": ">=8" + } + }, + "node_modules/is-glob": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz", + "integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==", + "dev": true, + "dependencies": { + "is-extglob": "^2.1.1" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/is-number": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", + "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", + "dev": true, + "engines": { + "node": ">=0.12.0" + } + }, + "node_modules/is-plain-obj": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-2.1.0.tgz", + "integrity": "sha512-YWnfyRwxL/+SsrWYfOpUtz5b3YD+nyfkHvjbcanzk8zgyO4ASD67uVMRt8k5bM4lLMDnXfriRhOpemw+NfT1eA==", + "dev": true, + "engines": { + "node": ">=8" + } + }, + "node_modules/is-unicode-supported": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/is-unicode-supported/-/is-unicode-supported-0.1.0.tgz", + "integrity": "sha512-knxG2q4UC3u8stRGyAVJCOdxFmv5DZiRcdlIaAQXAbSfJya+OhopNotLQrstBhququ4ZpuKbDc/8S6mgXgPFPw==", + "dev": true, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/js-yaml": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", + "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==", + "dev": true, + "dependencies": { + "argparse": "^2.0.1" + }, + "bin": { + "js-yaml": "bin/js-yaml.js" + } + }, + "node_modules/locate-path": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-6.0.0.tgz", + "integrity": "sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==", + "dev": true, + "dependencies": { + "p-locate": "^5.0.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/log-symbols": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-4.1.0.tgz", + "integrity": "sha512-8XPvpAA8uyhfteu8pIvQxpJZ7SYYdpUivZpGy6sFsBuKRY/7rQGavedeB8aK+Zkyq6upMFVL/9AW6vOYzfRyLg==", + "dev": true, + "dependencies": { + "chalk": "^4.1.0", + "is-unicode-supported": "^0.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/minimatch": { + "version": "5.1.6", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.6.tgz", + "integrity": "sha512-lKwV/1brpG6mBUFHtb7NUmtABCb2WZZmm2wNiOA5hAb8VdCS4B3dtMWyvcoViccwAW/COERjXLt0zP1zXUN26g==", + "dev": true, + "dependencies": { + "brace-expansion": "^2.0.1" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/mkdirp": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-3.0.1.tgz", + "integrity": "sha512-+NsyUUAZDmo6YVHzL/stxSu3t9YS1iljliy3BSDrXJ/dkn1KYdmtZODGGjLcc9XLgVVpH4KshHB8XmZgMhaBXg==", + "dev": true, + "bin": { + "mkdirp": "dist/cjs/src/bin.js" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/mocha": { + "version": "10.7.3", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-10.7.3.tgz", + "integrity": "sha512-uQWxAu44wwiACGqjbPYmjo7Lg8sFrS3dQe7PP2FQI+woptP4vZXSMcfMyFL/e1yFEeEpV4RtyTpZROOKmxis+A==", + "dev": true, + "dependencies": { + "ansi-colors": "^4.1.3", + "browser-stdout": "^1.3.1", + "chokidar": "^3.5.3", + "debug": "^4.3.5", + "diff": "^5.2.0", + "escape-string-regexp": "^4.0.0", + "find-up": "^5.0.0", + "glob": "^8.1.0", + "he": "^1.2.0", + "js-yaml": "^4.1.0", + "log-symbols": "^4.1.0", + "minimatch": "^5.1.6", + "ms": "^2.1.3", + "serialize-javascript": "^6.0.2", + "strip-json-comments": "^3.1.1", + "supports-color": "^8.1.1", + "workerpool": "^6.5.1", + "yargs": "^16.2.0", + "yargs-parser": "^20.2.9", + "yargs-unparser": "^2.0.0" + }, + "bin": { + "_mocha": "bin/_mocha", + "mocha": "bin/mocha.js" + }, + "engines": { + "node": ">= 14.0.0" + } + }, + "node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "dev": true + }, + "node_modules/normalize-path": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", + "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", + "dev": true, + "dependencies": { + "wrappy": "1" + } + }, + "node_modules/p-limit": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-3.1.0.tgz", + "integrity": "sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ==", + "dev": true, + "dependencies": { + "yocto-queue": "^0.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/p-locate": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-5.0.0.tgz", + "integrity": "sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw==", + "dev": true, + "dependencies": { + "p-limit": "^3.0.2" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/path-exists": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz", + "integrity": "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==", + "dev": true, + "engines": { + "node": ">=8" + } + }, + "node_modules/picomatch": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", + "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", + "dev": true, + "engines": { + "node": ">=8.6" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, + "node_modules/randombytes": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", + "integrity": "sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ==", + "dev": true, + "dependencies": { + "safe-buffer": "^5.1.0" + } + }, + "node_modules/readdirp": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz", + "integrity": "sha512-hOS089on8RduqdbhvQ5Z37A0ESjsqz6qnRcffsMU3495FuTdqSm+7bhJ29JvIOsBDEEnan5DPu9t3To9VRlMzA==", + "dev": true, + "dependencies": { + "picomatch": "^2.2.1" + }, + "engines": { + "node": ">=8.10.0" + } + }, + "node_modules/require-directory": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", + "integrity": "sha512-fGxEI7+wsG9xrvdjsrlmL22OMTTiHRwAMroiEeMgq8gzoLC/PQr7RsRDSTLUg/bZAZtF+TVIkHc6/4RIKrui+Q==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, + "node_modules/serialize-javascript": { + "version": "6.0.2", + "resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-6.0.2.tgz", + "integrity": "sha512-Saa1xPByTTq2gdeFZYLLo+RFE35NHZkAbqZeWNd3BpzppeVisAqpDjcp8dyf6uIvEqJRd46jemmyA4iFIeVk8g==", + "dev": true, + "dependencies": { + "randombytes": "^2.1.0" + } + }, + "node_modules/string-width": { + "version": "4.2.3", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", + "integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==", + "dev": true, + "dependencies": { + "emoji-regex": "^8.0.0", + "is-fullwidth-code-point": "^3.0.0", + "strip-ansi": "^6.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/strip-ansi": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz", + "integrity": "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==", + "dev": true, + "dependencies": { + "ansi-regex": "^5.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/strip-json-comments": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz", + "integrity": "sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==", + "dev": true, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/supports-color": { + "version": "8.1.1", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-8.1.1.tgz", + "integrity": "sha512-MpUEN2OodtUzxvKQl72cUF7RQ5EiHsGvSsVG0ia9c5RbWGL2CI4C7EpPS8UTBIplnlzZiNuV56w+FuNxy3ty2Q==", + "dev": true, + "dependencies": { + "has-flag": "^4.0.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/supports-color?sponsor=1" + } + }, + "node_modules/to-regex-range": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", + "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", + "dev": true, + "dependencies": { + "is-number": "^7.0.0" + }, + "engines": { + "node": ">=8.0" + } + }, + "node_modules/workerpool": { + "version": "6.5.1", + "resolved": "https://registry.npmjs.org/workerpool/-/workerpool-6.5.1.tgz", + "integrity": "sha512-Fs4dNYcsdpYSAfVxhnl1L5zTksjvOJxtC5hzMNl+1t9B8hTJTdKDyZ5ju7ztgPy+ft9tBFXoOlDNiOT9WUXZlA==", + "dev": true + }, + "node_modules/wrap-ansi": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz", + "integrity": "sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==", + "dev": true, + "dependencies": { + "ansi-styles": "^4.0.0", + "string-width": "^4.1.0", + "strip-ansi": "^6.0.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/wrap-ansi?sponsor=1" + } + }, + "node_modules/wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", + "dev": true + }, + "node_modules/y18n": { + "version": "5.0.8", + "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz", + "integrity": "sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA==", + "dev": true, + "engines": { + "node": ">=10" + } + }, + "node_modules/yargs": { + "version": "16.2.0", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-16.2.0.tgz", + "integrity": "sha512-D1mvvtDG0L5ft/jGWkLpG1+m0eQxOfaBvTNELraWj22wSVUMWxZUvYgJYcKh6jGGIkJFhH4IZPQhR4TKpc8mBw==", + "dev": true, + "dependencies": { + "cliui": "^7.0.2", + "escalade": "^3.1.1", + "get-caller-file": "^2.0.5", + "require-directory": "^2.1.1", + "string-width": "^4.2.0", + "y18n": "^5.0.5", + "yargs-parser": "^20.2.2" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/yargs-parser": { + "version": "20.2.9", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-20.2.9.tgz", + "integrity": "sha512-y11nGElTIV+CT3Zv9t7VKl+Q3hTQoT9a1Qzezhhl6Rp21gJ/IVTW7Z3y9EWXhuUBC2Shnf+DX0antecpAwSP8w==", + "dev": true, + "engines": { + "node": ">=10" + } + }, + "node_modules/yargs-unparser": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/yargs-unparser/-/yargs-unparser-2.0.0.tgz", + "integrity": "sha512-7pRTIA9Qc1caZ0bZ6RYRGbHJthJWuakf+WmHK0rVeLkNrrGhfoabBNdue6kdINI6r4if7ocq9aD/n7xwKOdzOA==", + "dev": true, + "dependencies": { + "camelcase": "^6.0.0", + "decamelize": "^4.0.0", + "flat": "^5.0.2", + "is-plain-obj": "^2.1.0" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/yocto-queue": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz", + "integrity": "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==", + "dev": true, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + } + } +} diff --git a/src/DynamicObj/DynObj.fs b/src/DynamicObj/DynObj.fs index f2c4eb9..a4acf55 100644 --- a/src/DynamicObj/DynObj.fs +++ b/src/DynamicObj/DynObj.fs @@ -2,52 +2,54 @@ open System.Collections.Generic +/// +/// This module contains lots of API functions for DynamicObj. +/// +/// These functions are not static methods on the DynamicObj type itself because that type is designed to be inherited from, +/// and a lot of these functions might not make sense as static methods on inheriting types. +/// module DynObj = - /// New DynamicObj of Dictionary - let ofDict dict = DynamicObj.fromDict dict - - /// New DynamicObj of a sequence of key value - let ofSeq kv = - let dict = new Dictionary() - kv |> Seq.iter (fun (k,v) -> dict.Add(k,v)) - DynamicObj.fromDict dict - - /// New DynamicObj of a list of key value - let ofList kv = - let dict = new Dictionary() - kv |> List.iter (fun (k,v) -> dict.Add(k,v)) - DynamicObj.fromDict dict - - - /// New DynamicObj of an array of key value - let ofArray kv = - let dict = new Dictionary() - kv |> Array.iter (fun (k,v) -> dict.Add(k,v)) - DynamicObj.fromDict dict - - - // - // let rec merge (first:#DynamicObj) (second:#DynamicObj) = - // let dict = new Dictionary() - - // Seq.append (first.GetProperties true) (second.GetProperties true) - // |> Seq.iter (fun kv -> - // if dict.ContainsKey(kv.Key) then - // match kv.Value with - // | :? #DynamicObj as o -> - // let oo = dict.[kv.Key] :?> #DynamicObj - // dict.[kv.Key] <- merge o oo - // | _ -> dict.[kv.Key] <- kv.Value - // else - // dict.Add(kv.Key, kv.Value) - // ) - // new DynamicObj(dict) - - //let rec combine<'t when 't :> DynamicObj > (first:'t) (second:'t) = - - - /// Merges two DynamicObj (Warning: In case of duplicate property names the members of the second object override those of the first) + /// + /// Creates a new DynamicObj from a Dictionary containing dynamic properties. + /// + /// The dictionary with the dynamic properties + let ofDict (dynamicProperties: Dictionary) = DynamicObj.fromDict dynamicProperties + + /// + /// Creates a new DynamicObj from a sequence of key value pairs containing dynamic properties. + /// + /// + let ofSeq (dynamicProperties: seq) = + dynamicProperties + |> dict + |> Dictionary + |> DynamicObj.fromDict + + /// + /// Creates a new DynamicObj from a list of key value pairs containing dynamic properties. + /// + /// + let ofList (dynamicProperties: (string * obj) list) = + dynamicProperties + |> ofSeq + + /// + /// Creates a new DynamicObj from an array of key value pairs containing dynamic properties. + /// + /// + let ofArray (dynamicProperties: (string * obj) array) = + dynamicProperties + |> ofSeq + + /// + /// Combines the dynamic properties of the second DynamicObj onto the first. + /// + /// In case of duplicate property names the members of the second object override those of the first. + /// + /// + /// + /// This function mutates the first input DynamicObj let rec combine (first:DynamicObj) (second:DynamicObj) = //printfn "Type %A" (first.GetType()) /// Consider deep-copy of first @@ -63,39 +65,128 @@ module DynObj = | _ -> first.SetValue(kv.Key,kv.Value) first - - let inline tryGetTypedValue<'a> (name) (dynObj : DynamicObj) = - match (dynObj.TryGetValue name) with + /// + /// Returns Some('TPropertyValue) when a dynamic property with the given name and type exists on the input DynamicObj, otherwise None. + /// + /// + /// + let inline tryGetTypedValue<'TPropertyValue> (propertyName:string) (dynObj : DynamicObj) : 'TPropertyValue option = + match (dynObj.TryGetValue propertyName) with | None -> None | Some o -> match o with - | :? 'a as o -> o |> Some + | :? 'TPropertyValue as o -> o |> Some | _ -> None - let setValue (dyn:DynamicObj) propName o = - dyn.SetValue(propName,o) - - let setValueOpt (dyn:DynamicObj) propName = - function - | Some o -> - dyn.SetValue(propName,o) + /// + /// Sets the given dynamic property name and value on the given DynamicObj. + /// + /// The name of the dynamic property to set + /// The value of the dynamic property to set + /// The DynamicObj to set the property on + /// This function mutates the input DynamicObj + let setValue (propertyName:string) (propertyValue:'TPropertyValue) (dynObj : DynamicObj) = + dynObj.SetValue(propertyName,propertyValue) + + /// + /// Sets the given dynamic property name and value on the given DynamicObj and returns it. + /// + /// The name of the dynamic property to set + /// The value of the dynamic property to set + /// The DynamicObj to set the property on + /// This function mutates the input DynamicObj + let withValue (propertyName:string) (propertyValue:'TPropertyValue) (dynObj: DynamicObj) = + setValue propertyName propertyValue dynObj + dynObj + + /// + /// Sets the given dynamic property name and value on the given DynamicObj if the value is Some('TPropertyValue). + /// If the given propertyValue is None, does nothing to the input DynamicObj. + /// + /// The name of the dynamic property to set + /// The value of the dynamic property to set + /// The DynamicObj to set the property on + /// This function mutates the input DynamicObj + let setValueOpt (propertyName: string) (propertyValue: 'TPropertyValue option) (dynObj: DynamicObj) = + match propertyValue with + | Some pv -> dynObj |> setValue propertyName pv | None -> () - let setValueOptBy (dyn:DynamicObj) propName f = - function - | Some o -> - dyn.SetValue(propName,f o) + /// + /// Sets the given dynamic property name and value on the given DynamicObj if the value is Some('TPropertyValue) and returns it. + /// If the given propertyValue is None, returns the unchanged DynamicObj. + /// + /// The name of the dynamic property to set + /// The value of the dynamic property to set + /// The DynamicObj to set the property on + /// This function mutates the input DynamicObj + let withValueOpt (propertyName: string) (propertyValue: 'TPropertyValue option) (dynObj: DynamicObj) = + match propertyValue with + | Some pv -> dynObj |> withValue propertyName pv + | None -> dynObj + + /// + /// Sets the given dynamic property name with the result of a mapping function applied to the given property value on the given DynamicObj if the value is Some('TPropertyValue). + /// If the given propertyValue is None, does nothing to the input DynamicObj. + /// + /// The name of the dynamic property to set + /// The value of the dynamic property to set + /// A function to apply to the property value before setting it on the DynamicObj + /// The DynamicObj to set the property on + /// This function mutates the input DynamicObj + let setValueOptBy (propertyName: string) (propertyValue: 'TPropertyValue option) (mapping: 'TPropertyValue -> 'UPropertyValue) (dynObj: DynamicObj) = + match propertyValue with + | Some pv -> dynObj |> setValue propertyName (mapping pv) | None -> () - - let tryGetValue (dyn:DynamicObj) name = - dyn.TryGetValue name - - let remove (dyn:DynamicObj) propName = - DynamicObj.remove (dyn, propName) |> ignore - let format (d:DynamicObj) = + /// + /// Sets the given dynamic property name with the result of a mapping function applied to the given property value on the given DynamicObj if the value is Some('TPropertyValue) and returns it. + /// If the given propertyValue is None, returns the unchanged DynamicObj. + /// + /// The name of the dynamic property to set + /// The value of the dynamic property to set + /// A function to apply to the property value before setting it on the DynamicObj + /// The DynamicObj to set the property on + /// This function mutates the input DynamicObj + let withValueOptBy (propertyName: string) (propertyValue: 'TPropertyValue option) (mapping: 'TPropertyValue -> 'UPropertyValue) (dynObj: DynamicObj) = + match propertyValue with + | Some pv -> dynObj |> withValue propertyName (mapping pv) + | None -> dynObj + + /// + /// Returns Some(boxed value) if the DynamicObj contains a dynamic property with the given name, and None otherwise. + /// + /// The name of the dynamic property to get + /// The DynamicObj to get the property from + let tryGetValue (propertyName: string) (dynObj: DynamicObj) = + dynObj.TryGetValue propertyName + + /// + /// Removes any dynamic property with the given name from the input DynamicObj. + /// + /// The name of the dynamic property to remove + /// The DynamicObj to remove the property from + /// This function mutates the input DynamicObj + let remove (propertyName: string) (dynObj: DynamicObj) = + DynamicObj.remove (dynObj, propertyName) |> ignore + + /// + /// Returns the input DynamicObj with any dynamic property with the given name removed. + /// + /// The name of the dynamic property to remove + /// The DynamicObj to remove the property from + /// This function mutates the input DynamicObj + let withoutProperty(propertyName: string) (dynObj: DynamicObj) = + dynObj |> remove propertyName + dynObj + + /// + /// Returns a formatted string containing all static and dynamic properties of the given DynamicObj + /// + /// The DynamicObj for which to generate a formatted string for + let format (dynObj:DynamicObj) = - let members = d.GetPropertyHelpers(true) |> List.ofSeq + let members = dynObj.GetPropertyHelpers(true) |> List.ofSeq let rec loop (object:DynamicObj) (indentationLevel:int) (membersLeft:PropertyHelper list) (acc:string list) = let indent = [for i in 0 .. indentationLevel-1 do yield " "] |> String.concat "" @@ -113,6 +204,10 @@ module DynObj = | item -> loop object indentationLevel rest ($"{indent}{dynamicIndicator}{name}: {item}"::acc) - loop d 0 members [] + loop dynObj 0 members [] - let print (d:DynamicObj) = printfn "%s" (d |> format) \ No newline at end of file + /// + /// Prints a formatted string containing all static and dynamic properties of the given DynamicObj + /// + /// The DynamicObj for which to print a formatted string for + let print (dynObj:DynamicObj) = printfn "%s" (dynObj |> format) \ No newline at end of file diff --git a/tests/DynamicObject.Tests/DynObjStaticMethods.fs b/tests/DynamicObject.Tests/DynObjStaticMethods.fs new file mode 100644 index 0000000..25b03c6 --- /dev/null +++ b/tests/DynamicObject.Tests/DynObjStaticMethods.fs @@ -0,0 +1,756 @@ +module DynObjStaticMethods.Tests + +open System +open System.Collections.Generic +open Fable.Pyxpecto +open DynamicObj + +let tests_ofDict = ptestList "ofDict" [ + testCase "Test ofDict" <| fun _ -> + let d = Dictionary() + d.Add("key1", box 1) + d.Add("key2", box 2) + let dyn = DynObj.ofDict d + Expect.equal (dyn.GetValue("key1")) 1 "Value should be 1" + Expect.equal (dyn.GetValue("key2")) 2 "Value should be 2" +] + +let tests_ofSeq = ptestList "ofSeq" [ + testCase "Test ofDict" <| fun _ -> + let d = + seq { + "key1", box 1 + "key2", box 2 + } + let dyn = DynObj.ofSeq d + Expect.equal (dyn.GetValue("key1")) 1 "Value should be 1" + Expect.equal (dyn.GetValue("key2")) 2 "Value should be 2" +] + +let tests_ofList = ptestList "ofList" [ + testCase "Test ofDict" <| fun _ -> + let d = [ + "key1", box 1 + "key2", box 2 + ] + let dyn = DynObj.ofList d + Expect.equal (dyn.GetValue("key1")) 1 "Value should be 1" + Expect.equal (dyn.GetValue("key2")) 2 "Value should be 2" +] + +let tests_ofArray = ptestList "ofArray" [ + testCase "Test ofDict" <| fun _ -> + let d = [| + "key1", box 1 + "key2", box 2 + |] + let dyn = DynObj.ofArray d + Expect.equal (dyn.GetValue("key1")) 1 "Value should be 1" + Expect.equal (dyn.GetValue("key2")) 2 "Value should be 2" +] + +let tests_combine = testList "combine" [ + + testCase "Combine flat DOs" <| fun _ -> + let target = DynamicObj() + + target.SetValue("target-unique", [42]) + target.SetValue("will-be-overridden", "WAS_NOT_OVERRIDDEN!") + + let source = DynamicObj() + + source.SetValue("source-unique", [42; 32]) + source.SetValue("will-be-overridden", "WAS_OVERRIDDEN =)") + + let combined = DynObj.combine target source + + let expected = DynamicObj() + + expected.SetValue("target-unique", [42]) + expected.SetValue("source-unique", [42; 32]) + expected.SetValue("will-be-overridden", "WAS_OVERRIDDEN =)") + + Expect.equal expected combined "Combine flat DOs failed" + + testCase "Combine nested DOs" <| fun _ -> + let target = DynamicObj() + + target.SetValue("target-unique", 1337) + target.SetValue("will-be-overridden", -42) + let something2BeCombined = DynamicObj() + something2BeCombined.SetValue("inner","I Am") + let something2BeOverriden = DynamicObj() + something2BeOverriden.SetValue("inner","NOT_OVERRIDDEN") + target.SetValue("nested-will-be-combined", something2BeCombined) + target.SetValue("nested-will-be-overridden", something2BeOverriden) + + let source = DynamicObj() + + source.SetValue("source-unique", 69) + source.SetValue("will-be-overridden", "WAS_OVERRIDDEN") + let alsoSomething2BeCombined = DynamicObj() + alsoSomething2BeCombined.SetValue("inner_combined","Complete") + source.SetValue("nested-will-be-combined", alsoSomething2BeCombined) + source.SetValue("nested-will-be-overridden", "WAS_OVERRIDDEN") + + let combined = DynObj.combine target source + + let expected = DynamicObj() + + expected.SetValue("source-unique", 69) + expected.SetValue("target-unique", 1337) + expected.SetValue("will-be-overridden", "WAS_OVERRIDDEN") + expected.SetValue("nested-will-be-overridden", "WAS_OVERRIDDEN") + expected.SetValue("nested-will-be-combined", + let inner = DynamicObj() + inner.SetValue("inner","I Am") + inner.SetValue("inner_combined","Complete") + inner + ) + + Expect.equal expected combined "Combine nested DOs failed" +] + +let tests_tryGetTypedValue = testList "tryGetTypedValue" [ + + testCase "typeof" <| fun _ -> + let a = typeof + Expect.equal a.Name "Int32" "Type should be Int32" + + testCase "NonExisting" <| fun _ -> + let a = DynamicObj() + let b = DynObj.tryGetTypedValue "a" a + Expect.isNone b "Value should not exist" + + testCase "Correct Int" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", 1) + let b = DynObj.tryGetTypedValue "a" a + Expect.equal b (Some 1) "Value should be 1" + + testCase "Incorrect Int" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", "1") + let b = DynObj.tryGetTypedValue "a" a + Expect.isNone b "Value should not be an int" + + testCase "Correct String" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", "1") + let b = DynObj.tryGetTypedValue "a" a + Expect.equal b (Some "1") "Value should be '1'" + + testCase "Incorrect String" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", 1) + let b = DynObj.tryGetTypedValue "a" a + Expect.isNone b "Value should not be a string" + + testCase "Correct List" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", [1; 2; 3]) + let b = DynObj.tryGetTypedValue "a" a + Expect.equal b (Some [1; 2; 3]) "Value should be [1; 2; 3]" + + ptestCase "Incorrect List" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", [1; 2; 3]) + let b = DynObj.tryGetTypedValue "a" a + Expect.isNone b "Value should not be a string list" + + testCase "Correct DynamicObj" <| fun _ -> + let a = DynamicObj() + let b = DynamicObj() + a.SetValue("a", b) + let c = DynObj.tryGetTypedValue "a" a + Expect.equal c (Some b) "Value should be a DynamicObj" + + testCase "Incorrect DynamicObj" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", 1) + let b = DynObj.tryGetTypedValue "a" a + Expect.isNone b "Value should not be a DynamicObj" +] + +let tests_setValue = testList "setValue" [ + + testCase "Same String" <| fun _ -> + let a = DynamicObj () + a |> DynObj.setValue "aaa" 5 + let b = DynamicObj () + b |> DynObj.setValue "aaa" 5 + Expect.equal a b "Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + + testCase "Different Strings" <| fun _ -> + let a = DynamicObj () + a |> DynObj.setValue "aaa" 1212 + let b = DynamicObj () + b |> DynObj.setValue "aaa" 5 + Expect.notEqual a b "Values should not be equal" + Expect.notEqual (a.GetHashCode()) (b.GetHashCode()) "Hash codes should not be equal" + + testCase "String only on one" <| fun _ -> + let a = DynamicObj () + let b = DynamicObj () + b |> DynObj.setValue "aaa" 5 + + Expect.notEqual a b "Values should not be equal" + Expect.notEqual b a "Values should not be equal (Reversed equality)" + + testCase "Same lists different keys" <| fun _ -> + let a = DynamicObj () + let b = DynamicObj () + a |> DynObj.setValue "quack!" [1; 2; 3] + b |> DynObj.setValue "quack!1" [1; 2; 3] + + Expect.notEqual a b "Values should not be equal" + Expect.notEqual (a.GetHashCode()) (b.GetHashCode()) "Hash codes should not be equal" + + testCase "Different lists" <| fun _ -> + let a = DynamicObj () + let b = DynamicObj () + a |> DynObj.setValue "quack!" [1; 2; 3] + b |> DynObj.setValue "quack!" [1; 2; 3; 4; 34] + Expect.notEqual a b "Values should not be equal" + Expect.notEqual (a.GetHashCode()) (b.GetHashCode()) "Hash codes should not be equal" + + testCase "Nested Same List Same String" <| fun _ -> + let a = DynamicObj () + let b = DynamicObj () + + let a' = DynamicObj () + let b' = DynamicObj () + + a' |> DynObj.setValue "quack!" [1; 2; 3] + b' |> DynObj.setValue "quack!" [1; 2; 3] + + a |> DynObj.setValue "aaa" a' + b |> DynObj.setValue "aaa" b' + + Expect.equal a' b' "New Values should be equal" + Expect.equal a b "Old Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Old Hash codes should be equal" + Expect.equal (a'.GetHashCode()) (b'.GetHashCode()) "New Hash codes should be equal" + + testCase "Nested Same List Different Strings" <| fun _ -> + let a = DynamicObj () + let b = DynamicObj () + + let a' = DynamicObj () + let b' = DynamicObj () + a' |> DynObj.setValue "quack!" [1; 2; 3] + b' |> DynObj.setValue "quack!" [1; 2; 3] + + a |> DynObj.setValue "aaa" a' + b |> DynObj.setValue "aaa1" b' + + Expect.equal a' b' "New Values should be equal" + Expect.notEqual a b "Old Values should not be equal" + Expect.equal (a'.GetHashCode()) (b'.GetHashCode()) "New Hash codes should be equal" + ] + +let tests_withValue = testList "withValue" [ + + testCase "Same String" <| fun _ -> + let a = + DynamicObj () + |> DynObj.withValue "aaa" 5 + |> DynObj.withValue "aaaa" 6 + let b = + DynamicObj () + |> DynObj.withValue "aaa" 5 + |> DynObj.withValue "aaaa" 6 + Expect.equal a b "Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + + testCase "Different Strings" <| fun _ -> + let a = + DynamicObj () + |> DynObj.withValue "111" 5 + |> DynObj.withValue "1111" 6 + let b = + DynamicObj () + |> DynObj.withValue "aaa" 5 + |> DynObj.withValue "aaaa" 6 + Expect.notEqual a b "Values should not be equal" + Expect.notEqual (a.GetHashCode()) (b.GetHashCode()) "Hash codes should not be equal" + + testCase "String different amounts" <| fun _ -> + let a = + DynamicObj () + |> DynObj.withValue "aaa" 5 + let b = + DynamicObj () + |> DynObj.withValue "aaa" 5 + |> DynObj.withValue "aaaa" 6 + + Expect.notEqual a b "Values should not be equal" + Expect.notEqual b a "Values should not be equal (Reversed equality)" + Expect.notEqual (a.GetHashCode()) (b.GetHashCode()) "Hash codes should not be equal" + + testCase "Same lists different keys" <| fun _ -> + let a = + DynamicObj () + |> DynObj.withValue "a" [1; 2; 3] + |> DynObj.withValue "aa" [1; 2; 3] + let b = + DynamicObj () + |> DynObj.withValue "aa" [1; 2; 3] + |> DynObj.withValue "bbb" [1; 2; 3] + + Expect.notEqual a b "Values should not be equal" + Expect.notEqual (a.GetHashCode()) (b.GetHashCode()) "Hash codes should not be equal" + + testCase "Different lists" <| fun _ -> + let a = DynamicObj () + let b = DynamicObj () + a |> DynObj.setValue "quack!" [1; 2; 3] + b |> DynObj.setValue "quack!" [1; 2; 3; 4; 34] + + Expect.notEqual a b "Values should not be equal" + Expect.notEqual (a.GetHashCode()) (b.GetHashCode()) "Hash codes should not be equal" + + testCase "Nested Same List Same String" <| fun _ -> + let a' = DynamicObj () + let b' = DynamicObj () + + let a = DynamicObj () |> DynObj.withValue "aaa" (a' |> DynObj.withValue "quack!" [1; 2; 3]) + let b = DynamicObj () |> DynObj.withValue "aaa" (b' |> DynObj.withValue "quack!" [1; 2; 3]) + + Expect.equal a' b' "New Values should be equal" + Expect.equal a b "Old Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Old Hash codes should be equal" + Expect.equal (a'.GetHashCode()) (b'.GetHashCode()) "New Hash codes should be equal" + + testCase "Nested Same List Different Strings" <| fun _ -> + let a' = DynamicObj () + let b' = DynamicObj () + + let a = DynamicObj () |> DynObj.withValue "aaa" (a' |> DynObj.withValue "quack!" [1; 2; 3]) + let b = DynamicObj () |> DynObj.withValue "aaa1" (b' |> DynObj.withValue "quack!" [1; 2; 3]) + + Expect.equal a' b' "New Values should be equal" + Expect.notEqual a b "Old Values should not be equal" + Expect.equal (a'.GetHashCode()) (b'.GetHashCode()) "New Hash codes should be equal" + ] + +let tests_setValueOpt = testList "setValueOpt" [ + testCase "Some" <| fun _ -> + let a = DynamicObj () + a |> DynObj.setValueOpt "aaa" (Some 5) + let b = DynamicObj () + b |> DynObj.setValue "aaa" 5 + + Expect.equal a b "Values should be equal" + Expect.equal (a |> DynObj.tryGetTypedValue "aaa" |> Option.get) 5 "Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + testCase "None" <| fun _ -> + let a = DynamicObj () + a |> DynObj.setValueOpt "aaa" None + let b = DynamicObj () + + Expect.equal a b "Values should be equal" + Expect.throws (fun _ -> a |> DynObj.tryGetTypedValue "aaa" |> Option.get |> ignore) "Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + testCase "Some and None" <| fun _ -> + let a = DynamicObj () + a |> DynObj.setValueOpt "aaa" (Some 5) + a |> DynObj.setValueOpt "aaaa" None + let b = DynamicObj () + b |> DynObj.setValue "aaa" 5 + b |> DynObj.setValue "aaaa" 5 + + Expect.notEqual a b "Values should not be equal" + Expect.notEqual (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + Expect.equal (a |> DynObj.tryGetTypedValue "aaa" |> Option.get) 5 "Values should be equal" + Expect.throws (fun _ -> a |> DynObj.tryGetTypedValue "aaaa" |> Option.get |> ignore) "Values should not exist" +] + +let tests_withValueOpt = testList "withValueOpt" [ + testCase "Some" <| fun _ -> + let a = DynamicObj () |> DynObj.withValueOpt "aaa" (Some 5) + let b = DynamicObj () |> DynObj.withValue "aaa" 5 + + Expect.equal a b "Values should be equal" + Expect.equal (a |> DynObj.tryGetTypedValue "aaa" |> Option.get) 5 "Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + + testCase "None" <| fun _ -> + let a = DynamicObj () |> DynObj.withValueOpt "aaa" None + let b = DynamicObj () + + Expect.equal a b "Values should be equal" + Expect.throws (fun _ -> a |> DynObj.tryGetTypedValue "aaa" |> Option.get |> ignore) "Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + testCase "Some and None" <| fun _ -> + let a = + DynamicObj () + |> DynObj.withValueOpt "aaa" (Some 5) + |> DynObj.withValueOpt "aaaa" None + let b = + DynamicObj () + |> DynObj.withValue "aaa" 5 + |> DynObj.withValue "aaaa" 5 + + Expect.notEqual a b "Values should not be equal" + Expect.notEqual (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + Expect.equal (a |> DynObj.tryGetTypedValue "aaa" |> Option.get) 5 "Values should be equal" + Expect.throws (fun _ -> a |> DynObj.tryGetTypedValue "aaaa" |> Option.get |> ignore) "Values should not exist" +] + +let tests_setValueOptBy = testList "setValueOptBy" [ + testCase "Some" <| fun _ -> + let a = DynamicObj () + a |> DynObj.setValueOptBy "aaa" (Some 5) (fun x -> x + 1) + let b = DynamicObj () + b |> DynObj.setValue "aaa" 6 + + Expect.equal a b "Values should be equal" + Expect.equal (a |> DynObj.tryGetTypedValue "aaa" |> Option.get) 6 "Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + testCase "None" <| fun _ -> + let a = DynamicObj () + a |> DynObj.setValueOptBy "aaa" None id + let b = DynamicObj () + + Expect.equal a b "Values should be equal" + Expect.throws (fun _ -> a |> DynObj.tryGetTypedValue "aaa" |> Option.get |> ignore) "Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + testCase "Some and None" <| fun _ -> + let a = DynamicObj () + a |> DynObj.setValueOptBy "aaa" (Some 5) (fun x -> x + 1) + a |> DynObj.setValueOptBy "aaaa" None id + let b = DynamicObj () + b |> DynObj.setValue "aaa" 6 + b |> DynObj.setValue "aaaa" 6 + + Expect.notEqual a b "Values should not be equal" + Expect.notEqual (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + Expect.equal (a |> DynObj.tryGetTypedValue "aaa" |> Option.get) 6 "Values should be equal" + Expect.throws (fun _ -> a |> DynObj.tryGetTypedValue "aaaa" |> Option.get |> ignore) "Values should not exist" +] + +let tests_withValueOptBy = testList "withValueOptBy" [ + testCase "Some" <| fun _ -> + let a = DynamicObj () |> DynObj.withValueOptBy "aaa" (Some 5) (fun x -> x+1) + let b = DynamicObj () |> DynObj.withValue "aaa" 6 + + Expect.equal a b "Values should be equal" + Expect.equal (a |> DynObj.tryGetTypedValue "aaa" |> Option.get) 6 "Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + + testCase "None" <| fun _ -> + let a = DynamicObj () |> DynObj.withValueOptBy "aaa" None id + let b = DynamicObj () + + Expect.equal a b "Values should be equal" + Expect.throws (fun _ -> a |> DynObj.tryGetTypedValue "aaa" |> Option.get |> ignore) "Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + testCase "Some and None" <| fun _ -> + let a = + DynamicObj () + |> DynObj.withValueOptBy "aaa" (Some 5) (fun x -> x+1) + |> DynObj.withValueOptBy "aaaa" None id + let b = + DynamicObj () + |> DynObj.withValue "aaa" 6 + |> DynObj.withValue "aaaa" 6 + + Expect.notEqual a b "Values should not be equal" + Expect.notEqual (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + Expect.equal (a |> DynObj.tryGetTypedValue "aaa" |> Option.get) 6 "Values should be equal" + Expect.throws (fun _ -> a |> DynObj.tryGetTypedValue "aaaa" |> Option.get |> ignore) "Values should not exist" +] + +let tests_tryGetValue = testList "tryGetValue" [ + + testCase "NonExisting" <| fun _ -> + let a = DynamicObj() + let b = a |> DynObj.tryGetValue "a" + Expect.isNone b "Value should not exist" + + testCase "Correct boxed Int" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", 1) + let b = a |> DynObj.tryGetValue "a" + Expect.equal (b) (Some (box 1)) "Value should be 1" + + testCase "Correct unboxed Int" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", 1) + let b = a |> DynObj.tryGetValue "a" + Expect.equal (b |> Option.map unbox) (Some 1) "Value should be 1" + + testCase "Correct boxed String" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", "1") + let b = a |> DynObj.tryGetValue "a" + Expect.equal (b) (Some (box "1")) "Value should be '1'" + + testCase "Correct unboxed String" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", "1") + let b = a |> DynObj.tryGetValue "a" + Expect.equal (b |> Option.map unbox) (Some "1") "Value should be '1'" + + testCase "Correct boxed List" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", [1; 2; 3]) + let b = a |> DynObj.tryGetValue "a" + Expect.equal (b) (Some (box [1; 2; 3])) "Value should be [1; 2; 3]" + + testCase "Correct unboxed List" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", [1; 2; 3]) + let b = a |> DynObj.tryGetValue "a" + Expect.equal (b |> Option.map unbox) (Some [1; 2; 3]) "Value should be [1; 2; 3]" + + testCase "Correct boxed DynamicObj" <| fun _ -> + let a = DynamicObj() + let b = DynamicObj() + a.SetValue("a", b) + let c = a |> DynObj.tryGetValue "a" + Expect.equal (c) (Some (box b)) "Value should be a DynamicObj" + + testCase "Correct unboxed DynamicObj" <| fun _ -> + let a = DynamicObj() + let b = DynamicObj() + a.SetValue("a", b) + let c = a |> DynObj.tryGetValue "a" + Expect.equal (c |> Option.map unbox) (Some b) "Value should be a DynamicObj" + +] + +let tests_remove = testList "remove" [ + + testCase "Remove" <| fun _ -> + let a = DynamicObj () + let b = DynamicObj () + + a.SetValue("quack!", "hello") + + a |> DynObj.remove "quack!" |> ignore + + Expect.equal a b "Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + + testCase "Remove Non-Existing" <| fun _ -> + let a = DynamicObj () + let b = DynamicObj () + + a.SetValue("quack!", "hello") + b.SetValue("quack!", "hello") + + a |> DynObj.remove "quecky!" |> ignore + + Expect.equal a b "Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + + testCase "Remove only on one" <| fun _ -> + let a = DynamicObj () + let b = DynamicObj () + + a.SetValue("quack!", "hello") + b.SetValue("quack!", "hello") + + a |> DynObj.remove "quack!" |> ignore + + Expect.notEqual a b "Values should be unequal" + Expect.notEqual (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be unequal" + + testCase "Nested Remove Non-Existing" <| fun _ -> + let a = DynamicObj () + let b = DynamicObj () + + let a' = DynamicObj () + let b' = DynamicObj () + a'.SetValue("quack!", [1; 2; 3]) + b'.SetValue("quack!", [1; 2; 3]) + + a.SetValue("aaa", a') + a |> DynObj.remove "quack!" |> ignore + b.SetValue("aaa", b') + + Expect.equal a b "Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + + testCase "Nested Remove only on one" <| fun _ -> + let a = DynamicObj () + let b = DynamicObj () + + let a' = DynamicObj () + let b' = DynamicObj () + a'.SetValue("quack!", [1; 2; 3]) + b'.SetValue("quack!", [1; 2; 3]) + + a.SetValue("aaa", a') + a' |> DynObj.remove "quack!" |> ignore + b.SetValue("aaa", b') + + Expect.notEqual a b "Values should be unequal" + Expect.notEqual (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be unequal" + + testCase "Nested Remove on both" <| fun _ -> + let a = DynamicObj () + let b = DynamicObj () + + let a' = DynamicObj () + let b' = DynamicObj () + a'.SetValue("quack!", [1; 2; 3]) + b'.SetValue("quack!", [1; 2; 3]) + + a.SetValue("aaa", a') + a |> DynObj.remove "quack!" |> ignore + b.SetValue("aaa", b') + b |> DynObj.remove "quack!" |> ignore + + Expect.equal a b "Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + +] + + +let tests_withoutProperty = testList "withoutProperty" [ + + testCase "Remove" <| fun _ -> + let a = + DynamicObj () + |> DynObj.withValue "quack!" "hello" + |> DynObj.withoutProperty "quack!" + + let b = DynamicObj () + + + Expect.equal a b "Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + + testCase "Remove Non-Existing" <| fun _ -> + let a = + DynamicObj () + |> DynObj.withValue "quack!" "hello" + |> DynObj.withoutProperty "quecky!" + + let b = DynamicObj () |> DynObj.withValue "quack!" "hello" + + + Expect.equal a b "Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + + testCase "Remove only on one" <| fun _ -> + let a = + DynamicObj () + |> DynObj.withValue "quack!" "hello" + |> DynObj.withoutProperty "quack!" + let b = DynamicObj () |> DynObj.withValue "quack!" "hello" + + Expect.notEqual a b "Values should be unequal" + Expect.notEqual (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be unequal" + + testCase "Nested Remove Non-Existing" <| fun _ -> + let a = + DynamicObj () + |> DynObj.withValue "a" [1; 2; 3] + |> DynObj.withValue "aaa" (DynamicObj ()) + |> DynObj.withoutProperty "quack!" + + let b = + DynamicObj () + |> DynObj.withValue "a" [1; 2; 3] + |> DynObj.withValue "aaa" (DynamicObj ()) + + Expect.equal a b "Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + + testCase "Nested Remove only on one" <| fun _ -> + let a = + DynamicObj () + |> DynObj.withValue "a" [1; 2; 3] + |> DynObj.withValue "aaa" (DynamicObj ()) + |> DynObj.withoutProperty "a" + + let b = + DynamicObj () + |> DynObj.withValue "a" [1; 2; 3] + |> DynObj.withValue "aaa" (DynamicObj ()) + + Expect.notEqual a b "Values should be unequal" + Expect.notEqual (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be unequal" + + testCase "Nested Remove on both" <| fun _ -> + let a = + DynamicObj () + |> DynObj.withValue "a" [1; 2; 3] + |> DynObj.withValue "aaa" (DynamicObj ()) + |> DynObj.withoutProperty "a" + + let b = + DynamicObj () + |> DynObj.withValue "a" [1; 2; 3] + |> DynObj.withValue "aaa" (DynamicObj ()) + |> DynObj.withoutProperty "a" + + Expect.equal a b "Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + +] + +let tests_formatString = testList "FormatString" [ + + testCase "Format string 1" <| fun _ -> + let foo = DynamicObj() + let list = [1;2;3;4] + foo.SetValue("bar", list) + let expected = $"?bar: {list}" + Expect.equal (foo |> DynObj.format) expected "Format string 1 failed" + + testCase "Format string 2" <| fun _ -> + let foo = DynamicObj() + let corgi = "corgi" + foo.SetValue("corgi", corgi) + let inner = DynamicObj() + let baz = "baz" + inner.SetValue("bar", baz) + foo.SetValue("foo", inner) + let expected = $"""?corgi: {corgi}{Environment.NewLine}?foo:{Environment.NewLine} ?bar: {baz}""" + Expect.equal (foo |> DynObj.format) expected "Format string 2 failed" + +] + +let tests_print = testList "Print" [ + + testCase "Test Print For Issue 14" <| fun _ -> + let outer = DynamicObj() + let inner = DynamicObj() + inner.SetValue("Level", "Information") + inner.SetValue("MessageTemplate","{Method} Request at {Path}") + outer.SetValue("serilog", inner) + + let print = + try + outer |> DynObj.print + true + with + | e -> false + + Expect.isTrue print "Print failed for issue 14" +] + +let main = testList "DynObj Static Methods" [ + tests_ofDict + tests_ofSeq + tests_ofList + tests_ofArray + tests_combine + tests_tryGetTypedValue + tests_setValue + tests_withValue + tests_setValueOpt + tests_withValueOpt + tests_setValueOptBy + tests_withValueOptBy + tests_tryGetValue + tests_withoutProperty + tests_formatString + tests_print +] \ No newline at end of file diff --git a/tests/DynamicObject.Tests/DynamicObj.fs b/tests/DynamicObject.Tests/DynamicObj.fs deleted file mode 100644 index 081bdf7..0000000 --- a/tests/DynamicObject.Tests/DynamicObj.fs +++ /dev/null @@ -1,384 +0,0 @@ -module DynamicObj.Tests - -open System -open Fable.Pyxpecto -open DynamicObj - -let tests_tryGetTypedValue = testList "TryGetTypedValue" [ - - testCase "typeof" <| fun _ -> - let a = typeof - Expect.equal a.Name "Int32" "Type should be Int32" - - testCase "NonExisting" <| fun _ -> - let a = DynamicObj() - let b = DynObj.tryGetTypedValue "a" a - Expect.isNone b "Value should not exist" - - testCase "Correct Int" <| fun _ -> - let a = DynamicObj() - a.SetValue("a", 1) - let b = DynObj.tryGetTypedValue "a" a - Expect.equal b (Some 1) "Value should be 1" - - testCase "Incorrect Int" <| fun _ -> - let a = DynamicObj() - a.SetValue("a", "1") - let b = DynObj.tryGetTypedValue "a" a - Expect.isNone b "Value should not be an int" - - testCase "Correct String" <| fun _ -> - let a = DynamicObj() - a.SetValue("a", "1") - let b = DynObj.tryGetTypedValue "a" a - Expect.equal b (Some "1") "Value should be '1'" - - testCase "Incorrect String" <| fun _ -> - let a = DynamicObj() - a.SetValue("a", 1) - let b = DynObj.tryGetTypedValue "a" a - Expect.isNone b "Value should not be a string" - - testCase "Correct List" <| fun _ -> - let a = DynamicObj() - a.SetValue("a", [1; 2; 3]) - let b = DynObj.tryGetTypedValue "a" a - Expect.equal b (Some [1; 2; 3]) "Value should be [1; 2; 3]" - - ptestCase "Incorrect List" <| fun _ -> - let a = DynamicObj() - a.SetValue("a", [1; 2; 3]) - let b = DynObj.tryGetTypedValue "a" a - Expect.isNone b "Value should not be a string list" - - testCase "Correct DynamicObj" <| fun _ -> - let a = DynamicObj() - let b = DynamicObj() - a.SetValue("a", b) - let c = DynObj.tryGetTypedValue "a" a - Expect.equal c (Some b) "Value should be a DynamicObj" - - testCase "Incorrect DynamicObj" <| fun _ -> - let a = DynamicObj() - a.SetValue("a", 1) - let b = DynObj.tryGetTypedValue "a" a - Expect.isNone b "Value should not be a DynamicObj" -] - - -let tests_set = testList "Set" [ - - testCase "Same String" <| fun _ -> - let a = DynamicObj () - a.SetValue("aaa", 5) - let b = DynamicObj () - b.SetValue("aaa", 5) - Expect.equal a b "Values should be equal" - Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" - - testCase "Different Strings" <| fun _ -> - let a = DynamicObj () - a.SetValue("aaa", 1212) - let b = DynamicObj () - b.SetValue("aaa", 5) - Expect.notEqual a b "Values should not be equal" - - testCase "String only on one" <| fun _ -> - let a = DynamicObj () - let b = DynamicObj () - b.SetValue("aaa", 5) - - Expect.notEqual a b "Values should not be equal" - Expect.notEqual b a "Values should not be equal (Reversed equality)" - - testCase "Same lists different keys" <| fun _ -> - let a' = DynamicObj () - let b' = DynamicObj () - a'.SetValue("quack!", [1; 2; 3]) - b'.SetValue("quack!1", [1; 2; 3]) - Expect.notEqual (a'.GetHashCode()) (b'.GetHashCode()) "Hash codes should not be equal" - - testCase "Different lists" <| fun _ -> - let a' = DynamicObj () - let b' = DynamicObj () - a'.SetValue("quack!", [1; 2; 3]) - b'.SetValue("quack!", [1; 2; 3; 4; 34]) - Expect.notEqual (a'.GetHashCode()) (b'.GetHashCode()) "Hash codes should not be equal" - - testCase "Nested Same List Same String" <| fun _ -> - let a = DynamicObj () - let b = DynamicObj () - - let a' = DynamicObj () - let b' = DynamicObj () - a'.SetValue("quack!", [1; 2; 3]) - b'.SetValue("quack!", [1; 2; 3]) - - a.SetValue("aaa", a') - b.SetValue("aaa", b') - Expect.equal a' b' "New Values should be equal" - Expect.equal a b "Old Values should be equal" - Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Old Hash codes should be equal" - Expect.equal (a'.GetHashCode()) (b'.GetHashCode()) "New Hash codes should be equal" - - testCase "Nested Same List Different Strings" <| fun _ -> - let a = DynamicObj () - let b = DynamicObj () - - let a' = DynamicObj () - let b' = DynamicObj () - a'.SetValue("quack!", [1; 2; 3]) - b'.SetValue("quack!", [1; 2; 3]) - - a.SetValue("aaa", a') - b.SetValue("aaa1", b') - Expect.equal a' b' "New Values should be equal" - Expect.notEqual a b "Old Values should not be equal" - Expect.equal (a'.GetHashCode()) (b'.GetHashCode()) "New Hash codes should be equal" - ] - -let tests_remove = testList "Remove" [ - - testCase "Remove" <| fun _ -> - let a = DynamicObj () - let b = DynamicObj () - - a.SetValue("quack!", "hello") - - a.Remove "quack!" |> ignore - - Expect.equal a b "Values should be equal" - Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" - - testCase "Remove Non-Existing" <| fun _ -> - let a = DynamicObj () - let b = DynamicObj () - - a.SetValue("quack!", "hello") - b.SetValue("quack!", "hello") - - a.Remove "quecky!" |> ignore - - Expect.equal a b "Values should be equal" - Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" - - testCase "Remove only on one" <| fun _ -> - let a = DynamicObj () - let b = DynamicObj () - - a.SetValue("quack!", "hello") - b.SetValue("quack!", "hello") - - a.Remove "quack!" |> ignore - - Expect.notEqual a b "Values should be unequal" - Expect.notEqual (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be unequal" - - testCase "Nested Remove Non-Existing" <| fun _ -> - let a = DynamicObj () - let b = DynamicObj () - - let a' = DynamicObj () - let b' = DynamicObj () - a'.SetValue("quack!", [1; 2; 3]) - b'.SetValue("quack!", [1; 2; 3]) - - a.SetValue("aaa", a') - a.Remove "quack!" |> ignore - b.SetValue("aaa", b') - - Expect.equal a b "Values should be equal" - Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" - - testCase "Nested Remove only on one" <| fun _ -> - let a = DynamicObj () - let b = DynamicObj () - - let a' = DynamicObj () - let b' = DynamicObj () - a'.SetValue("quack!", [1; 2; 3]) - b'.SetValue("quack!", [1; 2; 3]) - - a.SetValue("aaa", a') - a'.Remove "quack!" |> ignore - b.SetValue("aaa", b') - - Expect.notEqual a b "Values should be unequal" - Expect.notEqual (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be unequal" - - testCase "Nested Remove on both" <| fun _ -> - let a = DynamicObj () - let b = DynamicObj () - - let a' = DynamicObj () - let b' = DynamicObj () - a'.SetValue("quack!", [1; 2; 3]) - b'.SetValue("quack!", [1; 2; 3]) - - a.SetValue("aaa", a') - a.Remove "quack!" |> ignore - b.SetValue("aaa", b') - b.Remove "quack!" |> ignore - - Expect.equal a b "Values should be equal" - Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" - -] - -let tests_getProperties = testList "GetProperties" [ - testCase "GetProperties" <| fun _ -> - let a = DynamicObj() - a.SetValue("a", 1) - a.SetValue("b", 2) - let properties = a.GetPropertyHelpers(true) - let names = properties |> Seq.map (fun p -> p.Name) - Expect.equal (Seq.toList names) ["a"; "b"] "Should have all properties" -] - - -let tests_formatString = testList "FormatString" [ - - testCase "Format string 1" <| fun _ -> - let foo = DynamicObj() - let list = [1;2;3;4] - foo.SetValue("bar", list) - let expected = $"?bar: {list}" - Expect.equal (foo |> DynObj.format) expected "Format string 1 failed" - - testCase "Format string 2" <| fun _ -> - let foo = DynamicObj() - let corgi = "corgi" - foo.SetValue("corgi", corgi) - let inner = DynamicObj() - let baz = "baz" - inner.SetValue("bar", baz) - foo.SetValue("foo", inner) - let expected = $"""?corgi: {corgi}{Environment.NewLine}?foo:{Environment.NewLine} ?bar: {baz}""" - Expect.equal (foo |> DynObj.format) expected "Format string 2 failed" - -] - - -let tests_combine = testList "Combine" [ - - testCase "Combine flat DOs" <| fun _ -> - let target = DynamicObj() - - target.SetValue("target-unique", [42]) - target.SetValue("will-be-overridden", "WAS_NOT_OVERRIDDEN!") - - let source = DynamicObj() - - source.SetValue("source-unique", [42; 32]) - source.SetValue("will-be-overridden", "WAS_OVERRIDDEN =)") - - let combined = DynObj.combine target source - - let expected = DynamicObj() - - expected.SetValue("target-unique", [42]) - expected.SetValue("source-unique", [42; 32]) - expected.SetValue("will-be-overridden", "WAS_OVERRIDDEN =)") - - Expect.equal expected combined "Combine flat DOs failed" - - testCase "Combine nested DOs" <| fun _ -> - let target = DynamicObj() - - target.SetValue("target-unique", 1337) - target.SetValue("will-be-overridden", -42) - let something2BeCombined = DynamicObj() - something2BeCombined.SetValue("inner","I Am") - let something2BeOverriden = DynamicObj() - something2BeOverriden.SetValue("inner","NOT_OVERRIDDEN") - target.SetValue("nested-will-be-combined", something2BeCombined) - target.SetValue("nested-will-be-overridden", something2BeOverriden) - - let source = DynamicObj() - - source.SetValue("source-unique", 69) - source.SetValue("will-be-overridden", "WAS_OVERRIDDEN") - let alsoSomething2BeCombined = DynamicObj() - alsoSomething2BeCombined.SetValue("inner_combined","Complete") - source.SetValue("nested-will-be-combined", alsoSomething2BeCombined) - source.SetValue("nested-will-be-overridden", "WAS_OVERRIDDEN") - - let combined = DynObj.combine target source - - let expected = DynamicObj() - - expected.SetValue("source-unique", 69) - expected.SetValue("target-unique", 1337) - expected.SetValue("will-be-overridden", "WAS_OVERRIDDEN") - expected.SetValue("nested-will-be-overridden", "WAS_OVERRIDDEN") - expected.SetValue("nested-will-be-combined", - let inner = DynamicObj() - inner.SetValue("inner","I Am") - inner.SetValue("inner_combined","Complete") - inner - ) - - Expect.equal expected combined "Combine nested DOs failed" -] - -let tests_print = testList "Print" [ - - testCase "Test Print For Issue 14" <| fun _ -> - let outer = DynamicObj() - let inner = DynamicObj() - inner.SetValue("Level", "Information") - inner.SetValue("MessageTemplate","{Method} Request at {Path}") - outer.SetValue("serilog", inner) - - let print = - try - outer |> DynObj.print - true - with - | e -> false - - Expect.isTrue print "Print failed for issue 14" -] - -let tests_copyDynamicProperties = testList "CopyDynamicProperties" [ - testCase "NewObject" <| fun _ -> - let a = DynamicObj() - a.SetValue("a", 1) - a.SetValue("b", 2) - let b = a.CopyDynamicProperties() - Expect.equal a b "Values should be equal" - testCase "ExistingObject" <| fun _ -> - let a = DynamicObj() - a.SetValue("a", 1) - a.SetValue("b", 2) - let b = DynamicObj() - b.SetValue("c", 3) - a.CopyDynamicPropertiesTo(b) - Expect.equal (b.GetValue("a")) 1 "Value a should be copied" - Expect.equal (b.GetValue("b")) 2 "Value b should be copied" - Expect.equal (b.GetValue("c")) 3 "Value c should be unaffected" - testCase "NoOverwrite throws" <| fun _ -> - let a = DynamicObj() - a.SetValue("a", 1) - let b = DynamicObj() - b.SetValue("a", 3) - let f = fun () -> a.CopyDynamicPropertiesTo(b) - Expect.throws f "Should throw because property exists" - testCase "Overwrite" <| fun _ -> - let a = DynamicObj() - a.SetValue("a", 1) - let b = DynamicObj() - b.SetValue("a", 3) - Expect.notEqual a b "Values should not be equal before copying" - a.CopyDynamicPropertiesTo(b, true) - Expect.equal a b "Values should be equal" -] - -let main = testList "DynamicObj" [ - tests_tryGetTypedValue - tests_set - tests_remove - tests_formatString - tests_combine - tests_copyDynamicProperties -] \ No newline at end of file diff --git a/tests/DynamicObject.Tests/DynamicObject.Tests.fsproj b/tests/DynamicObject.Tests/DynamicObject.Tests.fsproj index 8d90464..08c1afb 100644 --- a/tests/DynamicObject.Tests/DynamicObject.Tests.fsproj +++ b/tests/DynamicObject.Tests/DynamicObject.Tests.fsproj @@ -9,9 +9,10 @@ - + + diff --git a/tests/DynamicObject.Tests/InstanceMethods.fs b/tests/DynamicObject.Tests/InstanceMethods.fs new file mode 100644 index 0000000..690195b --- /dev/null +++ b/tests/DynamicObject.Tests/InstanceMethods.fs @@ -0,0 +1,556 @@ +module InstanceMethods.Tests + +open System +open Fable.Pyxpecto +open DynamicObj + +let tests_TryGetValue = testList "TryGetValue" [ + testCase "NonExisting" <| fun _ -> + let a = DynamicObj() + let b = a.TryGetValue "a" + Expect.isNone b "Value should not exist" + + testCase "Correct boxed Int" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", 1) + let b = a.TryGetValue "a" + Expect.equal (b) (Some (box 1)) "Value should be 1" + + testCase "Correct unboxed Int" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", 1) + let b = a.TryGetValue "a" + Expect.equal (b |> Option.map unbox) (Some 1) "Value should be 1" + + testCase "Correct boxed String" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", "1") + let b = a.TryGetValue "a" + Expect.equal (b) (Some (box "1")) "Value should be '1'" + + testCase "Correct unboxed String" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", "1") + let b = a.TryGetValue "a" + Expect.equal (b |> Option.map unbox) (Some "1") "Value should be '1'" + + testCase "Correct boxed List" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", [1; 2; 3]) + let b = a.TryGetValue "a" + Expect.equal (b) (Some (box [1; 2; 3])) "Value should be [1; 2; 3]" + + testCase "Correct unboxed List" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", [1; 2; 3]) + let b = a.TryGetValue "a" + Expect.equal (b |> Option.map unbox) (Some [1; 2; 3]) "Value should be [1; 2; 3]" + + testCase "Correct boxed DynamicObj" <| fun _ -> + let a = DynamicObj() + let b = DynamicObj() + a.SetValue("a", b) + let c = a.TryGetValue "a" + Expect.equal (c) (Some (box b)) "Value should be a DynamicObj" + + testCase "Correct unboxed DynamicObj" <| fun _ -> + let a = DynamicObj() + let b = DynamicObj() + a.SetValue("a", b) + let c = a.TryGetValue "a" + Expect.equal (c |> Option.map unbox) (Some b) "Value should be a DynamicObj" + +] + +let tests_GetValue = testList "GetValue" [ + testCase "NonExisting" <| fun _ -> + let a = DynamicObj() + Expect.throws (fun () -> a.GetValue("b") |> ignore) "Value should not exist" + + testCase "Correct boxed Int" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", 1) + let b = a.GetValue "a" + Expect.equal (b) (box 1) "Value should be 1" + + testCase "Correct unboxed Int" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", 1) + let b = a.GetValue "a" + Expect.equal (b |> unbox) (1) "Value should be 1" + + testCase "Correct boxed String" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", "1") + let b = a.GetValue "a" + Expect.equal (b) (box "1") "Value should be '1'" + + testCase "Correct unboxed String" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", "1") + let b = a.GetValue "a" + Expect.equal (b |> unbox) ("1") "Value should be '1'" + + testCase "Correct boxed List" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", [1; 2; 3]) + let b = a.GetValue "a" + Expect.equal (b) (box [1; 2; 3]) "Value should be [1; 2; 3]" + + testCase "Correct unboxed List" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", [1; 2; 3]) + let b = a.GetValue "a" + Expect.equal (b |> unbox) ([1; 2; 3]) "Value should be [1; 2; 3]" + + testCase "Correct boxed DynamicObj" <| fun _ -> + let a = DynamicObj() + let b = DynamicObj() + a.SetValue("a", b) + let c = a.GetValue "a" + Expect.equal (c) (box b) "Value should be a DynamicObj" + + testCase "Correct unboxed DynamicObj" <| fun _ -> + let a = DynamicObj() + let b = DynamicObj() + a.SetValue("a", b) + let c = a.GetValue "a" + Expect.equal (c |> unbox) (b) "Value should be a DynamicObj" + +] + +#if !FABLE_COMPILER +// instance method TryGetTypedValue is not Fable-compatible +let tests_TryGetTypedValue = testList "TryGetTypedValue" [ + + testCase "typeof" <| fun _ -> + let a = typeof + Expect.equal a.Name "Int32" "Type should be Int32" + + testCase "NonExisting" <| fun _ -> + let a = DynamicObj() + let b = a.TryGetTypedValue "a" + Expect.isNone b "Value should not exist" + + testCase "Correct Int" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", 1) + let b = a.TryGetTypedValue "a" + Expect.equal b (Some 1) "Value should be 1" + + testCase "Incorrect Int" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", "1") + let b = a.TryGetTypedValue "a" + Expect.isNone b "Value should not be an int" + + testCase "Correct String" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", "1") + let b = a.TryGetTypedValue "a" + Expect.equal b (Some "1") "Value should be '1'" + + testCase "Incorrect String" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", 1) + let b = a.TryGetTypedValue "a" + Expect.isNone b "Value should not be a string" + + testCase "Correct List" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", [1; 2; 3]) + let b = a.TryGetTypedValue "a" + Expect.equal b (Some [1; 2; 3]) "Value should be [1; 2; 3]" + + testCase "Incorrect List" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", [1; 2; 3]) + let b = a.TryGetTypedValue "a" + Expect.isNone b "Value should not be a string list" + + testCase "Correct DynamicObj" <| fun _ -> + let a = DynamicObj() + let b = DynamicObj() + a.SetValue("a", b) + let c = a.TryGetTypedValue "a" + Expect.equal c (Some b) "Value should be a DynamicObj" + + testCase "Incorrect DynamicObj" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", 1) + let b = a.TryGetTypedValue "a" + Expect.isNone b "Value should not be a DynamicObj" +] +#endif + +let tests_TryGetStaticPropertyInfo = testList "TryGetStaticPropertyInfo" [ + testCase "NonExisting" <| fun _ -> + let a = DynamicObj() + let b = a.TryGetStaticPropertyInfo("a") + Expect.isNone b "Value should not exist" + + testCase "Properties dictionary is static property" <| fun _ -> + let a = DynamicObj() + let b = Expect.wantSome (a.TryGetStaticPropertyInfo("Properties")) "Value should exist" + Expect.isTrue b.IsStatic "Properties should be static" + Expect.isFalse b.IsDynamic "Properties should not be dynamic" + Expect.isTrue b.IsMutable "Properties should be mutable" + Expect.isFalse b.IsImmutable "Properties should not be immutable" + + testCase "dynamic property not retrieved as static" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", 1) + Expect.isNone (a.TryGetStaticPropertyInfo("a")) "dynamic property should not be retrieved via TryGetStaticPropertyInfo" +] + +let tests_TryGetDynamicPropertyInfo = testList "TryGetDynamicPropertyInfo" [ + testCase "NonExisting" <| fun _ -> + let a = DynamicObj() + let b = a.TryGetDynamicPropertyInfo("a") + Expect.isNone b "Value should not exist" + + testCase "Existing dynamic property" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", 1) + let b = Expect.wantSome (a.TryGetDynamicPropertyInfo("a")) "Value should exist" + Expect.isFalse b.IsStatic "Properties should be static" + Expect.isTrue b.IsDynamic "Properties should not be dynamic" + Expect.isTrue b.IsMutable "Properties should be mutable" + Expect.isFalse b.IsImmutable "Properties should not be immutable" + + testCase "static property not retrieved as dynamic" <| fun _ -> + let a = DynamicObj() + Expect.isNone (a.TryGetDynamicPropertyInfo("Properties")) "static property should not be retrieved via TryGetDynamicPropertyInfo" +] + +let tests_TryGetPropertyInfo = testList "TryGetPropertyInfo" [ + testCase "NonExisting" <| fun _ -> + let a = DynamicObj() + let b = a.TryGetPropertyInfo("a") + Expect.isNone b "Value should not exist" + + testCase "Existing dynamic property" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", 1) + let b = Expect.wantSome (a.TryGetPropertyInfo("a")) "Value should exist" + Expect.isFalse b.IsStatic "Properties should be static" + Expect.isTrue b.IsDynamic "Properties should not be dynamic" + Expect.isTrue b.IsMutable "Properties should be mutable" + Expect.isFalse b.IsImmutable "Properties should not be immutable" + + testCase "Existing static property" <| fun _ -> + let a = DynamicObj() + let b = Expect.wantSome (a.TryGetPropertyInfo("Properties")) "Value should exist" + Expect.isTrue b.IsStatic "Properties should be static" + Expect.isFalse b.IsDynamic "Properties should not be dynamic" + Expect.isTrue b.IsMutable "Properties should be mutable" + Expect.isFalse b.IsImmutable "Properties should not be immutable" +] + +let tests_SetValue = testList "SetValue" [ + + testCase "Same String" <| fun _ -> + let a = DynamicObj () + a.SetValue("aaa", 5) + let b = DynamicObj () + b.SetValue("aaa", 5) + Expect.equal a b "Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + + testCase "Different Strings" <| fun _ -> + let a = DynamicObj () + a.SetValue("aaa", 1212) + let b = DynamicObj () + b.SetValue("aaa", 5) + Expect.notEqual a b "Values should not be equal" + + testCase "String only on one" <| fun _ -> + let a = DynamicObj () + let b = DynamicObj () + b.SetValue("aaa", 5) + + Expect.notEqual a b "Values should not be equal" + Expect.notEqual b a "Values should not be equal (Reversed equality)" + + testCase "Same lists different keys" <| fun _ -> + let a' = DynamicObj () + let b' = DynamicObj () + a'.SetValue("quack!", [1; 2; 3]) + b'.SetValue("quack!1", [1; 2; 3]) + Expect.notEqual (a'.GetHashCode()) (b'.GetHashCode()) "Hash codes should not be equal" + + testCase "Different lists" <| fun _ -> + let a' = DynamicObj () + let b' = DynamicObj () + a'.SetValue("quack!", [1; 2; 3]) + b'.SetValue("quack!", [1; 2; 3; 4; 34]) + Expect.notEqual (a'.GetHashCode()) (b'.GetHashCode()) "Hash codes should not be equal" + + testCase "Nested Same List Same String" <| fun _ -> + let a = DynamicObj () + let b = DynamicObj () + + let a' = DynamicObj () + let b' = DynamicObj () + a'.SetValue("quack!", [1; 2; 3]) + b'.SetValue("quack!", [1; 2; 3]) + + a.SetValue("aaa", a') + b.SetValue("aaa", b') + Expect.equal a' b' "New Values should be equal" + Expect.equal a b "Old Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Old Hash codes should be equal" + Expect.equal (a'.GetHashCode()) (b'.GetHashCode()) "New Hash codes should be equal" + + testCase "Nested Same List Different Strings" <| fun _ -> + let a = DynamicObj () + let b = DynamicObj () + + let a' = DynamicObj () + let b' = DynamicObj () + a'.SetValue("quack!", [1; 2; 3]) + b'.SetValue("quack!", [1; 2; 3]) + + a.SetValue("aaa", a') + b.SetValue("aaa1", b') + Expect.equal a' b' "New Values should be equal" + Expect.notEqual a b "Old Values should not be equal" + Expect.equal (a'.GetHashCode()) (b'.GetHashCode()) "New Hash codes should be equal" + ] + +let tests_Remove = testList "Remove" [ + + testCase "Remove" <| fun _ -> + let a = DynamicObj () + let b = DynamicObj () + + a.SetValue("quack!", "hello") + + a.Remove "quack!" |> ignore + + Expect.equal a b "Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + + testCase "Remove Non-Existing" <| fun _ -> + let a = DynamicObj () + let b = DynamicObj () + + a.SetValue("quack!", "hello") + b.SetValue("quack!", "hello") + + a.Remove "quecky!" |> ignore + + Expect.equal a b "Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + + testCase "Remove only on one" <| fun _ -> + let a = DynamicObj () + let b = DynamicObj () + + a.SetValue("quack!", "hello") + b.SetValue("quack!", "hello") + + a.Remove "quack!" |> ignore + + Expect.notEqual a b "Values should be unequal" + Expect.notEqual (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be unequal" + + testCase "Nested Remove Non-Existing" <| fun _ -> + let a = DynamicObj () + let b = DynamicObj () + + let a' = DynamicObj () + let b' = DynamicObj () + a'.SetValue("quack!", [1; 2; 3]) + b'.SetValue("quack!", [1; 2; 3]) + + a.SetValue("aaa", a') + a.Remove "quack!" |> ignore + b.SetValue("aaa", b') + + Expect.equal a b "Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + + testCase "Nested Remove only on one" <| fun _ -> + let a = DynamicObj () + let b = DynamicObj () + + let a' = DynamicObj () + let b' = DynamicObj () + a'.SetValue("quack!", [1; 2; 3]) + b'.SetValue("quack!", [1; 2; 3]) + + a.SetValue("aaa", a') + a'.Remove "quack!" |> ignore + b.SetValue("aaa", b') + + Expect.notEqual a b "Values should be unequal" + Expect.notEqual (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be unequal" + + testCase "Nested Remove on both" <| fun _ -> + let a = DynamicObj () + let b = DynamicObj () + + let a' = DynamicObj () + let b' = DynamicObj () + a'.SetValue("quack!", [1; 2; 3]) + b'.SetValue("quack!", [1; 2; 3]) + + a.SetValue("aaa", a') + a.Remove "quack!" |> ignore + b.SetValue("aaa", b') + b.Remove "quack!" |> ignore + + Expect.equal a b "Values should be equal" + Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Hash codes should be equal" + +] + +let tests_GetPropertyHelpers = testList "GetPropertyHelpers" [ + testCase "GetPropertyHelpers" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", 1) + a.SetValue("b", 2) + let properties = a.GetPropertyHelpers(true) + let names = properties |> Seq.map (fun p -> p.Name) + Expect.equal (Seq.toList names) ["a"; "b"] "Should have all properties" +] + +let tests_GetProperties = testList "GetProperties" [ + testCase "GetProperties" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", 1) + a.SetValue("b", 2) + let properties = a.GetProperties(true) |> List.ofSeq + let expected = [ + System.Collections.Generic.KeyValuePair("a", box 1) + System.Collections.Generic.KeyValuePair("b", box 2) + ] + Expect.equal properties expected "Should have all properties" +] + +let tests_CopyDynamicPropertiesTo = testList "CopyDynamicPropertiesTo" [ + testCase "ExistingObject" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", 1) + a.SetValue("b", 2) + let b = DynamicObj() + b.SetValue("c", 3) + a.CopyDynamicPropertiesTo(b) + Expect.equal (b.GetValue("a")) 1 "Value a should be copied" + Expect.equal (b.GetValue("b")) 2 "Value b should be copied" + Expect.equal (b.GetValue("c")) 3 "Value c should be unaffected" + + testCase "NoOverwrite throws" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", 1) + let b = DynamicObj() + b.SetValue("a", 3) + let f = fun () -> a.CopyDynamicPropertiesTo(b) + Expect.throws f "Should throw because property exists" + + testCase "Overwrite" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", 1) + let b = DynamicObj() + b.SetValue("a", 3) + Expect.notEqual a b "Values should not be equal before copying" + a.CopyDynamicPropertiesTo(b, true) + Expect.equal a b "Values should be equal" +] + +let tests_CopyDynamicProperties = testList "CopyDynamicProperties" [ + testCase "NewObject" <| fun _ -> + let a = DynamicObj() + a.SetValue("a", 1) + a.SetValue("b", 2) + let b = a.CopyDynamicProperties() + Expect.equal a b "Values should be equal" +] + +let tests_Equals = testList "Equals" [ + testCase "Same Object" <| fun _ -> + let a = DynamicObj() + a.SetValue("b", 2) + Expect.isTrue (a.Equals(a)) "Values should be equal" + + testCase "Different Equal Objects" <| fun _ -> + let a = DynamicObj() + a.SetValue("b", 2) + let a2 = DynamicObj() + a2.SetValue("b", 2) + Expect.isTrue (a.Equals(a2)) "Values should be equal" + + testCase "Different Unequal Objects" <| fun _ -> + let a = DynamicObj() + a.SetValue("b", 2) + let a2 = DynamicObj() + a2.SetValue("b", 3) + Expect.isFalse (a.Equals(a2)) "Values should not be equal" + + testCase "nested DynamicObjs" <| fun _ -> + let a = DynamicObj() + let b = DynamicObj() + b.SetValue("c", 2) + a.SetValue("b", b) + let a2 = DynamicObj() + let b2 = DynamicObj() + b2.SetValue("c", 2) + a2.SetValue("b", b2) + Expect.isTrue (a.Equals(a2)) "Values should be equal" + +] + +let tests_GetHashCode = testList "GetHashCode" [ + testCase "Same Object" <| fun _ -> + let a = DynamicObj() + a.SetValue("b", 2) + Expect.equal (a.GetHashCode()) (a.GetHashCode()) "Values should be equal" + + testCase "Different Equal Objects" <| fun _ -> + let a = DynamicObj() + a.SetValue("b", 2) + let a2 = DynamicObj() + a2.SetValue("b", 2) + Expect.equal (a.GetHashCode()) (a2.GetHashCode()) "Values should be equal" + + testCase "Different Unequal Objects" <| fun _ -> + let a = DynamicObj() + a.SetValue("b", 2) + let a2 = DynamicObj() + a.SetValue("b", 3) + Expect.notEqual (a.GetHashCode()) (a2.GetHashCode()) "Values should not be equal" + + testCase "nested DynamicObjs" <| fun _ -> + let a = DynamicObj() + let b = DynamicObj() + b.SetValue("c", 2) + a.SetValue("b", b) + let a2 = DynamicObj() + let b2 = DynamicObj() + b2.SetValue("c", 2) + a2.SetValue("b", b2) + Expect.equal (a.GetHashCode()) (a2.GetHashCode()) "Values should be equal" +] + +let main = testList "Instance Methods" [ + tests_TryGetValue + tests_GetValue + + #if !FABLE_COMPILER + // instance method TryGetTypedValue is not Fable-compatible + tests_TryGetTypedValue + #endif + + tests_TryGetStaticPropertyInfo + tests_TryGetDynamicPropertyInfo + tests_TryGetPropertyInfo + tests_SetValue + tests_Remove + tests_GetPropertyHelpers + tests_GetProperties + tests_CopyDynamicPropertiesTo + tests_CopyDynamicProperties + tests_Equals + tests_GetHashCode +] \ No newline at end of file diff --git a/tests/DynamicObject.Tests/Main.fs b/tests/DynamicObject.Tests/Main.fs index 5d0bf8c..d1e33e0 100644 --- a/tests/DynamicObject.Tests/Main.fs +++ b/tests/DynamicObject.Tests/Main.fs @@ -4,7 +4,8 @@ open Fable.Pyxpecto let all = testSequenced <| testList "DynamicObj" [ ReflectionUtils.Tests.main - DynamicObj.Tests.main + InstanceMethods.Tests.main + DynObjStaticMethods.Tests.main Inheritance.Tests.main Interface.Tests.main ]