Skip to content

Commit 352a4e6

Browse files
committed
Add tests
1 parent d47dc31 commit 352a4e6

File tree

3 files changed

+242
-0
lines changed

3 files changed

+242
-0
lines changed

tests/common.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use rquickjs::{class::Trace, JsLifetime};
2+
3+
#[derive(Clone, Trace, JsLifetime)]
4+
#[rquickjs::class(frozen)]
5+
pub struct Printer {
6+
target: String,
7+
}
8+
9+
impl Printer {
10+
pub fn new(target: String) -> Self {
11+
Self { target }
12+
}
13+
}
14+
15+
#[rquickjs::methods]
16+
impl Printer {
17+
fn print(&self) -> String {
18+
format!("hello {}", self.target)
19+
}
20+
}
21+
22+
#[derive(JsLifetime, Debug)]
23+
pub struct PrinterOptions {
24+
pub target: String,
25+
}

tests/globals.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
use rquickjs::{async_with, AsyncContext, AsyncRuntime, CatchResultExt, Object, Result};
2+
use rquickjs_module::{globals_only_module, GlobalsOnly, ModuleDefExt, ModuleLoader};
3+
4+
use self::common::{Printer, PrinterOptions};
5+
6+
mod common;
7+
8+
struct PrinterModule {
9+
options: PrinterOptions,
10+
}
11+
12+
impl PrinterModule {
13+
pub fn new<T: Into<String>>(target: T) -> Self {
14+
Self {
15+
options: PrinterOptions {
16+
target: target.into(),
17+
},
18+
}
19+
}
20+
}
21+
22+
impl ModuleDefExt<PrinterOptions> for PrinterModule {
23+
type Implementation = GlobalsOnly;
24+
25+
fn implementation() -> &'static Self::Implementation {
26+
&GlobalsOnly
27+
}
28+
29+
fn options(self) -> PrinterOptions {
30+
self.options
31+
}
32+
33+
fn globals(globals: &Object<'_>, options: &PrinterOptions) -> Result<()> {
34+
globals.set("global_printer", Printer::new(options.target.clone()))?;
35+
Ok(())
36+
}
37+
}
38+
39+
struct PrinterModule2;
40+
globals_only_module!(PrinterModule2, |globals| {
41+
globals.set("global_printer", Printer::new("emile".to_string()))?;
42+
Ok(())
43+
});
44+
45+
#[tokio::test]
46+
async fn test_global() {
47+
let rt = AsyncRuntime::new().unwrap();
48+
49+
let (loader, resolver, initalizer) = ModuleLoader::builder()
50+
.with_module(PrinterModule::new("world"))
51+
.build();
52+
53+
rt.set_loader(resolver, loader).await;
54+
55+
let ctx = AsyncContext::full(&rt).await.unwrap();
56+
57+
async_with!(ctx => |ctx| {
58+
initalizer.init(&ctx).unwrap();
59+
60+
let result = ctx.eval::<String,_>(r#"
61+
global_printer.print()
62+
"#).catch(&ctx).unwrap();
63+
assert_eq!(result, "hello world");
64+
})
65+
.await;
66+
}
67+
68+
// Enable once https://github.com/DelSkayn/rquickjs/pull/395 is merged
69+
// #[tokio::test]
70+
// async fn test_global_macro() {
71+
// let rt = AsyncRuntime::new().unwrap();
72+
73+
// let (loader, resolver, initalizer) =
74+
// ModuleLoader::builder().with_module(PrinterModule2).build();
75+
76+
// rt.set_loader(resolver, loader).await;
77+
78+
// let ctx = AsyncContext::full(&rt).await.unwrap();
79+
80+
// async_with!(ctx => |ctx| {
81+
// initalizer.init(&ctx).unwrap();
82+
83+
// let result = ctx.eval::<String,_>(r#"
84+
// global_printer.print()
85+
// "#).catch(&ctx).unwrap();
86+
// assert_eq!(result, "hello emile");
87+
// })
88+
// .await;
89+
// }

tests/module.rs

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
use rquickjs::{
2+
async_with, AsyncContext, AsyncRuntime, CatchResultExt, Function, Module, Object, Result,
3+
};
4+
use rquickjs_module::{ModuleDefExt, ModuleImpl, ModuleLoader};
5+
6+
use self::common::{Printer, PrinterOptions};
7+
8+
mod common;
9+
10+
struct PrinterModule {
11+
options: PrinterOptions,
12+
}
13+
14+
impl PrinterModule {
15+
pub fn new<T: Into<String>>(target: T) -> Self {
16+
Self {
17+
options: PrinterOptions {
18+
target: target.into(),
19+
},
20+
}
21+
}
22+
}
23+
24+
impl ModuleDefExt<PrinterOptions> for PrinterModule {
25+
type Implementation = ModuleImpl<PrinterOptions>;
26+
27+
fn implementation() -> &'static Self::Implementation {
28+
&ModuleImpl {
29+
declare: |decl| {
30+
decl.declare("default")?;
31+
Ok(())
32+
},
33+
evaluate: |_ctx, exports, options| {
34+
exports.export("default", Printer::new(options.target.clone()))?;
35+
Ok(())
36+
},
37+
name: "printer",
38+
}
39+
}
40+
41+
fn options(self) -> PrinterOptions {
42+
self.options
43+
}
44+
45+
fn globals(globals: &Object<'_>, options: &PrinterOptions) -> Result<()> {
46+
globals.set("global_printer", Printer::new(options.target.clone()))?;
47+
Ok(())
48+
}
49+
}
50+
51+
#[tokio::test]
52+
async fn test_module() {
53+
let rt = AsyncRuntime::new().unwrap();
54+
55+
let (loader, resolver, initalizer) = ModuleLoader::builder()
56+
.with_module(PrinterModule::new("john"))
57+
.build();
58+
59+
rt.set_loader(resolver, loader).await;
60+
61+
let ctx = AsyncContext::full(&rt).await.unwrap();
62+
63+
async_with!(ctx => |ctx| {
64+
initalizer.init(&ctx).unwrap();
65+
66+
let (module, module_eval) = Module::declare(ctx.clone(), "test", r#"
67+
import printer from "printer";
68+
export function testFunc() {
69+
return printer.print();
70+
}
71+
"#).unwrap().eval().unwrap();
72+
module_eval.into_future::<()>().await.unwrap();
73+
let result = module.get::<_, Function>("testFunc").unwrap().call::<_, String>(()).unwrap();
74+
assert_eq!(result, "hello john");
75+
})
76+
.await;
77+
}
78+
79+
#[tokio::test]
80+
async fn test_module_named() {
81+
let rt = AsyncRuntime::new().unwrap();
82+
83+
let (loader, resolver, initalizer) = ModuleLoader::builder()
84+
.with_module_named(PrinterModule::new("arnold"), "custom_printer")
85+
.build();
86+
87+
rt.set_loader(resolver, loader).await;
88+
89+
let ctx = AsyncContext::full(&rt).await.unwrap();
90+
91+
async_with!(ctx => |ctx| {
92+
initalizer.init(&ctx).unwrap();
93+
94+
let (module, module_eval) = Module::declare(ctx.clone(), "test", r#"
95+
import printer from "custom_printer";
96+
export function testFunc() {
97+
return printer.print();
98+
}
99+
"#).unwrap().eval().unwrap();
100+
module_eval.into_future::<()>().await.unwrap();
101+
let result = module.get::<_, Function>("testFunc").unwrap().call::<_, String>(()).unwrap();
102+
assert_eq!(result, "hello arnold");
103+
})
104+
.await;
105+
}
106+
107+
#[tokio::test]
108+
async fn test_module_global() {
109+
let rt = AsyncRuntime::new().unwrap();
110+
111+
let (loader, resolver, initalizer) = ModuleLoader::builder()
112+
.with_module(PrinterModule::new("david"))
113+
.build();
114+
115+
rt.set_loader(resolver, loader).await;
116+
117+
let ctx = AsyncContext::full(&rt).await.unwrap();
118+
119+
async_with!(ctx => |ctx| {
120+
initalizer.init(&ctx).unwrap();
121+
122+
let result = ctx.eval::<String,_>(r#"
123+
global_printer.print()
124+
"#).catch(&ctx).unwrap();
125+
assert_eq!(result, "hello david");
126+
})
127+
.await;
128+
}

0 commit comments

Comments
 (0)