From f4ae035c8486128061885913b8507278f27a7b39 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 3 Jun 2021 23:57:52 +0800 Subject: [PATCH] test(napi): add tests for bytes in struct --- test_module/__test__/object.spec.ts | 2 +- test_module/__test__/serde/ser.spec.ts | 8 ++++++++ test_module/__test__/serde/ser.spec.ts.md | 11 +++++++++++ test_module/__test__/serde/ser.spec.ts.snap | Bin 1039 -> 1123 bytes test_module/src/serde.rs | 15 +++++++++++++++ 5 files changed, 35 insertions(+), 1 deletion(-) diff --git a/test_module/__test__/object.spec.ts b/test_module/__test__/object.spec.ts index 865113e5..2cce191f 100644 --- a/test_module/__test__/object.spec.ts +++ b/test_module/__test__/object.spec.ts @@ -236,7 +236,7 @@ test('is promise', (t) => { t.true(bindings.testIsPromise(Promise.reject().catch(() => {}))) t.true( bindings.testIsPromise( - new Promise((resolve) => { + new Promise((resolve) => { resolve() }), ), diff --git a/test_module/__test__/serde/ser.spec.ts b/test_module/__test__/serde/ser.spec.ts index 24d46fa0..e71c1a8b 100644 --- a/test_module/__test__/serde/ser.spec.ts +++ b/test_module/__test__/serde/ser.spec.ts @@ -12,6 +12,7 @@ const testFunc = [ 'make_buff', 'make_obj', 'make_map', + 'make_bytes_struct', ] if (napiVersion >= 6) { @@ -24,3 +25,10 @@ for (const func of testFunc) { t.snapshot(bindings[func]()) }) } + +test('serialize make_bytes_struct', (t) => { + t.deepEqual(bindings.make_bytes_struct(), { + code: Buffer.from([0, 1, 2, 3]), + map: 'source map', + }) +}) diff --git a/test_module/__test__/serde/ser.spec.ts.md b/test_module/__test__/serde/ser.spec.ts.md index 4c389086..1b511474 100644 --- a/test_module/__test__/serde/ser.spec.ts.md +++ b/test_module/__test__/serde/ser.spec.ts.md @@ -72,6 +72,17 @@ Generated by [AVA](https://avajs.dev). c: 3, } +## serialize make_bytes_struct from bindings + +> Snapshot 1 + + { + code: Buffer @Uint8Array [ + 00010203 + ], + map: 'source map', + } + ## serialize make_object from bindings > Snapshot 1 diff --git a/test_module/__test__/serde/ser.spec.ts.snap b/test_module/__test__/serde/ser.spec.ts.snap index 4bba6d53b573c6580e7542b714b4529e90edb178..66afca61460c7e0a1d394dc7674501c2ae61e40a 100644 GIT binary patch literal 1123 zcmV-p1f2UpRzVR0u_#Q)sP?9SY*O+gI%WA6Ot z%$alUIrrYo6GHriJ^RJpxj(hs9GR|;zI0s=CFb7(2sdx6zOetq*_j8f>iwCdf|RQ} zVX)?!9Vg$LUA%dajaNIbJe8+h9S8UI`c9pjFNTe-=Qm}xRDY#hJtz^9`R1dy&lQ`G zE_}6jYI<$Y50tA(A0gRC-#i*#KU=ybor;Y48)coO;4k*;Fnk$0)T&!C;Wk0RJ_?LLygCMyO1z zTS}MiDYXU35+qw-mcVR5wglN06iZNS!D9&?Tku+f*A{%1;Ijq4CHNiL%W+(90)3!p zXXxok4H)T)nW8leg+hcj6VpM^bjDpNVM>$r;x~e zi*+;B-m5Tp1DpV#3C2t?W@ljVBe)Q+uqM5ZpH*NzuU}gd*`Y*8_CG_ zH~hlY?pICITz-JB&Skr(wK}C8gm>F{*}XjBUHM;*BgC?_A*@sCUsSdw3nNCu>1Z@- z;x4GQ$9Q?}fXjP85|kBpc|2mHJ`IE2U>`Uv82iOqu23Y$A!$#Bx50bn+&;FfMs#dh zh3RN*17Am(d7Rw>w$zTY&XH`^D4Uybpf6vn=u5GN5Nj)Gp=@#CZ+oI$MGvILDn_?a z?62gOouLW|(^XwYp-^hOw^S}<9481+;G p%4wqw63_M!-aQQ75wn#VXN#Va*d?YbJO5>9`v*9avB$Lv007wYDW?Dc literal 1039 zcmV+q1n~PoRzVk}`Yrv$oZliybgZwxdf@o!xyP;>ILmS|66!7( zuD@yL7az?p-8IUm>Km^;Q6iyEfky@+C(fL!B<$W-Hsm+ge@_=;4Cb9KoMH?Ag)`04R(OtU>dvwj)FOG68s9T zrci>k4b^IPlE`btVlhTh@o$GiVb>66q%!xGX!)MV6BJiaJb}9c_XO1yR8P=cLGy%= zD}+2D>4` zvMX}g)nM7f#Cy7RsrC@{rl>STquY??07SFf^o>TRQDI;klb=@!w(=_E5tKN$@-)<&*49hUnX3DbCdNQ>x znM#}HI{Z_5kA=HTrbpLxXA<$}kj*ae8o%6Z#@c%uhWEg6@VR8>l9``^;TLdGGODt% z9lK#CxJ{Mjp%$En5elt`WfOQ9*kA|Ptp?+VrTEG;3I?`w{XXkOfsOSe}qPQD1;z57-ah zm&|Q)Ew4}{-y!J@M!3a$&e%S^qDC~iqQW%M*ud8icaF2W!RE$MwsE{ru&d4{933oG zYSwbDapc;?I*BhW{^bpHpy<)uWXow~kfIdH>NmD diff --git a/test_module/src/serde.rs b/test_module/src/serde.rs index 5ef08954..a6b4e13b 100644 --- a/test_module/src/serde.rs +++ b/test_module/src/serde.rs @@ -44,6 +44,13 @@ struct AnObjectTwo { r: i128, } +#[derive(Serialize, Debug, Deserialize)] +struct BytesObject<'a> { + #[serde(with = "serde_bytes")] + code: &'a [u8], + map: String, +} + macro_rules! make_test { ($name:ident, $val:expr) => { #[js_function] @@ -104,6 +111,13 @@ const NUMBER_BYTES: &[u8] = &[255u8, 254, 253]; make_test!(make_buff, { serde_bytes::Bytes::new(NUMBER_BYTES) }); +make_test!(make_bytes_struct, { + BytesObject { + code: &[0, 1, 2, 3], + map: "source map".to_owned(), + } +}); + macro_rules! make_expect { ($name:ident, $val:expr, $val_type:ty) => { #[js_function(1)] @@ -189,6 +203,7 @@ pub fn register_js(exports: &mut JsObject) -> Result<()> { exports.create_named_method("make_obj", make_obj)?; exports.create_named_method("make_object", make_object)?; exports.create_named_method("make_map", make_map)?; + exports.create_named_method("make_bytes_struct", make_bytes_struct)?; exports.create_named_method("expect_hello_world", expect_hello_world)?; exports.create_named_method("expect_obj", expect_obj)?;