Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk): add clear() method for MutArray #7202

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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