From cb2f0316becd6d5ac2952d2e04188b3d21d6328a Mon Sep 17 00:00:00 2001 From: Gary Sassano <10464497+garysassano@users.noreply.github.com> Date: Mon, 9 Oct 2023 02:13:43 +0200 Subject: [PATCH] chore(sdk): add missing tests for `Json` (#4399) Adding missing `Json` tests for tracking issue #2785 *By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*. --- examples/tests/sdk_tests/std/json.test.w | 94 +++++-- .../std/json.test.w_compile_tf-aws.md | 255 ++++++++++++++++-- .../sdk_tests/std/json.test.w_test_sim.md | 18 +- 3 files changed, 313 insertions(+), 54 deletions(-) diff --git a/examples/tests/sdk_tests/std/json.test.w b/examples/tests/sdk_tests/std/json.test.w index e459ac69c44..05e725b5747 100644 --- a/examples/tests/sdk_tests/std/json.test.w +++ b/examples/tests/sdk_tests/std/json.test.w @@ -3,6 +3,13 @@ //----------------------------------------------------------------------------- bring cloud; +test "has()" { + let obj = Json { key1: 1, key2: 2}; + + assert(Json.has(obj, "key1") == true); + assert(Json.has(obj, "key3") == false); +} + test "get()" { let assertThrows = (expected: str, block: (): void) => { let var error = false; @@ -104,24 +111,71 @@ test "keys(), values(), entries()" { } } -//----------------------------------------------------------------------------- -// tryParse() -assert(Json.tryParse(nil) == nil); -assert(Json.tryParse("boom") == nil); -assert(Json.tryParse("") == nil); - -/* -Will add test later: -test "setWithNonMutJsonObject()" { -let var error = ""; -try { - let f = MutJson { e: 4 }; - f.set("f", 9); - } catch e { - error = e; - } - - log(error); - assert(error == ""); +test "parse()" { + let obj = Json { key1: 1, key2: 2}; + let String = "{\"key\":1,\"key2\":2}"; + + assert(Json.parse("123") == 123); + assert(Json.parse("true") == true); + assert(Json.parse("\"foo\"") == "foo"); + /** + * To be fixed in the compiler + * see https://github.com/winglang/wing/issues/4131 + */ + // assert(Json.parse("[1,5,false]") == "[1,5,false]"); + // assert(Json.parse(String) == obj); + /** + * To uncomment once Json null is implemented + * see https://github.com/winglang/wing/issues/1819 + */ + // assert(Json.parse("null") == null); +} + +test "tryParse()" { + let obj = Json { key1: 1, key2: 2}; + let String = "{\"key\":1,\"key2\":2}"; + + assert(Json.tryParse("123") == 123); + assert(Json.tryParse("true") == true); + assert(Json.tryParse("\"foo\"") == "foo"); + /** + * To be fixed in the compiler + * see https://github.com/winglang/wing/issues/4131 + */ + // assert(Json.tryParse("[1,5,false]") == "[1,5,false]"); + // assert(Json.tryParse(String) == obj); + /** + * To uncomment once Json null is implemented + * see https://github.com/winglang/wing/issues/1819 + */ + // assert(Json.tryParse("null") == null); + assert(Json.tryParse("foo") == nil); + assert(Json.tryParse("") == nil); + assert(Json.tryParse(nil) == nil); } -*/ \ No newline at end of file + +test "deepCopy(), deepCopyMut()" { + let original = Json ({ + "string": "wing", + "number": 123, + "array": [1, 2, 3], + "true": true, + "false": false, + "object": { + "key1": "value1", + "key2": 2, + "key3": false, + "key5": [3, 2, 1] + } + }); + let mutation = Json{ key1: 1, key2: 2}; + + let copy = Json.deepCopy(original); + let copyMut = Json.deepCopyMut(original); + + assert(copy == copyMut); + copyMut.set("object", mutation); + assert(copy != copyMut); + + assert(copyMut.get("object") == mutation); +} \ No newline at end of file diff --git a/tools/hangar/__snapshots__/test_corpus/sdk_tests/std/json.test.w_compile_tf-aws.md b/tools/hangar/__snapshots__/test_corpus/sdk_tests/std/json.test.w_compile_tf-aws.md index fd90572da26..fba0ff1f8ff 100644 --- a/tools/hangar/__snapshots__/test_corpus/sdk_tests/std/json.test.w_compile_tf-aws.md +++ b/tools/hangar/__snapshots__/test_corpus/sdk_tests/std/json.test.w_compile_tf-aws.md @@ -2,8 +2,53 @@ ## inflight.$Closure1-1.js ```js -module.exports = function({ }) { +module.exports = function({ $std_Json }) { class $Closure1 { + constructor({ }) { + const $obj = (...args) => this.handle(...args); + Object.setPrototypeOf($obj, this); + return $obj; + } + async handle() { + const obj = ({"key1": 1,"key2": 2}); + {((cond) => {if (!cond) throw new Error("assertion failed: Json.has(obj, \"key1\") == true")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(((args) => { return args[0].hasOwnProperty(args[1]); })([obj,"key1"]),true)))}; + {((cond) => {if (!cond) throw new Error("assertion failed: Json.has(obj, \"key3\") == false")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(((args) => { return args[0].hasOwnProperty(args[1]); })([obj,"key3"]),false)))}; + } + } + return $Closure1; +} + +``` + +## inflight.$Closure10-1.js +```js +module.exports = function({ $std_Json }) { + class $Closure10 { + constructor({ }) { + const $obj = (...args) => this.handle(...args); + Object.setPrototypeOf($obj, this); + return $obj; + } + async handle() { + const original = ({"string": "wing","number": 123,"array": [1, 2, 3],"true": true,"false": false,"object": ({"key1": "value1","key2": 2,"key3": false,"key5": [3, 2, 1]})}); + const mutation = ({"key1": 1,"key2": 2}); + const copy = JSON.parse(JSON.stringify(original)); + const copyMut = (JSON.parse(JSON.stringify(original))); + {((cond) => {if (!cond) throw new Error("assertion failed: copy == copyMut")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(copy,copyMut)))}; + ((obj, args) => { obj[args[0]] = args[1]; })(copyMut, ["object",mutation]); + {((cond) => {if (!cond) throw new Error("assertion failed: copy != copyMut")})((((a,b) => { try { return require('assert').notDeepStrictEqual(a,b) === undefined; } catch { return false; } })(copy,copyMut)))}; + {((cond) => {if (!cond) throw new Error("assertion failed: copyMut.get(\"object\") == mutation")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(((obj, args) => { if (obj[args] === undefined) throw new Error(`Json property "${args}" does not exist`); return obj[args] })(copyMut, "object"),mutation)))}; + } + } + return $Closure10; +} + +``` + +## inflight.$Closure2-1.js +```js +module.exports = function({ }) { + class $Closure2 { constructor({ }) { const $obj = (...args) => this.handle(...args); Object.setPrototypeOf($obj, this); @@ -38,15 +83,15 @@ module.exports = function({ }) { )); } } - return $Closure1; + return $Closure2; } ``` -## inflight.$Closure2-1.js +## inflight.$Closure3-1.js ```js module.exports = function({ }) { - class $Closure2 { + class $Closure3 { constructor({ }) { const $obj = (...args) => this.handle(...args); Object.setPrototypeOf($obj, this); @@ -78,15 +123,15 @@ module.exports = function({ }) { )); } } - return $Closure2; + return $Closure3; } ``` -## inflight.$Closure3-1.js +## inflight.$Closure4-1.js ```js module.exports = function({ }) { - class $Closure3 { + class $Closure4 { constructor({ }) { const $obj = (...args) => this.handle(...args); Object.setPrototypeOf($obj, this); @@ -100,15 +145,15 @@ module.exports = function({ }) { {((cond) => {if (!cond) throw new Error("assertion failed: mutObj.get(\"z\") == 3")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(((obj, args) => { if (obj[args] === undefined) throw new Error(`Json property "${args}" does not exist`); return obj[args] })(mutObj, "z"),3)))}; } } - return $Closure3; + return $Closure4; } ``` -## inflight.$Closure4-1.js +## inflight.$Closure5-1.js ```js module.exports = function({ }) { - class $Closure4 { + class $Closure5 { constructor({ }) { const $obj = (...args) => this.handle(...args); Object.setPrototypeOf($obj, this); @@ -122,15 +167,15 @@ module.exports = function({ }) { {((cond) => {if (!cond) throw new Error("assertion failed: mutJsonArray.getAt(3) == 3")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(((obj, args) => { if (obj[args] === undefined) throw new Error("Index out of bounds"); return obj[args] })(mutJsonArray, 3),3)))}; } } - return $Closure4; + return $Closure5; } ``` -## inflight.$Closure5-1.js +## inflight.$Closure6-1.js ```js module.exports = function({ $std_Json }) { - class $Closure5 { + class $Closure6 { constructor({ }) { const $obj = (...args) => this.handle(...args); Object.setPrototypeOf($obj, this); @@ -144,15 +189,15 @@ module.exports = function({ $std_Json }) { {((cond) => {if (!cond) throw new Error("assertion failed: stringifiedIndent == \"{\\n \\\"a\\\": 1,\\n \\\"b\\\": 2\\n}\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(stringifiedIndent,"{\n \"a\": 1,\n \"b\": 2\n}")))}; } } - return $Closure5; + return $Closure6; } ``` -## inflight.$Closure6-1.js +## inflight.$Closure7-1.js ```js module.exports = function({ $std_Json }) { - class $Closure6 { + class $Closure7 { constructor({ }) { const $obj = (...args) => this.handle(...args); Object.setPrototypeOf($obj, this); @@ -171,7 +216,54 @@ module.exports = function({ $std_Json }) { } } } - return $Closure6; + return $Closure7; +} + +``` + +## inflight.$Closure8-1.js +```js +module.exports = function({ $std_Json }) { + class $Closure8 { + constructor({ }) { + const $obj = (...args) => this.handle(...args); + Object.setPrototypeOf($obj, this); + return $obj; + } + async handle() { + const obj = ({"key1": 1,"key2": 2}); + const String = "{\"key\":1,\"key2\":2}"; + {((cond) => {if (!cond) throw new Error("assertion failed: Json.parse(\"123\") == 123")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })((JSON.parse("123")),123)))}; + {((cond) => {if (!cond) throw new Error("assertion failed: Json.parse(\"true\") == true")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })((JSON.parse("true")),true)))}; + {((cond) => {if (!cond) throw new Error("assertion failed: Json.parse(\"\\\"foo\\\"\") == \"foo\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })((JSON.parse("\"foo\"")),"foo")))}; + } + } + return $Closure8; +} + +``` + +## inflight.$Closure9-1.js +```js +module.exports = function({ $std_Json }) { + class $Closure9 { + constructor({ }) { + const $obj = (...args) => this.handle(...args); + Object.setPrototypeOf($obj, this); + return $obj; + } + async handle() { + const obj = ({"key1": 1,"key2": 2}); + const String = "{\"key\":1,\"key2\":2}"; + {((cond) => {if (!cond) throw new Error("assertion failed: Json.tryParse(\"123\") == 123")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(((args) => { try { return (args === undefined) ? undefined : JSON.parse(args); } catch (err) { return undefined; } })("123"),123)))}; + {((cond) => {if (!cond) throw new Error("assertion failed: Json.tryParse(\"true\") == true")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(((args) => { try { return (args === undefined) ? undefined : JSON.parse(args); } catch (err) { return undefined; } })("true"),true)))}; + {((cond) => {if (!cond) throw new Error("assertion failed: Json.tryParse(\"\\\"foo\\\"\") == \"foo\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(((args) => { try { return (args === undefined) ? undefined : JSON.parse(args); } catch (err) { return undefined; } })("\"foo\""),"foo")))}; + {((cond) => {if (!cond) throw new Error("assertion failed: Json.tryParse(\"foo\") == nil")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(((args) => { try { return (args === undefined) ? undefined : JSON.parse(args); } catch (err) { return undefined; } })("foo"),undefined)))}; + {((cond) => {if (!cond) throw new Error("assertion failed: Json.tryParse(\"\") == nil")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(((args) => { try { return (args === undefined) ? undefined : JSON.parse(args); } catch (err) { return undefined; } })(""),undefined)))}; + {((cond) => {if (!cond) throw new Error("assertion failed: Json.tryParse(nil) == nil")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(((args) => { try { return (args === undefined) ? undefined : JSON.parse(args); } catch (err) { return undefined; } })(undefined),undefined)))}; + } + } + return $Closure9; } ``` @@ -227,6 +319,7 @@ class $Root extends $stdlib.std.Resource { static _toInflightType(context) { return ` require("./inflight.$Closure1-1.js")({ + $std_Json: ${context._lift($stdlib.core.toLiftableModuleType(std.Json, "@winglang/sdk/std", "Json"))}, }) `; } @@ -331,7 +424,6 @@ class $Root extends $stdlib.std.Resource { static _toInflightType(context) { return ` require("./inflight.$Closure5-1.js")({ - $std_Json: ${context._lift($stdlib.core.toLiftableModuleType(std.Json, "@winglang/sdk/std", "Json"))}, }) `; } @@ -377,15 +469,124 @@ class $Root extends $stdlib.std.Resource { return ["handle", "$inflight_init"]; } } - this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:get()",new $Closure1(this,"$Closure1")); - this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:getAt()",new $Closure2(this,"$Closure2")); - this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:set()",new $Closure3(this,"$Closure3")); - this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:setAt()",new $Closure4(this,"$Closure4")); - this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:stringify()",new $Closure5(this,"$Closure5")); - this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:keys(), values(), entries()",new $Closure6(this,"$Closure6")); - {((cond) => {if (!cond) throw new Error("assertion failed: Json.tryParse(nil) == nil")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(((args) => { try { return (args === undefined) ? undefined : JSON.parse(args); } catch (err) { return undefined; } })(undefined),undefined)))}; - {((cond) => {if (!cond) throw new Error("assertion failed: Json.tryParse(\"boom\") == nil")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(((args) => { try { return (args === undefined) ? undefined : JSON.parse(args); } catch (err) { return undefined; } })("boom"),undefined)))}; - {((cond) => {if (!cond) throw new Error("assertion failed: Json.tryParse(\"\") == nil")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(((args) => { try { return (args === undefined) ? undefined : JSON.parse(args); } catch (err) { return undefined; } })(""),undefined)))}; + class $Closure7 extends $stdlib.std.Resource { + constructor(scope, id, ) { + super(scope, id); + (std.Node.of(this)).hidden = true; + } + static _toInflightType(context) { + return ` + require("./inflight.$Closure7-1.js")({ + $std_Json: ${context._lift($stdlib.core.toLiftableModuleType(std.Json, "@winglang/sdk/std", "Json"))}, + }) + `; + } + _toInflight() { + return ` + (await (async () => { + const $Closure7Client = ${$Closure7._toInflightType(this)}; + const client = new $Closure7Client({ + }); + if (client.$inflight_init) { await client.$inflight_init(); } + return client; + })()) + `; + } + _getInflightOps() { + return ["handle", "$inflight_init"]; + } + } + class $Closure8 extends $stdlib.std.Resource { + constructor(scope, id, ) { + super(scope, id); + (std.Node.of(this)).hidden = true; + } + static _toInflightType(context) { + return ` + require("./inflight.$Closure8-1.js")({ + $std_Json: ${context._lift($stdlib.core.toLiftableModuleType(std.Json, "@winglang/sdk/std", "Json"))}, + }) + `; + } + _toInflight() { + return ` + (await (async () => { + const $Closure8Client = ${$Closure8._toInflightType(this)}; + const client = new $Closure8Client({ + }); + if (client.$inflight_init) { await client.$inflight_init(); } + return client; + })()) + `; + } + _getInflightOps() { + return ["handle", "$inflight_init"]; + } + } + class $Closure9 extends $stdlib.std.Resource { + constructor(scope, id, ) { + super(scope, id); + (std.Node.of(this)).hidden = true; + } + static _toInflightType(context) { + return ` + require("./inflight.$Closure9-1.js")({ + $std_Json: ${context._lift($stdlib.core.toLiftableModuleType(std.Json, "@winglang/sdk/std", "Json"))}, + }) + `; + } + _toInflight() { + return ` + (await (async () => { + const $Closure9Client = ${$Closure9._toInflightType(this)}; + const client = new $Closure9Client({ + }); + if (client.$inflight_init) { await client.$inflight_init(); } + return client; + })()) + `; + } + _getInflightOps() { + return ["handle", "$inflight_init"]; + } + } + class $Closure10 extends $stdlib.std.Resource { + constructor(scope, id, ) { + super(scope, id); + (std.Node.of(this)).hidden = true; + } + static _toInflightType(context) { + return ` + require("./inflight.$Closure10-1.js")({ + $std_Json: ${context._lift($stdlib.core.toLiftableModuleType(std.Json, "@winglang/sdk/std", "Json"))}, + }) + `; + } + _toInflight() { + return ` + (await (async () => { + const $Closure10Client = ${$Closure10._toInflightType(this)}; + const client = new $Closure10Client({ + }); + if (client.$inflight_init) { await client.$inflight_init(); } + return client; + })()) + `; + } + _getInflightOps() { + return ["handle", "$inflight_init"]; + } + } + this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:has()",new $Closure1(this,"$Closure1")); + this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:get()",new $Closure2(this,"$Closure2")); + this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:getAt()",new $Closure3(this,"$Closure3")); + this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:set()",new $Closure4(this,"$Closure4")); + this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:setAt()",new $Closure5(this,"$Closure5")); + this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:stringify()",new $Closure6(this,"$Closure6")); + this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:keys(), values(), entries()",new $Closure7(this,"$Closure7")); + this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:parse()",new $Closure8(this,"$Closure8")); + this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:tryParse()",new $Closure9(this,"$Closure9")); + this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:deepCopy(), deepCopyMut()",new $Closure10(this,"$Closure10")); } } const $App = $stdlib.core.App.for(process.env.WING_TARGET); diff --git a/tools/hangar/__snapshots__/test_corpus/sdk_tests/std/json.test.w_test_sim.md b/tools/hangar/__snapshots__/test_corpus/sdk_tests/std/json.test.w_test_sim.md index 7f169b55d8a..a6b942e1186 100644 --- a/tools/hangar/__snapshots__/test_corpus/sdk_tests/std/json.test.w_test_sim.md +++ b/tools/hangar/__snapshots__/test_corpus/sdk_tests/std/json.test.w_test_sim.md @@ -2,15 +2,19 @@ ## stdout.log ```log -pass ─ json.test.wsim » root/env0/test:get() -pass ─ json.test.wsim » root/env1/test:getAt() -pass ─ json.test.wsim » root/env2/test:set() -pass ─ json.test.wsim » root/env3/test:setAt() -pass ─ json.test.wsim » root/env4/test:stringify() -pass ─ json.test.wsim » root/env5/test:keys(), values(), entries() +pass ─ json.test.wsim » root/env0/test:has() +pass ─ json.test.wsim » root/env1/test:get() +pass ─ json.test.wsim » root/env2/test:getAt() +pass ─ json.test.wsim » root/env3/test:set() +pass ─ json.test.wsim » root/env4/test:setAt() +pass ─ json.test.wsim » root/env5/test:stringify() +pass ─ json.test.wsim » root/env6/test:keys(), values(), entries() +pass ─ json.test.wsim » root/env7/test:parse() +pass ─ json.test.wsim » root/env8/test:tryParse() +pass ─ json.test.wsim » root/env9/test:deepCopy(), deepCopyMut() -Tests 6 passed (6) +Tests 10 passed (10) Test Files 1 passed (1) Duration ```