Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sytten committed Nov 25, 2024
1 parent d47dc31 commit 352a4e6
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use rquickjs::{class::Trace, JsLifetime};

#[derive(Clone, Trace, JsLifetime)]
#[rquickjs::class(frozen)]
pub struct Printer {
target: String,
}

impl Printer {
pub fn new(target: String) -> Self {
Self { target }
}
}

#[rquickjs::methods]
impl Printer {
fn print(&self) -> String {
format!("hello {}", self.target)
}
}

#[derive(JsLifetime, Debug)]
pub struct PrinterOptions {
pub target: String,
}
89 changes: 89 additions & 0 deletions tests/globals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use rquickjs::{async_with, AsyncContext, AsyncRuntime, CatchResultExt, Object, Result};
use rquickjs_module::{globals_only_module, GlobalsOnly, ModuleDefExt, ModuleLoader};

use self::common::{Printer, PrinterOptions};

mod common;

struct PrinterModule {
options: PrinterOptions,
}

impl PrinterModule {
pub fn new<T: Into<String>>(target: T) -> Self {
Self {
options: PrinterOptions {
target: target.into(),
},
}
}
}

impl ModuleDefExt<PrinterOptions> for PrinterModule {
type Implementation = GlobalsOnly;

fn implementation() -> &'static Self::Implementation {
&GlobalsOnly
}

fn options(self) -> PrinterOptions {
self.options
}

fn globals(globals: &Object<'_>, options: &PrinterOptions) -> Result<()> {
globals.set("global_printer", Printer::new(options.target.clone()))?;
Ok(())
}
}

struct PrinterModule2;
globals_only_module!(PrinterModule2, |globals| {
globals.set("global_printer", Printer::new("emile".to_string()))?;
Ok(())
});

#[tokio::test]
async fn test_global() {
let rt = AsyncRuntime::new().unwrap();

let (loader, resolver, initalizer) = ModuleLoader::builder()
.with_module(PrinterModule::new("world"))
.build();

rt.set_loader(resolver, loader).await;

let ctx = AsyncContext::full(&rt).await.unwrap();

async_with!(ctx => |ctx| {
initalizer.init(&ctx).unwrap();

let result = ctx.eval::<String,_>(r#"
global_printer.print()
"#).catch(&ctx).unwrap();
assert_eq!(result, "hello world");
})
.await;
}

// Enable once https://github.com/DelSkayn/rquickjs/pull/395 is merged
// #[tokio::test]
// async fn test_global_macro() {
// let rt = AsyncRuntime::new().unwrap();

// let (loader, resolver, initalizer) =
// ModuleLoader::builder().with_module(PrinterModule2).build();

// rt.set_loader(resolver, loader).await;

// let ctx = AsyncContext::full(&rt).await.unwrap();

// async_with!(ctx => |ctx| {
// initalizer.init(&ctx).unwrap();

// let result = ctx.eval::<String,_>(r#"
// global_printer.print()
// "#).catch(&ctx).unwrap();
// assert_eq!(result, "hello emile");
// })
// .await;
// }
128 changes: 128 additions & 0 deletions tests/module.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
use rquickjs::{
async_with, AsyncContext, AsyncRuntime, CatchResultExt, Function, Module, Object, Result,
};
use rquickjs_module::{ModuleDefExt, ModuleImpl, ModuleLoader};

use self::common::{Printer, PrinterOptions};

mod common;

struct PrinterModule {
options: PrinterOptions,
}

impl PrinterModule {
pub fn new<T: Into<String>>(target: T) -> Self {
Self {
options: PrinterOptions {
target: target.into(),
},
}
}
}

impl ModuleDefExt<PrinterOptions> for PrinterModule {
type Implementation = ModuleImpl<PrinterOptions>;

fn implementation() -> &'static Self::Implementation {
&ModuleImpl {
declare: |decl| {
decl.declare("default")?;
Ok(())
},
evaluate: |_ctx, exports, options| {
exports.export("default", Printer::new(options.target.clone()))?;
Ok(())
},
name: "printer",
}
}

fn options(self) -> PrinterOptions {
self.options
}

fn globals(globals: &Object<'_>, options: &PrinterOptions) -> Result<()> {
globals.set("global_printer", Printer::new(options.target.clone()))?;
Ok(())
}
}

#[tokio::test]
async fn test_module() {
let rt = AsyncRuntime::new().unwrap();

let (loader, resolver, initalizer) = ModuleLoader::builder()
.with_module(PrinterModule::new("john"))
.build();

rt.set_loader(resolver, loader).await;

let ctx = AsyncContext::full(&rt).await.unwrap();

async_with!(ctx => |ctx| {
initalizer.init(&ctx).unwrap();

let (module, module_eval) = Module::declare(ctx.clone(), "test", r#"
import printer from "printer";
export function testFunc() {
return printer.print();
}
"#).unwrap().eval().unwrap();
module_eval.into_future::<()>().await.unwrap();
let result = module.get::<_, Function>("testFunc").unwrap().call::<_, String>(()).unwrap();
assert_eq!(result, "hello john");
})
.await;
}

#[tokio::test]
async fn test_module_named() {
let rt = AsyncRuntime::new().unwrap();

let (loader, resolver, initalizer) = ModuleLoader::builder()
.with_module_named(PrinterModule::new("arnold"), "custom_printer")
.build();

rt.set_loader(resolver, loader).await;

let ctx = AsyncContext::full(&rt).await.unwrap();

async_with!(ctx => |ctx| {
initalizer.init(&ctx).unwrap();

let (module, module_eval) = Module::declare(ctx.clone(), "test", r#"
import printer from "custom_printer";
export function testFunc() {
return printer.print();
}
"#).unwrap().eval().unwrap();
module_eval.into_future::<()>().await.unwrap();
let result = module.get::<_, Function>("testFunc").unwrap().call::<_, String>(()).unwrap();
assert_eq!(result, "hello arnold");
})
.await;
}

#[tokio::test]
async fn test_module_global() {
let rt = AsyncRuntime::new().unwrap();

let (loader, resolver, initalizer) = ModuleLoader::builder()
.with_module(PrinterModule::new("david"))
.build();

rt.set_loader(resolver, loader).await;

let ctx = AsyncContext::full(&rt).await.unwrap();

async_with!(ctx => |ctx| {
initalizer.init(&ctx).unwrap();

let result = ctx.eval::<String,_>(r#"
global_printer.print()
"#).catch(&ctx).unwrap();
assert_eq!(result, "hello david");
})
.await;
}

0 comments on commit 352a4e6

Please sign in to comment.