mirror of https://github.com/boa-dev/boa.git
Browse Source
* Add support for boa(rename = "") in TryFromJs derive * Fix clippies and fmt * Move macro test to boa_enginepull/3985/head
Hans Larsen
3 months ago
committed by
GitHub
2 changed files with 43 additions and 4 deletions
@ -0,0 +1,36 @@ |
|||||||
|
#![allow(unused_crate_dependencies)] |
||||||
|
use boa_engine::value::TryFromJs; |
||||||
|
use boa_engine::{js_string, Context, JsResult, JsValue, Source}; |
||||||
|
use boa_string::JsString; |
||||||
|
|
||||||
|
#[test] |
||||||
|
fn try_from_js_derive() { |
||||||
|
#[derive(Debug, TryFromJs, Eq, PartialEq)] |
||||||
|
struct TryFromJsTest { |
||||||
|
a: JsString, |
||||||
|
#[boa(rename = "bBB")] |
||||||
|
b: i32, |
||||||
|
#[boa(from_js_with = "check_tfj_called")] |
||||||
|
c: i32, |
||||||
|
} |
||||||
|
|
||||||
|
fn check_tfj_called(value: &JsValue, context: &mut Context) -> JsResult<i32> { |
||||||
|
let v = value.to_i32(context)?; |
||||||
|
Ok(v / 2) |
||||||
|
} |
||||||
|
|
||||||
|
let mut context = Context::default(); |
||||||
|
let obj = context |
||||||
|
.eval(Source::from_bytes(br#"({ a: "hello", bBB: 42, c: 120 })"#)) |
||||||
|
.unwrap(); |
||||||
|
|
||||||
|
let result = TryFromJsTest::try_from_js(&obj, &mut context).unwrap(); |
||||||
|
assert_eq!( |
||||||
|
result, |
||||||
|
TryFromJsTest { |
||||||
|
a: js_string!("hello"), |
||||||
|
b: 42, |
||||||
|
c: 60 |
||||||
|
} |
||||||
|
); |
||||||
|
} |
Loading…
Reference in new issue