Best JavaScript code snippet using sinon
light.js
Source:light.js
...13 setUp: function(done) {14 this.sandbox = sinon.sandbox.create();15 this.board = newBoard();16 this.clock = this.sandbox.useFakeTimers();17 this.sandbox.spy(Board, "Component");18 done();19 },20 tearDown: function(done) {21 Board.purge();22 Light.purge();23 this.sandbox.restore();24 done();25 },26 shape: function(test) {27 test.expect(proto.length + instance.length);28 this.light = new Light({29 pin: "A1",30 freq: 100,31 board: this.board32 });33 proto.forEach(function(method) {34 test.equal(typeof this.light[method.name], "function");35 }, this);36 instance.forEach(function(property) {37 test.notEqual(typeof this.light[property.name], 0);38 }, this);39 test.done();40 },41 instanceof: function(test) {42 test.expect(1);43 test.equal(Light({ board: this.board }) instanceof Light, true);44 test.done();45 },46 component: function(test) {47 test.expect(1);48 new Light({ board: this.board });49 test.equal(Board.Component.callCount, 1);50 test.done();51 },52 emitter: function(test) {53 test.expect(1);54 this.light = new Light({55 pin: "A1",56 freq: 100,57 board: this.board58 });59 test.ok(this.light instanceof Emitter);60 test.done();61 },62 customIntensityLevel: function(test) {63 test.expect(2);64 this.light = new Light({65 board: this.board,66 controller: {},67 toIntensityLevel: function(x) {68 test.ok(true);69 return x * x;70 },71 });72 test.equal(this.light.toIntensityLevel(2), 4);73 test.done();74 },75 fallbackIntensityLevel: function(test) {76 test.expect(1);77 this.light = new Light({78 board: this.board,79 controller: {},80 });81 test.equal(this.light.toIntensityLevel(2), 2);82 test.done();83 },84};85exports["Light: ALSPT19"] = {86 setUp: function(done) {87 this.sandbox = sinon.sandbox.create();88 this.board = newBoard();89 this.clock = this.sandbox.useFakeTimers();90 this.analogRead = this.sandbox.spy(MockFirmata.prototype, "analogRead");91 this.light = new Light({92 controller: "ALSPT19",93 pin: "A1",94 freq: 100,95 board: this.board96 });97 done();98 },99 tearDown: function(done) {100 Board.purge();101 Light.purge();102 this.sandbox.restore();103 done();104 },105 shape: function(test) {106 test.expect(proto.length + instance.length);107 proto.forEach(function(method) {108 test.equal(typeof this.light[method.name], "function");109 }, this);110 instance.forEach(function(property) {111 test.notEqual(typeof this.light[property.name], 0);112 }, this);113 test.done();114 },115 emitter: function(test) {116 test.expect(1);117 test.ok(this.light instanceof Emitter);118 test.done();119 }120};121exports["Light: BH1750"] = {122 setUp: function(done) {123 this.sandbox = sinon.sandbox.create();124 this.board = newBoard();125 this.clock = this.sandbox.useFakeTimers();126 this.i2cConfig = this.sandbox.spy(MockFirmata.prototype, "i2cConfig");127 this.i2cWrite = this.sandbox.spy(MockFirmata.prototype, "i2cWrite");128 this.i2cReadOnce = this.sandbox.spy(MockFirmata.prototype, "i2cReadOnce");129 this.light = new Light({130 controller: "BH1750",131 board: this.board132 });133 done();134 },135 tearDown: function(done) {136 Board.purge();137 Light.purge();138 this.sandbox.restore();139 done();140 },141 fwdOptionsToi2cConfig: function(test) {142 test.expect(3);143 this.i2cConfig.reset();144 new Light({145 controller: "BH1750",146 address: 0xff,147 bus: "i2c-1",148 board: this.board149 });150 var forwarded = this.i2cConfig.lastCall.args[0];151 test.equal(this.i2cConfig.callCount, 1);152 test.equal(forwarded.address, 0xff);153 test.equal(forwarded.bus, "i2c-1");154 test.done();155 },156 initialization: function(test) {157 test.expect(4);158 test.equal(this.i2cConfig.callCount, 1);159 test.equal(this.i2cWrite.callCount, 1);160 test.equal(this.i2cWrite.lastCall.args[0], 0x23);161 test.equal(this.i2cWrite.lastCall.args[1], 0x10);162 test.done();163 },164 data: function(test) {165 test.expect(3);166 this.clock.tick(120);167 var read = this.i2cReadOnce.lastCall.args[2];168 var spy = this.sandbox.spy();169 this.light.on("data", spy);170 read([ 3, 84 ]);171 this.clock.tick(25);172 test.equal(spy.callCount, 1);173 test.equal(this.light.level, 0.01);174 test.equal(this.light.lux, 710);175 test.done();176 },177 change: function(test) {178 test.expect(6);179 this.clock.tick(120);180 var read = this.i2cReadOnce.lastCall.args[2];181 var spy = this.sandbox.spy();182 this.light.on("change", spy);183 read([ 3, 84 ]);184 this.clock.tick(25);185 test.equal(spy.callCount, 1);186 test.equal(this.light.level, 0.01);187 test.equal(this.light.lux, 710);188 this.clock.tick(120);189 read([ 3, 95 ]);190 this.clock.tick(25);191 test.equal(spy.callCount, 2);192 test.equal(this.light.level, 0.01);193 test.equal(this.light.lux, 719);194 test.done();195 },196};197exports["Light: TSL2561"] = {198 setUp: function(done) {199 this.sandbox = sinon.sandbox.create();200 this.board = newBoard();201 this.clock = this.sandbox.useFakeTimers();202 this.i2cConfig = this.sandbox.spy(MockFirmata.prototype, "i2cConfig");203 this.i2cWrite = this.sandbox.spy(MockFirmata.prototype, "i2cWrite");204 this.i2cWriteReg = this.sandbox.spy(MockFirmata.prototype, "i2cWriteReg");205 this.i2cReadOnce = this.sandbox.spy(MockFirmata.prototype, "i2cReadOnce");206 this.light = new Light({207 controller: "TSL2561",208 board: this.board209 });210 done();211 },212 tearDown: function(done) {213 Board.purge();214 Light.purge();215 this.sandbox.restore();216 done();217 },218 fwdOptionsToi2cConfig: function(test) {219 test.expect(3);220 this.i2cConfig.reset();221 new Light({222 controller: "TSL2561",223 address: 0xff,224 bus: "i2c-1",225 board: this.board226 });227 var forwarded = this.i2cConfig.lastCall.args[0];228 test.equal(this.i2cConfig.callCount, 1);229 test.equal(forwarded.address, 0xff);230 test.equal(forwarded.bus, "i2c-1");231 test.done();232 },233 initialization: function(test) {234 test.expect(4);235 test.equal(this.i2cConfig.callCount, 1);236 test.equal(this.i2cWriteReg.callCount, 3);237 test.equal(this.i2cWriteReg.lastCall.args[0], 0x39);238 test.equal(this.i2cWriteReg.lastCall.args[1], 0x81);239 test.done();240 },241};242exports["Light: EVS_EV3, Ambient (Default)"] = {243 setUp: function(done) {244 this.sandbox = sinon.sandbox.create();245 this.board = newBoard();246 this.clock = this.sandbox.useFakeTimers();247 this.evssetup = this.sandbox.spy(EVS.prototype, "setup");248 this.evswrite = this.sandbox.spy(EVS.prototype, "write");249 this.evsread = this.sandbox.spy(EVS.prototype, "read");250 this.i2cConfig = this.sandbox.spy(MockFirmata.prototype, "i2cConfig");251 this.i2cWrite = this.sandbox.spy(MockFirmata.prototype, "i2cWrite");252 this.i2cRead = this.sandbox.stub(MockFirmata.prototype, "i2cRead", function(address, register, numBytes, callback) {253 callback([15, 0]);254 });255 this.light = new Light({256 controller: "EVS_EV3",257 pin: "BAS1",258 freq: 100,259 board: this.board260 });261 done();262 },263 tearDown: function(done) {264 Board.purge();265 Light.purge();266 this.sandbox.restore();267 done();268 },269 shape: function(test) {270 test.expect(proto.length + instance.length);271 proto.forEach(function(method) {272 test.equal(typeof this.light[method.name], "function");273 }, this);274 instance.forEach(function(property) {275 test.notEqual(typeof this.light[property.name], 0);276 }, this);277 test.done();278 },279 initialization: function(test) {280 test.expect(2);281 var shield = {282 address: 26,283 analog: 112,284 bank: "a",285 mode: 111,286 motor: undefined,287 offset: 0,288 port: 1,289 sensor: 1290 };291 test.deepEqual(this.evssetup.lastCall.args, [shield, EVS.Type_EV3]);292 test.deepEqual(this.evswrite.lastCall.args, [shield, 0x81 + shield.offset, EVS.Type_EV3_LIGHT]);293 test.done();294 },295 data: function(test) {296 var spy = this.sandbox.spy();297 test.expect(1);298 this.light.on("data", spy);299 this.clock.tick(100);300 test.equal(spy.callCount, 1);301 test.done();302 },303 change: function(test) {304 test.expect(1);305 var spy = this.sandbox.spy();306 this.light.on("change", spy);307 this.clock.tick(100);308 test.ok(spy.called);309 test.done();310 },311 within: function(test) {312 var spy = this.sandbox.spy();313 test.expect(2);314 this.clock.tick(250);315 this.light.within([0.10, 0.20], "level", function() {316 test.equal(this.level, 0.15);317 spy();318 });319 this.clock.tick(100);320 test.ok(spy.calledOnce);321 test.done();322 }323};324exports["Light: EVS_EV3, Reflected"] = {325 setUp: function(done) {326 this.sandbox = sinon.sandbox.create();327 this.board = newBoard();328 this.clock = this.sandbox.useFakeTimers();329 this.evssetup = this.sandbox.spy(EVS.prototype, "setup");330 this.evswrite = this.sandbox.spy(EVS.prototype, "write");331 this.evsread = this.sandbox.spy(EVS.prototype, "read");332 this.i2cConfig = this.sandbox.spy(MockFirmata.prototype, "i2cConfig");333 this.i2cWrite = this.sandbox.spy(MockFirmata.prototype, "i2cWrite");334 this.i2cRead = this.sandbox.stub(MockFirmata.prototype, "i2cRead", function(address, register, numBytes, callback) {335 callback([15, 0]);336 });337 this.light = new Light({338 controller: "EVS_EV3",339 pin: "BAS1",340 mode: "reflected",341 freq: 100,342 board: this.board343 });344 done();345 },346 tearDown: function(done) {347 Board.purge();348 Light.purge();349 this.sandbox.restore();350 done();351 },352 shape: function(test) {353 test.expect(proto.length + instance.length);354 proto.forEach(function(method) {355 test.equal(typeof this.light[method.name], "function");356 }, this);357 instance.forEach(function(property) {358 test.notEqual(typeof this.light[property.name], 0);359 }, this);360 test.done();361 },362 initialization: function(test) {363 test.expect(2);364 var shield = {365 address: 26,366 analog: 112,367 bank: "a",368 mode: 111,369 motor: undefined,370 offset: 0,371 port: 1,372 sensor: 1373 };374 test.deepEqual(this.evssetup.lastCall.args, [shield, EVS.Type_EV3]);375 test.deepEqual(this.evswrite.lastCall.args, [shield, 0x81 + shield.offset, EVS.Type_EV3_LIGHT_REFLECTED]);376 test.done();377 },378 data: function(test) {379 var spy = this.sandbox.spy();380 test.expect(1);381 this.light.on("data", spy);382 this.clock.tick(100);383 test.equal(spy.callCount, 1);384 test.done();385 },386 change: function(test) {387 test.expect(1);388 var spy = this.sandbox.spy();389 this.light.on("change", spy);390 this.clock.tick(100);391 test.ok(spy.called);392 test.done();393 },394 within: function(test) {395 var spy = this.sandbox.spy();396 test.expect(2);397 this.clock.tick(250);398 this.light.within([0.10, 0.20], "level", function() {399 test.equal(this.level, 0.15);400 spy();401 });402 this.clock.tick(100);403 test.ok(spy.calledOnce);404 test.done();405 }406};407exports["Light: EVS_NXT, Ambient (Default)"] = {408 setUp: function(done) {409 this.sandbox = sinon.sandbox.create();410 this.board = newBoard();411 this.clock = this.sandbox.useFakeTimers();412 this.evssetup = this.sandbox.spy(EVS.prototype, "setup");413 this.evswrite = this.sandbox.spy(EVS.prototype, "write");414 this.evsread = this.sandbox.spy(EVS.prototype, "read");415 this.i2cConfig = this.sandbox.spy(MockFirmata.prototype, "i2cConfig");416 this.i2cWrite = this.sandbox.spy(MockFirmata.prototype, "i2cWrite");417 this.i2cRead = this.sandbox.stub(MockFirmata.prototype, "i2cRead", function(address, register, numBytes, callback) {418 callback([100, 3]);419 });420 this.light = new Light({421 controller: "EVS_NXT",422 pin: "BAS1",423 freq: 100,424 board: this.board425 });426 done();427 },428 tearDown: function(done) {429 Board.purge();430 Light.purge();431 this.sandbox.restore();432 done();433 },434 shape: function(test) {435 test.expect(proto.length + instance.length);436 proto.forEach(function(method) {437 test.equal(typeof this.light[method.name], "function");438 }, this);439 instance.forEach(function(property) {440 test.notEqual(typeof this.light[property.name], 0);441 }, this);442 test.done();443 },444 initialization: function(test) {445 test.expect(1);446 var shield = {447 address: 26,448 analog: 112,449 bank: "a",450 mode: 111,451 motor: undefined,452 offset: 0,453 port: 1,454 sensor: 1455 };456 test.deepEqual(this.evssetup.lastCall.args, [shield, EVS.Type_NXT_LIGHT]);457 test.done();458 },459 data: function(test) {460 var spy = this.sandbox.spy();461 test.expect(1);462 this.light.on("data", spy);463 this.clock.tick(100);464 test.equal(spy.callCount, 1);465 test.done();466 },467 change: function(test) {468 test.expect(1);469 var spy = this.sandbox.spy();470 this.light.on("change", spy);471 this.clock.tick(100);472 test.ok(spy.called);473 test.done();474 },475 within: function(test) {476 var spy = this.sandbox.spy();477 test.expect(2);478 this.clock.tick(250);479 this.light.within([0.10, 0.20], "level", function() {480 test.equal(this.level, 0.15);481 spy();482 });483 this.clock.tick(100);484 test.ok(spy.calledOnce);485 test.done();486 }487};488exports["Light: EVS_NXT, Reflected"] = {489 setUp: function(done) {490 this.sandbox = sinon.sandbox.create();491 this.board = newBoard();492 this.clock = this.sandbox.useFakeTimers();493 this.evssetup = this.sandbox.spy(EVS.prototype, "setup");494 this.evswrite = this.sandbox.spy(EVS.prototype, "write");495 this.evsread = this.sandbox.spy(EVS.prototype, "read");496 this.i2cConfig = this.sandbox.spy(MockFirmata.prototype, "i2cConfig");497 this.i2cWrite = this.sandbox.spy(MockFirmata.prototype, "i2cWrite");498 this.i2cRead = this.sandbox.stub(MockFirmata.prototype, "i2cRead", function(address, register, numBytes, callback) {499 callback([100, 3]);500 });501 this.light = new Light({502 controller: "EVS_NXT",503 pin: "BAS1",504 mode: "reflected",505 freq: 100,506 board: this.board507 });508 done();509 },510 tearDown: function(done) {511 Board.purge();512 Light.purge();513 this.sandbox.restore();514 done();515 },516 shape: function(test) {517 test.expect(proto.length + instance.length);518 proto.forEach(function(method) {519 test.equal(typeof this.light[method.name], "function");520 }, this);521 instance.forEach(function(property) {522 test.notEqual(typeof this.light[property.name], 0);523 }, this);524 test.done();525 },526 initialization: function(test) {527 test.expect(1);528 var shield = {529 address: 26,530 analog: 112,531 bank: "a",532 mode: 111,533 motor: undefined,534 offset: 0,535 port: 1,536 sensor: 1537 };538 test.deepEqual(this.evssetup.lastCall.args, [shield, EVS.Type_NXT_LIGHT_REFLECTED]);539 test.done();540 },541 data: function(test) {542 var spy = this.sandbox.spy();543 test.expect(1);544 this.light.on("data", spy);545 this.clock.tick(100);546 test.equal(spy.callCount, 1);547 test.done();548 },549 change: function(test) {550 test.expect(1);551 var spy = this.sandbox.spy();552 this.light.on("change", spy);553 this.clock.tick(100);554 test.ok(spy.called);555 test.done();556 },557 within: function(test) {558 var spy = this.sandbox.spy();559 test.expect(2);560 this.clock.tick(250);561 this.light.within([0.10, 0.20], "level", function() {562 test.equal(this.level, 0.15);563 spy();564 });565 this.clock.tick(100);566 test.ok(spy.calledOnce);567 test.done();568 }...
motion.js
Source:motion.js
...3 setUp: function(done) {4 this.sandbox = sinon.sandbox.create();5 this.board = newBoard();6 this.clock = this.sandbox.useFakeTimers();7 this.digitalRead = this.sandbox.spy(MockFirmata.prototype, "digitalRead");8 this.motion = new Motion({9 pin: 7,10 calibrationDelay: 10,11 board: this.board12 });13 this.instance = [{14 name: "detectedMotion"15 }, {16 name: "isCalibrated"17 }];18 done();19 },20 tearDown: function(done) {21 Board.purge();22 this.sandbox.restore();23 done();24 },25 shape: function(test) {26 test.expect(this.instance.length);27 this.instance.forEach(function(property) {28 test.notEqual(typeof this.motion[property.name], "undefined");29 }, this);30 test.done();31 },32 emitter: function(test) {33 test.expect(1);34 test.ok(this.motion instanceof Emitter);35 test.done();36 }37};38exports["Motion - PIR"] = {39 setUp: function(done) {40 this.sandbox = sinon.sandbox.create();41 this.board = newBoard();42 this.clock = this.sandbox.useFakeTimers();43 this.digitalRead = this.sandbox.spy(MockFirmata.prototype, "digitalRead");44 this.motion = new Motion({45 pin: 7,46 calibrationDelay: 10,47 board: this.board48 });49 done();50 },51 tearDown: function(done) {52 Board.purge();53 this.sandbox.restore();54 done();55 },56 calibrated: function(test) {57 test.expect(1);58 var spy = this.sandbox.spy();59 this.motion.on("calibrated", spy);60 this.clock.tick(10);61 test.ok(spy.calledOnce);62 test.done();63 },64 data: function(test) {65 test.expect(1);66 var spy = this.sandbox.spy();67 this.motion.on("data", spy);68 this.clock.tick(25);69 test.ok(spy.calledOnce);70 test.done();71 },72 change: function(test) {73 test.expect(1);74 var spy = this.sandbox.spy();75 var callback = this.digitalRead.firstCall.args[1];76 this.motion.on("change", spy);77 this.clock.tick(25);78 callback(0);79 this.clock.tick(25);80 callback(1);81 this.clock.tick(25);82 callback(0);83 this.clock.tick(25);84 test.ok(spy.calledTwice);85 test.done();86 },87 noChange: function(test) {88 test.expect(1);89 var spy = this.sandbox.spy();90 var callback = this.digitalRead.firstCall.args[1];91 this.motion.on("change", spy);92 this.clock.tick(25);93 callback(0);94 this.clock.tick(25);95 callback(0);96 this.clock.tick(25);97 callback(0);98 this.clock.tick(25);99 test.ok(spy.notCalled);100 test.done();101 },102 motionstart: function(test) {103 var spy = this.sandbox.spy();104 var callback = this.digitalRead.firstCall.args[1];105 test.expect(1);106 this.motion.on("motionstart", spy);107 // 0 then changes to 1108 callback(0);109 callback(1);110 this.clock.tick(25);111 test.ok(spy.calledOnce);112 test.done();113 },114 motionend: function(test) {115 var spy = this.sandbox.spy();116 var callback = this.digitalRead.firstCall.args[1];117 test.expect(1);118 this.motion.on("motionend", spy);119 // 1 then changes to 0120 callback(1);121 this.clock.tick(25);122 callback(0);123 this.clock.tick(25);124 test.ok(spy.calledOnce);125 test.done();126 }127};128exports["Motion - GP2Y0D805Z0F"] = {129 setUp: function(done) {130 this.sandbox = sinon.sandbox.create();131 this.board = newBoard();132 this.clock = this.sandbox.useFakeTimers();133 this.i2cRead = this.sandbox.spy(MockFirmata.prototype, "i2cRead");134 this.i2cWrite = this.sandbox.spy(MockFirmata.prototype, "i2cWrite");135 this.i2cWriteReg = this.sandbox.spy(MockFirmata.prototype, "i2cWriteReg");136 this.i2cConfig = this.sandbox.spy(MockFirmata.prototype, "i2cConfig");137 this.motion = new Motion({138 controller: "GP2Y0D805Z0F",139 calibrationDelay: 10,140 board: this.board141 });142 done();143 },144 tearDown: function(done) {145 Board.purge();146 this.sandbox.restore();147 done();148 },149 fwdOptionsToi2cConfig: function(test) {150 test.expect(3);151 this.i2cConfig.reset();152 new Motion({153 controller: "GP2Y0D805Z0F",154 address: 0xff,155 bus: "i2c-1",156 board: this.board157 });158 var forwarded = this.i2cConfig.lastCall.args[0];159 test.equal(this.i2cConfig.callCount, 1);160 test.equal(forwarded.address, 0xff);161 test.equal(forwarded.bus, "i2c-1");162 test.done();163 },164 initialize: function(test) {165 test.expect(8);166 test.ok(this.i2cConfig.called);167 test.ok(this.i2cWriteReg.calledOnce);168 test.ok(this.i2cWrite.calledOnce);169 test.ok(this.i2cRead.calledOnce);170 test.deepEqual(this.i2cWriteReg.firstCall.args, [0x26, 0x03, 0xFE]);171 test.deepEqual(this.i2cWrite.firstCall.args, [0x26, [0x00]]);172 test.equal(this.i2cRead.firstCall.args[0], 0x26);173 test.equal(this.i2cRead.firstCall.args[1], 1);174 test.done();175 },176 calibrated: function(test) {177 var spy = this.sandbox.spy();178 test.expect(1);179 this.motion.on("calibrated", spy);180 this.clock.tick(10);181 test.ok(spy.calledOnce);182 test.done();183 },184 data: function(test) {185 var spy = this.sandbox.spy();186 test.expect(1);187 this.motion.on("data", spy);188 this.clock.tick(25);189 test.ok(spy.calledOnce);190 test.done();191 },192 change: function(test) {193 var spy = this.sandbox.spy();194 var callback = this.i2cRead.firstCall.args[2];195 test.expect(1);196 this.motion.on("change", spy);197 callback([0x00]);198 this.clock.tick(25);199 callback([0x03]);200 this.clock.tick(25);201 test.ok(spy.calledTwice);202 test.done();203 },204 noChange: function(test) {205 test.expect(1);206 var spy = this.sandbox.spy();207 var callback = this.i2cRead.firstCall.args[2];208 this.motion.on("change", spy);209 this.clock.tick(25);210 callback([0x03]);211 this.clock.tick(25);212 callback([0x03]);213 this.clock.tick(25);214 callback([0x03]);215 this.clock.tick(25);216 test.ok(spy.notCalled);217 test.done();218 },219 motionstart: function(test) {220 // this.clock.tick(250);221 var spy = this.sandbox.spy();222 var callback = this.i2cRead.firstCall.args[2];223 test.expect(1);224 this.motion.on("motionstart", spy);225 callback([3]);226 callback([1]);227 this.clock.tick(25);228 test.ok(spy.calledOnce);229 test.done();230 },231 motionend: function(test) {232 // this.clock.tick(250);233 var spy = this.sandbox.spy();234 var callback = this.i2cRead.firstCall.args[2];235 test.expect(1);236 this.motion.on("motionend", spy);237 callback([1]);238 this.clock.tick(25);239 callback([3]);240 this.clock.tick(25);241 test.ok(spy.calledOnce);242 test.done();243 }244};245exports["Motion - GP2Y0D810Z0F"] = {246 setUp: function(done) {247 this.sandbox = sinon.sandbox.create();248 this.board = newBoard();249 this.clock = this.sandbox.useFakeTimers();250 this.pinMode = this.sandbox.spy(MockFirmata.prototype, "pinMode");251 this.analogRead = this.sandbox.spy(MockFirmata.prototype, "analogRead");252 this.motion = new Motion({253 controller: "GP2Y0D810Z0F",254 pin: "A0",255 board: this.board256 });257 done();258 },259 tearDown: function(done) {260 Board.purge();261 this.sandbox.restore();262 done();263 },264 initialize: function(test) {265 test.expect(2);266 test.ok(this.pinMode.calledOnce);267 test.ok(this.analogRead.calledOnce);268 test.done();269 },270 calibrated: function(test) {271 var spy = this.sandbox.spy();272 test.expect(1);273 this.motion.on("calibrated", spy);274 this.clock.tick(10);275 test.ok(spy.calledOnce);276 test.done();277 },278 data: function(test) {279 var spy = this.sandbox.spy();280 test.expect(1);281 this.motion.on("data", spy);282 this.clock.tick(25);283 test.ok(spy.calledOnce);284 test.done();285 },286 change: function(test) {287 var spy = this.sandbox.spy();288 var callback = this.analogRead.firstCall.args[1];289 test.expect(1);290 this.motion.on("change", spy);291 callback(1023);292 this.clock.tick(25);293 callback(100);294 this.clock.tick(25);295 callback(1023);296 this.clock.tick(25);297 test.ok(spy.calledTwice);298 test.done();299 },300 noChange: function(test) {301 test.expect(1);302 var spy = this.sandbox.spy();303 var callback = this.analogRead.firstCall.args[1];304 this.motion.on("change", spy);305 callback(1023);306 this.clock.tick(25);307 callback(1023);308 this.clock.tick(25);309 callback(1023);310 this.clock.tick(25);311 callback(1023);312 this.clock.tick(25);313 test.ok(spy.notCalled);314 test.done();315 },316 motionstart: function(test) {317 var spy = this.sandbox.spy();318 var callback = this.analogRead.firstCall.args[1];319 test.expect(1);320 this.motion.on("motionstart", spy);321 callback(100);322 this.clock.tick(25);323 callback(1023);324 this.clock.tick(25);325 test.ok(spy.calledOnce);326 test.done();327 },328 motionend: function(test) {329 // this.clock.tick(250);330 var spy = this.sandbox.spy();331 var callback = this.analogRead.firstCall.args[1];332 test.expect(1);333 this.motion.on("motionend", spy);334 callback(100);335 this.clock.tick(25);336 callback(1023);337 this.clock.tick(25);338 test.ok(spy.calledOnce);339 test.done();340 }341};342exports["Motion - GP2Y0A60SZLF"] = {343 setUp: function(done) {344 this.sandbox = sinon.sandbox.create();345 this.board = newBoard();346 this.clock = this.sandbox.useFakeTimers();347 this.pinMode = this.sandbox.spy(MockFirmata.prototype, "pinMode");348 this.analogRead = this.sandbox.spy(MockFirmata.prototype, "analogRead");349 this.motion = new Motion({350 controller: "GP2Y0A60SZLF",351 pin: "A0",352 board: this.board353 });354 done();355 },356 tearDown: function(done) {357 Board.purge();358 this.sandbox.restore();359 done();360 },361 initialize: function(test) {362 test.expect(2);363 test.ok(this.pinMode.calledOnce);364 test.ok(this.analogRead.calledOnce);365 test.done();366 },367 calibrated: function(test) {368 var spy = this.sandbox.spy();369 test.expect(1);370 this.motion.on("calibrated", spy);371 this.clock.tick(10);372 test.ok(spy.calledOnce);373 test.done();374 },375 data: function(test) {376 var spy = this.sandbox.spy();377 test.expect(1);378 this.motion.on("data", spy);379 this.clock.tick(25);380 test.ok(spy.calledOnce);381 test.done();382 },383 change: function(test) {384 var spy = this.sandbox.spy();385 var callback = this.analogRead.firstCall.args[1];386 test.expect(1);387 this.motion.on("change", spy);388 callback(100);389 this.clock.tick(25);390 callback(1023);391 this.clock.tick(25);392 callback(100);393 this.clock.tick(25);394 test.ok(spy.calledTwice);395 test.done();396 },397 noChange: function(test) {398 test.expect(1);399 var spy = this.sandbox.spy();400 var callback = this.analogRead.firstCall.args[1];401 this.motion.on("change", spy);402 callback(100);403 this.clock.tick(25);404 callback(100);405 this.clock.tick(25);406 callback(100);407 this.clock.tick(25);408 callback(100);409 this.clock.tick(25);410 test.ok(spy.notCalled);411 test.done();412 },413 motionstart: function(test) {414 var spy = this.sandbox.spy();415 var callback = this.analogRead.firstCall.args[1];416 test.expect(1);417 this.motion.on("motionstart", spy);418 callback(1023);419 this.clock.tick(25);420 callback(100);421 this.clock.tick(25);422 test.ok(spy.calledOnce);423 test.done();424 },425 motionend: function(test) {426 // this.clock.tick(250);427 var spy = this.sandbox.spy();428 var callback = this.analogRead.firstCall.args[1];429 test.expect(1);430 this.motion.on("motionend", spy);431 callback(1023);432 this.clock.tick(25);433 callback(100);434 this.clock.tick(25);435 test.ok(spy.calledOnce);436 test.done();437 }438};439Object.keys(Motion.Controllers).forEach(function(name) {440 exports["Motion - Controller, " + name] = addControllerTest(Motion, Motion.Controllers[name], {441 controller: name,...
color.js
Source:color.js
...10 setUp: function(done) {11 this.sandbox = sinon.sandbox.create();12 this.board = newBoard();13 this.clock = this.sandbox.useFakeTimers();14 this.evssetup = this.sandbox.spy(EVS.prototype, "setup");15 this.evsread = this.sandbox.spy(EVS.prototype, "read");16 this.i2cConfig = this.sandbox.spy(MockFirmata.prototype, "i2cConfig");17 this.i2cWrite = this.sandbox.spy(MockFirmata.prototype, "i2cWrite");18 this.i2cRead = this.sandbox.stub(MockFirmata.prototype, "i2cRead", function(address, register, numBytes, callback) {19 callback([0, 0]);20 });21 this.color = new Color({22 controller: "EVS_EV3",23 pin: "BAS1",24 freq: 10,25 board: this.board26 });27 done();28 },29 tearDown: function(done) {30 Board.purge();31 Color.purge();32 this.sandbox.restore();33 done();34 },35 shape: function(test) {36 test.expect(proto.length + instance.length);37 proto.forEach(function(method) {38 test.equal(typeof this.color[method.name], "function");39 }, this);40 instance.forEach(function(property) {41 test.notEqual(typeof this.color[property.name], "undefined");42 }, this);43 test.done();44 },45 data: function(test) {46 var spy = this.sandbox.spy();47 test.expect(1);48 this.color.on("data", spy);49 this.clock.tick(10);50 test.equal(spy.callCount, 1);51 test.done();52 },53 change: function(test) {54 test.expect(1);55 var spy = this.sandbox.spy();56 this.color.on("change", spy);57 this.clock.tick(10);58 test.ok(spy.called);59 test.done();60 },61};62exports["Color - EVS_NXT"] = {63 setUp: function(done) {64 this.sandbox = sinon.sandbox.create();65 this.board = newBoard();66 this.clock = this.sandbox.useFakeTimers();67 this.evssetup = this.sandbox.spy(EVS.prototype, "setup");68 this.evsread = this.sandbox.spy(EVS.prototype, "read");69 this.i2cConfig = this.sandbox.spy(MockFirmata.prototype, "i2cConfig");70 this.i2cWrite = this.sandbox.spy(MockFirmata.prototype, "i2cWrite");71 this.i2cRead = this.sandbox.stub(MockFirmata.prototype, "i2cRead", function(address, register, numBytes, callback) {72 callback([0]);73 });74 this.color = new Color({75 controller: "EVS_NXT",76 pin: "BAS1",77 freq: 10,78 board: this.board79 });80 done();81 },82 tearDown: function(done) {83 Board.purge();84 Color.purge();85 this.sandbox.restore();86 done();87 },88 shape: function(test) {89 test.expect(proto.length + instance.length);90 proto.forEach(function(method) {91 test.equal(typeof this.color[method.name], "function");92 }, this);93 instance.forEach(function(property) {94 test.notEqual(typeof this.color[property.name], "undefined");95 }, this);96 test.done();97 },98 data: function(test) {99 var spy = this.sandbox.spy();100 test.expect(1);101 this.color.on("data", spy);102 this.clock.tick(10);103 test.equal(spy.callCount, 1);104 test.done();105 },106 change: function(test) {107 test.expect(1);108 var spy = this.sandbox.spy();109 this.color.on("change", spy);110 this.clock.tick(10);111 test.ok(spy.called);112 test.done();113 },114};115exports["Color - ISL29125"] = {116 setUp: function(done) {117 this.sandbox = sinon.sandbox.create();118 this.board = newBoard();119 this.clock = this.sandbox.useFakeTimers();120 this.i2cConfig = this.sandbox.spy(MockFirmata.prototype, "i2cConfig");121 this.i2cWrite = this.sandbox.spy(MockFirmata.prototype, "i2cWrite");122 this.i2cRead = this.sandbox.stub(MockFirmata.prototype, "i2cRead", function(address, register, numBytes, callback) {123 callback([0]);124 });125 this.color = new Color({126 controller: "ISL29125",127 freq: 10,128 board: this.board129 });130 done();131 },132 tearDown: function(done) {133 Board.purge();134 Color.purge();135 this.sandbox.restore();136 done();137 },138 shape: function(test) {139 test.expect(proto.length + instance.length);140 proto.forEach(function(method) {141 test.equal(typeof this.color[method.name], "function");142 }, this);143 instance.forEach(function(property) {144 test.notEqual(typeof this.color[property.name], "undefined");145 }, this);146 test.done();147 },148 data: function(test) {149 var spy = this.sandbox.spy();150 test.expect(1);151 this.color.on("data", spy);152 this.clock.tick(10);153 test.equal(spy.callCount, 1);154 test.done();155 },156 change: function(test) {157 test.expect(1);158 var spy = this.sandbox.spy();159 this.color.on("change", spy);160 this.clock.tick(10);161 test.ok(spy.called);162 test.done();163 },164};165Object.keys(Color.Controllers).forEach(function(name) {166 exports["Color - Controller, " + name] = addControllerTest(Color, Color.Controllers[name], {167 controller: name,168 pin: "BAS1"169 });...
Using AI Code Generation
1var sinon = require('sinon');2var spy = sinon.spy();3var obj = {4};5obj.method(42);6var sinon = require('sinon');7var stub = sinon.stub().returns(42);8var obj = {9};10var val = obj.method(42);11var sinon = require('sinon');12var mock = sinon.mock();13var obj = {14};15obj.method(42);16console.log(mock.verify());17var sinon = require('sinon');18var clock = sinon.useFakeTimers();19clock.tick(100);20var sinon = require('sinon');21var server = sinon.fakeServer.create();22server.respondWith("Hello World!");23server.respond();24var sinon = require('sinon');25var requests = sinon.requests;26requests[0].respond(200, { "Content-Type": "text/plain" }, "Hello World!");27var sinon = require('sinon');28var xhr = sinon.useFakeXMLHttpRequest();29xhr.onCreate = function (req) {30 requests.push(req);31};32var sinon = require('sinon');33var spy = sinon.spy();34var obj = {35};36obj.method(42);37console.log(spy
Using AI Code Generation
1const sinon = require('sinon');2const assert = require('assert');3const myObj = {4 myMethod: function () {5 console.log('myMethod called');6 },7};8const spy = sinon.spy(myObj, 'myMethod');9myObj.myMethod();10const sinon = require('sinon');11const assert = require('assert');12const myObj = {13 myMethod: function () {14 console.log('myMethod called');15 },16};17const stub = sinon.stub(myObj, 'myMethod');18myObj.myMethod();19const sinon = require('sinon');20const assert = require('assert');21const myObj = {22 myMethod: function () {23 console.log('myMethod called');24 },25};26const mock = sinon.mock(myObj);27mock.expects('myMethod').once();28myObj.myMethod();29mock.verify();30const sinon = require('sinon');31const assert = require('assert');32const myObj = {33 myMethod: function () {34 console.log('myMethod called');35 },36};37const stub = sinon.stub().returns(42);38sinon.replace(myObj, 'myMethod', stub);39const result = myObj.myMethod();40const sinon = require('sinon');41const assert = require('assert');42const myObj = {43 myMethod: function () {44 console.log('myMethod called');45 },46};47const stub = sinon.createStubInstance(myObj);48stub.myMethod.returns(42);49const result = stub.myMethod();50const sinon = require('sinon');51const assert = require('assert');52const clock = sinon.useFakeTimers();53setTimeout(function () {54 console.log('timeout called');55}, 1000);56const sinon = require('sinon');57const assert = require('assert');58const requests = [];
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('chai').assert;3describe('test suite', function() {4 var obj = {5 method: function() {6 return 1;7 }8 };9 beforeEach(function() {10 this.sandbox = sinon.sandbox.create();11 });12 afterEach(function() {13 this.sandbox.restore();14 });15 it('should call method', function() {16 var spy = this.sandbox.spy(obj, 'method');17 obj.method();18 assert(spy.called);19 });20});21var sinon = require('sinon');22var assert = require('assert');23var foo = {24 bar: function() {25 return 1;26 }27};28var test = function() {29 var stub = sinon.stub(foo, 'bar');30 stub.returns(2);31 var result = foo.bar();32 assert.equal(result, 2);33};34test();35var sinon = require('sinon');36var assert = require('assert');37var foo = {38 bar: function(callback) {39 callback();40 }41};42var test = function() {43 var spy = sinon.spy(foo, 'bar');44 foo.bar(function() {45 assert(spy.called);46 });47};48test();49var sinon = require('sinon');50var assert = require('assert');51var foo = {52 bar: function(callback) {
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('assert');3describe('Sinon spy test', function() {4 it('should return true', function() {5 var obj = {6 foo: function() {7 return true;8 }9 };10 var spy = sinon.spy(obj, 'foo');11 obj.foo();12 assert(spy.called);13 });14});15var sinon = require('sinon');16var assert = require('assert');17describe('Sinon spy test', function() {18 it('should return true', function() {19 var obj = {20 foo: function() {21 return true;22 }23 };24 var spy = sinon.spy(obj, 'foo');25 obj.foo();26 assert(spy.called);27 });28});29var sinon = require('sinon');30var assert = require('assert');31describe('Sinon spy test', function() {32 it('should return true', function() {33 var obj = {34 foo: function() {35 return true;36 }37 };38 var spy = sinon.spy(obj, 'foo');39 obj.foo();40 assert(spy.called);41 });42});43var sinon = require('sinon');44var assert = require('assert');45describe('Sinon spy test', function() {46 it('should return true', function() {47 var obj = {48 foo: function() {49 return true;50 }51 };52 var spy = sinon.spy(obj, 'foo');53 obj.foo();54 assert(spy.called);55 });56});
Using AI Code Generation
1var sinon = require('sinon');2var sandbox = sinon.sandbox.create();3describe('Test', function () {4 beforeEach(function () {5 sandbox.stub(global, 'setTimeout');6 });7 afterEach(function () {8 sandbox.restore();9 });10 it('should call setTimeout with 1000ms', function () {11 setTimeout(1000);12 sinon.assert.calledWith(setTimeout, 1000);13 });14});15var sinon = require('sinon');16var sandbox = sinon.sandbox.create();17describe('Test', function () {18 beforeEach(function () {19 sandbox.stub(global, 'setTimeout');20 });21 afterEach(function () {22 sandbox.restore();23 });24 it('should call setTimeout with 1000ms', function () {25 setTimeout(1000);26 sinon.assert.calledWith(setTimeout, 1000);27 });28});29var sinon = require('sinon');30var sandbox = sinon.sandbox.create();31describe('Test', function () {32 beforeEach(function () {33 sandbox.stub(global, 'setTimeout');34 });35 afterEach(function () {36 sandbox.restore();37 });38 it('should call setTimeout with 1000ms', function () {39 setTimeout(1000);40 sinon.assert.calledWith(setTimeout, 1000);41 });42});43var sinon = require('sinon');44var sandbox = sinon.sandbox.create();45describe('Test', function () {46 beforeEach(function () {47 sandbox.stub(global, 'setTimeout');48 });49 afterEach(function () {50 sandbox.restore();51 });52 it('should call setTimeout with 1000ms', function () {53 setTimeout(1000);54 sinon.assert.calledWith(setTimeout, 1000);55 });56});57var sinon = require('sinon');58var sandbox = sinon.sandbox.create();59describe('Test', function () {60 beforeEach(function () {61 sandbox.stub(global, 'setTimeout');62 });63 afterEach(function () {64 sandbox.restore();65 });66 it('should call setTimeout with 1000ms', function () {67 setTimeout(1000);68 sinon.assert.calledWith(setTimeout, 1000
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!!