Skip to content

Commit

Permalink
feat(sdk): add clear() method for MutArray (#7202)
Browse files Browse the repository at this point in the history
## Checklist
Resolves #7139 
This PR adds `clear()` method for `MutArray`.

- [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] Tests added (always)
- [x] Docs updated (only required for features)
- [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing

*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)*.
  • Loading branch information
Pushkarm029 authored Oct 28, 2024
1 parent 1fcd84e commit 3e993ad
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
9 changes: 9 additions & 0 deletions docs/api/04-standard-library/std/array.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ Mutable Array.
| **Name** | **Description** |
| --- | --- |
| <code><a href="#@winglang/sdk.std.MutArray.at">at</a></code> | Get the value at the given index. |
| <code><a href="#@winglang/sdk.std.MutArray.clear">clear</a></code> | Removes all elements. |
| <code><a href="#@winglang/sdk.std.MutArray.concat">concat</a></code> | Merge arr to the end of this array. |
| <code><a href="#@winglang/sdk.std.MutArray.contains">contains</a></code> | Checks if this array includes searchElement. |
| <code><a href="#@winglang/sdk.std.MutArray.copy">copy</a></code> | Create an immutable shallow copy of this array. |
Expand Down Expand Up @@ -236,6 +237,14 @@ index of the value to get.

---

##### `clear` <a name="clear" id="@winglang/sdk.std.MutArray.clear"></a>

```wing
clear(): void
```

Removes all elements.

##### `concat` <a name="concat" id="@winglang/sdk.std.MutArray.concat"></a>

```wing
Expand Down
10 changes: 10 additions & 0 deletions packages/@winglang/sdk/src/std/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ export class MutArray {
throw new Error("Abstract");
}

/**
* Removes all elements
*
* @macro $self$.length = 0;
*
*/
public clear(): void {
throw new Error("Macro");
}

/**
* Get the value at the given index
* @macro ((arr, index) => { if (index < 0 || index >= arr.length) throw new Error("Index out of bounds"); return arr[index]; })($self$, $args$)
Expand Down
33 changes: 33 additions & 0 deletions tests/sdk_tests/std/array.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,39 @@ test "length" {
assert(MutArray<str>["hello"].length == 1);
}

//-----------------------------------------------------------------------------
// clear()

test "clear()" {
let arr = MutArray<str>["hello", "world", "wingcloud"];
assert(arr.length == 3);

arr.clear();

assert(arr.length == 0);

// Verify that accessing elements throws an error
let assertThrows = (expected: str, block: (): void) => {
let var error = false;
try {
block();
} catch actual {
assert(actual == expected);
error = true;
}
assert(error);
};

assertThrows("Index out of bounds", () => {
arr.at(0);
});

// Verify that we can add new elements after clearing
arr.push("new element");
assert(arr.length == 1);
assert(arr.at(0) == "new element");
}

//-----------------------------------------------------------------------------
// at()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## stdout.log
```log
pass ─ array.test.wsim » root/Default/test:at()
pass ─ array.test.wsim » root/Default/test:clear()
pass ─ array.test.wsim » root/Default/test:concatArray()
pass ─ array.test.wsim » root/Default/test:concatMutArray()
pass ─ array.test.wsim » root/Default/test:contains()
Expand All @@ -21,7 +22,7 @@ pass ─ array.test.wsim » root/Default/test:removeFirst()
pass ─ array.test.wsim » root/Default/test:set()
pass ─ array.test.wsim » root/Default/test:slice()
Tests 18 passed (18)
Tests 19 passed (19)
Snapshots 1 skipped
Test Files 1 passed (1)
Duration <DURATION>
Expand Down

0 comments on commit 3e993ad

Please sign in to comment.