wasm-bindgen/tests/wasm/simple.js

108 lines
2.9 KiB
JavaScript
Raw Normal View History

const assert = require('assert');
const wasm = require('wasm-bindgen-test');
exports.test_add = function() {
assert.strictEqual(wasm.simple_add(1, 2), 3);
assert.strictEqual(wasm.simple_add(2, 3), 5);
assert.strictEqual(wasm.simple_add3(2), 5);
assert.strictEqual(wasm.simple_get2(true), 2);
assert.strictEqual(wasm.simple_return_and_take_bool(true, false), false);
};
exports.test_string_arguments = function() {
wasm.simple_assert_foo("foo");
wasm.simple_assert_foo_and_bar("foo2", "bar");
};
exports.test_return_a_string = function() {
assert.strictEqual(wasm.simple_clone("foo"), "foo");
assert.strictEqual(wasm.simple_clone("another"), "another");
assert.strictEqual(wasm.simple_concat("a", "b", 3), "a b 3");
assert.strictEqual(wasm.simple_concat("c", "d", -2), "c d -2");
};
exports.test_wrong_types = function() {
// this test only works when `--debug` is passed to `wasm-bindgen` (or the
// equivalent thereof)
if (require('process').env.WASM_BINDGEN_NO_DEBUG)
return;
assert.throws(() => wasm.simple_int('a'), /expected a number argument/);
assert.throws(() => wasm.simple_str(3), /expected a string argument/);
};
exports.test_other_exports_still_available = function() {
Dramatically improving the build time of web-sys (#2012) * Pre-generating web-sys * Fixing build errors * Minor refactor for the unit tests * Changing to generate #[wasm_bindgen} annotations * Fixing code generation * Adding in main bin to wasm-bindgen-webidl * Fixing more problems * Adding in support for unstable APIs * Fixing bug with code generation * More code generation fixes * Improving the webidl program * Removing unnecessary cfg from the generated code * Splitting doc comments onto separate lines * Improving the generation for unstable features * Adding in support for string values in enums * Now runs rustfmt on the mod.rs file * Fixing codegen for constructors * Fixing webidl-tests * Fixing build errors * Another fix for build errors * Renaming typescript_name to typescript_type * Adding in docs for typescript_type * Adding in CI script to verify that web-sys is up to date * Fixing CI script * Fixing CI script * Don't suppress git diff output * Remove duplicate definitions of `Location` Looks to be a preexisting bug in wasm-bindgen? * Regenerate webidl * Try to get the git diff command right * Handle named constructors in WebIDL * Remove stray rustfmt.toml * Add back NamedConstructorBar definition in tests * Run stable rustfmt over everything * Don't run Cargo in a build script Instead refactor things so webidl-tests can use the Rust-code-generation as a library in a build script. Also fixes `cargo fmt` in the repository. * Fixup generated code * Running web-sys checks on stable * Improving the code generation a little * Running rustfmt Co-authored-by: Alex Crichton <alex@alexcrichton.com>
2020-03-03 00:39:36 +01:00
require('wasm-bindgen-test').__wasm.foo(3);
};
exports.test_jsvalue_typeof = function() {
assert.ok(wasm.is_object({}));
assert.ok(!wasm.is_object(42));
assert.ok(wasm.is_function(function() {}));
assert.ok(!wasm.is_function(42));
assert.ok(wasm.is_string("2b or !2b"));
assert.ok(!wasm.is_string(42));
};
exports.optional_str_none = function(x) {
assert.strictEqual(x, undefined);
};
exports.optional_str_some = function(x) {
assert.strictEqual(x, 'x');
};
exports.optional_slice_none = function(x) {
assert.strictEqual(x, undefined);
};
exports.optional_slice_some = function(x) {
assert.strictEqual(x.length, 3);
assert.strictEqual(x[0], 1);
assert.strictEqual(x[1], 2);
assert.strictEqual(x[2], 3);
}
exports.optional_string_none = function(x) {
assert.strictEqual(x, undefined);
};
exports.optional_string_some = function(x) {
assert.strictEqual(x, 'abcd');
};
exports.optional_string_some_empty = function(x) {
assert.strictEqual(x, '');
};
exports.return_string_none = function() {};
exports.return_string_some = function() {
return 'foo';
};
exports.test_rust_optional = function() {
wasm.take_optional_str_none();
wasm.take_optional_str_none(null);
wasm.take_optional_str_none(undefined);
wasm.take_optional_str_some('hello');
assert.strictEqual(wasm.return_optional_str_none(), undefined);
assert.strictEqual(wasm.return_optional_str_some(), 'world');
};
exports.RenamedInRust = class {};
exports.new_renamed = () => new exports.RenamedInRust;
exports.import_export_same_name = () => {};
2019-05-13 08:12:32 -07:00
exports.test_string_roundtrip = () => {
const test = s => {
assert.strictEqual(wasm.do_string_roundtrip(s), s);
};
test('');
test('a');
test('💖');
test('a longer string');
test('a longer 💖 string');
};