@@ -67,9 +67,113 @@ tape( 'if an environment does not support `WebAssembly`, the function throws an
6767} ) ;
6868
6969tape ( 'the function is a constructor' , opts , function test ( t ) {
70- // TODO: write tests
70+ var mod ;
71+ var wasm ;
7172
73+ wasm = new Uint8Array ( [ 0x00 , 0x61 , 0x73 , 0x6d , 0x01 , 0x00 , 0x00 , 0x00 ] ) ;
74+ mod = new Module ( wasm , null ) ;
75+
76+ t . strictEqual ( mod instanceof Module , true , 'returns an instance' ) ;
77+ t . end ( ) ;
78+ } ) ;
79+
80+ tape ( 'the `initialize` method returns a promise' , opts , function test ( t ) {
81+ var mod ;
82+ var wasm ;
83+ var p ;
84+
85+ wasm = new Uint8Array ( [ 0x00 , 0x61 , 0x73 , 0x6d , 0x01 , 0x00 , 0x00 , 0x00 ] ) ;
86+ mod = new Module ( wasm , null ) ;
87+ p = mod . initialize ( ) ;
88+
89+ t . strictEqual ( typeof p . then , 'function' , 'returns a promise' ) ;
7290 t . end ( ) ;
7391} ) ;
7492
75- // TODO: add tests
93+ tape ( 'the `initialize` method properly sets `_module` and `_instance` properties' , opts , function test ( t ) {
94+ var mod ;
95+ var wasm ;
96+
97+ wasm = new Uint8Array ( [ 0x00 , 0x61 , 0x73 , 0x6d , 0x01 , 0x00 , 0x00 , 0x00 ] ) ;
98+ mod = new Module ( wasm , null ) ;
99+
100+ mod . initialize ( ) . then ( onResolve , onReject ) ;
101+
102+ function onResolve ( result ) {
103+ t . strictEqual ( result , mod , 'resolves with module instance' ) ;
104+ t . strictEqual ( typeof result . _module , 'object' , '_module is set' ) ;
105+ t . strictEqual ( typeof result . _instance , 'object' , '_instance is set' ) ;
106+ t . strictEqual ( result . _module . constructor . name , 'Module' , '_module is a WebAssembly.Module' ) ;
107+ t . strictEqual ( result . _instance . constructor . name , 'Instance' , '_instance is a WebAssembly.Instance' ) ;
108+ t . end ( ) ;
109+ }
110+
111+ function onReject ( error ) {
112+ t . fail ( 'should not reject: ' + error . message ) ;
113+ t . end ( ) ;
114+ }
115+ } ) ;
116+
117+ tape ( 'the `initializeAsync` method invokes a callback' , opts , function test ( t ) {
118+ var mod ;
119+ var wasm ;
120+
121+ wasm = new Uint8Array ( [ 0x00 , 0x61 , 0x73 , 0x6d , 0x01 , 0x00 , 0x00 , 0x00 ] ) ;
122+ mod = new Module ( wasm , null ) ;
123+
124+ mod . initializeAsync ( clbk ) ;
125+
126+ function clbk ( error , result ) {
127+ if ( error ) {
128+ t . fail ( 'callback received an error: ' + error . message ) ;
129+ } else {
130+ t . pass ( 'callback invoked without error' ) ;
131+ }
132+ t . end ( ) ;
133+ }
134+ } ) ;
135+
136+ tape ( 'the `initializeAsync` method properly sets `_module` and `_instance` properties' , opts , function test ( t ) {
137+ var mod ;
138+ var wasm ;
139+
140+ wasm = new Uint8Array ( [ 0x00 , 0x61 , 0x73 , 0x6d , 0x01 , 0x00 , 0x00 , 0x00 ] ) ;
141+ mod = new Module ( wasm , null ) ;
142+
143+ mod . initializeAsync ( clbk ) ;
144+
145+ function clbk ( error , result ) {
146+ if ( error ) {
147+ t . fail ( 'callback received an error: ' + error . message ) ;
148+ t . end ( ) ;
149+ return ;
150+ }
151+ t . strictEqual ( result , mod , 'callback receives module instance' ) ;
152+ t . strictEqual ( typeof result . _module , 'object' , '_module is set' ) ;
153+ t . strictEqual ( typeof result . _instance , 'object' , '_instance is set' ) ;
154+ t . strictEqual ( result . _module . constructor . name , 'Module' , '_module is a WebAssembly.Module' ) ;
155+ t . strictEqual ( result . _instance . constructor . name , 'Instance' , '_instance is a WebAssembly.Instance' ) ;
156+ t . end ( ) ;
157+ }
158+ } ) ;
159+
160+ tape ( 'the `exports` property returns instance exports after initialization' , opts , function test ( t ) {
161+ var mod ;
162+ var wasm ;
163+
164+ wasm = new Uint8Array ( [ 0x00 , 0x61 , 0x73 , 0x6d , 0x01 , 0x00 , 0x00 , 0x00 ] ) ;
165+ mod = new Module ( wasm , null ) ;
166+
167+ mod . initialize ( ) . then ( onResolve , onReject ) ;
168+
169+ function onResolve ( result ) {
170+ var exports = result . exports ;
171+ t . strictEqual ( typeof exports , 'object' , 'exports is an object' ) ;
172+ t . end ( ) ;
173+ }
174+
175+ function onReject ( error ) {
176+ t . fail ( 'should not reject: ' + error . message ) ;
177+ t . end ( ) ;
178+ }
179+ } ) ;
0 commit comments