|
1 | | -var fs = require("fs"); |
2 | | -var test = require("tape"); |
3 | | - |
4 | | -var isUpdate = process.argv[2] === "update"; |
5 | | - |
6 | | -// This test case exists to catch the most obvious issues before pushing the binary back to GitHub. |
7 | | -// It's not intended to be a full test suite, but feel free to extend it / send a PR if necessary! |
8 | | - |
9 | | -var binaryen; |
10 | | -test("requiring binaryen", function(test) { |
11 | | - test.doesNotThrow(function() { |
12 | | - binaryen = require(".."); |
13 | | - }); |
14 | | - test.end(); |
15 | | -}); |
16 | | - |
17 | | -var mod; |
18 | | -test("constructing a module", function(test) { |
19 | | - test.doesNotThrow(function() { |
20 | | - mod = new binaryen.Module(); |
21 | | - }); |
22 | | - test.end(); |
23 | | -}); |
24 | | - |
25 | | -var ftype; |
26 | | -test("adding a function type", function(test) { |
27 | | - test.doesNotThrow(function() { |
28 | | - ftype = mod.addFunctionType("i", binaryen.i32, []); |
29 | | - }); |
30 | | - test.end(); |
31 | | -}); |
32 | | - |
33 | | -var expr; |
34 | | -test("creating an expression", function(test) { |
35 | | - test.doesNotThrow(function() { |
36 | | - expr = mod.i32.const(0); |
37 | | - }); |
38 | | - test.end(); |
39 | | -}); |
40 | | - |
41 | | -var stmt; |
42 | | -test("creating a statement", function(test) { |
43 | | - test.doesNotThrow(function() { |
44 | | - stmt = mod.return(expr); |
45 | | - }); |
46 | | - test.end(); |
47 | | -}); |
48 | | - |
49 | | -test("adding a function", function(test) { |
50 | | - test.doesNotThrow(function() { |
51 | | - mod.addFunction("main", ftype, [], stmt); |
52 | | - }); |
53 | | - test.end(); |
54 | | -}); |
55 | | - |
56 | | -test("adding a function import", function(test) { |
57 | | - test.doesNotThrow(function() { |
58 | | - mod.addFunctionImport("imported", "env", "provided", ftype); |
59 | | - }); |
60 | | - test.end(); |
61 | | -}); |
62 | | - |
63 | | -test("adding a function export", function(test) { |
64 | | - test.doesNotThrow(function() { |
65 | | - mod.addFunctionExport("main", "main"); |
66 | | - }); |
67 | | - test.end(); |
68 | | -}); |
69 | | - |
70 | | -test("adding a memory import", function(test) { |
71 | | - test.doesNotThrow(function() { |
72 | | - mod.addMemoryImport("0", "env", "memory"); // doesn't work with a different internal name, yet |
73 | | - }); |
74 | | - test.end(); |
75 | | -}); |
76 | | - |
77 | | -test("adding a memory export", function(test) { |
78 | | - test.doesNotThrow(function() { |
79 | | - mod.addMemoryExport("0", "memory"); |
80 | | - }); |
81 | | - test.end(); |
82 | | -}); |
83 | | - |
84 | | -var text; |
85 | | -test("emitting text", function(test) { |
86 | | - test.doesNotThrow(function() { |
87 | | - text = mod.emitText(); |
88 | | - }); |
89 | | - test.ok(typeof text === "string" && text.length, "should return be a non-empty string"); |
90 | | - test.end(); |
91 | | -}); |
92 | | - |
93 | | -var binary; |
94 | | -test("emitting binary", function(test) { |
95 | | - test.doesNotThrow(function() { |
96 | | - binary = mod.emitBinary(); |
97 | | - }); |
98 | | - test.ok(binary && binary.length, "should return a non-empty array-like object"); |
99 | | - test.end(); |
100 | | -}); |
101 | | - |
102 | | -test("validating the module", function(test) { |
103 | | - var valid; |
104 | | - test.doesNotThrow(function() { |
105 | | - valid = mod.validate(); |
106 | | - }); |
107 | | - test.ok(valid, "the module should be valid"); |
108 | | - test.end(); |
109 | | -}); |
110 | | - |
111 | | -test("optimizing the module", function(test) { |
112 | | - test.doesNotThrow(function() { |
113 | | - mod.optimize(); |
114 | | - }); |
115 | | - test.end(); |
116 | | -}); |
117 | | - |
118 | | -var asmjs; |
119 | | -test("emitting asmjs", function(test) { |
120 | | - test.doesNotThrow(function() { |
121 | | - mod.removeImport("0"); // wasm2js doesn't support non-function imports (here: memory) |
122 | | - asmjs = mod.emitAsmjs(); |
123 | | - }); |
124 | | - test.ok(typeof asmjs === "string" && asmjs.length, "should return a non-empty string"); |
125 | | - test.end(); |
126 | | -}); |
127 | | - |
128 | | -function stripComments(text) { |
129 | | - return text.replace(/ \(; [\w$_]+ ;\)/g, ""); |
130 | | -} |
131 | | - |
132 | | -test("fixtures", function(test) { |
133 | | - |
134 | | - if (isUpdate) { |
135 | | - fs.writeFileSync(__dirname + "/fixtures/index.wast", text, "utf8"); |
136 | | - } else { |
137 | | - var textComp = fs.readFileSync(__dirname + "/fixtures/index.wast", "utf8").replace(/\r?\n/g, "\n"); |
138 | | - test.strictEqual(stripComments(text), stripComments(textComp), "should match text output"); |
139 | | - } |
140 | | - |
141 | | - if (isUpdate) { |
142 | | - fs.writeFileSync(__dirname + "/fixtures/index.wasm", binary); |
143 | | - } else { |
144 | | - var binaryComp = fs.readFileSync(__dirname + "/fixtures/index.wasm"); |
145 | | - test.ok(Buffer.compare(binary, binaryComp) === 0, "should match binary output"); |
146 | | - } |
147 | | - |
148 | | - if (isUpdate) { |
149 | | - fs.writeFileSync(__dirname + "/fixtures/index.asm.js", asmjs, "utf8"); |
150 | | - } else { |
151 | | - // var asmjsComp = fs.readFileSync(__dirname + "/fixtures/index.asm.js"); |
152 | | - // test.strictEqual(asmjs, asmjsComp, "should match asm.js output"); |
153 | | - } |
154 | | - |
155 | | - test.end(); |
156 | | -}); |
| 1 | +require("./test-wasm"); |
| 2 | +require("./test-js"); |
0 commit comments