Migrate the import_class to wasm

This commit is contained in:
Alex Crichton 2018-08-05 09:12:23 -07:00
parent e9f9ede1fa
commit 66d51f13ee
5 changed files with 327 additions and 608 deletions

View File

@ -1,607 +0,0 @@
use super::project;
#[test]
fn simple() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn get_random() -> f64 {
random()
}
#[wasm_bindgen]
pub fn do_log(a: f64) -> f64 {
log(a)
}
#[wasm_bindgen]
extern {
#[wasm_bindgen(js_namespace = Math)]
fn random() -> f64;
#[wasm_bindgen(js_namespace = Math)]
fn log(a: f64) -> f64;
}
"#,
)
.file(
"test.js",
r#"
import * as wasm from "./out";
import * as assert from "assert";
export function test() {
wasm.get_random();
assert.strictEqual(wasm.do_log(1.0), Math.log(1.0));
}
"#,
)
.test();
}
#[test]
fn import_class() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./another")]
extern {
#[wasm_bindgen(js_namespace = Foo)]
fn bar();
}
#[wasm_bindgen]
pub fn baz() {
bar();
}
"#,
)
.file(
"test.js",
r#"
import { baz } from "./out";
import { called } from "./another";
import * as assert from "assert";
export function test() {
baz();
assert.strictEqual(called, true);
}
"#,
)
.file(
"another.js",
r#"
export let called = false;
export class Foo {
static bar() {
called = true;
}
}
"#,
)
.test();
}
#[test]
fn construct() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./another")]
extern {
#[derive(Clone)]
type Foo;
#[wasm_bindgen(js_namespace = Foo)]
fn create() -> Foo;
#[wasm_bindgen(method)]
fn get_internal_string(this: &Foo) -> String;
#[wasm_bindgen(method)]
fn append_to_internal_string(this: &Foo, s: &str);
#[wasm_bindgen(method)]
fn assert_internal_string(this: &Foo, s: &str);
}
#[wasm_bindgen]
pub fn run() {
let f = Foo::create();
assert_eq!(f.get_internal_string(), "this");
assert_eq!(f.clone().get_internal_string(), "this");
f.append_to_internal_string(" foo");
f.assert_internal_string("this foo");
}
"#,
)
.file(
"test.js",
r#"
import { run } from "./out";
import { called } from "./another";
import * as assert from "assert";
export function test() {
run();
assert.strictEqual(called, true);
}
"#,
)
.file(
"another.js",
r#"
import * as assert from "assert";
export let called = false;
export class Foo {
static create() {
const ret = new Foo();
ret.internal_string = 'this';
return ret;
}
get_internal_string() {
return this.internal_string;
}
append_to_internal_string(s) {
this.internal_string += s;
}
assert_internal_string(s) {
assert.strictEqual(this.internal_string, s);
called = true;
}
}
Foo.internal_string = '';
"#,
)
.test();
}
#[test]
fn new_constructors() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./another")]
extern {
type Foo;
#[wasm_bindgen(constructor)]
fn new(arg: i32) -> Foo;
#[wasm_bindgen(method)]
fn get(this: &Foo) -> i32;
}
#[wasm_bindgen]
pub fn run() {
let f = Foo::new(1);
assert_eq!(f.get(), 2);
}
"#,
)
.file(
"test.js",
r#"
import { run } from "./out";
export function test() {
run();
}
"#,
)
.file(
"another.js",
r#"
export class Foo {
constructor(field) {
this.field = field;
}
get() {
return this.field + 1;
}
}
"#,
)
.test();
}
#[test]
fn switch_methods() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./another")]
extern {
type Foo;
#[wasm_bindgen(constructor)]
fn new() -> Foo;
#[wasm_bindgen(js_namespace = Foo)]
fn a();
#[wasm_bindgen(method)]
fn b(this: &Foo);
}
#[wasm_bindgen]
pub fn a() {
Foo::a();
}
#[wasm_bindgen]
pub fn b() {
Foo::new().b();
}
"#,
)
.file(
"test.js",
r#"
import { a, b } from "./out";
import { Foo, called } from "./another";
import * as assert from "assert";
export function test() {
assert.strictEqual(called.a, false);
a();
assert.strictEqual(called.a, true);
called.a = false;
Foo.a = function() {};
assert.strictEqual(called.a, false);
a();
assert.strictEqual(called.a, true);
called.a = false;
assert.strictEqual(called.a, false);
b();
assert.strictEqual(called.a, true);
called.a = false;
Foo.prototype.b = function() {};
assert.strictEqual(called.a, false);
b();
assert.strictEqual(called.a, true);
}
"#,
)
.file(
"another.js",
r#"
export let called = { a: false };
export class Foo {
constructor() {
}
static a() {
called.a = true;
}
b() {
called.a = true;
}
}
"#,
)
.test();
}
#[test]
fn properties() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./another")]
extern {
type Foo;
#[wasm_bindgen(constructor)]
fn new() -> Foo;
#[wasm_bindgen(getter, method)]
fn a(this: &Foo) -> i32;
#[wasm_bindgen(setter, method)]
fn set_a(this: &Foo, a: i32);
}
#[wasm_bindgen]
pub fn run() {
let a = Foo::new();
assert_eq!(a.a(), 1);
a.set_a(2);
assert_eq!(a.a(), 2);
}
"#,
)
.file(
"test.js",
r#"
import { run } from "./out";
export function test() {
run();
}
"#,
)
.file(
"another.js",
r#"
export class Foo {
constructor() {
this.num = 1;
}
get a() {
return this.num;
}
set a(val) {
this.num = val;
}
}
"#,
)
.test();
}
#[test]
fn rename_setter_getter() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./another")]
extern {
type Foo;
#[wasm_bindgen(constructor)]
fn new() -> Foo;
#[wasm_bindgen(getter = a, method)]
fn test(this: &Foo) -> i32;
#[wasm_bindgen(setter = a, method)]
fn another(this: &Foo, a: i32);
}
#[wasm_bindgen]
pub fn run() {
let x: fn() -> Foo = Foo::new;
let a = x();
assert_eq!(a.test(), 1);
a.another(2);
assert_eq!(a.test(), 2);
}
"#,
)
.file(
"test.js",
r#"
import { run } from "./out";
export function test() {
run();
}
"#,
)
.file(
"another.js",
r#"
export class Foo {
constructor() {
this.num = 1;
}
get a() {
return this.num;
}
set a(val) {
this.num = val;
}
}
"#,
)
.test();
}
#[test]
fn deny_missing_docs() {
project()
.file(
"src/lib.rs",
r#"
//! dox
#![feature(use_extern_macros)]
#![deny(missing_docs)]
#![allow(dead_code)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
/// dox
#[wasm_bindgen]
pub struct Bar {
/// dox
pub a: u32,
b: i64,
}
#[wasm_bindgen]
extern {
/// dox
pub type Foo;
/// dox
#[wasm_bindgen(constructor)]
pub fn new() -> Foo;
/// dox
#[wasm_bindgen(getter = a, method)]
pub fn test(this: &Foo) -> i32;
/// dox
pub fn foo();
}
/// dox
#[wasm_bindgen]
pub fn test() {
}
"#,
)
.test();
}
#[test]
fn options() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "./foo")]
extern {
pub type Foo;
#[wasm_bindgen(constructor)]
fn new() -> Foo;
fn take_none(val: Option<Foo>);
fn take_some(val: Option<Foo>);
fn return_null() -> Option<Foo>;
fn return_undefined() -> Option<Foo>;
fn return_some() -> Option<Foo>;
fn run_rust_tests();
}
#[wasm_bindgen]
pub fn test() {
take_none(None);
take_some(Some(Foo::new()));
assert!(return_null().is_none());
assert!(return_undefined().is_none());
assert!(return_some().is_some());
run_rust_tests();
}
#[wasm_bindgen]
pub fn rust_take_none(a: Option<Foo>) {
assert!(a.is_none());
}
#[wasm_bindgen]
pub fn rust_take_some(a: Option<Foo>) {
assert!(a.is_some());
}
#[wasm_bindgen]
pub fn rust_return_none() -> Option<Foo> {
None
}
#[wasm_bindgen]
pub fn rust_return_some() -> Option<Foo> {
Some(Foo::new())
}
"#,
)
.file(
"foo.js",
r#"
import { strictEqual } from "assert";
import * as wasm from "./out";
export class Foo {
}
export function take_none(val) {
strictEqual(val, undefined);
}
export function take_some(val) {
strictEqual(val === undefined, false);
}
export function return_null() {
return null;
}
export function return_undefined() {
return undefined;
}
export function return_some() {
return new Foo();
}
export function run_rust_tests() {
wasm.rust_take_none();
wasm.rust_take_none(null);
wasm.rust_take_none(undefined);
wasm.rust_take_some(new Foo());
strictEqual(wasm.rust_return_none(), undefined);
strictEqual(wasm.rust_return_none(), undefined);
strictEqual(wasm.rust_return_some() === undefined, false);
}
"#,
)
.test();
}

