Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: introduce serde_v8 #9722

Merged
merged 24 commits into from
Mar 26, 2021
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
serde_v8: add tests/ser
  • Loading branch information
AaronO committed Mar 24, 2021
commit 3a05f88c4ebdcc576cc9e992885ee900e8f45238
115 changes: 115 additions & 0 deletions serde_v8/tests/ser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
use rusty_v8 as v8;

use serde::Serialize;

use serde_v8::utils::{js_exec, v8_do};

#[derive(Debug, Serialize, PartialEq)]
struct MathOp {
pub a: u64,
pub b: u64,
pub operator: Option<String>,
}

// Utility JS code (obj equality, etc...)
const JS_UTILS: &str = r#"
// Shallow obj equality (don't use deep objs for now)
function objEqual(object1, object2) {
const keys1 = Object.keys(object1);
const keys2 = Object.keys(object2);

if (keys1.length !== keys2.length) {
return false;
}

for (let key of keys1) {
if (object1[key] !== object2[key]) {
return false;
}
}

return true;
}

function arrEqual(a, b) {
return Array.isArray(a) &&
Array.isArray(b) &&
a.length === b.length &&
a.every((val, index) => val === b[index]);
}
"#;

fn sercheck<T: Serialize>(val: T, code: &str) -> bool {
let mut equal = false;

v8_do(|| {
// Setup isolate
let isolate = &mut v8::Isolate::new(v8::CreateParams::default());
let handle_scope = &mut v8::HandleScope::new(isolate);
let context = v8::Context::new(handle_scope);
let scope = &mut v8::ContextScope::new(handle_scope, context);

// Set value as "x" in global scope
let global = context.global(scope);
let v8_key = serde_v8::to_v8(scope, "x").unwrap();
let v8_val = serde_v8::to_v8(scope, val).unwrap();
global.set(scope, v8_key, v8_val);

// Load util functions
js_exec(scope, JS_UTILS);
// Execute equality check in JS (e.g: x == ...)
let v = js_exec(scope, code);
// Cast to bool
equal = serde_v8::from_v8(scope, v).unwrap();
});

equal
}

macro_rules! sertest {
($fn_name:ident, $rust:expr, $src:expr) => {
#[test]
fn $fn_name() {
assert!(
sercheck($rust, $src),
format!("Expected: {} where x={:?}", $src, $rust),
);
}
};
}

sertest!(ser_option_some, Some(true), "x === true");
sertest!(ser_option_null, None as Option<bool>, "x === null");
sertest!(ser_unit_null, (), "x === null");
sertest!(ser_bool, true, "x === true");
sertest!(ser_u64, 32, "x === 32");
sertest!(ser_f64, 12345.0, "x === 12345.0");
sertest!(ser_string, "Hello".to_owned(), "x === 'Hello'");
sertest!(ser_vec_u64, vec![1, 2, 3, 4, 5], "arrEqual(x, [1,2,3,4,5])");
sertest!(
ser_vec_string,
vec!["hello".to_owned(), "world".to_owned(),],
"arrEqual(x, ['hello', 'world'])"
);
sertest!(ser_tuple, (123, true, ()), "arrEqual(x, [123, true, null])");
sertest!(
ser_mathop,
MathOp {
a: 1,
b: 3,
operator: None
},
"objEqual(x, {a: 1, b: 3, operator: null})"
);

use std::collections::BTreeMap;

sertest!(
ser_map,
{
let map: BTreeMap<&str, u32> =
vec![("a", 1), ("b", 2), ("c", 3)].drain(..).collect();
map
},
"objEqual(x, {a: 1, b: 2, c: 3})"
);