Best JavaScript code snippet using stryker-parent
DcsParser.test.ts
Source:DcsParser.test.ts
...79 });80 });81 describe('handler registration', () => {82 it('setDcsHandler', () => {83 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new TestHandler(reports, 'th'));84 parser.hook(identifier({intermediates: '+', final: 'p'}), Params.fromArray([1, 2, 3]));85 let data = toUtf32('Here comes');86 parser.put(data, 0, data.length);87 data = toUtf32('the mouse!');88 parser.put(data, 0, data.length);89 parser.unhook(true);90 assert.deepEqual(reports, [91 // messages from TestHandler92 ['th', 'HOOK', [1, 2, 3]],93 ['th', 'PUT', 'Here comes'],94 ['th', 'PUT', 'the mouse!'],95 ['th', 'UNHOOK', true]96 ]);97 });98 it('clearDcsHandler', () => {99 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new TestHandler(reports, 'th'));100 parser.clearHandler(identifier({intermediates: '+', final: 'p'}));101 parser.hook(identifier({intermediates: '+', final: 'p'}), Params.fromArray([1, 2, 3]));102 let data = toUtf32('Here comes');103 parser.put(data, 0, data.length);104 data = toUtf32('the mouse!');105 parser.put(data, 0, data.length);106 parser.unhook(true);107 assert.deepEqual(reports, [108 // messages from fallback handler109 [identifier({intermediates: '+', final: 'p'}), 'HOOK', [1, 2, 3]],110 [identifier({intermediates: '+', final: 'p'}), 'PUT', 'Here comes'],111 [identifier({intermediates: '+', final: 'p'}), 'PUT', 'the mouse!'],112 [identifier({intermediates: '+', final: 'p'}), 'UNHOOK', true]113 ]);114 });115 it('addDcsHandler', () => {116 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new TestHandler(reports, 'th1'));117 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new TestHandler(reports, 'th2'));118 parser.hook(identifier({intermediates: '+', final: 'p'}), Params.fromArray([1, 2, 3]));119 let data = toUtf32('Here comes');120 parser.put(data, 0, data.length);121 data = toUtf32('the mouse!');122 parser.put(data, 0, data.length);123 parser.unhook(true);124 assert.deepEqual(reports, [125 ['th2', 'HOOK', [1, 2, 3]],126 ['th1', 'HOOK', [1, 2, 3]],127 ['th2', 'PUT', 'Here comes'],128 ['th1', 'PUT', 'Here comes'],129 ['th2', 'PUT', 'the mouse!'],130 ['th1', 'PUT', 'the mouse!'],131 ['th2', 'UNHOOK', true],132 ['th1', 'UNHOOK', false] // false due being already handled by th2!133 ]);134 });135 it('addDcsHandler with return false', () => {136 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new TestHandler(reports, 'th1'));137 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new TestHandler(reports, 'th2', true));138 parser.hook(identifier({intermediates: '+', final: 'p'}), Params.fromArray([1, 2, 3]));139 let data = toUtf32('Here comes');140 parser.put(data, 0, data.length);141 data = toUtf32('the mouse!');142 parser.put(data, 0, data.length);143 parser.unhook(true);144 assert.deepEqual(reports, [145 ['th2', 'HOOK', [1, 2, 3]],146 ['th1', 'HOOK', [1, 2, 3]],147 ['th2', 'PUT', 'Here comes'],148 ['th1', 'PUT', 'Here comes'],149 ['th2', 'PUT', 'the mouse!'],150 ['th1', 'PUT', 'the mouse!'],151 ['th2', 'UNHOOK', true],152 ['th1', 'UNHOOK', true] // true since th2 indicated to keep bubbling153 ]);154 });155 it('dispose handlers', () => {156 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new TestHandler(reports, 'th1'));157 const dispo = parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new TestHandler(reports, 'th2', true));158 dispo.dispose();159 parser.hook(identifier({intermediates: '+', final: 'p'}), Params.fromArray([1, 2, 3]));160 let data = toUtf32('Here comes');161 parser.put(data, 0, data.length);162 data = toUtf32('the mouse!');163 parser.put(data, 0, data.length);164 parser.unhook(true);165 assert.deepEqual(reports, [166 ['th1', 'HOOK', [1, 2, 3]],167 ['th1', 'PUT', 'Here comes'],168 ['th1', 'PUT', 'the mouse!'],169 ['th1', 'UNHOOK', true]170 ]);171 });172 });173 describe('DcsHandlerFactory', () => {174 it('should be called once on end(true)', () => {175 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new DcsHandler((data, params) => { reports.push([params.toArray(), data]); return true; }));176 parser.hook(identifier({intermediates: '+', final: 'p'}), Params.fromArray([1, 2, 3]));177 let data = toUtf32('Here comes');178 parser.put(data, 0, data.length);179 data = toUtf32(' the mouse!');180 parser.put(data, 0, data.length);181 parser.unhook(true);182 assert.deepEqual(reports, [[[1, 2, 3], 'Here comes the mouse!']]);183 });184 it('should not be called on end(false)', () => {185 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new DcsHandler((data, params) => { reports.push([params.toArray(), data]); return true; }));186 parser.hook(identifier({intermediates: '+', final: 'p'}), Params.fromArray([1, 2, 3]));187 let data = toUtf32('Here comes');188 parser.put(data, 0, data.length);189 data = toUtf32(' the mouse!');190 parser.put(data, 0, data.length);191 parser.unhook(false);192 assert.deepEqual(reports, []);193 });194 it('should be disposable', () => {195 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new DcsHandler((data, params) => { reports.push(['one', params.toArray(), data]); return true; }));196 const dispo = parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new DcsHandler((data, params) => { reports.push(['two', params.toArray(), data]); return true; }));197 parser.hook(identifier({intermediates: '+', final: 'p'}), Params.fromArray([1, 2, 3]));198 let data = toUtf32('Here comes');199 parser.put(data, 0, data.length);200 data = toUtf32(' the mouse!');201 parser.put(data, 0, data.length);202 parser.unhook(true);203 assert.deepEqual(reports, [['two', [1, 2, 3], 'Here comes the mouse!']]);204 dispo.dispose();205 parser.hook(identifier({intermediates: '+', final: 'p'}), Params.fromArray([1, 2, 3]));206 data = toUtf32('some other');207 parser.put(data, 0, data.length);208 data = toUtf32(' data');209 parser.put(data, 0, data.length);210 parser.unhook(true);211 assert.deepEqual(reports, [['two', [1, 2, 3], 'Here comes the mouse!'], ['one', [1, 2, 3], 'some other data']]);212 });213 it('should respect return false', () => {214 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new DcsHandler((data, params) => { reports.push(['one', params.toArray(), data]); return true; }));215 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new DcsHandler((data, params) => { reports.push(['two', params.toArray(), data]); return false; }));216 parser.hook(identifier({intermediates: '+', final: 'p'}), Params.fromArray([1, 2, 3]));217 let data = toUtf32('Here comes');218 parser.put(data, 0, data.length);219 data = toUtf32(' the mouse!');220 parser.put(data, 0, data.length);221 parser.unhook(true);222 assert.deepEqual(reports, [['two', [1, 2, 3], 'Here comes the mouse!'], ['one', [1, 2, 3], 'Here comes the mouse!']]);223 });224 it('should work up to payload limit', function(): void {225 this.timeout(10000);226 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new DcsHandler((data, params) => { reports.push([params.toArray(), data]); return true; }));227 parser.hook(identifier({intermediates: '+', final: 'p'}), Params.fromArray([1, 2, 3]));228 const data = toUtf32('A'.repeat(1000));229 for (let i = 0; i < PAYLOAD_LIMIT; i += 1000) {230 parser.put(data, 0, data.length);231 }232 parser.unhook(true);233 assert.deepEqual(reports, [[[1, 2, 3], 'A'.repeat(PAYLOAD_LIMIT)]]);234 });235 it('should abort for payload limit +1', function(): void {236 this.timeout(10000);237 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new DcsHandler((data, params) => { reports.push([params.toArray(), data]); return true; }));238 parser.hook(identifier({intermediates: '+', final: 'p'}), Params.fromArray([1, 2, 3]));239 let data = toUtf32('A'.repeat(1000));240 for (let i = 0; i < PAYLOAD_LIMIT; i += 1000) {241 parser.put(data, 0, data.length);242 }243 data = toUtf32('A');244 parser.put(data, 0, data.length);245 parser.unhook(true);246 assert.deepEqual(reports, []);247 });248 });249});250class TestHandlerAsync implements IDcsHandler {251 constructor(public output: any[], public msg: string, public returnFalse: boolean = false) {}252 public hook(params: IParams): void {253 this.output.push([this.msg, 'HOOK', params.toArray()]);254 }255 public put(data: Uint32Array, start: number, end: number): void {256 this.output.push([this.msg, 'PUT', utf32ToString(data, start, end)]);257 }258 public async unhook(success: boolean): Promise<boolean> {259 // simple sleep to check in tests whether ordering gets messed up260 await new Promise(res => setTimeout(res, 20));261 this.output.push([this.msg, 'UNHOOK', success]);262 if (this.returnFalse) {263 return false;264 }265 return true;266 }267}268async function unhookP(parser: DcsParser, success: boolean): Promise<void> {269 let result: void | Promise<boolean>;270 let prev: boolean | undefined;271 while (result = parser.unhook(success, prev)) {272 prev = await result;273 }274}275describe('DcsParser - async tests', () => {276 let parser: DcsParser;277 let reports: any[] = [];278 beforeEach(() => {279 reports = [];280 parser = new DcsParser();281 parser.setHandlerFallback((id, action, data) => {282 if (action === 'HOOK') {283 data = data.toArray();284 }285 reports.push([id, action, data]);286 });287 });288 describe('sync and async mixed', () => {289 describe('sync | async | sync', () => {290 it('first should run, cleanup action for others', async () => {291 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new TestHandler(reports, 's1', false));292 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new TestHandlerAsync(reports, 'a1', false));293 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new TestHandler(reports, 's2', false));294 parser.hook(identifier({intermediates: '+', final: 'p'}), Params.fromArray([1, 2, 3]));295 let data = toUtf32('Here comes');296 parser.put(data, 0, data.length);297 data = toUtf32('the mouse!');298 parser.put(data, 0, data.length);299 await unhookP(parser, true);300 assert.deepEqual(reports, [301 // messages from TestHandler302 ['s2', 'HOOK', [1, 2, 3]],303 ['a1', 'HOOK', [1, 2, 3]],304 ['s1', 'HOOK', [1, 2, 3]],305 ['s2', 'PUT', 'Here comes'],306 ['a1', 'PUT', 'Here comes'],307 ['s1', 'PUT', 'Here comes'],308 ['s2', 'PUT', 'the mouse!'],309 ['a1', 'PUT', 'the mouse!'],310 ['s1', 'PUT', 'the mouse!'],311 ['s2', 'UNHOOK', true],312 ['a1', 'UNHOOK', false], // important: a1 before s1313 ['s1', 'UNHOOK', false]314 ]);315 });316 it('all should run', async () => {317 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new TestHandler(reports, 's1', true));318 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new TestHandlerAsync(reports, 'a1', true));319 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new TestHandler(reports, 's2', true));320 parser.hook(identifier({intermediates: '+', final: 'p'}), Params.fromArray([1, 2, 3]));321 let data = toUtf32('Here comes');322 parser.put(data, 0, data.length);323 data = toUtf32('the mouse!');324 parser.put(data, 0, data.length);325 await unhookP(parser, true);326 assert.deepEqual(reports, [327 // messages from TestHandler328 ['s2', 'HOOK', [1, 2, 3]],329 ['a1', 'HOOK', [1, 2, 3]],330 ['s1', 'HOOK', [1, 2, 3]],331 ['s2', 'PUT', 'Here comes'],332 ['a1', 'PUT', 'Here comes'],333 ['s1', 'PUT', 'Here comes'],334 ['s2', 'PUT', 'the mouse!'],335 ['a1', 'PUT', 'the mouse!'],336 ['s1', 'PUT', 'the mouse!'],337 ['s2', 'UNHOOK', true],338 ['a1', 'UNHOOK', true], // important: a1 before s1339 ['s1', 'UNHOOK', true]340 ]);341 });342 });343 describe('async | sync | async', () => {344 it('first should run, cleanup action for others', async () => {345 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new TestHandlerAsync(reports, 'a1', false));346 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new TestHandler(reports, 's1', false));347 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new TestHandlerAsync(reports, 'a2', false));348 parser.hook(identifier({intermediates: '+', final: 'p'}), Params.fromArray([1, 2, 3]));349 let data = toUtf32('Here comes');350 parser.put(data, 0, data.length);351 data = toUtf32('the mouse!');352 parser.put(data, 0, data.length);353 await unhookP(parser, true);354 assert.deepEqual(reports, [355 // messages from TestHandler356 ['a2', 'HOOK', [1, 2, 3]],357 ['s1', 'HOOK', [1, 2, 3]],358 ['a1', 'HOOK', [1, 2, 3]],359 ['a2', 'PUT', 'Here comes'],360 ['s1', 'PUT', 'Here comes'],361 ['a1', 'PUT', 'Here comes'],362 ['a2', 'PUT', 'the mouse!'],363 ['s1', 'PUT', 'the mouse!'],364 ['a1', 'PUT', 'the mouse!'],365 ['a2', 'UNHOOK', true],366 ['s1', 'UNHOOK', false], // important: s1 between a2 .. a1367 ['a1', 'UNHOOK', false]368 ]);369 });370 it('all should run', async () => {371 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new TestHandlerAsync(reports, 'a1', true));372 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new TestHandler(reports, 's1', true));373 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new TestHandlerAsync(reports, 'a2', true));374 parser.hook(identifier({intermediates: '+', final: 'p'}), Params.fromArray([1, 2, 3]));375 let data = toUtf32('Here comes');376 parser.put(data, 0, data.length);377 data = toUtf32('the mouse!');378 parser.put(data, 0, data.length);379 await unhookP(parser, true);380 assert.deepEqual(reports, [381 // messages from TestHandler382 ['a2', 'HOOK', [1, 2, 3]],383 ['s1', 'HOOK', [1, 2, 3]],384 ['a1', 'HOOK', [1, 2, 3]],385 ['a2', 'PUT', 'Here comes'],386 ['s1', 'PUT', 'Here comes'],387 ['a1', 'PUT', 'Here comes'],388 ['a2', 'PUT', 'the mouse!'],389 ['s1', 'PUT', 'the mouse!'],390 ['a1', 'PUT', 'the mouse!'],391 ['a2', 'UNHOOK', true],392 ['s1', 'UNHOOK', true], // important: s1 between a2 .. a1393 ['a1', 'UNHOOK', true]394 ]);395 });396 });397 describe('DcsHandlerFactory', () => {398 it('should be called once on end(true)', async () => {399 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new DcsHandler(async (data, params) => { reports.push([params.toArray(), data]); return true; }));400 parser.hook(identifier({intermediates: '+', final: 'p'}), Params.fromArray([1, 2, 3]));401 let data = toUtf32('Here comes');402 parser.put(data, 0, data.length);403 data = toUtf32(' the mouse!');404 parser.put(data, 0, data.length);405 await unhookP(parser, true);406 assert.deepEqual(reports, [[[1, 2, 3], 'Here comes the mouse!']]);407 });408 it('should not be called on end(false)', async () => {409 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new DcsHandler(async (data, params) => { reports.push([params.toArray(), data]); return true; }));410 parser.hook(identifier({intermediates: '+', final: 'p'}), Params.fromArray([1, 2, 3]));411 let data = toUtf32('Here comes');412 parser.put(data, 0, data.length);413 data = toUtf32(' the mouse!');414 parser.put(data, 0, data.length);415 await unhookP(parser, false);416 assert.deepEqual(reports, []);417 });418 it('should be disposable', async () => {419 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new DcsHandler(async (data, params) => { reports.push(['one', params.toArray(), data]); return true; }));420 const dispo = parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new DcsHandler(async (data, params) => { reports.push(['two', params.toArray(), data]); return true; }));421 parser.hook(identifier({intermediates: '+', final: 'p'}), Params.fromArray([1, 2, 3]));422 let data = toUtf32('Here comes');423 parser.put(data, 0, data.length);424 data = toUtf32(' the mouse!');425 parser.put(data, 0, data.length);426 await unhookP(parser, true);427 assert.deepEqual(reports, [['two', [1, 2, 3], 'Here comes the mouse!']]);428 dispo.dispose();429 parser.hook(identifier({intermediates: '+', final: 'p'}), Params.fromArray([1, 2, 3]));430 data = toUtf32('some other');431 parser.put(data, 0, data.length);432 data = toUtf32(' data');433 parser.put(data, 0, data.length);434 await unhookP(parser, true);435 assert.deepEqual(reports, [['two', [1, 2, 3], 'Here comes the mouse!'], ['one', [1, 2, 3], 'some other data']]);436 });437 it('should respect return false', async () => {438 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new DcsHandler(async (data, params) => { reports.push(['one', params.toArray(), data]); return true; }));439 parser.registerHandler(identifier({intermediates: '+', final: 'p'}), new DcsHandler(async (data, params) => { reports.push(['two', params.toArray(), data]); return false; }));440 parser.hook(identifier({intermediates: '+', final: 'p'}), Params.fromArray([1, 2, 3]));441 let data = toUtf32('Here comes');442 parser.put(data, 0, data.length);443 data = toUtf32(' the mouse!');444 parser.put(data, 0, data.length);445 await unhookP(parser, true);446 assert.deepEqual(reports, [['two', [1, 2, 3], 'Here comes the mouse!'], ['one', [1, 2, 3], 'Here comes the mouse!']]);447 });448 });449 });...
OscParser.test.ts
Source:OscParser.test.ts
...75 });76 });77 describe('handler registration', () => {78 it('setOscHandler', () => {79 parser.registerHandler(1234, new TestHandler(1234, reports, 'th'));80 parser.start();81 let data = toUtf32('1234;Here comes');82 parser.put(data, 0, data.length);83 data = toUtf32('the mouse!');84 parser.put(data, 0, data.length);85 parser.end(true);86 assert.deepEqual(reports, [87 // messages from TestHandler88 ['th', 1234, 'START'],89 ['th', 1234, 'PUT', 'Here comes'],90 ['th', 1234, 'PUT', 'the mouse!'],91 ['th', 1234, 'END', true]92 ]);93 });94 it('clearOscHandler', () => {95 parser.registerHandler(1234, new TestHandler(1234, reports, 'th'));96 parser.clearHandler(1234);97 parser.start();98 let data = toUtf32('1234;Here comes');99 parser.put(data, 0, data.length);100 data = toUtf32('the mouse!');101 parser.put(data, 0, data.length);102 parser.end(true);103 assert.deepEqual(reports, [104 // messages from fallback handler105 [1234, 'START', undefined],106 [1234, 'PUT', 'Here comes'],107 [1234, 'PUT', 'the mouse!'],108 [1234, 'END', true]109 ]);110 });111 it('addOscHandler', () => {112 parser.registerHandler(1234, new TestHandler(1234, reports, 'th1'));113 parser.registerHandler(1234, new TestHandler(1234, reports, 'th2'));114 parser.start();115 let data = toUtf32('1234;Here comes');116 parser.put(data, 0, data.length);117 data = toUtf32('the mouse!');118 parser.put(data, 0, data.length);119 parser.end(true);120 assert.deepEqual(reports, [121 ['th2', 1234, 'START'],122 ['th1', 1234, 'START'],123 ['th2', 1234, 'PUT', 'Here comes'],124 ['th1', 1234, 'PUT', 'Here comes'],125 ['th2', 1234, 'PUT', 'the mouse!'],126 ['th1', 1234, 'PUT', 'the mouse!'],127 ['th2', 1234, 'END', true],128 ['th1', 1234, 'END', false] // false due being already handled by th2!129 ]);130 });131 it('addOscHandler with return false', () => {132 parser.registerHandler(1234, new TestHandler(1234, reports, 'th1'));133 parser.registerHandler(1234, new TestHandler(1234, reports, 'th2', true));134 parser.start();135 let data = toUtf32('1234;Here comes');136 parser.put(data, 0, data.length);137 data = toUtf32('the mouse!');138 parser.put(data, 0, data.length);139 parser.end(true);140 assert.deepEqual(reports, [141 ['th2', 1234, 'START'],142 ['th1', 1234, 'START'],143 ['th2', 1234, 'PUT', 'Here comes'],144 ['th1', 1234, 'PUT', 'Here comes'],145 ['th2', 1234, 'PUT', 'the mouse!'],146 ['th1', 1234, 'PUT', 'the mouse!'],147 ['th2', 1234, 'END', true],148 ['th1', 1234, 'END', true] // true since th2 indicated to keep bubbling149 ]);150 });151 it('dispose handlers', () => {152 parser.registerHandler(1234, new TestHandler(1234, reports, 'th1'));153 const dispo = parser.registerHandler(1234, new TestHandler(1234, reports, 'th2', true));154 dispo.dispose();155 parser.start();156 let data = toUtf32('1234;Here comes');157 parser.put(data, 0, data.length);158 data = toUtf32('the mouse!');159 parser.put(data, 0, data.length);160 parser.end(true);161 assert.deepEqual(reports, [162 ['th1', 1234, 'START'],163 ['th1', 1234, 'PUT', 'Here comes'],164 ['th1', 1234, 'PUT', 'the mouse!'],165 ['th1', 1234, 'END', true]166 ]);167 });168 });169 describe('OscHandlerFactory', () => {170 it('should be called once on end(true)', () => {171 parser.registerHandler(1234, new OscHandler(data => { reports.push([1234, data]); return true; }));172 parser.start();173 let data = toUtf32('1234;Here comes');174 parser.put(data, 0, data.length);175 data = toUtf32(' the mouse!');176 parser.put(data, 0, data.length);177 parser.end(true);178 assert.deepEqual(reports, [[1234, 'Here comes the mouse!']]);179 });180 it('should not be called on end(false)', () => {181 parser.registerHandler(1234, new OscHandler(data => { reports.push([1234, data]); return true; }));182 parser.start();183 let data = toUtf32('1234;Here comes');184 parser.put(data, 0, data.length);185 data = toUtf32(' the mouse!');186 parser.put(data, 0, data.length);187 parser.end(false);188 assert.deepEqual(reports, []);189 });190 it('should be disposable', () => {191 parser.registerHandler(1234, new OscHandler(data => { reports.push(['one', data]); return true; }));192 const dispo = parser.registerHandler(1234, new OscHandler(data => { reports.push(['two', data]); return true; }));193 parser.start();194 let data = toUtf32('1234;Here comes');195 parser.put(data, 0, data.length);196 data = toUtf32(' the mouse!');197 parser.put(data, 0, data.length);198 parser.end(true);199 assert.deepEqual(reports, [['two', 'Here comes the mouse!']]);200 dispo.dispose();201 parser.start();202 data = toUtf32('1234;some other');203 parser.put(data, 0, data.length);204 data = toUtf32(' data');205 parser.put(data, 0, data.length);206 parser.end(true);207 assert.deepEqual(reports, [['two', 'Here comes the mouse!'], ['one', 'some other data']]);208 });209 it('should respect return false', () => {210 parser.registerHandler(1234, new OscHandler(data => { reports.push(['one', data]); return true; }));211 parser.registerHandler(1234, new OscHandler(data => { reports.push(['two', data]); return false; }));212 parser.start();213 let data = toUtf32('1234;Here comes');214 parser.put(data, 0, data.length);215 data = toUtf32(' the mouse!');216 parser.put(data, 0, data.length);217 parser.end(true);218 assert.deepEqual(reports, [['two', 'Here comes the mouse!'], ['one', 'Here comes the mouse!']]);219 });220 it('should work up to payload limit', function(): void {221 this.timeout(10000);222 parser.registerHandler(1234, new OscHandler(data => { reports.push([1234, data]); return true; }));223 parser.start();224 let data = toUtf32('1234;');225 parser.put(data, 0, data.length);226 data = toUtf32('A'.repeat(1000));227 for (let i = 0; i < PAYLOAD_LIMIT; i += 1000) {228 parser.put(data, 0, data.length);229 }230 parser.end(true);231 assert.deepEqual(reports, [[1234, 'A'.repeat(PAYLOAD_LIMIT)]]);232 });233 it('should abort for payload limit +1', function(): void {234 this.timeout(10000);235 parser.registerHandler(1234, new OscHandler(data => { reports.push([1234, data]); return true; }));236 parser.start();237 let data = toUtf32('1234;');238 parser.put(data, 0, data.length);239 data = toUtf32('A'.repeat(1000));240 for (let i = 0; i < PAYLOAD_LIMIT; i += 1000) {241 parser.put(data, 0, data.length);242 }243 data = toUtf32('A');244 parser.put(data, 0, data.length);245 parser.end(true);246 assert.deepEqual(reports, []);247 });248 });249});250class TestHandlerAsync implements IOscHandler {251 constructor(public id: number, public output: any[], public msg: string, public returnFalse: boolean = false) {}252 public start(): void {253 this.output.push([this.msg, this.id, 'START']);254 }255 public put(data: Uint32Array, start: number, end: number): void {256 this.output.push([this.msg, this.id, 'PUT', utf32ToString(data, start, end)]);257 }258 public async end(success: boolean): Promise<boolean> {259 await new Promise(res => setTimeout(res, 20));260 this.output.push([this.msg, this.id, 'END', success]);261 if (this.returnFalse) {262 return false;263 }264 return true;265 }266}267async function endP(parser: OscParser, success: boolean): Promise<void> {268 let result: void | Promise<boolean>;269 let prev: boolean | undefined;270 while (result = parser.end(success, prev)) {271 prev = await result;272 }273}274describe('OscParser - async tests', () => {275 let parser: OscParser;276 let reports: any[] = [];277 beforeEach(() => {278 reports = [];279 parser = new OscParser();280 parser.setHandlerFallback((id, action, data) => {281 reports.push([id, action, data]);282 });283 });284 describe('sync and async mixed', () => {285 describe('sync | async | sync', () => {286 it('first should run, cleanup action for others', async () => {287 parser.registerHandler(1234, new TestHandler(1234, reports, 's1'));288 parser.registerHandler(1234, new TestHandlerAsync(1234, reports, 'a1'));289 parser.registerHandler(1234, new TestHandler(1234, reports, 's2'));290 parser.start();291 let data = toUtf32('1234;Here comes');292 parser.put(data, 0, data.length);293 data = toUtf32('the mouse!');294 parser.put(data, 0, data.length);295 await endP(parser, true);296 assert.deepEqual(reports, [297 // messages from TestHandler298 ['s2', 1234, 'START'],299 ['a1', 1234, 'START'],300 ['s1', 1234, 'START'],301 ['s2', 1234, 'PUT', 'Here comes'],302 ['a1', 1234, 'PUT', 'Here comes'],303 ['s1', 1234, 'PUT', 'Here comes'],304 ['s2', 1234, 'PUT', 'the mouse!'],305 ['a1', 1234, 'PUT', 'the mouse!'],306 ['s1', 1234, 'PUT', 'the mouse!'],307 ['s2', 1234, 'END', true],308 ['a1', 1234, 'END', false],309 ['s1', 1234, 'END', false]310 ]);311 });312 it('all should run', async () => {313 parser.registerHandler(1234, new TestHandler(1234, reports, 's1', true));314 parser.registerHandler(1234, new TestHandlerAsync(1234, reports, 'a1', true));315 parser.registerHandler(1234, new TestHandler(1234, reports, 's2', true));316 parser.start();317 let data = toUtf32('1234;Here comes');318 parser.put(data, 0, data.length);319 data = toUtf32('the mouse!');320 parser.put(data, 0, data.length);321 await endP(parser, true);322 assert.deepEqual(reports, [323 // messages from TestHandler324 ['s2', 1234, 'START'],325 ['a1', 1234, 'START'],326 ['s1', 1234, 'START'],327 ['s2', 1234, 'PUT', 'Here comes'],328 ['a1', 1234, 'PUT', 'Here comes'],329 ['s1', 1234, 'PUT', 'Here comes'],330 ['s2', 1234, 'PUT', 'the mouse!'],331 ['a1', 1234, 'PUT', 'the mouse!'],332 ['s1', 1234, 'PUT', 'the mouse!'],333 ['s2', 1234, 'END', true],334 ['a1', 1234, 'END', true],335 ['s1', 1234, 'END', true]336 ]);337 });338 });339 describe('async | sync | async', () => {340 it('first should run, cleanup action for others', async () => {341 parser.registerHandler(1234, new TestHandlerAsync(1234, reports, 's1'));342 parser.registerHandler(1234, new TestHandler(1234, reports, 'a1'));343 parser.registerHandler(1234, new TestHandlerAsync(1234, reports, 's2'));344 parser.start();345 let data = toUtf32('1234;Here comes');346 parser.put(data, 0, data.length);347 data = toUtf32('the mouse!');348 parser.put(data, 0, data.length);349 await endP(parser, true);350 assert.deepEqual(reports, [351 // messages from TestHandler352 ['s2', 1234, 'START'],353 ['a1', 1234, 'START'],354 ['s1', 1234, 'START'],355 ['s2', 1234, 'PUT', 'Here comes'],356 ['a1', 1234, 'PUT', 'Here comes'],357 ['s1', 1234, 'PUT', 'Here comes'],358 ['s2', 1234, 'PUT', 'the mouse!'],359 ['a1', 1234, 'PUT', 'the mouse!'],360 ['s1', 1234, 'PUT', 'the mouse!'],361 ['s2', 1234, 'END', true],362 ['a1', 1234, 'END', false],363 ['s1', 1234, 'END', false]364 ]);365 });366 it('all should run', async () => {367 parser.registerHandler(1234, new TestHandlerAsync(1234, reports, 's1', true));368 parser.registerHandler(1234, new TestHandler(1234, reports, 'a1', true));369 parser.registerHandler(1234, new TestHandlerAsync(1234, reports, 's2', true));370 parser.start();371 let data = toUtf32('1234;Here comes');372 parser.put(data, 0, data.length);373 data = toUtf32('the mouse!');374 parser.put(data, 0, data.length);375 await endP(parser, true);376 assert.deepEqual(reports, [377 // messages from TestHandler378 ['s2', 1234, 'START'],379 ['a1', 1234, 'START'],380 ['s1', 1234, 'START'],381 ['s2', 1234, 'PUT', 'Here comes'],382 ['a1', 1234, 'PUT', 'Here comes'],383 ['s1', 1234, 'PUT', 'Here comes'],384 ['s2', 1234, 'PUT', 'the mouse!'],385 ['a1', 1234, 'PUT', 'the mouse!'],386 ['s1', 1234, 'PUT', 'the mouse!'],387 ['s2', 1234, 'END', true],388 ['a1', 1234, 'END', true],389 ['s1', 1234, 'END', true]390 ]);391 });392 });393 describe('OscHandlerFactory', () => {394 it('should be called once on end(true)', async () => {395 parser.registerHandler(1234, new OscHandler(async data => { reports.push([1234, data]); return true; }));396 parser.start();397 let data = toUtf32('1234;Here comes');398 parser.put(data, 0, data.length);399 data = toUtf32(' the mouse!');400 parser.put(data, 0, data.length);401 parser.end(true);402 await endP(parser, true);403 assert.deepEqual(reports, [[1234, 'Here comes the mouse!']]);404 });405 it('should not be called on end(false)', async () => {406 parser.registerHandler(1234, new OscHandler(async data => { reports.push([1234, data]); return true; }));407 parser.start();408 let data = toUtf32('1234;Here comes');409 parser.put(data, 0, data.length);410 data = toUtf32(' the mouse!');411 parser.put(data, 0, data.length);412 await endP(parser, false);413 assert.deepEqual(reports, []);414 });415 it('should be disposable', async () => {416 parser.registerHandler(1234, new OscHandler(async data => { reports.push(['one', data]); return true; }));417 const dispo = parser.registerHandler(1234, new OscHandler(async data => { reports.push(['two', data]); return true; }));418 parser.start();419 let data = toUtf32('1234;Here comes');420 parser.put(data, 0, data.length);421 data = toUtf32(' the mouse!');422 parser.put(data, 0, data.length);423 await endP(parser, true);424 assert.deepEqual(reports, [['two', 'Here comes the mouse!']]);425 dispo.dispose();426 parser.start();427 data = toUtf32('1234;some other');428 parser.put(data, 0, data.length);429 data = toUtf32(' data');430 parser.put(data, 0, data.length);431 await endP(parser, true);432 assert.deepEqual(reports, [['two', 'Here comes the mouse!'], ['one', 'some other data']]);433 });434 it('should respect return false', async () => {435 parser.registerHandler(1234, new OscHandler(async data => { reports.push(['one', data]); return true; }));436 parser.registerHandler(1234, new OscHandler(async data => { reports.push(['two', data]); return false; }));437 parser.start();438 let data = toUtf32('1234;Here comes');439 parser.put(data, 0, data.length);440 data = toUtf32(' the mouse!');441 parser.put(data, 0, data.length);442 await endP(parser, true);443 assert.deepEqual(reports, [['two', 'Here comes the mouse!'], ['one', 'Here comes the mouse!']]);444 });445 });446 });...
bridge.js
Source:bridge.js
...9 })10 })11}12JsBridge.prototype = {13 registerHandler(actionname, callback, data = {}) {14 data = JSON.stringify(data)15 this.init(YonYouJSBridge => {16 YonYouJSBridge.registerHandler(actionname, (data, responseData) => {17 callback()18 })19 })20 },21 init(callback) {22 if (window.WebViewJavascriptBridge) {23 callback(WebViewJavascriptBridge)24 } else {25 document.addEventListener(26 "WebViewJavascriptBridgeReady",27 function() {28 callback(WebViewJavascriptBridge)29 },30 false31 )32 }33 },34 do(action = "", parameters = {}, fn = () => {}) {35 let registerHandler = ""36 if (parameters.registerHandler) {37 registerHandler = parameters.registerHandler38 delete parameters.registerHandler39 }40 let data = {41 function: action,42 parameters: parameters43 }44 data = JSON.stringify(data)45 if (registerHandler.length > 0) {46 registerHandler.forEach((value, index, registerHandler) => {47 let action = value.action48 let actionname = value.parameters49 this.init(YonYouJSBridge => {50 YonYouJSBridge.registerHandler(51 actionname,52 (data, responseData) => {53 action(data)54 }55 )56 YonYouJSBridge.send(data, responseData => {57 fn && fn(responseData)58 })59 })60 })61 } else if (registerHandler.length == undefined) {62 let action = registerHandler.action63 let actionname = registerHandler.parameters64 this.init(YonYouJSBridge => {65 YonYouJSBridge.registerHandler(66 actionname,67 (data, responseData) => {68 fn && fn(responseData)69 }70 )71 YonYouJSBridge.send(data, responseData => {72 fn && fn(responseData)73 })74 })75 } else {76 this.init(YonYouJSBridge => {77 YonYouJSBridge.send(data, responseData => {78 fn && fn(responseData)79 })...
Using AI Code Generation
1module.exports = function (strykerConfig, options) {2 return {3 }4}5module.exports = function (strykerConfig, options) {6 return {7 }8}9module.exports = function (strykerConfig, options) {10 return {11 }12}13module.exports = function (strykerConfig, options) {14 return {15 }16}17module.exports = function (strykerConfig, options) {18 return {19 }20}21module.exports = function (strykerConfig, options) {22 return {
Using AI Code Generation
1require('stryker-parent').registerHandler(function (message) {2 console.log('Message received from stryker-parent: ' + message);3});4require('stryker-parent').registerHandler(function (message) {5 console.log('Message received from stryker-parent: ' + message);6});7### registerHandler(handler: (message: any) => void): void8### send(message: any): void
Using AI Code Generation
1const strykerParent = require('stryker-parent');2strykerParent.registerHandler((mutant, filePath) => {3});4module.exports = function(config) {5 config.set({6 });7};8module.exports = function(config) {9 config.set({10 });11};12module.exports = function(config) {13 config.set({14 });15};16module.exports = function(config) {17 config.set({18 });19};20module.exports = function(config) {21 config.set({22 });23};24module.exports = function(config) {25 config.set({26 });27};28module.exports = function(config) {29 config.set({30 });31};32module.exports = function(config) {33 config.set({34 });35};36module.exports = function(config) {37 config.set({
Using AI Code Generation
1var stryker = require('stryker-parent');2var handler = function (message) {3 console.log('Message received ' + message);4};5stryker.registerHandler(handler);6var stryker = require('stryker-parent');7var handler = function (message) {8 console.log('Message received ' + message);9};10stryker.registerHandler(handler);11var stryker = require('stryker-parent');12var handler = function (message) {13 console.log('Message received ' + message);14};15stryker.registerHandler(handler);16var stryker = require('stryker-parent');17var handler = function (message) {18 console.log('Message received ' + message);19};20stryker.registerHandler(handler);21var stryker = require('stryker-parent');22var handler = function (message) {23 console.log('Message received ' + message);24};25stryker.registerHandler(handler);26var stryker = require('stryker-parent');27var handler = function (message) {28 console.log('Message received ' + message);29};30stryker.registerHandler(handler);31var stryker = require('stryker-parent');32var handler = function (message) {33 console.log('Message received ' + message);34};35stryker.registerHandler(handler);36var stryker = require('stryker-parent');37var handler = function (message) {38 console.log('Message received ' + message);39};40stryker.registerHandler(handler);41var stryker = require('stryker-parent');42var handler = function (message) {43 console.log('Message received ' + message);44};45stryker.registerHandler(handler);
Using AI Code Generation
1var stryker = require('stryker-parent');2stryker.registerHandler('test', function (obj) {3 console.log('test handler called');4 return 'test';5});6var stryker = require('stryker-parent');7stryker.registerHandler('test', function (obj) {8 console.log('test handler called');9 return 'test';10});11var stryker = require('stryker-parent');12stryker.registerHandler('test', function (obj) {13 console.log('test handler called');14 return 'test';15});16var stryker = require('stryker-parent');17stryker.registerHandler('test', function (obj) {18 console.log('test handler called');19 return 'test';20});21var stryker = require('stryker-parent');22stryker.registerHandler('test', function (obj) {23 console.log('test handler called');24 return 'test';25});26var stryker = require('stryker-parent');27stryker.registerHandler('test', function (obj) {28 console.log('test handler called');29 return 'test';30});31var stryker = require('stryker-parent');32stryker.registerHandler('test', function (obj) {33 console.log('test handler called');34 return 'test';35});36var stryker = require('stryker-parent');37stryker.registerHandler('test', function (obj) {38 console.log('test handler called');39 return 'test';40});41var stryker = require('stryker-parent');42stryker.registerHandler('test', function (obj) {43 console.log('test handler called');44 return 'test';45});46var stryker = require('stryker-parent');47stryker.registerHandler('test', function (obj) {48 console.log('test handler called');
Using AI Code Generation
1const registerHandler = require('stryker-parent').registerHandler;2registerHandler('myCustomEvent', (data) => {3 console.log('Got data from event', data);4});5const emit = require('stryker-parent').emit;6emit('myCustomEvent', { data: 'my custom data' });7emitSync('myCustomEvent', { data: 'my custom data' });8const { registerHandler } = require('stryker-api/core');9registerHandler('myCustomEvent', (data) => {10 console.log('Got data from event', data);11});12const { emit } = require('stryker-api/core');13emit('myCustomEvent', { data: 'my custom data' });14The above code will output “Got data from event { data: 'my custom data' }” in the
Using AI Code Generation
1var stryker = require('stryker-parent');2stryker.registerHandler(function (event) {3 console.log('event received: ' + event);4});5stryker.emit('event');6var events = require('events');7var eventEmitter = new events.EventEmitter();8var registerHandler = function (handler) {9 eventEmitter.on('event', handler);10};11var emit = function (event) {12 eventEmitter.emit('event', event);13};14module.exports = {15};16In Node.js, you can use the require() method to import other modules. The require() method accepts a single parameter, which is the name of the module you want to import. To import the stryker-parent module, use the following code:17var stryker = require('stryker-parent');18The require() method returns the exports object of the stryker-parent module. The exports object is an object that contains all the functions and objects that are exposed by the stryker-parent module. In the stryker-parent module, the registerHandler() method is exposed as a property of the exports object. You can access the registerHandler() method as follows:19stryker.registerHandler(function (event) {20 console.log('event received: ' + event);21});22To emit an event, use the emit() method of the stryker-parent module. The emit() method accepts a single parameter, which is the name of the event to be emitted. To emit the event , use the following code:23stryker.emit('event');
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!