View File

@ -6,7 +6,6 @@ use project_builder::{project, run};
mod comments;
mod dependencies;
mod import_class;
mod imports;
mod js_objects;
mod node;

125
tests/wasm/import_class.js Normal file
View File

@ -0,0 +1,125 @@
const assert = require('assert');
const wasm = require('wasm-bindgen-test');
exports.math_log = Math.log;
exports.StaticFunction = class {
static bar() { return 2; }
};
class Construct {
static create() {
const ret = new Construct();
ret.internal_string = 'this';
return ret;
}
get_internal_string() {
return this.internal_string;
}
append_to_internal_string(s) {
this.internal_string += s;
}
assert_internal_string(s) {
assert.strictEqual(this.internal_string, s);
}
}
Construct.internal_string = '';
exports.Construct = Construct;
exports.NewConstructors = class {
constructor(field) {
this.field = field;
}
get() {
return this.field + 1;
}
};
let switch_called = false;
class SwitchMethods {
constructor() {
}
static a() {
switch_called = true;
}
b() {
switch_called = true;
}
}
exports.SwitchMethods = SwitchMethods;
exports.switch_methods_called = function() {
const tmp = switch_called;
switch_called = false;
return tmp;
};
exports.switch_methods_a = function() { SwitchMethods.a = function() {}; };
exports.switch_methods_b = function() { SwitchMethods.prototype.b = function() {}; };
exports.Properties = class {
constructor() {
this.num = 1;
}
get a() {
return this.num;
}
set a(val) {
this.num = val;
}
};
exports.RenameProperties = class {
constructor() {
this.num = 1;
}
get a() {
return this.num;
}
set a(val) {
this.num = val;
}
};
class Options {
}
exports.Options = Options;
exports.take_none = function(val) {
assert.strictEqual(val, undefined);
};
exports.take_some = function(val) {
assert.strictEqual(val === undefined, false);
};
exports.return_null = function() {
return null;
};
exports.return_undefined = function() {
return undefined;
};
exports.return_some = function() {
return new Options();
};
exports.run_rust_option_tests = function() {
wasm.rust_take_none();
wasm.rust_take_none(null)
wasm.rust_take_none(undefined);
wasm.rust_take_some(new Options());
assert.strictEqual(wasm.rust_return_none(), undefined);
assert.strictEqual(wasm.rust_return_none(), undefined);
assert.strictEqual(wasm.rust_return_some() === undefined, false);
};

