-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathdylib_test.rs
More file actions
50 lines (36 loc) · 1.37 KB
/
Copy pathdylib_test.rs
File metadata and controls
50 lines (36 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use anyhow::Result;
use javy_runner::{Plugin, Runner, RunnerError};
use std::str;
#[test]
fn test_dylib_with_no_fn_name() -> Result<()> {
let js_src = "console.error(42);";
let mut runner = Runner::with_dylib(plugin_module()?)?;
let (_, logs, _) = runner.exec_through_dylib(js_src, None)?;
assert_eq!("42\n", str::from_utf8(&logs)?);
Ok(())
}
#[test]
fn test_dylib_with_error() -> Result<()> {
let js_src = "function foo() { throw new Error('foo error'); } foo();";
let mut runner = Runner::with_dylib(plugin_module()?)?;
let res = runner.exec_through_dylib(js_src, None);
assert!(res.is_err());
let e = res.err().unwrap();
let expected_log_output = "Error:1:24 foo error\n at foo (function.mjs:1:24)\n at <anonymous> (function.mjs:1:50)\n\n";
assert_eq!(
expected_log_output,
e.downcast_ref::<RunnerError>().unwrap().stderr
);
Ok(())
}
#[test]
fn test_dylib_with_exported_func() -> Result<()> {
let js_src = "export function foo() { console.error('In foo'); }; console.error('Toplevel');";
let mut runner = Runner::with_dylib(plugin_module()?)?;
let (_, logs, _) = runner.exec_through_dylib(js_src, Some("foo"))?;
assert_eq!("Toplevel\nIn foo\n", str::from_utf8(&logs)?);
Ok(())
}
fn plugin_module() -> Result<Vec<u8>> {
std::fs::read(Plugin::Default.path()).map_err(Into::into)
}