diff --git a/docs/docs/04-standard-library/02-std/api-reference.md b/docs/docs/04-standard-library/02-std/api-reference.md index e1ce2b51dc8..46aa7a6ed45 100644 --- a/docs/docs/04-standard-library/02-std/api-reference.md +++ b/docs/docs/04-standard-library/02-std/api-reference.md @@ -1099,7 +1099,9 @@ Mutable Array. | join | Returns a new string containing the concatenated values in this array, separated by commas or a specified separator string. | | lastIndexOf | Returns the index of the last occurrence of searchElement found. | | pop | Remove value from end of array. | +| popAt | Removes value from the given index of an array. | | push | Add value to end of array. | +| removeFirst | Removes first occurence of a given value in an array. | | set | Sets a new value at the given index of an array. | --- @@ -1241,6 +1243,22 @@ pop(): Remove value from end of array. +##### `popAt` + +```wing +popAt(index: num): +``` + +Removes value from the given index of an array. + +###### `index`Required + +- *Type:* num + +the index to remove the value at. + +--- + ##### `push` ```wing @@ -1257,6 +1275,22 @@ value to add. --- +##### `removeFirst` + +```wing +removeFirst(value: ): bool +``` + +Removes first occurence of a given value in an array. + +###### `value`Required + +- *Type:* <T> + +the value to remove. + +--- + ##### `set` ```wing diff --git a/examples/tests/sdk_tests/std/array.w b/examples/tests/sdk_tests/std/array.w index d8a171d5c84..4f383927293 100644 --- a/examples/tests/sdk_tests/std/array.w +++ b/examples/tests/sdk_tests/std/array.w @@ -59,6 +59,39 @@ test "pushAndPop()" { assert(a.at(0) == "hello"); } +//----------------------------------------------------------------------------- +// popAt() + +test "popAt()" { + let assertThrows = (expected: str, block: (): void) => { + let var error = false; + try { + block(); + } catch actual { + assert(actual == expected); + error = true; + } + assert(error); + }; + + let INDEX_OUT_OF_BOUNDS_ERROR = "Index out of bounds"; + let mutArr = MutArray["hello", "world"]; + + let item = mutArr.popAt(0); + + assert(item == "hello"); + assert(mutArr.length == 1); + assert(mutArr.at(0) == "world"); + + assertThrows(INDEX_OUT_OF_BOUNDS_ERROR, () => { + mutArr.popAt(-3); + }); + + assertThrows(INDEX_OUT_OF_BOUNDS_ERROR, () => { + mutArr.popAt(3); + }); +} + //----------------------------------------------------------------------------- // concat() let array = Array["hello"]; @@ -307,4 +340,22 @@ test "insert()" { assert(mutArr.length == 5); assert(mutArr.at(4) == 25); +} + +//----------------------------------------------------------------------------- +// removeFirst() + +test "removeFirst()" { + let mutArr = MutArray[3, 6, 9, 3]; + + let r1 = mutArr.removeFirst(3); + + assert(r1 == true); + assert(mutArr.length == 3); + assert(mutArr == MutArray [6, 9, 3]); + + let r2 = mutArr.removeFirst(-42); + + assert(r2 == false); + assert(mutArr.length == 3); } \ No newline at end of file diff --git a/libs/wingsdk/src/std/array.ts b/libs/wingsdk/src/std/array.ts index 077d797df4f..7a347e918e4 100644 --- a/libs/wingsdk/src/std/array.ts +++ b/libs/wingsdk/src/std/array.ts @@ -250,6 +250,20 @@ export class MutArray { throw new Error("Abstract"); } + /** + * Removes value from the given index of an array + * + * @macro ((obj, args) => { if (args[0] < 0 || args[0] >= $self$.length) throw new Error("Index out of bounds"); return obj.splice(args[0], 1)[0]; })($self$, [$args$]) + * + * @param index the index to remove the value at + * @returns the value removed + * @throws index out of bounds error if the given index does not exist for the array + */ + public popAt(index: number): T1 { + index; + throw new Error("Macro"); + } + /** * Sets a new value at the given index of an array * @@ -279,4 +293,17 @@ export class MutArray { value; throw new Error("Macro"); } + + /** + * Removes first occurence of a given value in an array + * + * @macro ((obj, args) => { if (obj.indexOf(args[0]) !== -1) { obj.splice(obj.indexOf(args[0]), 1); return true; } return false; })($self$, [$args$]) + * + * @param value the value to remove + * @returns true if value was removed + */ + public removeFirst(value: T1): boolean { + value; + throw new Error("Macro"); + } } diff --git a/tools/hangar/__snapshots__/test_corpus/sdk_tests/std/array.w_compile_tf-aws.md b/tools/hangar/__snapshots__/test_corpus/sdk_tests/std/array.w_compile_tf-aws.md index 11b4d755547..b55cf8f7ab8 100644 --- a/tools/hangar/__snapshots__/test_corpus/sdk_tests/std/array.w_compile_tf-aws.md +++ b/tools/hangar/__snapshots__/test_corpus/sdk_tests/std/array.w_compile_tf-aws.md @@ -30,8 +30,8 @@ module.exports = function({ }) { } async handle() { const i = ["hello", "wing"]; - const separator = ","; - const joinedString = (await i.join()); + const separator = ";"; + const joinedString = (await i.join(separator)); const expectedString = (((await i.at(0)) + separator) + (await i.at(1))); {((cond) => {if (!cond) throw new Error("assertion failed: joinedString == expectedString")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(joinedString,expectedString)))}; } @@ -45,6 +45,28 @@ module.exports = function({ }) { ```js module.exports = function({ }) { class $Closure11 { + constructor({ }) { + const $obj = (...args) => this.handle(...args); + Object.setPrototypeOf($obj, this); + return $obj; + } + async handle() { + const i = ["hello", "wing"]; + const separator = ","; + const joinedString = (await i.join()); + const expectedString = (((await i.at(0)) + separator) + (await i.at(1))); + {((cond) => {if (!cond) throw new Error("assertion failed: joinedString == expectedString")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(joinedString,expectedString)))}; + } + } + return $Closure11; +} + +``` + +## inflight.$Closure12-1.js +```js +module.exports = function({ }) { + class $Closure12 { constructor({ }) { const $obj = (...args) => this.handle(...args); Object.setPrototypeOf($obj, this); @@ -57,15 +79,15 @@ module.exports = function({ }) { {((cond) => {if (!cond) throw new Error("assertion failed: o.at(0) == p.at(0)")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })((await o.at(0)),(await p.at(0)))))}; } } - return $Closure11; + return $Closure12; } ``` -## inflight.$Closure12-1.js +## inflight.$Closure13-1.js ```js module.exports = function({ }) { - class $Closure12 { + class $Closure13 { constructor({ }) { const $obj = (...args) => this.handle(...args); Object.setPrototypeOf($obj, this); @@ -78,15 +100,15 @@ module.exports = function({ }) { {((cond) => {if (!cond) throw new Error("assertion failed: q.at(0) == r.at(0)")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })((await q.at(0)),(await r.at(0)))))}; } } - return $Closure12; + return $Closure13; } ``` -## inflight.$Closure13-1.js +## inflight.$Closure14-1.js ```js module.exports = function({ }) { - class $Closure13 { + class $Closure14 { constructor({ }) { const $obj = (...args) => this.handle(...args); Object.setPrototypeOf($obj, this); @@ -99,15 +121,15 @@ module.exports = function({ }) { {((cond) => {if (!cond) throw new Error("assertion failed: s.lastIndexOf(\"something\") == -1")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(s.lastIndexOf("something"),(-1))))}; } } - return $Closure13; + return $Closure14; } ``` -## inflight.$Closure14-1.js +## inflight.$Closure15-1.js ```js module.exports = function({ }) { - class $Closure14 { + class $Closure15 { constructor({ }) { const $obj = (...args) => this.handle(...args); Object.setPrototypeOf($obj, this); @@ -142,15 +164,15 @@ module.exports = function({ }) { )); } } - return $Closure14; + return $Closure15; } ``` -## inflight.$Closure15-1.js +## inflight.$Closure16-1.js ```js module.exports = function({ }) { - class $Closure15 { + class $Closure16 { constructor({ }) { const $obj = (...args) => this.handle(...args); Object.setPrototypeOf($obj, this); @@ -191,7 +213,32 @@ module.exports = function({ }) { {((cond) => {if (!cond) throw new Error("assertion failed: mutArr.at(4) == 25")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })((await mutArr.at(4)),25)))}; } } - return $Closure15; + return $Closure16; +} + +``` + +## inflight.$Closure17-1.js +```js +module.exports = function({ }) { + class $Closure17 { + constructor({ }) { + const $obj = (...args) => this.handle(...args); + Object.setPrototypeOf($obj, this); + return $obj; + } + async handle() { + const mutArr = [3, 6, 9, 3]; + const r1 = ((obj, args) => { if (obj.indexOf(args[0]) !== -1) { obj.splice(obj.indexOf(args[0]), 1); return true; } return false; })(mutArr, [3]); + {((cond) => {if (!cond) throw new Error("assertion failed: r1 == true")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(r1,true)))}; + {((cond) => {if (!cond) throw new Error("assertion failed: mutArr.length == 3")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(mutArr.length,3)))}; + {((cond) => {if (!cond) throw new Error("assertion failed: mutArr == MutArray [6, 9, 3]")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(mutArr,[6, 9, 3])))}; + const r2 = ((obj, args) => { if (obj.indexOf(args[0]) !== -1) { obj.splice(obj.indexOf(args[0]), 1); return true; } return false; })(mutArr, [(-42)]); + {((cond) => {if (!cond) throw new Error("assertion failed: r2 == false")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(r2,false)))}; + {((cond) => {if (!cond) throw new Error("assertion failed: mutArr.length == 3")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(mutArr.length,3)))}; + } + } + return $Closure17; } ``` @@ -252,12 +299,33 @@ module.exports = function({ }) { return $obj; } async handle() { - const b = ["hello"]; - {((cond) => {if (!cond) throw new Error("assertion failed: b.length == 1")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(b.length,1)))}; - const d = (await b.concat(["wing"])); - {((cond) => {if (!cond) throw new Error("assertion failed: d.length == 2")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(d.length,2)))}; - {((cond) => {if (!cond) throw new Error("assertion failed: d.at(0) == \"hello\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })((await d.at(0)),"hello")))}; - {((cond) => {if (!cond) throw new Error("assertion failed: d.at(1) == \"wing\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })((await d.at(1)),"wing")))}; + const assertThrows = async (expected, block) => { + let error = false; + try { + (await block()); + } + catch ($error_actual) { + const actual = $error_actual.message; + {((cond) => {if (!cond) throw new Error("assertion failed: actual == expected")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(actual,expected)))}; + error = true; + } + {((cond) => {if (!cond) throw new Error("assertion failed: error")})(error)}; + } + ; + const INDEX_OUT_OF_BOUNDS_ERROR = "Index out of bounds"; + const mutArr = ["hello", "world"]; + const item = ((obj, args) => { if (args[0] < 0 || args[0] >= mutArr.length) throw new Error("Index out of bounds"); return obj.splice(args[0], 1)[0]; })(mutArr, [0]); + {((cond) => {if (!cond) throw new Error("assertion failed: item == \"hello\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(item,"hello")))}; + {((cond) => {if (!cond) throw new Error("assertion failed: mutArr.length == 1")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(mutArr.length,1)))}; + {((cond) => {if (!cond) throw new Error("assertion failed: mutArr.at(0) == \"world\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })((await mutArr.at(0)),"world")))}; + (await assertThrows(INDEX_OUT_OF_BOUNDS_ERROR,async () => { + ((obj, args) => { if (args[0] < 0 || args[0] >= mutArr.length) throw new Error("Index out of bounds"); return obj.splice(args[0], 1)[0]; })(mutArr, [(-3)]); + } + )); + (await assertThrows(INDEX_OUT_OF_BOUNDS_ERROR,async () => { + ((obj, args) => { if (args[0] < 0 || args[0] >= mutArr.length) throw new Error("Index out of bounds"); return obj.splice(args[0], 1)[0]; })(mutArr, [3]); + } + )); } } return $Closure4; @@ -275,14 +343,12 @@ module.exports = function({ }) { return $obj; } async handle() { - const array = ["hello"]; - {((cond) => {if (!cond) throw new Error("assertion failed: array.length == 1")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(array.length,1)))}; - const anotherArray = ["wing"]; - {((cond) => {if (!cond) throw new Error("assertion failed: anotherArray.length == 1")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(anotherArray.length,1)))}; - const mergedArray = (await array.concat(anotherArray)); - {((cond) => {if (!cond) throw new Error("assertion failed: mergedArray.length == 2")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(mergedArray.length,2)))}; - {((cond) => {if (!cond) throw new Error("assertion failed: mergedArray.at(0) == \"hello\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })((await mergedArray.at(0)),"hello")))}; - {((cond) => {if (!cond) throw new Error("assertion failed: mergedArray.at(1) == \"wing\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })((await mergedArray.at(1)),"wing")))}; + const b = ["hello"]; + {((cond) => {if (!cond) throw new Error("assertion failed: b.length == 1")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(b.length,1)))}; + const d = (await b.concat(["wing"])); + {((cond) => {if (!cond) throw new Error("assertion failed: d.length == 2")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(d.length,2)))}; + {((cond) => {if (!cond) throw new Error("assertion failed: d.at(0) == \"hello\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })((await d.at(0)),"hello")))}; + {((cond) => {if (!cond) throw new Error("assertion failed: d.at(1) == \"wing\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })((await d.at(1)),"wing")))}; } } return $Closure5; @@ -300,12 +366,14 @@ module.exports = function({ }) { return $obj; } async handle() { - const e = ["hello", "wing"]; - {((cond) => {if (!cond) throw new Error("assertion failed: e.contains(\"wing\")")})(e.includes("wing"))}; - {((cond) => {if (!cond) throw new Error("assertion failed: !e.contains(\"NotThere\")")})((!e.includes("NotThere")))}; - const h = ["hello", "wing"]; - {((cond) => {if (!cond) throw new Error("assertion failed: h.contains(\"wing\")")})(h.includes("wing"))}; - {((cond) => {if (!cond) throw new Error("assertion failed: !h.contains(\"NotThere\")")})((!h.includes("NotThere")))}; + const array = ["hello"]; + {((cond) => {if (!cond) throw new Error("assertion failed: array.length == 1")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(array.length,1)))}; + const anotherArray = ["wing"]; + {((cond) => {if (!cond) throw new Error("assertion failed: anotherArray.length == 1")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(anotherArray.length,1)))}; + const mergedArray = (await array.concat(anotherArray)); + {((cond) => {if (!cond) throw new Error("assertion failed: mergedArray.length == 2")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(mergedArray.length,2)))}; + {((cond) => {if (!cond) throw new Error("assertion failed: mergedArray.at(0) == \"hello\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })((await mergedArray.at(0)),"hello")))}; + {((cond) => {if (!cond) throw new Error("assertion failed: mergedArray.at(1) == \"wing\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })((await mergedArray.at(1)),"wing")))}; } } return $Closure6; @@ -323,9 +391,12 @@ module.exports = function({ }) { return $obj; } async handle() { - const g = ["hello", "wing"]; - {((cond) => {if (!cond) throw new Error("assertion failed: g.indexOf(\"wing\") == 1")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(g.indexOf("wing"),1)))}; - {((cond) => {if (!cond) throw new Error("assertion failed: g.indexOf(\"notThere\") == -1")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(g.indexOf("notThere"),(-1))))}; + const e = ["hello", "wing"]; + {((cond) => {if (!cond) throw new Error("assertion failed: e.contains(\"wing\")")})(e.includes("wing"))}; + {((cond) => {if (!cond) throw new Error("assertion failed: !e.contains(\"NotThere\")")})((!e.includes("NotThere")))}; + const h = ["hello", "wing"]; + {((cond) => {if (!cond) throw new Error("assertion failed: h.contains(\"wing\")")})(h.includes("wing"))}; + {((cond) => {if (!cond) throw new Error("assertion failed: !h.contains(\"NotThere\")")})((!h.includes("NotThere")))}; } } return $Closure7; @@ -363,11 +434,9 @@ module.exports = function({ }) { return $obj; } async handle() { - const i = ["hello", "wing"]; - const separator = ";"; - const joinedString = (await i.join(separator)); - const expectedString = (((await i.at(0)) + separator) + (await i.at(1))); - {((cond) => {if (!cond) throw new Error("assertion failed: joinedString == expectedString")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(joinedString,expectedString)))}; + const g = ["hello", "wing"]; + {((cond) => {if (!cond) throw new Error("assertion failed: g.indexOf(\"wing\") == 1")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(g.indexOf("wing"),1)))}; + {((cond) => {if (!cond) throw new Error("assertion failed: g.indexOf(\"notThere\") == -1")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(g.indexOf("notThere"),(-1))))}; } } return $Closure9; @@ -396,7 +465,7 @@ module.exports = function({ }) { }, "output": { "WING_TEST_RUNNER_FUNCTION_ARNS": { - "value": "[[\"root/Default/Default/test:length\",\"${aws_lambda_function.testlength_Handler_BFD8933F.arn}\"],[\"root/Default/Default/test:at()\",\"${aws_lambda_function.testat_Handler_E4F013BC.arn}\"],[\"root/Default/Default/test:pushAndPop()\",\"${aws_lambda_function.testpushAndPop_Handler_EAC0C8FF.arn}\"],[\"root/Default/Default/test:concatMutArray()\",\"${aws_lambda_function.testconcatMutArray_Handler_40D88E89.arn}\"],[\"root/Default/Default/test:concatArray()\",\"${aws_lambda_function.testconcatArray_Handler_F66848AE.arn}\"],[\"root/Default/Default/test:contains()\",\"${aws_lambda_function.testcontains_Handler_F60865D9.arn}\"],[\"root/Default/Default/test:indexOf()\",\"${aws_lambda_function.testindexOf_Handler_BD91EA6F.arn}\"],[\"root/Default/Default/test:indexOfArray()\",\"${aws_lambda_function.testindexOfArray_Handler_DB3A81F5.arn}\"],[\"root/Default/Default/test:join()\",\"${aws_lambda_function.testjoin_Handler_6AC62A8E.arn}\"],[\"root/Default/Default/test:joinWithDefaultSeparator()\",\"${aws_lambda_function.testjoinWithDefaultSeparator_Handler_7AE1258D.arn}\"],[\"root/Default/Default/test:copy()\",\"${aws_lambda_function.testcopy_Handler_27A14A0E.arn}\"],[\"root/Default/Default/test:copyMut()\",\"${aws_lambda_function.testcopyMut_Handler_851E24B4.arn}\"],[\"root/Default/Default/test:lastIndexOf()\",\"${aws_lambda_function.testlastIndexOf_Handler_FFB2061F.arn}\"],[\"root/Default/Default/test:set()\",\"${aws_lambda_function.testset_Handler_ADDF1A01.arn}\"],[\"root/Default/Default/test:insert()\",\"${aws_lambda_function.testinsert_Handler_20BB87F8.arn}\"]]" + "value": "[[\"root/Default/Default/test:length\",\"${aws_lambda_function.testlength_Handler_BFD8933F.arn}\"],[\"root/Default/Default/test:at()\",\"${aws_lambda_function.testat_Handler_E4F013BC.arn}\"],[\"root/Default/Default/test:pushAndPop()\",\"${aws_lambda_function.testpushAndPop_Handler_EAC0C8FF.arn}\"],[\"root/Default/Default/test:popAt()\",\"${aws_lambda_function.testpopAt_Handler_A6739840.arn}\"],[\"root/Default/Default/test:concatMutArray()\",\"${aws_lambda_function.testconcatMutArray_Handler_40D88E89.arn}\"],[\"root/Default/Default/test:concatArray()\",\"${aws_lambda_function.testconcatArray_Handler_F66848AE.arn}\"],[\"root/Default/Default/test:contains()\",\"${aws_lambda_function.testcontains_Handler_F60865D9.arn}\"],[\"root/Default/Default/test:indexOf()\",\"${aws_lambda_function.testindexOf_Handler_BD91EA6F.arn}\"],[\"root/Default/Default/test:indexOfArray()\",\"${aws_lambda_function.testindexOfArray_Handler_DB3A81F5.arn}\"],[\"root/Default/Default/test:join()\",\"${aws_lambda_function.testjoin_Handler_6AC62A8E.arn}\"],[\"root/Default/Default/test:joinWithDefaultSeparator()\",\"${aws_lambda_function.testjoinWithDefaultSeparator_Handler_7AE1258D.arn}\"],[\"root/Default/Default/test:copy()\",\"${aws_lambda_function.testcopy_Handler_27A14A0E.arn}\"],[\"root/Default/Default/test:copyMut()\",\"${aws_lambda_function.testcopyMut_Handler_851E24B4.arn}\"],[\"root/Default/Default/test:lastIndexOf()\",\"${aws_lambda_function.testlastIndexOf_Handler_FFB2061F.arn}\"],[\"root/Default/Default/test:set()\",\"${aws_lambda_function.testset_Handler_ADDF1A01.arn}\"],[\"root/Default/Default/test:insert()\",\"${aws_lambda_function.testinsert_Handler_20BB87F8.arn}\"],[\"root/Default/Default/test:removeFirst()\",\"${aws_lambda_function.testremoveFirst_Handler_4D1D9086.arn}\"]]" } }, "provider": { @@ -523,6 +592,15 @@ module.exports = function({ }) { }, "assume_role_policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Principal\":{\"Service\":\"lambda.amazonaws.com\"},\"Effect\":\"Allow\"}]}" }, + "testpopAt_Handler_IamRole_D00C1FE4": { + "//": { + "metadata": { + "path": "root/Default/Default/test:popAt()/Handler/IamRole", + "uniqueId": "testpopAt_Handler_IamRole_D00C1FE4" + } + }, + "assume_role_policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Principal\":{\"Service\":\"lambda.amazonaws.com\"},\"Effect\":\"Allow\"}]}" + }, "testpushAndPop_Handler_IamRole_5F6E6E00": { "//": { "metadata": { @@ -532,6 +610,15 @@ module.exports = function({ }) { }, "assume_role_policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Principal\":{\"Service\":\"lambda.amazonaws.com\"},\"Effect\":\"Allow\"}]}" }, + "testremoveFirst_Handler_IamRole_A9164B23": { + "//": { + "metadata": { + "path": "root/Default/Default/test:removeFirst()/Handler/IamRole", + "uniqueId": "testremoveFirst_Handler_IamRole_A9164B23" + } + }, + "assume_role_policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Principal\":{\"Service\":\"lambda.amazonaws.com\"},\"Effect\":\"Allow\"}]}" + }, "testset_Handler_IamRole_B9B79227": { "//": { "metadata": { @@ -673,6 +760,16 @@ module.exports = function({ }) { "policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"none:null\",\"Resource\":\"*\"}]}", "role": "${aws_iam_role.testlength_Handler_IamRole_0AFDC7CB.name}" }, + "testpopAt_Handler_IamRolePolicy_9E762AA5": { + "//": { + "metadata": { + "path": "root/Default/Default/test:popAt()/Handler/IamRolePolicy", + "uniqueId": "testpopAt_Handler_IamRolePolicy_9E762AA5" + } + }, + "policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"none:null\",\"Resource\":\"*\"}]}", + "role": "${aws_iam_role.testpopAt_Handler_IamRole_D00C1FE4.name}" + }, "testpushAndPop_Handler_IamRolePolicy_7A2A0323": { "//": { "metadata": { @@ -683,6 +780,16 @@ module.exports = function({ }) { "policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"none:null\",\"Resource\":\"*\"}]}", "role": "${aws_iam_role.testpushAndPop_Handler_IamRole_5F6E6E00.name}" }, + "testremoveFirst_Handler_IamRolePolicy_878A7F57": { + "//": { + "metadata": { + "path": "root/Default/Default/test:removeFirst()/Handler/IamRolePolicy", + "uniqueId": "testremoveFirst_Handler_IamRolePolicy_878A7F57" + } + }, + "policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"none:null\",\"Resource\":\"*\"}]}", + "role": "${aws_iam_role.testremoveFirst_Handler_IamRole_A9164B23.name}" + }, "testset_Handler_IamRolePolicy_ADE48415": { "//": { "metadata": { @@ -825,6 +932,16 @@ module.exports = function({ }) { "policy_arn": "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole", "role": "${aws_iam_role.testlength_Handler_IamRole_0AFDC7CB.name}" }, + "testpopAt_Handler_IamRolePolicyAttachment_11532865": { + "//": { + "metadata": { + "path": "root/Default/Default/test:popAt()/Handler/IamRolePolicyAttachment", + "uniqueId": "testpopAt_Handler_IamRolePolicyAttachment_11532865" + } + }, + "policy_arn": "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole", + "role": "${aws_iam_role.testpopAt_Handler_IamRole_D00C1FE4.name}" + }, "testpushAndPop_Handler_IamRolePolicyAttachment_D18E216F": { "//": { "metadata": { @@ -835,6 +952,16 @@ module.exports = function({ }) { "policy_arn": "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole", "role": "${aws_iam_role.testpushAndPop_Handler_IamRole_5F6E6E00.name}" }, + "testremoveFirst_Handler_IamRolePolicyAttachment_511FE450": { + "//": { + "metadata": { + "path": "root/Default/Default/test:removeFirst()/Handler/IamRolePolicyAttachment", + "uniqueId": "testremoveFirst_Handler_IamRolePolicyAttachment_511FE450" + } + }, + "policy_arn": "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole", + "role": "${aws_iam_role.testremoveFirst_Handler_IamRole_A9164B23.name}" + }, "testset_Handler_IamRolePolicyAttachment_58805670": { "//": { "metadata": { @@ -1224,6 +1351,35 @@ module.exports = function({ }) { "subnet_ids": [] } }, + "testpopAt_Handler_A6739840": { + "//": { + "metadata": { + "path": "root/Default/Default/test:popAt()/Handler/Default", + "uniqueId": "testpopAt_Handler_A6739840" + } + }, + "architectures": [ + "arm64" + ], + "environment": { + "variables": { + "WING_FUNCTION_NAME": "Handler-c817e47d", + "WING_TARGET": "tf-aws" + } + }, + "function_name": "Handler-c817e47d", + "handler": "index.handler", + "publish": true, + "role": "${aws_iam_role.testpopAt_Handler_IamRole_D00C1FE4.arn}", + "runtime": "nodejs18.x", + "s3_bucket": "${aws_s3_bucket.Code.bucket}", + "s3_key": "${aws_s3_object.testpopAt_Handler_S3Object_0EEB898A.key}", + "timeout": 30, + "vpc_config": { + "security_group_ids": [], + "subnet_ids": [] + } + }, "testpushAndPop_Handler_EAC0C8FF": { "//": { "metadata": { @@ -1253,6 +1409,35 @@ module.exports = function({ }) { "subnet_ids": [] } }, + "testremoveFirst_Handler_4D1D9086": { + "//": { + "metadata": { + "path": "root/Default/Default/test:removeFirst()/Handler/Default", + "uniqueId": "testremoveFirst_Handler_4D1D9086" + } + }, + "architectures": [ + "arm64" + ], + "environment": { + "variables": { + "WING_FUNCTION_NAME": "Handler-c8d404f0", + "WING_TARGET": "tf-aws" + } + }, + "function_name": "Handler-c8d404f0", + "handler": "index.handler", + "publish": true, + "role": "${aws_iam_role.testremoveFirst_Handler_IamRole_A9164B23.arn}", + "runtime": "nodejs18.x", + "s3_bucket": "${aws_s3_bucket.Code.bucket}", + "s3_key": "${aws_s3_object.testremoveFirst_Handler_S3Object_9034C8E7.key}", + "timeout": 30, + "vpc_config": { + "security_group_ids": [], + "subnet_ids": [] + } + }, "testset_Handler_ADDF1A01": { "//": { "metadata": { @@ -1518,6 +1703,17 @@ module.exports = function({ }) { "key": "", "source": "" }, + "testpopAt_Handler_S3Object_0EEB898A": { + "//": { + "metadata": { + "path": "root/Default/Default/test:popAt()/Handler/S3Object", + "uniqueId": "testpopAt_Handler_S3Object_0EEB898A" + } + }, + "bucket": "${aws_s3_bucket.Code.bucket}", + "key": "", + "source": "" + }, "testpushAndPop_Handler_S3Object_3F93E368": { "//": { "metadata": { @@ -1529,6 +1725,17 @@ module.exports = function({ }) { "key": "", "source": "" }, + "testremoveFirst_Handler_S3Object_9034C8E7": { + "//": { + "metadata": { + "path": "root/Default/Default/test:removeFirst()/Handler/S3Object", + "uniqueId": "testremoveFirst_Handler_S3Object_9034C8E7" + } + }, + "bucket": "${aws_s3_bucket.Code.bucket}", + "key": "", + "source": "" + }, "testset_Handler_S3Object_A8FBF518": { "//": { "metadata": { @@ -1946,6 +2153,58 @@ class $Root extends $stdlib.std.Resource { return ["handle", "$inflight_init"]; } } + class $Closure16 extends $stdlib.std.Resource { + constructor(scope, id, ) { + super(scope, id); + (std.Node.of(this)).hidden = true; + } + static _toInflightType(context) { + return ` + require("./inflight.$Closure16-1.js")({ + }) + `; + } + _toInflight() { + return ` + (await (async () => { + const $Closure16Client = ${$Closure16._toInflightType(this)}; + const client = new $Closure16Client({ + }); + if (client.$inflight_init) { await client.$inflight_init(); } + return client; + })()) + `; + } + _getInflightOps() { + return ["handle", "$inflight_init"]; + } + } + class $Closure17 extends $stdlib.std.Resource { + constructor(scope, id, ) { + super(scope, id); + (std.Node.of(this)).hidden = true; + } + static _toInflightType(context) { + return ` + require("./inflight.$Closure17-1.js")({ + }) + `; + } + _toInflight() { + return ` + (await (async () => { + const $Closure17Client = ${$Closure17._toInflightType(this)}; + const client = new $Closure17Client({ + }); + if (client.$inflight_init) { await client.$inflight_init(); } + return client; + })()) + `; + } + _getInflightOps() { + return ["handle", "$inflight_init"]; + } + } const bucket = this.node.root.newAbstract("@winglang/sdk.cloud.Bucket",this,"myBucket"); const buckets = [bucket]; const anotherBucket = this.node.root.newAbstract("@winglang/sdk.cloud.Bucket",this,"mySecondBucket"); @@ -1969,6 +2228,7 @@ class $Root extends $stdlib.std.Resource { {((cond) => {if (!cond) throw new Error("assertion failed: a.length == 1")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(a.length,1)))}; {((cond) => {if (!cond) throw new Error("assertion failed: a.at(0) == \"hello\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })((a.at(0)),"hello")))}; this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:pushAndPop()",new $Closure3(this,"$Closure3")); + this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:popAt()",new $Closure4(this,"$Closure4")); const array = ["hello"]; {((cond) => {if (!cond) throw new Error("assertion failed: array.length == 1")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(array.length,1)))}; const mergedArray = (array.concat(["wing"])); @@ -1985,8 +2245,8 @@ class $Root extends $stdlib.std.Resource { {((cond) => {if (!cond) throw new Error("assertion failed: mergedBuckets.length == 2")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(mergedBuckets.length,2)))}; {((cond) => {if (!cond) throw new Error("assertion failed: mergedBuckets.at(0).node.id == \"myBucket\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })((mergedBuckets.at(0)).node.id,"myBucket")))}; {((cond) => {if (!cond) throw new Error("assertion failed: mergedBuckets.at(1).node.id == \"mySecondBucket\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })((mergedBuckets.at(1)).node.id,"mySecondBucket")))}; - this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:concatMutArray()",new $Closure4(this,"$Closure4")); - this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:concatArray()",new $Closure5(this,"$Closure5")); + this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:concatMutArray()",new $Closure5(this,"$Closure5")); + this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:concatArray()",new $Closure6(this,"$Closure6")); const e = ["hello", "wing"]; {((cond) => {if (!cond) throw new Error("assertion failed: e.contains(\"wing\")")})(e.includes("wing"))}; {((cond) => {if (!cond) throw new Error("assertion failed: !e.contains(\"NotThere\")")})((!e.includes("NotThere")))}; @@ -1996,17 +2256,17 @@ class $Root extends $stdlib.std.Resource { const h = ["hello", "wing"]; {((cond) => {if (!cond) throw new Error("assertion failed: h.contains(\"wing\")")})(h.includes("wing"))}; {((cond) => {if (!cond) throw new Error("assertion failed: !h.contains(\"NotThere\")")})((!h.includes("NotThere")))}; - this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:contains()",new $Closure6(this,"$Closure6")); + this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:contains()",new $Closure7(this,"$Closure7")); const g = ["hello", "wing"]; {((cond) => {if (!cond) throw new Error("assertion failed: g.indexOf(\"wing\") == 1")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(g.indexOf("wing"),1)))}; {((cond) => {if (!cond) throw new Error("assertion failed: g.indexOf(\"notThere\") == -1")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(g.indexOf("notThere"),(-1))))}; {((cond) => {if (!cond) throw new Error("assertion failed: buckets.indexOf(bucket) == 0")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(buckets.indexOf(bucket),0)))}; {((cond) => {if (!cond) throw new Error("assertion failed: buckets.indexOf(dummyBucket) == -1")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(buckets.indexOf(dummyBucket),(-1))))}; - this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:indexOf()",new $Closure7(this,"$Closure7")); + this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:indexOf()",new $Closure8(this,"$Closure8")); const q = ["hello", "wing"]; {((cond) => {if (!cond) throw new Error("assertion failed: q.indexOf(\"wing\") == 1")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(q.indexOf("wing"),1)))}; {((cond) => {if (!cond) throw new Error("assertion failed: q.indexOf(\"notThere\") == -1")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(q.indexOf("notThere"),(-1))))}; - this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:indexOfArray()",new $Closure8(this,"$Closure8")); + this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:indexOfArray()",new $Closure9(this,"$Closure9")); const m = ["hello", "wing"]; const delimeter = ";"; const joinedString = (m.join(delimeter)); @@ -2017,8 +2277,8 @@ class $Root extends $stdlib.std.Resource { const joinedStringWithDefault = (m.join()); const expectedStringWithDefault = (((m.at(0)) + separator) + (m.at(1))); {((cond) => {if (!cond) throw new Error("assertion failed: joinedStringWithDefault == expectedStringWithDefault")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(joinedStringWithDefault,expectedStringWithDefault)))}; - this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:join()",new $Closure9(this,"$Closure9")); - this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:joinWithDefaultSeparator()",new $Closure10(this,"$Closure10")); + this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:join()",new $Closure10(this,"$Closure10")); + this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:joinWithDefaultSeparator()",new $Closure11(this,"$Closure11")); const o = ["hello", "wing"]; const p = [...(o)]; {((cond) => {if (!cond) throw new Error("assertion failed: o.length == p.length")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(o.length,p.length)))}; @@ -2026,12 +2286,12 @@ class $Root extends $stdlib.std.Resource { const copiedBuckets = [...(buckets)]; {((cond) => {if (!cond) throw new Error("assertion failed: copiedBuckets.length == 1")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(copiedBuckets.length,1)))}; {((cond) => {if (!cond) throw new Error("assertion failed: copiedBuckets.at(0).node.id == \"myBucket\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })((copiedBuckets.at(0)).node.id,"myBucket")))}; - this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:copy()",new $Closure11(this,"$Closure11")); + this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:copy()",new $Closure12(this,"$Closure12")); const v = ["hello", "wing"]; const r = [...(v)]; {((cond) => {if (!cond) throw new Error("assertion failed: q.length == r.length")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(q.length,r.length)))}; {((cond) => {if (!cond) throw new Error("assertion failed: q.at(0) == r.at(0)")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })((q.at(0)),(r.at(0)))))}; - this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:copyMut()",new $Closure12(this,"$Closure12")); + this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:copyMut()",new $Closure13(this,"$Closure13")); const lastStr = "wing"; const s = ["hello", lastStr, lastStr]; {((cond) => {if (!cond) throw new Error("assertion failed: s.lastIndexOf(lastStr) == 2")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(s.lastIndexOf(lastStr),2)))}; @@ -2039,9 +2299,10 @@ class $Root extends $stdlib.std.Resource { const multipleBuckets = [bucket, bucket, anotherBucket]; {((cond) => {if (!cond) throw new Error("assertion failed: multipleBuckets.lastIndexOf(bucket) == 1")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(multipleBuckets.lastIndexOf(bucket),1)))}; {((cond) => {if (!cond) throw new Error("assertion failed: multipleBuckets.lastIndexOf(dummyBucket) == -1")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(multipleBuckets.lastIndexOf(dummyBucket),(-1))))}; - this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:lastIndexOf()",new $Closure13(this,"$Closure13")); - this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:set()",new $Closure14(this,"$Closure14")); - this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:insert()",new $Closure15(this,"$Closure15")); + this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:lastIndexOf()",new $Closure14(this,"$Closure14")); + this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:set()",new $Closure15(this,"$Closure15")); + this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:insert()",new $Closure16(this,"$Closure16")); + this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:removeFirst()",new $Closure17(this,"$Closure17")); } } const $App = $stdlib.core.App.for(process.env.WING_TARGET); diff --git a/tools/hangar/__snapshots__/test_corpus/sdk_tests/std/array.w_test_sim.md b/tools/hangar/__snapshots__/test_corpus/sdk_tests/std/array.w_test_sim.md index 9e06a26a976..5b43eaca61c 100644 --- a/tools/hangar/__snapshots__/test_corpus/sdk_tests/std/array.w_test_sim.md +++ b/tools/hangar/__snapshots__/test_corpus/sdk_tests/std/array.w_test_sim.md @@ -2,24 +2,26 @@ ## stdout.log ```log -pass ─ array.wsim » root/env0/test:length -pass ─ array.wsim » root/env1/test:at() -pass ─ array.wsim » root/env10/test:copy() -pass ─ array.wsim » root/env11/test:copyMut() -pass ─ array.wsim » root/env12/test:lastIndexOf() -pass ─ array.wsim » root/env13/test:set() -pass ─ array.wsim » root/env14/test:insert() -pass ─ array.wsim » root/env2/test:pushAndPop() -pass ─ array.wsim » root/env3/test:concatMutArray() -pass ─ array.wsim » root/env4/test:concatArray() -pass ─ array.wsim » root/env5/test:contains() -pass ─ array.wsim » root/env6/test:indexOf() -pass ─ array.wsim » root/env7/test:indexOfArray() -pass ─ array.wsim » root/env8/test:join() -pass ─ array.wsim » root/env9/test:joinWithDefaultSeparator() +pass ─ array.wsim » root/env0/test:length +pass ─ array.wsim » root/env1/test:at() +pass ─ array.wsim » root/env10/test:joinWithDefaultSeparator() +pass ─ array.wsim » root/env11/test:copy() +pass ─ array.wsim » root/env12/test:copyMut() +pass ─ array.wsim » root/env13/test:lastIndexOf() +pass ─ array.wsim » root/env14/test:set() +pass ─ array.wsim » root/env15/test:insert() +pass ─ array.wsim » root/env16/test:removeFirst() +pass ─ array.wsim » root/env2/test:pushAndPop() +pass ─ array.wsim » root/env3/test:popAt() +pass ─ array.wsim » root/env4/test:concatMutArray() +pass ─ array.wsim » root/env5/test:concatArray() +pass ─ array.wsim » root/env6/test:contains() +pass ─ array.wsim » root/env7/test:indexOf() +pass ─ array.wsim » root/env8/test:indexOfArray() +pass ─ array.wsim » root/env9/test:join() -Tests 15 passed (15) +Tests 17 passed (17) Test Files 1 passed (1) Duration ```