201
tests/wasm/import_class.rs Normal file
View File

@ -0,0 +1,201 @@
//! dox
#![deny(missing_docs)] // test that documenting public bindings is enough
use wasm_bindgen_test::*;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = "tests/wasm/import_class.js", version = "*")]
extern {
fn math_log(f: f64) -> f64;
#[wasm_bindgen(js_namespace = StaticFunction)]
fn bar() -> u32;
#[derive(Clone)]
type Construct;
#[wasm_bindgen(js_namespace = Construct)]
fn create() -> Construct;
#[wasm_bindgen(method)]
fn get_internal_string(this: &Construct) -> String;
#[wasm_bindgen(method)]
fn append_to_internal_string(this: &Construct, s: &str);
#[wasm_bindgen(method)]
fn assert_internal_string(this: &Construct, s: &str);
type NewConstructors;
#[wasm_bindgen(constructor)]
fn new(arg: i32) -> NewConstructors;
#[wasm_bindgen(method)]
fn get(this: &NewConstructors) -> i32;
fn switch_methods_a();
fn switch_methods_b();
type SwitchMethods;
#[wasm_bindgen(constructor)]
fn new() -> SwitchMethods;
#[wasm_bindgen(js_namespace = SwitchMethods)]
fn a();
fn switch_methods_called() -> bool;
#[wasm_bindgen(method)]
fn b(this: &SwitchMethods);
type Properties;
#[wasm_bindgen(constructor)]
fn new() -> Properties;
#[wasm_bindgen(getter, method)]
fn a(this: &Properties) -> i32;
#[wasm_bindgen(setter, method)]
fn set_a(this: &Properties, a: i32);
type RenameProperties;
#[wasm_bindgen(constructor)]
fn new() -> RenameProperties;
#[wasm_bindgen(getter = a, method)]
fn test(this: &RenameProperties) -> i32;
#[wasm_bindgen(setter = a, method)]
fn another(this: &RenameProperties, a: i32);
/// dox
pub type AssertImportDenyDocsWorks;
/// dox
#[wasm_bindgen(constructor)]
pub fn new() -> AssertImportDenyDocsWorks;
/// dox
#[wasm_bindgen(getter = a, method)]
pub fn test(this: &AssertImportDenyDocsWorks) -> i32;
/// dox
pub fn foo();
pub type Options;
#[wasm_bindgen(constructor)]
fn new() -> Options;
fn take_none(val: Option<Options>);
fn take_some(val: Option<Options>);
fn return_null() -> Option<Options>;
fn return_undefined() -> Option<Options>;
fn return_some() -> Option<Options>;
fn run_rust_option_tests();
}
#[wasm_bindgen]
extern {
#[wasm_bindgen(js_namespace = Math)]
fn random() -> f64;
#[wasm_bindgen(js_namespace = Math)]
fn log(a: f64) -> f64;
}
#[wasm_bindgen_test]
fn simple() {
random();
assert_eq!(log(1.0), math_log(1.0));
}
#[wasm_bindgen_test]
fn import_class() {
assert_eq!(bar(), 2);
}
#[wasm_bindgen_test]
fn construct() {
let f = Construct::create();
assert_eq!(f.get_internal_string(), "this");
assert_eq!(f.clone().get_internal_string(), "this");
f.append_to_internal_string(" foo");
f.assert_internal_string("this foo");
}
#[wasm_bindgen_test]
fn new_constructors() {
let f = NewConstructors::new(1);
assert_eq!(f.get(), 2);
}
#[wasm_bindgen_test]
fn switch_methods() {
assert!(!switch_methods_called());
SwitchMethods::a();
assert!(switch_methods_called());
switch_methods_a();
assert!(!switch_methods_called());
SwitchMethods::a();
assert!(switch_methods_called());
assert!(!switch_methods_called());
SwitchMethods::new().b();
assert!(switch_methods_called());
switch_methods_a();
assert!(!switch_methods_called());
SwitchMethods::new().b();
assert!(switch_methods_called());
}
#[wasm_bindgen_test]
fn properties() {
let a = Properties::new();
assert_eq!(a.a(), 1);
a.set_a(2);
assert_eq!(a.a(), 2);
}
#[wasm_bindgen_test]
fn rename_setter_getter() {
let x: fn() -> RenameProperties = RenameProperties::new;
let a = x();
assert_eq!(a.test(), 1);
a.another(2);
assert_eq!(a.test(), 2);
}
/// dox
#[wasm_bindgen]
pub struct AssertDenyDocsWorks {
/// dox
pub a: u32,
_b: i64,
}
/// dox
#[wasm_bindgen]
pub fn assert_deny_docs_works() {
}
#[wasm_bindgen_test]
fn options() {
take_none(None);
take_some(Some(Options::new()));
assert!(return_null().is_none());
assert!(return_undefined().is_none());
assert!(return_some().is_some());
run_rust_option_tests();
}
/// doc
#[wasm_bindgen]
pub fn rust_take_none(a: Option<Options>) {
assert!(a.is_none());
}
/// doc
#[wasm_bindgen]
pub fn rust_take_some(a: Option<Options>) {
assert!(a.is_some());
}
/// doc
#[wasm_bindgen]
pub fn rust_return_none() -> Option<Options> {
None
}
/// doc
#[wasm_bindgen]
pub fn rust_return_some() -> Option<Options> {
Some(Options::new())
}

View File

@ -10,6 +10,7 @@ pub mod classes;
pub mod closures;
pub mod duplicates;
pub mod enums;
pub mod import_class;
pub mod js_objects;
pub mod math;
pub mod option;