Skip to content

Commit

Permalink
Add snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Systemcluster committed Aug 4, 2024
1 parent 14894d2 commit c68cb30
Show file tree
Hide file tree
Showing 12 changed files with 534 additions and 0 deletions.
7 changes: 7 additions & 0 deletions crates/cli-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ wasm-bindgen-threads-xform = { path = '../threads-xform', version = '=0.2.92' }
wasm-bindgen-wasm-conventions = { path = '../wasm-conventions', version = '=0.2.92' }
wasm-bindgen-wasm-interpreter = { path = "../wasm-interpreter", version = '=0.2.92' }
unicode-ident = "1.0.5"

[dev-dependencies]
insta = "1.39.0"

[profile.dev.package]
insta.opt-level = 3
similar.opt-level = 3
8 changes: 8 additions & 0 deletions crates/cli-support/tests/snapshots/tests__bundler_js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: crates/cli-support/tests/tests.rs
expression: output.js()
---
let wasm;
export function __wbg_set_wasm(val) {
wasm = val;
}
27 changes: 27 additions & 0 deletions crates/cli-support/tests/snapshots/tests__deno_js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
source: crates/cli-support/tests/tests.rs
expression: output.js()
---
const imports = {
__wbindgen_placeholder__: {
},

};

const wasm_url = new URL('add_bg.wasm', import.meta.url);
let wasmCode = '';
switch (wasm_url.protocol) {
case 'file:':
wasmCode = await Deno.readFile(wasm_url);
break
case 'https:':
case 'http:':
wasmCode = await (await fetch(wasm_url)).arrayBuffer();
break
default:
throw new Error(`Unsupported protocol: ${wasm_url.protocol}`);
}

const wasmInstance = (await WebAssembly.instantiate(wasmCode, imports)).instance;
const wasm = wasmInstance.exports;
export const __wasm = wasm;
14 changes: 14 additions & 0 deletions crates/cli-support/tests/snapshots/tests__node_bg.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: crates/cli-support/tests/tests.rs
expression: output.js()
---
let imports = {};
let wasm;

const path = require('path').join(__dirname, 'add_bg.wasm');
const bytes = require('fs').readFileSync(path);

const wasmModule = new WebAssembly.Module(bytes);
const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
wasm = wasmInstance.exports;
module.exports.__wasm = wasm;
8 changes: 8 additions & 0 deletions crates/cli-support/tests/snapshots/tests__node_module_bg.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: crates/cli-support/tests/tests.rs
expression: output.js()
---
let wasm;
export function __wbg_set_wasm(val) {
wasm = val;
}
20 changes: 20 additions & 0 deletions crates/cli-support/tests/snapshots/tests__node_module_js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
source: crates/cli-support/tests/tests.rs
expression: output.start().unwrap()
---
let imports = {};

import * as path from 'node:path';
import * as fs from 'node:fs';
import * as process from 'node:process';

let file = path.dirname(new URL(import.meta.url).pathname);
if (process.platform === 'win32') {
file = file.substring(1);
}
const bytes = fs.readFileSync(path.join(file, 'add_bg.wasm'));

const wasmModule = new WebAssembly.Module(bytes);
const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
const wasm = wasmInstance.exports;
export const __wasm = wasm;
14 changes: 14 additions & 0 deletions crates/cli-support/tests/snapshots/tests__nodejs_js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: crates/cli-support/tests/tests.rs
expression: output.js()
---
let imports = {};
let wasm;

const path = require('path').join(__dirname, 'add_bg.wasm');
const bytes = require('fs').readFileSync(path);

const wasmModule = new WebAssembly.Module(bytes);
const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
wasm = wasmInstance.exports;
module.exports.__wasm = wasm;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: crates/cli-support/tests/tests.rs
expression: output.js()
---
let wasm;
export function __wbg_set_wasm(val) {
wasm = val;
}
20 changes: 20 additions & 0 deletions crates/cli-support/tests/snapshots/tests__nodejs_module_start.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
source: crates/cli-support/tests/tests.rs
expression: output.start().unwrap()
---
let imports = {};

import * as path from 'node:path';
import * as fs from 'node:fs';
import * as process from 'node:process';

let file = path.dirname(new URL(import.meta.url).pathname);
if (process.platform === 'win32') {
file = file.substring(1);
}
const bytes = fs.readFileSync(path.join(file, 'add_bg.wasm'));

