-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Initial implementation of assert module (#668)
* feat: Initial implementation of assert module --------- Co-authored-by: Richard Davison <[email protected]>
- Loading branch information
1 parent
1b20ad1
commit 77cf8ef
Showing
15 changed files
with
263 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "llrt_assert" | ||
description = "LLRT Module assert" | ||
version = "0.3.0-beta" | ||
edition = "2021" | ||
license = "Apache-2.0" | ||
repository = "https://github.com/awslabs/llrt" | ||
|
||
[lib] | ||
name = "llrt_assert" | ||
path = "src/lib.rs" | ||
|
||
[dependencies] | ||
llrt_utils = { version = "0.3.0-beta", path = "../../libs/llrt_utils", default-features = false } | ||
rquickjs = { git = "https://github.com/DelSkayn/rquickjs.git", rev = "3af3f46b13eb89a2694e5e4e2e73924a20fa9dd1", default-features = false } | ||
|
||
[dev-dependencies] | ||
llrt_test = { path = "../../libs/llrt_test" } | ||
tokio = { version = "1", features = ["full"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
use llrt_utils::module::ModuleInfo; | ||
use rquickjs::{ | ||
module::{Declarations, Exports, ModuleDef}, | ||
prelude::Opt, | ||
Ctx, Exception, Function, Result, Type, Value, | ||
}; | ||
|
||
fn ok(ctx: Ctx, value: Value, message: Opt<Value>) -> Result<()> { | ||
match value.type_of() { | ||
Type::Bool => { | ||
if value.as_bool().unwrap() { | ||
return Ok(()); | ||
} | ||
}, | ||
Type::Float | Type::Int => { | ||
if value.as_number().unwrap() != 0.0 { | ||
return Ok(()); | ||
} | ||
}, | ||
Type::String => { | ||
if !value.as_string().unwrap().to_string().unwrap().is_empty() { | ||
return Ok(()); | ||
} | ||
}, | ||
Type::Array | ||
| Type::BigInt | ||
| Type::Constructor | ||
| Type::Exception | ||
| Type::Function | ||
| Type::Symbol | ||
| Type::Object => { | ||
return Ok(()); | ||
}, | ||
_ => {}, | ||
} | ||
|
||
if let Some(obj) = message.0 { | ||
match obj.type_of() { | ||
Type::String => { | ||
let msg = obj.as_string().unwrap().to_string().unwrap(); | ||
return Err(Exception::throw_message(&ctx, &msg)); | ||
}, | ||
Type::Exception => return Err(obj.as_exception().cloned().unwrap().throw()), | ||
_ => {}, | ||
}; | ||
} | ||
|
||
Err(Exception::throw_message( | ||
&ctx, | ||
"AssertionError: The expression was evaluated to a falsy value", | ||
)) | ||
} | ||
|
||
pub struct AssertModule; | ||
|
||
impl ModuleDef for AssertModule { | ||
fn declare(declare: &Declarations) -> Result<()> { | ||
declare.declare("ok")?; | ||
|
||
declare.declare("default")?; | ||
Ok(()) | ||
} | ||
|
||
fn evaluate<'js>(ctx: &Ctx<'js>, exports: &Exports<'js>) -> Result<()> { | ||
let ok_function = Function::new(ctx.clone(), ok)?.with_name("ok")?; | ||
ok_function.set("ok", ok_function.clone())?; | ||
|
||
exports.export("ok", ok_function.clone())?; | ||
exports.export("default", ok_function)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl From<AssertModule> for ModuleInfo<AssertModule> { | ||
fn from(val: AssertModule) -> Self { | ||
ModuleInfo { | ||
name: "assert", | ||
module: val, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import assert from "assert"; | ||
|
||
describe("assert.ok", () => { | ||
it("Should be returned 'undefined' (So it's not an error)", () => { | ||
expect(assert.ok(true)).toBeUndefined(); //bool | ||
expect(assert.ok(1)).toBeUndefined(); // numeric | ||
expect(assert.ok("non-empty string")).toBeUndefined(); // string | ||
expect(assert.ok([])).toBeUndefined(); // array | ||
expect(assert.ok({})).toBeUndefined(); // object | ||
expect(assert.ok(() => {})).toBeUndefined(); // function | ||
expect(assert.ok(123n)).toBeUndefined(); // bigint | ||
expect(assert.ok(Symbol())).toBeUndefined(); // symbol | ||
expect(assert.ok(new Error())).toBeUndefined(); // error | ||
class AssertTestClass {} | ||
expect(assert.ok(AssertTestClass)).toBeUndefined(); // constructor | ||
}); | ||
|
||
it("Should be returned exception", () => { | ||
const errMsg = | ||
"AssertionError: The expression was evaluated to a falsy value"; | ||
expect(() => assert.ok(false)).toThrow(errMsg); | ||
expect(() => assert.ok(0)).toThrow(errMsg); | ||
expect(() => assert.ok("")).toThrow(errMsg); | ||
expect(() => assert.ok(null)).toThrow(errMsg); | ||
}); | ||
|
||
it("should be returned as original error message", () => { | ||
const errMsg = "Error: Value must be true"; | ||
expect(() => assert.ok(false, errMsg)).toThrow(errMsg); | ||
}); | ||
|
||
it("should be returned as original error", () => { | ||
const errMsg = "Error: This is error"; | ||
expect(() => assert.ok(false, Error(errMsg))).toThrow(errMsg); | ||
}); | ||
|
||
it("Should be handled correctly even within functions", () => { | ||
const errMsg = "Error: Value should be truthy"; | ||
function checkValue(value) { | ||
assert.ok(value, errMsg); | ||
} | ||
expect(checkValue(true)).toBeUndefined(); | ||
expect(() => checkValue(false)).toThrow(errMsg); | ||
}); | ||
}); | ||
|
||
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(); | ||
}); | ||
}); |
Oops, something went wrong.