Skip to content

Commit 4adaca6

Browse files
author
fengwan
committed
add test
1 parent a13d553 commit 4adaca6

File tree

10 files changed

+220
-2
lines changed

10 files changed

+220
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "assemblyscript-typescript-loader",
3-
"version": "1.3.1",
3+
"version": "1.3.2",
44
"description": "A webpack loader for compiles typescript with assemblyscript and bundles it as wasm or btyes string",
55
"main": "dist/index.js",
66
"files": [

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function mkDirsSync(dirname) {
7474
if (fs.existsSync(dirname)) {
7575
return true;
7676
} else {
77-
if (mkdirsSync(path.dirname(dirname))) {
77+
if (mkDirsSync(path.dirname(dirname))) {
7878
fs.mkdirSync(dirname);
7979
return true;
8080
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Loader Defaults 1`] = `
4+
"
5+
var p = new Promise(function(resolve){
6+
var compatibleModule;
7+
if (typeof WebAssembly !== 'undefined') {
8+
var buffer = new ArrayBuffer(478);
9+
var uint8 = new Uint8Array(buffer);
10+
uint8.set([0,97,115,109,1,0,0,0,1,9,2,96,2,127,127,0,96,0,0,3,3,2,0,1,5,3,1,0,1,6,16,3,127,1,65,0,11,127,1,65,0,11,127,1,65,0,11,7,24,3,4,105,110,105,116,0,0,4,115,116,101,112,0,1,6,109,101,109,111,114,121,2,0,10,181,2,2,17,0,32,0,36,0,32,1,36,1,35,0,35,1,108,36,2,11,160,2,1,8,127,35,1,65,1,107,33,6,35,0,65,1,107,33,7,3,64,32,0,35,1,73,4,64,32,0,65,1,107,32,6,32,0,27,33,4,65,0,32,0,65,1,106,32,0,32,6,70,27,33,5,65,0,33,1,3,64,32,1,35,0,73,4,64,32,4,35,0,108,32,1,65,1,107,32,7,32,1,27,34,2,106,45,0,0,32,4,35,0,108,32,1,106,45,0,0,106,32,4,35,0,108,65,0,32,1,65,1,106,32,1,32,7,70,27,34,3,106,45,0,0,106,32,0,35,0,108,32,2,106,45,0,0,106,32,0,35,0,108,32,3,106,45,0,0,106,32,5,35,0,108,32,2,106,45,0,0,106,32,5,35,0,108,32,1,106,45,0,0,106,32,5,35,0,108,32,3,106,45,0,0,106,33,2,32,0,35,0,108,32,1,106,45,0,0,4,64,32,2,65,2,72,34,3,4,127,32,3,5,32,2,65,3,74,11,65,1,113,4,64,35,2,32,0,35,0,108,106,32,1,106,65,0,58,0,0,11,5,32,2,65,3,70,4,64,35,2,32,0,35,0,108,106,32,1,106,65,1,58,0,0,11,11,32,1,65,1,106,33,1,12,1,11,11,32,0,65,1,106,33,0,12,1,11,11,11,0,58,4,110,97,109,101,1,51,2,0,23,116,101,115,116,47,102,105,120,116,117,114,101,115,47,115,116,101,112,47,105,110,105,116,1,23,116,101,115,116,47,102,105,120,116,117,114,101,115,47,115,116,101,112,47,115,116,101,112,0,31,16,115,111,117,114,99,101,77,97,112,112,105,110,103,85,82,76,13,115,116,101,112,46,119,97,115,109,46,109,97,112]);
11+
var WebAssemblyModule = function(deps) {
12+
var defaultDeps = {
13+
'global': { },
14+
'env': {
15+
'memory': new WebAssembly.Memory({
16+
initial: 10,
17+
limit: 100}),
18+
'table': new WebAssembly.Table({
19+
initial: 0,
20+
element: 'anyfunc'})
21+
}
22+
};
23+
return new WebAssembly.Instance(new WebAssembly.Module(buffer), deps || defaultDeps);
24+
};
25+
26+
compatibleModule = WebAssemblyModule;
27+
28+
}
29+
else {
30+
\\"use strict\\";
31+
// A simplified version of the game of life as seen on http://dcode.io
32+
Object.defineProperty(exports, \\"__esModule\\", { value: true });
33+
var w, // width
34+
h, // height
35+
s; // total size
36+
/** Initializes width and height. */
37+
function init(w_, h_) {
38+
w = w_;
39+
h = h_;
40+
s = w * h;
41+
}
42+
exports.init = init;
43+
/** Performs one step. */
44+
function step() {
45+
var hm1 = h - 1, wm1 = w - 1;
46+
for (var y = 0; y < h; ++y) {
47+
var ym1 = select(hm1, y - 1, y == 0), yp1 = select(0, y + 1, y == hm1);
48+
for (var x = 0; x < w; ++x) {
49+
var xm1 = select(wm1, x - 1, x == 0), xp1 = select(0, x + 1, x == wm1);
50+
var n = (load(ym1 * w + xm1) + load(ym1 * w + x) + load(ym1 * w + xp1) +
51+
load(y * w + xm1) + load(y * w + xp1) +
52+
load(yp1 * w + xm1) + load(yp1 * w + x) + load(yp1 * w + xp1));
53+
if (load(y * w + x)) {
54+
if (n < 2 || n > 3)
55+
store(s + y * w + x, 0);
56+
}
57+
else if (n == 3)
58+
store(s + y * w + x, 1);
59+
}
60+
}
61+
}
62+
exports.step = step;
63+
// Performing a step uses bytes [0, s-1] as the input and writes the output to [s, 2*s-1].
64+
// Note that the code above wastes a lot of space by using one byte per cell.
65+
66+
compatibleModule = function() {}; compatibleModule.prototype.exports = exports;
67+
}
68+
resolve(compatibleModule().exports);;
69+
});
70+
module.exports =p;"
71+
`;

test/fixtures/fixture.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import asmPromise from "./step.ts";
2+
asmPromise.then(function(asmModule){
3+
asmModule.step();
4+
})
5+

test/fixtures/step.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// A simplified version of the game of life as seen on http://dcode.io
2+
3+
var w: u32, // width
4+
h: u32, // height
5+
s: u32; // total size
6+
7+
/** Initializes width and height. */
8+
export function init(w_: u32, h_: u32): void {
9+
w = w_;
10+
h = h_;
11+
s = w * h;
12+
}
13+
14+
/** Performs one step. */
15+
export function step(): void {
16+
var hm1 = h - 1,
17+
wm1 = w - 1;
18+
for (var y: u32 = 0; y < h; ++y) {
19+
var ym1 = select<u32>(hm1, y - 1, y == 0),
20+
yp1 = select<u32>(0, y + 1, y == hm1);
21+
for (var x: u32 = 0; x < w; ++x) {
22+
var xm1 = select<u32>(wm1, x - 1, x == 0),
23+
xp1 = select<u32>(0, x + 1, x == wm1);
24+
var n = (
25+
load<u8>(ym1 * w + xm1) + load<u8>(ym1 * w + x) + load<u8>(ym1 * w + xp1) +
26+
load<u8>(y * w + xm1) + load<u8>(y * w + xp1) +
27+
load<u8>(yp1 * w + xm1) + load<u8>(yp1 * w + x) + load<u8>(yp1 * w + xp1)
28+
);
29+
if (load<u8>(y * w + x)) {
30+
if (n < 2 || n > 3)
31+
store<u8>(s + y * w + x, 0);
32+
} else if (n == 3)
33+
store<u8>(s + y * w + x, 1);
34+
}
35+
}
36+
}
37+
38+
// Performing a step uses bytes [0, s-1] as the input and writes the output to [s, 2*s-1].
39+
// Note that the code above wastes a lot of space by using one byte per cell.

test/fixtures/temp/assembly/step.wasm

478 Bytes
Binary file not shown.

test/fixtures/temp/assembly/step.wasm.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/test.asc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export function add(a: int, b: int): int {
2+
return a + b;
3+
}
4+
5+
export function subtract(a: int, b: int): int {
6+
return a - b;
7+
}
8+
9+
export function multiply(a: int, b: int): int {
10+
return a * b;
11+
}
12+
13+
export function divide(a: int, b: int): int {
14+
return a / b;
15+
}

test/helpers/compiler.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* eslint-disable
2+
import/order,
3+
multiline-ternary,
4+
no-param-reassign,
5+
*/
6+
import del from 'del';
7+
import path from 'path';
8+
import webpack from 'webpack';
9+
import MemoryFS from 'memory-fs';
10+
11+
const module = (config) => {
12+
return {
13+
rules: config.rules || config.loader
14+
? [
15+
{
16+
test: config.loader.test || /\.ts$/,
17+
use: {
18+
loader: path.resolve(__dirname, '../../src'),
19+
options: config.loader.options || {},
20+
},
21+
},
22+
]
23+
: [],
24+
};
25+
};
26+
27+
const plugins = config => ([
28+
new webpack.optimize.CommonsChunkPlugin({
29+
name: ['runtime'],
30+
minChunks: Infinity,
31+
}),
32+
].concat(config.plugins || []));
33+
34+
const output = (config) => {
35+
return {
36+
path: path.resolve(
37+
__dirname,
38+
`../outputs/${config.output ? config.output : ''}`,
39+
),
40+
filename: '[name].bundle.js',
41+
};
42+
};
43+
44+
export default function (fixture, config, options) {
45+
// webpack Config
46+
config = {
47+
devtool: config.devtool || 'sourcemap',
48+
context: path.resolve(__dirname, '..', 'fixtures'),
49+
entry: `./${fixture}`,
50+
output: output(config),
51+
module: module(config),
52+
plugins: plugins(config),
53+
};
54+
// Compiler Options
55+
options = Object.assign({ output: false }, options);
56+
57+
if (options.output) del.sync(config.output.path);
58+
59+
const compiler = webpack(config);
60+
61+
if (!options.output) compiler.outputFileSystem = new MemoryFS();
62+
63+
return new Promise((resolve, reject) => compiler.run((err, stats) => {
64+
if (err) reject(err);
65+
66+
resolve(stats);
67+
}));
68+
}

test/loader.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
import webpack from './helpers/compiler';
3+
4+
describe('Loader', () => {
5+
test('Defaults', async () => {
6+
const config = {
7+
loader: {
8+
test: /(ts)/,
9+
options: {
10+
name: '[path][name].wasm'
11+
}
12+
},
13+
};
14+
const stats = await webpack('fixture.js', config);
15+
const { source } = stats.toJson().modules[1];
16+
17+
expect(source).toMatchSnapshot();
18+
});
19+
});

0 commit comments

Comments
 (0)