Migrate the serde-serialize test to wasm

This commit is contained in:
Alex Crichton 2018-08-06 09:43:55 -07:00
parent 4661588171
commit 0bdb31d41e
7 changed files with 71 additions and 98 deletions

View File

@ -52,6 +52,7 @@ matrix:
script:
- cargo test --release
- cargo test --target wasm32-unknown-unknown
- cargo test --target wasm32-unknown-unknown --features serde-serialize
# Check JS output from all tests against eslint
- npm run run-lint-generated-tests
# Check Examples against eslint

View File

@ -33,6 +33,7 @@ serde_json = { version = "1.0", optional = true }
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = { path = 'crates/test', version = '=0.2.15' }
serde_derive = "1.0"
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
wasm-bindgen-test-project-builder = { path = "crates/test-project-builder", version = '=0.2.15' }

View File

@ -32,9 +32,16 @@ pub fn wasm_bindgen_test(
let mut body = TokenStream::from(body).into_iter();
// Assume the input item is of the form `fn #ident ...`, and extract
// `#ident`
let fn_tok = body.next();
// Skip over other attributes to `fn #ident ...`, and extract `#ident`
let mut leading_tokens = Vec::new();
while let Some(token) = body.next() {
leading_tokens.push(token.clone());
if let TokenTree::Ident(token) = token {
if token == "fn" {
break
}
}
}
let ident = match body.next() {
Some(TokenTree::Ident(token)) => token,
_ => panic!("expected a function name"),
@ -64,7 +71,7 @@ pub fn wasm_bindgen_test(
}
}).into_iter());
tokens.extend(fn_tok);
tokens.extend(leading_tokens);
tokens.push(ident.into());
tokens.extend(body);

View File

@ -1,94 +0,0 @@
use super::project;
#[test]
fn serde() {
project()
.serde(true)
.depend("serde = '1.0'")
.depend("serde_derive = '1.0'")
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
#[macro_use]
extern crate serde_derive;
use wasm_bindgen::prelude::*;
#[derive(Deserialize, Serialize)]
pub struct Foo {
a: u32,
b: String,
c: Option<Bar>,
d: Bar,
}
#[derive(Deserialize, Serialize)]
pub struct Bar {
a: u32,
}
#[wasm_bindgen(module = "./test")]
extern {
fn verify(a: JsValue) -> JsValue;
}
#[wasm_bindgen]
pub fn run() {
let js = JsValue::from_serde("foo").unwrap();
assert_eq!(js.as_string(), Some("foo".to_string()));
let ret = verify(JsValue::from_serde(&Foo {
a: 0,
b: "foo".to_string(),
c: None,
d: Bar { a: 1 },
}).unwrap());
let foo = ret.into_serde::<Foo>().unwrap();
assert_eq!(foo.a, 2);
assert_eq!(foo.b, "bar");
assert!(foo.c.is_some());
assert_eq!(foo.c.as_ref().unwrap().a, 3);
assert_eq!(foo.d.a, 4);
}
#[wasm_bindgen]
pub fn parse(j: &JsValue) {
let s = j.into_serde::<String>().unwrap();
assert_eq!(s, "bar");
}
"#,
)
.file(
"test.js",
r#"
import { run, parse } from "./out";
import * as assert from "assert";
export function verify(a) {
assert.deepStrictEqual(a, {
a: 0,
b: 'foo',
c: null,
d: { a: 1 }
});
return {
a: 2,
b: 'bar',
c: { a: 3 },
d: { a: 4 },
}
}
export function test() {
run();
parse('bar');
}
"#,
)
.test();
}

View File

@ -82,3 +82,19 @@ exports.js_returning_vector = () => {
exports.js_another_vector_return = () => {
assert.deepStrictEqual(wasm.another_vector_return_get_array(), [1, 2, 3, 4, 5, 6]);
};
exports.verify_serde = function(a) {
assert.deepStrictEqual(a, {
a: 0,
b: 'foo',
c: null,
d: { a: 1 }
});
return {
a: 2,
b: 'bar',
c: { a: 3 },
d: { a: 4 },
}
};

View File

@ -26,6 +26,7 @@ extern {
fn js_returning_vector();
fn js_another_vector_return();
fn verify_serde(val: JsValue) -> JsValue;
}
#[wasm_bindgen]
@ -105,3 +106,40 @@ pub fn another_vector_return_get_array() -> Vec<JsValue> {
fn another_vector_return() {
js_another_vector_return();
}
#[cfg(feature = "serde-serialize")]
#[wasm_bindgen_test]
fn serde() {
#[derive(Deserialize, Serialize)]
pub struct Foo {
a: u32,
b: String,
c: Option<Bar>,
d: Bar,
}
#[derive(Deserialize, Serialize)]
pub struct Bar {
a: u32,
}
let js = JsValue::from_serde("foo").unwrap();
assert_eq!(js.as_string(), Some("foo".to_string()));
let ret = verify_serde(JsValue::from_serde(&Foo {
a: 0,
b: "foo".to_string(),
c: None,
d: Bar { a: 1 },
}).unwrap());
let foo = ret.into_serde::<Foo>().unwrap();
assert_eq!(foo.a, 2);
assert_eq!(foo.b, "bar");
assert!(foo.c.is_some());
assert_eq!(foo.c.as_ref().unwrap().a, 3);
assert_eq!(foo.d.a, 4);
assert_eq!(JsValue::from("bar").into_serde::<String>().unwrap(), "bar");
}

View File

@ -4,6 +4,10 @@
extern crate wasm_bindgen_test;
extern crate wasm_bindgen;
#[cfg(feature = "serde-serialize")]
#[macro_use]
extern crate serde_derive;
pub mod api;
pub mod char;
pub mod classes;