From 9e24dabe9c48f21a5c0c6090d216460b9f0639cd Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Wed, 7 Mar 2018 09:49:22 -0800 Subject: [PATCH 1/2] test-support: Add ability to add new local dependencies to Cargo.toml --- crates/test-support/src/lib.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/crates/test-support/src/lib.rs b/crates/test-support/src/lib.rs index aff7398f9..04c789bde 100644 --- a/crates/test-support/src/lib.rs +++ b/crates/test-support/src/lib.rs @@ -42,6 +42,8 @@ pub fn project() -> Project { [lib] crate-type = ["cdylib"] + # XXX: It is important that `[dependencies]` is the last section + # here, so that `add_local_dependency` functions correctly! [dependencies] wasm-bindgen = {{ path = '{}' }} "#, IDX.with(|x| *x), dir.display())), @@ -142,6 +144,20 @@ impl Project { self } + pub fn add_local_dependency(&mut self, name: &str, path: &str) -> &mut Project { + { + let cargo_toml = self.files + .iter_mut() + .find(|f| f.0 == "Cargo.toml") + .expect("should have Cargo.toml file!"); + cargo_toml.1.push_str(name); + cargo_toml.1.push_str(" = { path = \""); + cargo_toml.1.push_str(path); + cargo_toml.1.push_str("\" }"); + } + self + } + pub fn test(&mut self) { let root = root(); drop(fs::remove_dir_all(&root)); From 12d7c93a40bbd0169b59ee3b35fe498e4ba5e367 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Wed, 7 Mar 2018 11:57:51 -0800 Subject: [PATCH 2/2] Add a test for using a dependency's types in bindings --- tests/dependencies.rs | 92 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 tests/dependencies.rs diff --git a/tests/dependencies.rs b/tests/dependencies.rs new file mode 100644 index 000000000..36ac6fda2 --- /dev/null +++ b/tests/dependencies.rs @@ -0,0 +1,92 @@ +extern crate test_support; + +#[test] +fn dependencies_work() { + test_support::project() + .file( + "src/lib.rs", + r#" + #![feature(proc_macro)] + extern crate wasm_bindgen; + extern crate dependency; + use wasm_bindgen::prelude::*; + + #[wasm_bindgen] + pub fn return_dep_ty(x: f64) -> dependency::Foo { + dependency::Foo(x) + } + + #[wasm_bindgen] + pub fn takes_own_dep_ty(foo: dependency::Foo) -> f64 { + foo.0 + } + + #[wasm_bindgen] + pub fn takes_ref_dep_ty(foo: &dependency::Foo) -> f64 { + foo.0 + } + + #[wasm_bindgen] + pub fn takes_mut_dep_ty(foo: &mut dependency::Foo, x: f64) { + foo.0 = x; + } + "#, + ) + .add_local_dependency("dependency", "vendor/dependency") + .file( + "vendor/dependency/Cargo.toml", + &format!( + r#" + [package] + name = "dependency" + version = "0.0.1" + authors = [] + + [dependencies] + wasm-bindgen = {{ path = '{}' }} + "#, + env!("CARGO_MANIFEST_DIR") + ), + ) + .file( + "vendor/dependency/src/lib.rs", + r#" + #![feature(proc_macro)] + extern crate wasm_bindgen; + use wasm_bindgen::prelude::*; + + #[wasm_bindgen] + pub struct Foo(pub f64); + + #[wasm_bindgen] + impl Foo { + pub fn new(x: f64) -> Foo { Foo(x) } + pub fn get(&self) -> f64 { self.0 } + } + "#, + ) + .file( + "test.ts", + r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + const foo = wasm.return_dep_ty(42); + assert.strictEqual(foo.get(), 42); + + const x = wasm.takes_ref_dep_ty(foo); + assert.strictEqual(x, 42); + + const y = 1337; + wasm.takes_mut_dep_ty(foo, y); + assert.strictEqual(foo.get(), y); + + const z = wasm.takes_own_dep_ty(foo); + assert.strictEqual(z, y); + assert.strictEqual(foo.ptr, 0); + } + "#, + ) + .test(); +}