Skip to content

Commit

Permalink
Added support for calling "assert" as an alias to "assert.ok"
Browse files Browse the repository at this point in the history
  • Loading branch information
nabetti1720 committed Nov 11, 2024
1 parent b28f210 commit dd25160
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
17 changes: 9 additions & 8 deletions modules/llrt_assert/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
use llrt_utils::module::{export_default, ModuleInfo};
use llrt_utils::module::ModuleInfo;
use rquickjs::{
module::{Declarations, Exports, ModuleDef},
prelude::{Func, Opt},
Ctx, Exception, Result, Type, Value,
prelude::Opt,
Ctx, Exception, Function, Result, Type, Value,
};

fn assert(ctx: Ctx, value: Value, message: Opt<Value>) -> Result<()> {
fn ok(ctx: Ctx, value: Value, message: Opt<Value>) -> Result<()> {
match value.type_of() {
Type::Bool => {
if value.as_bool().unwrap() {
Expand Down Expand Up @@ -58,11 +58,12 @@ impl ModuleDef for AssertModule {
}

fn evaluate<'js>(ctx: &Ctx<'js>, exports: &Exports<'js>) -> Result<()> {
export_default(ctx, exports, |default| {
default.set("ok", Func::from(assert))?;
let ok_function = Function::new(ctx.clone(), ok)?;
ok_function.set("ok", ok_function.clone())?;

Ok(())
})
exports.export("ok", ok_function.clone())?;
exports.export("default", ok_function)?;
Ok(())
}
}

Expand Down
12 changes: 11 additions & 1 deletion tests/unit/assert.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as assert from "assert";
import assert from "assert";

describe("assert.ok", () => {
it("Should be returned 'undefined' (So it's not an error)", () => {
Expand Down Expand Up @@ -61,3 +61,13 @@ describe("assert.ok", () => {
}
});
});

describe("assert", () => {
it("Should be returned 'undefined' (So it's not an error)", () => {
expect(assert(true)).toBeUndefined();
expect(assert(1)).toBeUndefined();
expect(assert("non-empty string")).toBeUndefined();
expect(assert([])).toBeUndefined();
expect(assert({})).toBeUndefined();
});
});
5 changes: 5 additions & 0 deletions types/assert.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
* The `assert` module provides a set of assertion functions for verifying invariants.
*/
declare module "assert" {
/**
* An alias of {@link ok}.
* @param value The input that is checked for being truthy.
*/
function assert(value: unknown, message?: string | Error): asserts value;
/**
* Tests if `value` is truthy.
*
Expand Down

0 comments on commit dd25160

Please sign in to comment.