const wasmModule = new WebAssembly.Module(bytes);
const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
const wasm = wasmInstance.exports;
export const __wasm = wasm;
111 changes: 111 additions & 0 deletions crates/cli-support/tests/snapshots/tests__nomodule_js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
---
source: crates/cli-support/tests/tests.rs
expression: output.js()
---
let wasm_bindgen;
(function() {
const __exports = {};
let script_src;
if (typeof document !== 'undefined' && document.currentScript !== null) {
script_src = new URL(document.currentScript.src, location.href).toString();
}
let wasm = undefined;
async function __wbg_load(module, imports) {
if (typeof Response === 'function' && module instanceof Response) {
if (typeof WebAssembly.instantiateStreaming === 'function') {
try {
return await WebAssembly.instantiateStreaming(module, imports);

} catch (e) {
if (module.headers.get('Content-Type') != 'application/wasm') {
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);

} else {
throw e;
}
}
}

const bytes = await module.arrayBuffer();
return await WebAssembly.instantiate(bytes, imports);

} else {
const instance = await WebAssembly.instantiate(module, imports);

if (instance instanceof WebAssembly.Instance) {
return { instance, module };

} else {
return instance;
}
}
}

function __wbg_get_imports() {
const imports = {};
imports.wbg = {};

return imports;
}

function __wbg_init_memory(imports, memory) {

}

function __wbg_finalize_init(instance, module) {
wasm = instance.exports;
__wbg_init.__wbindgen_wasm_module = module;



return wasm;
}

function initSync(module) {
if (wasm !== undefined) return wasm;


if (typeof module !== 'undefined' && Object.getPrototypeOf(module) === Object.prototype)
({module} = module)
else
console.warn('using deprecated parameters for `initSync()`; pass a single object instead')

const imports = __wbg_get_imports();

__wbg_init_memory(imports);

if (!(module instanceof WebAssembly.Module)) {
module = new WebAssembly.Module(module);
}

const instance = new WebAssembly.Instance(module, imports);

return __wbg_finalize_init(instance, module);
}

async function __wbg_init(module_or_path) {
if (wasm !== undefined) return wasm;


if (typeof module_or_path !== 'undefined' && Object.getPrototypeOf(module_or_path) === Object.prototype)
({module_or_path} = module_or_path)
else
console.warn('using deprecated parameters for the initialization function; pass a single object instead')


const imports = __wbg_get_imports();

if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
module_or_path = fetch(module_or_path);
}

__wbg_init_memory(imports);

const { instance, module } = await __wbg_load(await module_or_path, imports);

return __wbg_finalize_init(instance, module);
}

wasm_bindgen = Object.assign(__wbg_init, { initSync }, __exports);

})();
104 changes: 104 additions & 0 deletions crates/cli-support/tests/snapshots/tests__web_js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
---
source: crates/cli-support/tests/tests.rs
expression: output.js()
---
let wasm;

async function __wbg_load(module, imports) {
if (typeof Response === 'function' && module instanceof Response) {
if (typeof WebAssembly.instantiateStreaming === 'function') {
try {
return await WebAssembly.instantiateStreaming(module, imports);

} catch (e) {
if (module.headers.get('Content-Type') != 'application/wasm') {
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);

} else {
throw e;
}
}
}

const bytes = await module.arrayBuffer();
return await WebAssembly.instantiate(bytes, imports);

} else {
const instance = await WebAssembly.instantiate(module, imports);

if (instance instanceof WebAssembly.Instance) {
return { instance, module };

} else {
return instance;
}
}
}

function __wbg_get_imports() {
const imports = {};
imports.wbg = {};

return imports;
}

function __wbg_init_memory(imports, memory) {

}

function __wbg_finalize_init(instance, module) {
wasm = instance.exports;
__wbg_init.__wbindgen_wasm_module = module;



return wasm;
}

function initSync(module) {
if (wasm !== undefined) return wasm;


if (typeof module !== 'undefined' && Object.getPrototypeOf(module) === Object.prototype)
({module} = module)
else
console.warn('using deprecated parameters for `initSync()`; pass a single object instead')

const imports = __wbg_get_imports();

__wbg_init_memory(imports);

if (!(module instanceof WebAssembly.Module)) {
module = new WebAssembly.Module(module);
}

const instance = new WebAssembly.Instance(module, imports);

return __wbg_finalize_init(instance, module);
}

async function __wbg_init(module_or_path) {
if (wasm !== undefined) return wasm;


if (typeof module_or_path !== 'undefined' && Object.getPrototypeOf(module_or_path) === Object.prototype)
({module_or_path} = module_or_path)
else
console.warn('using deprecated parameters for the initialization function; pass a single object instead')


const imports = __wbg_get_imports();

if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
module_or_path = fetch(module_or_path);
}

__wbg_init_memory(imports);

const { instance, module } = await __wbg_load(await module_or_path, imports);

return __wbg_finalize_init(instance, module);
}

export { initSync };
export default __wbg_init;
Loading

0 comments on commit c68cb30

Please sign in to comment.