How to use assertDecreases method in chai

Best JavaScript code snippet using chai

assertions.js

Source:assertions.js Github

copy

Full Screen

1/*!2 * chai3 * http://chaijs.com4 * Copyright(c) 2011-2014 Jake Luer <jake@alogicalparadox.com>5 * MIT Licensed6 */7module.exports = function (chai, _) {8 var Assertion = chai.Assertion9 , toString = Object.prototype.toString10 , flag = _.flag;11 /**12 * ### Language Chains13 *14 * The following are provided as chainable getters to15 * improve the readability of your assertions. They16 * do not provide testing capabilities unless they17 * have been overwritten by a plugin.18 *19 * **Chains**20 *21 * - to22 * - be23 * - been24 * - is25 * - that26 * - which27 * - and28 * - has29 * - have30 * - with31 * - at32 * - of33 * - same34 *35 * @name language chains36 * @api public37 */38 [ 'to', 'be', 'been'39 , 'is', 'and', 'has', 'have'40 , 'with', 'that', 'which', 'at'41 , 'of', 'same' ].forEach(function (chain) {42 Assertion.addProperty(chain, function () {43 return this;44 });45 });46 /**47 * ### .not48 *49 * Negates any of assertions following in the chain.50 *51 * expect(foo).to.not.equal('bar');52 * expect(goodFn).to.not.throw(Error);53 * expect({ foo: 'baz' }).to.have.property('foo')54 * .and.not.equal('bar');55 *56 * @name not57 * @api public58 */59 Assertion.addProperty('not', function () {60 flag(this, 'negate', true);61 });62 /**63 * ### .deep64 *65 * Sets the `deep` flag, later used by the `equal` and66 * `property` assertions.67 *68 * expect(foo).to.deep.equal({ bar: 'baz' });69 * expect({ foo: { bar: { baz: 'quux' } } })70 * .to.have.deep.property('foo.bar.baz', 'quux');71 *72 * @name deep73 * @api public74 */75 Assertion.addProperty('deep', function () {76 flag(this, 'deep', true);77 });78 /**79 * ### .any80 *81 * Sets the `any` flag, (opposite of the `all` flag)82 * later used in the `keys` assertion. 83 *84 * expect(foo).to.have.any.keys('bar', 'baz');85 *86 * @name any87 * @api public88 */89 Assertion.addProperty('any', function () {90 flag(this, 'any', true);91 flag(this, 'all', false)92 });93 /**94 * ### .all95 *96 * Sets the `all` flag (opposite of the `any` flag) 97 * later used by the `keys` assertion.98 *99 * expect(foo).to.have.all.keys('bar', 'baz');100 *101 * @name all102 * @api public103 */104 Assertion.addProperty('all', function () {105 flag(this, 'all', true);106 flag(this, 'any', false);107 });108 /**109 * ### .a(type)110 *111 * The `a` and `an` assertions are aliases that can be112 * used either as language chains or to assert a value's113 * type.114 *115 * // typeof116 * expect('test').to.be.a('string');117 * expect({ foo: 'bar' }).to.be.an('object');118 * expect(null).to.be.a('null');119 * expect(undefined).to.be.an('undefined');120 *121 * // language chain122 * expect(foo).to.be.an.instanceof(Foo);123 *124 * @name a125 * @alias an126 * @param {String} type127 * @param {String} message _optional_128 * @api public129 */130 function an (type, msg) {131 if (msg) flag(this, 'message', msg);132 type = type.toLowerCase();133 var obj = flag(this, 'object')134 , article = ~[ 'a', 'e', 'i', 'o', 'u' ].indexOf(type.charAt(0)) ? 'an ' : 'a ';135 this.assert(136 type === _.type(obj)137 , 'expected #{this} to be ' + article + type138 , 'expected #{this} not to be ' + article + type139 );140 }141 Assertion.addChainableMethod('an', an);142 Assertion.addChainableMethod('a', an);143 /**144 * ### .include(value)145 *146 * The `include` and `contain` assertions can be used as either property147 * based language chains or as methods to assert the inclusion of an object148 * in an array or a substring in a string. When used as language chains,149 * they toggle the `contains` flag for the `keys` assertion.150 *151 * expect([1,2,3]).to.include(2);152 * expect('foobar').to.contain('foo');153 * expect({ foo: 'bar', hello: 'universe' }).to.include.keys('foo');154 *155 * @name include156 * @alias contain157 * @alias includes158 * @alias contains159 * @param {Object|String|Number} obj160 * @param {String} message _optional_161 * @api public162 */163 function includeChainingBehavior () {164 flag(this, 'contains', true);165 }166 function include (val, msg) {167 if (msg) flag(this, 'message', msg);168 var obj = flag(this, 'object');169 var expected = false;170 if (_.type(obj) === 'array' && _.type(val) === 'object') {171 for (var i in obj) {172 if (_.eql(obj[i], val)) {173 expected = true;174 break;175 }176 }177 } else if (_.type(val) === 'object') {178 if (!flag(this, 'negate')) {179 for (var k in val) new Assertion(obj).property(k, val[k]);180 return;181 }182 var subset = {};183 for (var k in val) subset[k] = obj[k];184 expected = _.eql(subset, val);185 } else {186 expected = obj && ~obj.indexOf(val);187 }188 this.assert(189 expected190 , 'expected #{this} to include ' + _.inspect(val)191 , 'expected #{this} to not include ' + _.inspect(val));192 }193 Assertion.addChainableMethod('include', include, includeChainingBehavior);194 Assertion.addChainableMethod('contain', include, includeChainingBehavior);195 Assertion.addChainableMethod('contains', include, includeChainingBehavior);196 Assertion.addChainableMethod('includes', include, includeChainingBehavior);197 /**198 * ### .ok199 *200 * Asserts that the target is truthy.201 *202 * expect('everthing').to.be.ok;203 * expect(1).to.be.ok;204 * expect(false).to.not.be.ok;205 * expect(undefined).to.not.be.ok;206 * expect(null).to.not.be.ok;207 *208 * @name ok209 * @api public210 */211 Assertion.addProperty('ok', function () {212 this.assert(213 flag(this, 'object')214 , 'expected #{this} to be truthy'215 , 'expected #{this} to be falsy');216 });217 /**218 * ### .true219 *220 * Asserts that the target is `true`.221 *222 * expect(true).to.be.true;223 * expect(1).to.not.be.true;224 *225 * @name true226 * @api public227 */228 Assertion.addProperty('true', function () {229 this.assert(230 true === flag(this, 'object')231 , 'expected #{this} to be true'232 , 'expected #{this} to be false'233 , this.negate ? false : true234 );235 });236 /**237 * ### .false238 *239 * Asserts that the target is `false`.240 *241 * expect(false).to.be.false;242 * expect(0).to.not.be.false;243 *244 * @name false245 * @api public246 */247 Assertion.addProperty('false', function () {248 this.assert(249 false === flag(this, 'object')250 , 'expected #{this} to be false'251 , 'expected #{this} to be true'252 , this.negate ? true : false253 );254 });255 /**256 * ### .null257 *258 * Asserts that the target is `null`.259 *260 * expect(null).to.be.null;261 * expect(undefined).not.to.be.null;262 *263 * @name null264 * @api public265 */266 Assertion.addProperty('null', function () {267 this.assert(268 null === flag(this, 'object')269 , 'expected #{this} to be null'270 , 'expected #{this} not to be null'271 );272 });273 /**274 * ### .undefined275 *276 * Asserts that the target is `undefined`.277 *278 * expect(undefined).to.be.undefined;279 * expect(null).to.not.be.undefined;280 *281 * @name undefined282 * @api public283 */284 Assertion.addProperty('undefined', function () {285 this.assert(286 undefined === flag(this, 'object')287 , 'expected #{this} to be undefined'288 , 'expected #{this} not to be undefined'289 );290 });291 /**292 * ### .exist293 *294 * Asserts that the target is neither `null` nor `undefined`.295 *296 * var foo = 'hi'297 * , bar = null298 * , baz;299 *300 * expect(foo).to.exist;301 * expect(bar).to.not.exist;302 * expect(baz).to.not.exist;303 *304 * @name exist305 * @api public306 */307 Assertion.addProperty('exist', function () {308 this.assert(309 null != flag(this, 'object')310 , 'expected #{this} to exist'311 , 'expected #{this} to not exist'312 );313 });314 /**315 * ### .empty316 *317 * Asserts that the target's length is `0`. For arrays and strings, it checks318 * the `length` property. For objects, it gets the count of319 * enumerable keys.320 *321 * expect([]).to.be.empty;322 * expect('').to.be.empty;323 * expect({}).to.be.empty;324 *325 * @name empty326 * @api public327 */328 Assertion.addProperty('empty', function () {329 var obj = flag(this, 'object')330 , expected = obj;331 if (Array.isArray(obj) || 'string' === typeof object) {332 expected = obj.length;333 } else if (typeof obj === 'object') {334 expected = Object.keys(obj).length;335 }336 this.assert(337 !expected338 , 'expected #{this} to be empty'339 , 'expected #{this} not to be empty'340 );341 });342 /**343 * ### .arguments344 *345 * Asserts that the target is an arguments object.346 *347 * function test () {348 * expect(arguments).to.be.arguments;349 * }350 *351 * @name arguments352 * @alias Arguments353 * @api public354 */355 function checkArguments () {356 var obj = flag(this, 'object')357 , type = Object.prototype.toString.call(obj);358 this.assert(359 '[object Arguments]' === type360 , 'expected #{this} to be arguments but got ' + type361 , 'expected #{this} to not be arguments'362 );363 }364 Assertion.addProperty('arguments', checkArguments);365 Assertion.addProperty('Arguments', checkArguments);366 /**367 * ### .equal(value)368 *369 * Asserts that the target is strictly equal (`===`) to `value`.370 * Alternately, if the `deep` flag is set, asserts that371 * the target is deeply equal to `value`.372 *373 * expect('hello').to.equal('hello');374 * expect(42).to.equal(42);375 * expect(1).to.not.equal(true);376 * expect({ foo: 'bar' }).to.not.equal({ foo: 'bar' });377 * expect({ foo: 'bar' }).to.deep.equal({ foo: 'bar' });378 *379 * @name equal380 * @alias equals381 * @alias eq382 * @alias deep.equal383 * @param {Mixed} value384 * @param {String} message _optional_385 * @api public386 */387 function assertEqual (val, msg) {388 if (msg) flag(this, 'message', msg);389 var obj = flag(this, 'object');390 if (flag(this, 'deep')) {391 return this.eql(val);392 } else {393 this.assert(394 val === obj395 , 'expected #{this} to equal #{exp}'396 , 'expected #{this} to not equal #{exp}'397 , val398 , this._obj399 , true400 );401 }402 }403 Assertion.addMethod('equal', assertEqual);404 Assertion.addMethod('equals', assertEqual);405 Assertion.addMethod('eq', assertEqual);406 /**407 * ### .eql(value)408 *409 * Asserts that the target is deeply equal to `value`.410 *411 * expect({ foo: 'bar' }).to.eql({ foo: 'bar' });412 * expect([ 1, 2, 3 ]).to.eql([ 1, 2, 3 ]);413 *414 * @name eql415 * @alias eqls416 * @param {Mixed} value417 * @param {String} message _optional_418 * @api public419 */420 function assertEql(obj, msg) {421 if (msg) flag(this, 'message', msg);422 this.assert(423 _.eql(obj, flag(this, 'object'))424 , 'expected #{this} to deeply equal #{exp}'425 , 'expected #{this} to not deeply equal #{exp}'426 , obj427 , this._obj428 , true429 );430 }431 Assertion.addMethod('eql', assertEql);432 Assertion.addMethod('eqls', assertEql);433 /**434 * ### .above(value)435 *436 * Asserts that the target is greater than `value`.437 *438 * expect(10).to.be.above(5);439 *440 * Can also be used in conjunction with `length` to441 * assert a minimum length. The benefit being a442 * more informative error message than if the length443 * was supplied directly.444 *445 * expect('foo').to.have.length.above(2);446 * expect([ 1, 2, 3 ]).to.have.length.above(2);447 *448 * @name above449 * @alias gt450 * @alias greaterThan451 * @param {Number} value452 * @param {String} message _optional_453 * @api public454 */455 function assertAbove (n, msg) {456 if (msg) flag(this, 'message', msg);457 var obj = flag(this, 'object');458 if (flag(this, 'doLength')) {459 new Assertion(obj, msg).to.have.property('length');460 var len = obj.length;461 this.assert(462 len > n463 , 'expected #{this} to have a length above #{exp} but got #{act}'464 , 'expected #{this} to not have a length above #{exp}'465 , n466 , len467 );468 } else {469 this.assert(470 obj > n471 , 'expected #{this} to be above ' + n472 , 'expected #{this} to be at most ' + n473 );474 }475 }476 Assertion.addMethod('above', assertAbove);477 Assertion.addMethod('gt', assertAbove);478 Assertion.addMethod('greaterThan', assertAbove);479 /**480 * ### .least(value)481 *482 * Asserts that the target is greater than or equal to `value`.483 *484 * expect(10).to.be.at.least(10);485 *486 * Can also be used in conjunction with `length` to487 * assert a minimum length. The benefit being a488 * more informative error message than if the length489 * was supplied directly.490 *491 * expect('foo').to.have.length.of.at.least(2);492 * expect([ 1, 2, 3 ]).to.have.length.of.at.least(3);493 *494 * @name least495 * @alias gte496 * @param {Number} value497 * @param {String} message _optional_498 * @api public499 */500 function assertLeast (n, msg) {501 if (msg) flag(this, 'message', msg);502 var obj = flag(this, 'object');503 if (flag(this, 'doLength')) {504 new Assertion(obj, msg).to.have.property('length');505 var len = obj.length;506 this.assert(507 len >= n508 , 'expected #{this} to have a length at least #{exp} but got #{act}'509 , 'expected #{this} to have a length below #{exp}'510 , n511 , len512 );513 } else {514 this.assert(515 obj >= n516 , 'expected #{this} to be at least ' + n517 , 'expected #{this} to be below ' + n518 );519 }520 }521 Assertion.addMethod('least', assertLeast);522 Assertion.addMethod('gte', assertLeast);523 /**524 * ### .below(value)525 *526 * Asserts that the target is less than `value`.527 *528 * expect(5).to.be.below(10);529 *530 * Can also be used in conjunction with `length` to531 * assert a maximum length. The benefit being a532 * more informative error message than if the length533 * was supplied directly.534 *535 * expect('foo').to.have.length.below(4);536 * expect([ 1, 2, 3 ]).to.have.length.below(4);537 *538 * @name below539 * @alias lt540 * @alias lessThan541 * @param {Number} value542 * @param {String} message _optional_543 * @api public544 */545 function assertBelow (n, msg) {546 if (msg) flag(this, 'message', msg);547 var obj = flag(this, 'object');548 if (flag(this, 'doLength')) {549 new Assertion(obj, msg).to.have.property('length');550 var len = obj.length;551 this.assert(552 len < n553 , 'expected #{this} to have a length below #{exp} but got #{act}'554 , 'expected #{this} to not have a length below #{exp}'555 , n556 , len557 );558 } else {559 this.assert(560 obj < n561 , 'expected #{this} to be below ' + n562 , 'expected #{this} to be at least ' + n563 );564 }565 }566 Assertion.addMethod('below', assertBelow);567 Assertion.addMethod('lt', assertBelow);568 Assertion.addMethod('lessThan', assertBelow);569 /**570 * ### .most(value)571 *572 * Asserts that the target is less than or equal to `value`.573 *574 * expect(5).to.be.at.most(5);575 *576 * Can also be used in conjunction with `length` to577 * assert a maximum length. The benefit being a578 * more informative error message than if the length579 * was supplied directly.580 *581 * expect('foo').to.have.length.of.at.most(4);582 * expect([ 1, 2, 3 ]).to.have.length.of.at.most(3);583 *584 * @name most585 * @alias lte586 * @param {Number} value587 * @param {String} message _optional_588 * @api public589 */590 function assertMost (n, msg) {591 if (msg) flag(this, 'message', msg);592 var obj = flag(this, 'object');593 if (flag(this, 'doLength')) {594 new Assertion(obj, msg).to.have.property('length');595 var len = obj.length;596 this.assert(597 len <= n598 , 'expected #{this} to have a length at most #{exp} but got #{act}'599 , 'expected #{this} to have a length above #{exp}'600 , n601 , len602 );603 } else {604 this.assert(605 obj <= n606 , 'expected #{this} to be at most ' + n607 , 'expected #{this} to be above ' + n608 );609 }610 }611 Assertion.addMethod('most', assertMost);612 Assertion.addMethod('lte', assertMost);613 /**614 * ### .within(start, finish)615 *616 * Asserts that the target is within a range.617 *618 * expect(7).to.be.within(5,10);619 *620 * Can also be used in conjunction with `length` to621 * assert a length range. The benefit being a622 * more informative error message than if the length623 * was supplied directly.624 *625 * expect('foo').to.have.length.within(2,4);626 * expect([ 1, 2, 3 ]).to.have.length.within(2,4);627 *628 * @name within629 * @param {Number} start lowerbound inclusive630 * @param {Number} finish upperbound inclusive631 * @param {String} message _optional_632 * @api public633 */634 Assertion.addMethod('within', function (start, finish, msg) {635 if (msg) flag(this, 'message', msg);636 var obj = flag(this, 'object')637 , range = start + '..' + finish;638 if (flag(this, 'doLength')) {639 new Assertion(obj, msg).to.have.property('length');640 var len = obj.length;641 this.assert(642 len >= start && len <= finish643 , 'expected #{this} to have a length within ' + range644 , 'expected #{this} to not have a length within ' + range645 );646 } else {647 this.assert(648 obj >= start && obj <= finish649 , 'expected #{this} to be within ' + range650 , 'expected #{this} to not be within ' + range651 );652 }653 });654 /**655 * ### .instanceof(constructor)656 *657 * Asserts that the target is an instance of `constructor`.658 *659 * var Tea = function (name) { this.name = name; }660 * , Chai = new Tea('chai');661 *662 * expect(Chai).to.be.an.instanceof(Tea);663 * expect([ 1, 2, 3 ]).to.be.instanceof(Array);664 *665 * @name instanceof666 * @param {Constructor} constructor667 * @param {String} message _optional_668 * @alias instanceOf669 * @api public670 */671 function assertInstanceOf (constructor, msg) {672 if (msg) flag(this, 'message', msg);673 var name = _.getName(constructor);674 this.assert(675 flag(this, 'object') instanceof constructor676 , 'expected #{this} to be an instance of ' + name677 , 'expected #{this} to not be an instance of ' + name678 );679 };680 Assertion.addMethod('instanceof', assertInstanceOf);681 Assertion.addMethod('instanceOf', assertInstanceOf);682 /**683 * ### .property(name, [value])684 *685 * Asserts that the target has a property `name`, optionally asserting that686 * the value of that property is strictly equal to `value`.687 * If the `deep` flag is set, you can use dot- and bracket-notation for deep688 * references into objects and arrays.689 *690 * // simple referencing691 * var obj = { foo: 'bar' };692 * expect(obj).to.have.property('foo');693 * expect(obj).to.have.property('foo', 'bar');694 *695 * // deep referencing696 * var deepObj = {697 * green: { tea: 'matcha' }698 * , teas: [ 'chai', 'matcha', { tea: 'konacha' } ]699 * };700 * expect(deepObj).to.have.deep.property('green.tea', 'matcha');701 * expect(deepObj).to.have.deep.property('teas[1]', 'matcha');702 * expect(deepObj).to.have.deep.property('teas[2].tea', 'konacha');703 *704 * You can also use an array as the starting point of a `deep.property`705 * assertion, or traverse nested arrays.706 *707 * var arr = [708 * [ 'chai', 'matcha', 'konacha' ]709 * , [ { tea: 'chai' }710 * , { tea: 'matcha' }711 * , { tea: 'konacha' } ]712 * ];713 *714 * expect(arr).to.have.deep.property('[0][1]', 'matcha');715 * expect(arr).to.have.deep.property('[1][2].tea', 'konacha');716 *717 * Furthermore, `property` changes the subject of the assertion718 * to be the value of that property from the original object. This719 * permits for further chainable assertions on that property.720 *721 * expect(obj).to.have.property('foo')722 * .that.is.a('string');723 * expect(deepObj).to.have.property('green')724 * .that.is.an('object')725 * .that.deep.equals({ tea: 'matcha' });726 * expect(deepObj).to.have.property('teas')727 * .that.is.an('array')728 * .with.deep.property('[2]')729 * .that.deep.equals({ tea: 'konacha' });730 *731 * @name property732 * @alias deep.property733 * @param {String} name734 * @param {Mixed} value (optional)735 * @param {String} message _optional_736 * @returns value of property for chaining737 * @api public738 */739 Assertion.addMethod('property', function (name, val, msg) {740 if (msg) flag(this, 'message', msg);741 var isDeep = !!flag(this, 'deep')742 , descriptor = isDeep ? 'deep property ' : 'property '743 , negate = flag(this, 'negate')744 , obj = flag(this, 'object')745 , pathInfo = isDeep ? _.getPathInfo(name, obj) : null746 , hasProperty = isDeep747 ? pathInfo.exists748 : _.hasProperty(name, obj)749 , value = isDeep750 ? pathInfo.value751 : obj[name];752 if (negate && undefined !== val) {753 if (undefined === value) {754 msg = (msg != null) ? msg + ': ' : '';755 throw new Error(msg + _.inspect(obj) + ' has no ' + descriptor + _.inspect(name));756 }757 } else {758 this.assert(759 hasProperty760 , 'expected #{this} to have a ' + descriptor + _.inspect(name)761 , 'expected #{this} to not have ' + descriptor + _.inspect(name));762 }763 if (undefined !== val) {764 this.assert(765 val === value766 , 'expected #{this} to have a ' + descriptor + _.inspect(name) + ' of #{exp}, but got #{act}'767 , 'expected #{this} to not have a ' + descriptor + _.inspect(name) + ' of #{act}'768 , val769 , value770 );771 }772 flag(this, 'object', value);773 });774 /**775 * ### .ownProperty(name)776 *777 * Asserts that the target has an own property `name`.778 *779 * expect('test').to.have.ownProperty('length');780 *781 * @name ownProperty782 * @alias haveOwnProperty783 * @param {String} name784 * @param {String} message _optional_785 * @api public786 */787 function assertOwnProperty (name, msg) {788 if (msg) flag(this, 'message', msg);789 var obj = flag(this, 'object');790 this.assert(791 obj.hasOwnProperty(name)792 , 'expected #{this} to have own property ' + _.inspect(name)793 , 'expected #{this} to not have own property ' + _.inspect(name)794 );795 }796 Assertion.addMethod('ownProperty', assertOwnProperty);797 Assertion.addMethod('haveOwnProperty', assertOwnProperty);798 /**799 * ### .length(value)800 *801 * Asserts that the target's `length` property has802 * the expected value.803 *804 * expect([ 1, 2, 3]).to.have.length(3);805 * expect('foobar').to.have.length(6);806 *807 * Can also be used as a chain precursor to a value808 * comparison for the length property.809 *810 * expect('foo').to.have.length.above(2);811 * expect([ 1, 2, 3 ]).to.have.length.above(2);812 * expect('foo').to.have.length.below(4);813 * expect([ 1, 2, 3 ]).to.have.length.below(4);814 * expect('foo').to.have.length.within(2,4);815 * expect([ 1, 2, 3 ]).to.have.length.within(2,4);816 *817 * @name length818 * @alias lengthOf819 * @param {Number} length820 * @param {String} message _optional_821 * @api public822 */823 function assertLengthChain () {824 flag(this, 'doLength', true);825 }826 function assertLength (n, msg) {827 if (msg) flag(this, 'message', msg);828 var obj = flag(this, 'object');829 new Assertion(obj, msg).to.have.property('length');830 var len = obj.length;831 this.assert(832 len == n833 , 'expected #{this} to have a length of #{exp} but got #{act}'834 , 'expected #{this} to not have a length of #{act}'835 , n836 , len837 );838 }839 Assertion.addChainableMethod('length', assertLength, assertLengthChain);840 Assertion.addMethod('lengthOf', assertLength);841 /**842 * ### .match(regexp)843 *844 * Asserts that the target matches a regular expression.845 *846 * expect('foobar').to.match(/^foo/);847 *848 * @name match849 * @param {RegExp} RegularExpression850 * @param {String} message _optional_851 * @api public852 */853 Assertion.addMethod('match', function (re, msg) {854 if (msg) flag(this, 'message', msg);855 var obj = flag(this, 'object');856 this.assert(857 re.exec(obj)858 , 'expected #{this} to match ' + re859 , 'expected #{this} not to match ' + re860 );861 });862 /**863 * ### .string(string)864 *865 * Asserts that the string target contains another string.866 *867 * expect('foobar').to.have.string('bar');868 *869 * @name string870 * @param {String} string871 * @param {String} message _optional_872 * @api public873 */874 Assertion.addMethod('string', function (str, msg) {875 if (msg) flag(this, 'message', msg);876 var obj = flag(this, 'object');877 new Assertion(obj, msg).is.a('string');878 this.assert(879 ~obj.indexOf(str)880 , 'expected #{this} to contain ' + _.inspect(str)881 , 'expected #{this} to not contain ' + _.inspect(str)882 );883 });884 /**885 * ### .keys(key1, [key2], [...])886 *887 * Asserts that the target contains any or all of the passed-in keys.888 * Use in combination with `any`, `all`, `contains`, or `have` will affect 889 * what will pass.890 * 891 * When used in conjunction with `any`, at least one key that is passed 892 * in must exist in the target object. This is regardless whether or not 893 * the `have` or `contain` qualifiers are used. Note, either `any` or `all`894 * should be used in the assertion. If neither are used, the assertion is895 * defaulted to `all`.896 * 897 * When both `all` and `contain` are used, the target object must have at 898 * least all of the passed-in keys but may have more keys not listed.899 * 900 * When both `all` and `have` are used, the target object must both contain901 * all of the passed-in keys AND the number of keys in the target object must902 * match the number of keys passed in (in other words, a target object must 903 * have all and only all of the passed-in keys).904 * 905 * expect({ foo: 1, bar: 2 }).to.have.any.keys('foo', 'baz');906 * expect({ foo: 1, bar: 2 }).to.have.any.keys('foo');907 * expect({ foo: 1, bar: 2 }).to.contain.any.keys('bar', 'baz');908 * expect({ foo: 1, bar: 2 }).to.contain.any.keys(['foo']);909 * expect({ foo: 1, bar: 2 }).to.contain.any.keys({'foo': 6});910 * expect({ foo: 1, bar: 2 }).to.have.all.keys(['bar', 'foo']);911 * expect({ foo: 1, bar: 2 }).to.have.all.keys({'bar': 6, 'foo', 7});912 * expect({ foo: 1, bar: 2, baz: 3 }).to.contain.all.keys(['bar', 'foo']);913 * expect({ foo: 1, bar: 2, baz: 3 }).to.contain.all.keys([{'bar': 6}}]);914 *915 *916 * @name keys917 * @alias key918 * @param {String...|Array|Object} keys919 * @api public920 */921 function assertKeys (keys) {922 var obj = flag(this, 'object')923 , str924 , ok = true925 , mixedArgsMsg = 'keys must be given single argument of Array|Object|String, or multiple String arguments';926 switch (_.type(keys)) {927 case "array":928 if (arguments.length > 1) throw (new Error(mixedArgsMsg));929 break;930 case "object":931 if (arguments.length > 1) throw (new Error(mixedArgsMsg));932 keys = Object.keys(keys);933 break;934 default:935 keys = Array.prototype.slice.call(arguments);936 }937 if (!keys.length) throw new Error('keys required');938 var actual = Object.keys(obj)939 , expected = keys940 , len = keys.length941 , any = flag(this, 'any')942 , all = flag(this, 'all');943 if (!any && !all) {944 all = true;945 }946 // Has any947 if (any) {948 var intersection = expected.filter(function(key) {949 return ~actual.indexOf(key);950 });951 ok = intersection.length > 0;952 }953 // Has all954 if (all) {955 ok = keys.every(function(key){956 return ~actual.indexOf(key);957 });958 if (!flag(this, 'negate') && !flag(this, 'contains')) {959 ok = ok && keys.length == actual.length;960 }961 }962 // Key string963 if (len > 1) {964 keys = keys.map(function(key){965 return _.inspect(key);966 });967 var last = keys.pop();968 if (all) {969 str = keys.join(', ') + ', and ' + last;970 }971 if (any) {972 str = keys.join(', ') + ', or ' + last;973 }974 } else {975 str = _.inspect(keys[0]);976 }977 // Form978 str = (len > 1 ? 'keys ' : 'key ') + str;979 // Have / include980 str = (flag(this, 'contains') ? 'contain ' : 'have ') + str;981 // Assertion982 this.assert(983 ok984 , 'expected #{this} to ' + str985 , 'expected #{this} to not ' + str986 , expected.slice(0).sort()987 , actual.sort()988 , true989 );990 }991 Assertion.addMethod('keys', assertKeys);992 Assertion.addMethod('key', assertKeys);993 /**994 * ### .throw(constructor)995 *996 * Asserts that the function target will throw a specific error, or specific type of error997 * (as determined using `instanceof`), optionally with a RegExp or string inclusion test998 * for the error's message.999 *1000 * var err = new ReferenceError('This is a bad function.');1001 * var fn = function () { throw err; }1002 * expect(fn).to.throw(ReferenceError);1003 * expect(fn).to.throw(Error);1004 * expect(fn).to.throw(/bad function/);1005 * expect(fn).to.not.throw('good function');1006 * expect(fn).to.throw(ReferenceError, /bad function/);1007 * expect(fn).to.throw(err);1008 * expect(fn).to.not.throw(new RangeError('Out of range.'));1009 *1010 * Please note that when a throw expectation is negated, it will check each1011 * parameter independently, starting with error constructor type. The appropriate way1012 * to check for the existence of a type of error but for a message that does not match1013 * is to use `and`.1014 *1015 * expect(fn).to.throw(ReferenceError)1016 * .and.not.throw(/good function/);1017 *1018 * @name throw1019 * @alias throws1020 * @alias Throw1021 * @param {ErrorConstructor} constructor1022 * @param {String|RegExp} expected error message1023 * @param {String} message _optional_1024 * @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error#Error_types1025 * @returns error for chaining (null if no error)1026 * @api public1027 */1028 function assertThrows (constructor, errMsg, msg) {1029 if (msg) flag(this, 'message', msg);1030 var obj = flag(this, 'object');1031 new Assertion(obj, msg).is.a('function');1032 var thrown = false1033 , desiredError = null1034 , name = null1035 , thrownError = null;1036 if (arguments.length === 0) {1037 errMsg = null;1038 constructor = null;1039 } else if (constructor && (constructor instanceof RegExp || 'string' === typeof constructor)) {1040 errMsg = constructor;1041 constructor = null;1042 } else if (constructor && constructor instanceof Error) {1043 desiredError = constructor;1044 constructor = null;1045 errMsg = null;1046 } else if (typeof constructor === 'function') {1047 name = constructor.prototype.name || constructor.name;1048 if (name === 'Error' && constructor !== Error) {1049 name = (new constructor()).name;1050 }1051 } else {1052 constructor = null;1053 }1054 try {1055 obj();1056 } catch (err) {1057 // first, check desired error1058 if (desiredError) {1059 this.assert(1060 err === desiredError1061 , 'expected #{this} to throw #{exp} but #{act} was thrown'1062 , 'expected #{this} to not throw #{exp}'1063 , (desiredError instanceof Error ? desiredError.toString() : desiredError)1064 , (err instanceof Error ? err.toString() : err)1065 );1066 flag(this, 'object', err);1067 return this;1068 }1069 // next, check constructor1070 if (constructor) {1071 this.assert(1072 err instanceof constructor1073 , 'expected #{this} to throw #{exp} but #{act} was thrown'1074 , 'expected #{this} to not throw #{exp} but #{act} was thrown'1075 , name1076 , (err instanceof Error ? err.toString() : err)1077 );1078 if (!errMsg) {1079 flag(this, 'object', err);1080 return this;1081 }1082 }1083 // next, check message1084 var message = 'object' === _.type(err) && "message" in err1085 ? err.message1086 : '' + err;1087 if ((message != null) && errMsg && errMsg instanceof RegExp) {1088 this.assert(1089 errMsg.exec(message)1090 , 'expected #{this} to throw error matching #{exp} but got #{act}'1091 , 'expected #{this} to throw error not matching #{exp}'1092 , errMsg1093 , message1094 );1095 flag(this, 'object', err);1096 return this;1097 } else if ((message != null) && errMsg && 'string' === typeof errMsg) {1098 this.assert(1099 ~message.indexOf(errMsg)1100 , 'expected #{this} to throw error including #{exp} but got #{act}'1101 , 'expected #{this} to throw error not including #{act}'1102 , errMsg1103 , message1104 );1105 flag(this, 'object', err);1106 return this;1107 } else {1108 thrown = true;1109 thrownError = err;1110 }1111 }1112 var actuallyGot = ''1113 , expectedThrown = name !== null1114 ? name1115 : desiredError1116 ? '#{exp}' //_.inspect(desiredError)1117 : 'an error';1118 if (thrown) {1119 actuallyGot = ' but #{act} was thrown'1120 }1121 this.assert(1122 thrown === true1123 , 'expected #{this} to throw ' + expectedThrown + actuallyGot1124 , 'expected #{this} to not throw ' + expectedThrown + actuallyGot1125 , (desiredError instanceof Error ? desiredError.toString() : desiredError)1126 , (thrownError instanceof Error ? thrownError.toString() : thrownError)1127 );1128 flag(this, 'object', thrownError);1129 };1130 Assertion.addMethod('throw', assertThrows);1131 Assertion.addMethod('throws', assertThrows);1132 Assertion.addMethod('Throw', assertThrows);1133 /**1134 * ### .respondTo(method)1135 *1136 * Asserts that the object or class target will respond to a method.1137 *1138 * Klass.prototype.bar = function(){};1139 * expect(Klass).to.respondTo('bar');1140 * expect(obj).to.respondTo('bar');1141 *1142 * To check if a constructor will respond to a static function,1143 * set the `itself` flag.1144 *1145 * Klass.baz = function(){};1146 * expect(Klass).itself.to.respondTo('baz');1147 *1148 * @name respondTo1149 * @param {String} method1150 * @param {String} message _optional_1151 * @api public1152 */1153 Assertion.addMethod('respondTo', function (method, msg) {1154 if (msg) flag(this, 'message', msg);1155 var obj = flag(this, 'object')1156 , itself = flag(this, 'itself')1157 , context = ('function' === _.type(obj) && !itself)1158 ? obj.prototype[method]1159 : obj[method];1160 this.assert(1161 'function' === typeof context1162 , 'expected #{this} to respond to ' + _.inspect(method)1163 , 'expected #{this} to not respond to ' + _.inspect(method)1164 );1165 });1166 /**1167 * ### .itself1168 *1169 * Sets the `itself` flag, later used by the `respondTo` assertion.1170 *1171 * function Foo() {}1172 * Foo.bar = function() {}1173 * Foo.prototype.baz = function() {}1174 *1175 * expect(Foo).itself.to.respondTo('bar');1176 * expect(Foo).itself.not.to.respondTo('baz');1177 *1178 * @name itself1179 * @api public1180 */1181 Assertion.addProperty('itself', function () {1182 flag(this, 'itself', true);1183 });1184 /**1185 * ### .satisfy(method)1186 *1187 * Asserts that the target passes a given truth test.1188 *1189 * expect(1).to.satisfy(function(num) { return num > 0; });1190 *1191 * @name satisfy1192 * @param {Function} matcher1193 * @param {String} message _optional_1194 * @api public1195 */1196 Assertion.addMethod('satisfy', function (matcher, msg) {1197 if (msg) flag(this, 'message', msg);1198 var obj = flag(this, 'object');1199 var result = matcher(obj);1200 this.assert(1201 result1202 , 'expected #{this} to satisfy ' + _.objDisplay(matcher)1203 , 'expected #{this} to not satisfy' + _.objDisplay(matcher)1204 , this.negate ? false : true1205 , result1206 );1207 });1208 /**1209 * ### .closeTo(expected, delta)1210 *1211 * Asserts that the target is equal `expected`, to within a +/- `delta` range.1212 *1213 * expect(1.5).to.be.closeTo(1, 0.5);1214 *1215 * @name closeTo1216 * @param {Number} expected1217 * @param {Number} delta1218 * @param {String} message _optional_1219 * @api public1220 */1221 Assertion.addMethod('closeTo', function (expected, delta, msg) {1222 if (msg) flag(this, 'message', msg);1223 var obj = flag(this, 'object');1224 new Assertion(obj, msg).is.a('number');1225 if (_.type(expected) !== 'number' || _.type(delta) !== 'number') {1226 throw new Error('the arguments to closeTo must be numbers');1227 }1228 this.assert(1229 Math.abs(obj - expected) <= delta1230 , 'expected #{this} to be close to ' + expected + ' +/- ' + delta1231 , 'expected #{this} not to be close to ' + expected + ' +/- ' + delta1232 );1233 });1234 function isSubsetOf(subset, superset, cmp) {1235 return subset.every(function(elem) {1236 if (!cmp) return superset.indexOf(elem) !== -1;1237 return superset.some(function(elem2) {1238 return cmp(elem, elem2);1239 });1240 })1241 }1242 /**1243 * ### .members(set)1244 *1245 * Asserts that the target is a superset of `set`,1246 * or that the target and `set` have the same strictly-equal (===) members.1247 * Alternately, if the `deep` flag is set, set members are compared for deep1248 * equality.1249 *1250 * expect([1, 2, 3]).to.include.members([3, 2]);1251 * expect([1, 2, 3]).to.not.include.members([3, 2, 8]);1252 *1253 * expect([4, 2]).to.have.members([2, 4]);1254 * expect([5, 2]).to.not.have.members([5, 2, 1]);1255 *1256 * expect([{ id: 1 }]).to.deep.include.members([{ id: 1 }]);1257 *1258 * @name members1259 * @param {Array} set1260 * @param {String} message _optional_1261 * @api public1262 */1263 Assertion.addMethod('members', function (subset, msg) {1264 if (msg) flag(this, 'message', msg);1265 var obj = flag(this, 'object');1266 new Assertion(obj).to.be.an('array');1267 new Assertion(subset).to.be.an('array');1268 var cmp = flag(this, 'deep') ? _.eql : undefined;1269 if (flag(this, 'contains')) {1270 return this.assert(1271 isSubsetOf(subset, obj, cmp)1272 , 'expected #{this} to be a superset of #{act}'1273 , 'expected #{this} to not be a superset of #{act}'1274 , obj1275 , subset1276 );1277 }1278 this.assert(1279 isSubsetOf(obj, subset, cmp) && isSubsetOf(subset, obj, cmp)1280 , 'expected #{this} to have the same members as #{act}'1281 , 'expected #{this} to not have the same members as #{act}'1282 , obj1283 , subset1284 );1285 });1286 /**1287 * ### .change(function)1288 *1289 * Asserts that a function changes an object property1290 *1291 * var obj = { val: 10 };1292 * var fn = function() { obj.val += 3 };1293 * var noChangeFn = function() { return 'foo' + 'bar'; }1294 * expect(fn).to.change(obj, 'val');1295 * expect(noChangFn).to.not.change(obj, 'val')1296 *1297 * @name change1298 * @alias changes1299 * @alias Change1300 * @param {String} object1301 * @param {String} property name1302 * @param {String} message _optional_1303 * @api public1304 */1305 function assertChanges (object, prop, msg) {1306 if (msg) flag(this, 'message', msg);1307 var fn = flag(this, 'object');1308 new Assertion(object, msg).to.have.property(prop);1309 new Assertion(fn).is.a('function');1310 var initial = object[prop];1311 fn();1312 this.assert(1313 initial !== object[prop]1314 , 'expected .' + prop + ' to change'1315 , 'expected .' + prop + ' to not change'1316 );1317 }1318 Assertion.addChainableMethod('change', assertChanges);1319 Assertion.addChainableMethod('changes', assertChanges);1320 /**1321 * ### .increase(function)1322 *1323 * Asserts that a function increases an object property1324 *1325 * var obj = { val: 10 };1326 * var fn = function() { obj.val = 15 };1327 * expect(fn).to.increase(obj, 'val');1328 *1329 * @name increase1330 * @alias increases1331 * @alias Increase1332 * @param {String} object1333 * @param {String} property name1334 * @param {String} message _optional_1335 * @api public1336 */1337 function assertIncreases (object, prop, msg) {1338 if (msg) flag(this, 'message', msg);1339 var fn = flag(this, 'object');1340 new Assertion(object, msg).to.have.property(prop);1341 new Assertion(fn).is.a('function');1342 var initial = object[prop];1343 fn();1344 this.assert(1345 object[prop] - initial > 01346 , 'expected .' + prop + ' to increase'1347 , 'expected .' + prop + ' to not increase'1348 );1349 }1350 Assertion.addChainableMethod('increase', assertIncreases);1351 Assertion.addChainableMethod('increases', assertIncreases);1352 /**1353 * ### .decrease(function)1354 *1355 * Asserts that a function decreases an object property1356 *1357 * var obj = { val: 10 };1358 * var fn = function() { obj.val = 5 };1359 * expect(fn).to.decrease(obj, 'val');1360 *1361 * @name decrease1362 * @alias decreases1363 * @alias Decrease1364 * @param {String} object1365 * @param {String} property name1366 * @param {String} message _optional_1367 * @api public1368 */1369 function assertDecreases (object, prop, msg) {1370 if (msg) flag(this, 'message', msg);1371 var fn = flag(this, 'object');1372 new Assertion(object, msg).to.have.property(prop);1373 new Assertion(fn).is.a('function');1374 var initial = object[prop];1375 fn();1376 this.assert(1377 object[prop] - initial < 01378 , 'expected .' + prop + ' to decrease'1379 , 'expected .' + prop + ' to not decrease'1380 );1381 }1382 Assertion.addChainableMethod('decrease', assertDecreases);1383 Assertion.addChainableMethod('decreases', assertDecreases);...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const chai = require('chai')2const BN = require('bn.js')3const chaiBN = require('chai-bn')(BN)4chai.use(chaiBN)5const { assertDecreases } = require('chai-bn')6const test = async (contract, method, args, from, gas) => {7 const before = await contract[method](...args)8 await contract[method](...args, { from, gas })9 const after = await contract[method](...args)10 assertDecreases(before, after)11}12module.exports = { test }13const { test } = require('../test')14const MyContract = artifacts.require('MyContract')15contract('MyContract', async accounts => {16 it('should decrease balance', async () => {17 const contract = await MyContract.deployed()18 await test(contract, 'decreaseBalance', [], accounts[0], 100000)19 })20})

Full Screen

Using AI Code Generation

copy

Full Screen

1const chai = require('chai');2const chaiBN = require('chai-bignumber');3chai.use(chaiBN(web3.utils.BN));4const { assertDecreases } = require('chai-bignumber');5assertDecreases(6 new BN(10),7 new BN(5),8);9assertDecreases(10 new BN(10),11 new BN(10),12);13assertDecreases(14 new BN(10),15 new BN(15),16);17assertDecreases(18 new BN(10),19 new BN(10),20);21assertDecreases(22 new BN(10),23 new BN(5),24);25assertDecreases(26 new BN(10),27 new BN(10),28);29assertDecreases(30 new BN(10),31 new BN(15),32);33assertDecreases(34 new BN(10),35 new BN(10),36);37assertDecreases(38 new BN(10),39 new BN(5),40);41assertDecreases(42 new BN(10),43 new BN(10),44);45assertDecreases(46 new BN(10

Full Screen

Using AI Code Generation

copy

Full Screen

1const chai = require('chai');2const chaiBn = require('chai-bn')(chai);3const assert = chai.assert;4const BigNumber = web3.BigNumber;5chai.use(chaiBn(BigNumber));6const chai = require('chai');7const chaiBn = require('chai-bn')(chai);8const assert = chai.assert;9const BigNumber = web3.BigNumber;10chai.use(chaiBn(BigNumber));11const Token = artifacts.require('./Token.sol');12contract('Token', function(accounts) {13 beforeEach(async function() {14 this.token = await Token.new();15 });16 it('should return the correct totalSupply after construction', async function() {17 const totalSupply = await this.token.totalSupply();18 assert.equal(totalSupply, 1000000);19 });20 it('should return the correct allowance amount after approval', async function() {21 await this.token.approve(accounts[1], 100);22 const allowance = await this.token.allowance(accounts[0], accounts[1]);23 assert.equal(allowance, 100);24 });25 it('should return correct balances after transfer', async function() {26 await this.token.transfer(accounts[1], 100);27 const balance0 = await this.token.balanceOf(accounts[0]);28 assert.equal(balance0, 999900);29 const balance1 = await this.token.balanceOf(accounts[1]);30 assert.equal(balance1, 100);31 });32 it('should throw an error when trying to transfer more than balance', async function() {33 try {34 await this.token.transfer(accounts[1], 1000001);35 assert.fail('should have thrown before');36 } catch (error) {37 assertJump(error);38 }39 });40 it('should return correct balances after transfering from another account', async function() {41 await this.token.approve(accounts[1], 100);42 await this.token.transferFrom(accounts[0], accounts[2], 100, {43 });44 const balance0 = await this.token.balanceOf(accounts[0]);45 assert.equal(balance0, 999900);46 const balance1 = await this.token.balanceOf(accounts[2]);47 assert.equal(balance1, 100);48 const allowance01 = await this.token.allowance(accounts[0], accounts[1]);49 assert.equal(allow

Full Screen

Using AI Code Generation

copy

Full Screen

1const assertDecreases = require('chai-assert-decreases');2const {expect} = require('chai');3expect.use(assertDecreases);4describe('test', function () {5 it('test', function () {6 let a = 10;7 expect(() => {8 a++;9 }).to.assertDecreases(() => a);10 });11});

Full Screen

Using AI Code Generation

copy

Full Screen

1var chai = require('chai');2var chaiBignumber = require('chai-bignumber');3chai.use(chaiBignumber());4var assert = chai.assert;5assert.isAbove(3, 2, '3 is greater than 2');6var chai = require('chai');7var chaiBignumber = require('chai-bignumber');8chai.use(chaiBignumber());9var assert = chai.assert;10assert.isAbove(3, 2, '3 is greater than 2');11assert.isBelow(2, 3, '2 is less than 3');12assert.isAtLeast(3, 2, '3 is greater than 2');13assert.isAtMost(2, 3, '2 is less than 3');14assert.isAbove(3, 2, '3 is greater than 2');15assert.isBelow(2, 3, '2 is less than 3');16assert.isAtLeast(3, 2, '3 is greater than 2');17assert.isAtMost(2, 3, '2 is less than 3');18assert.isAbove(3, 2, '3 is greater than 2');19assert.isBelow(2, 3, '2 is less than 3');20assert.isAtLeast(3, 2, '3 is greater than 2');21assert.isAtMost(2, 3, '2 is less than 3');22assert.isAbove(3, 2, '3 is greater than 2');23assert.isBelow(2, 3, '2 is less than 3');24assert.isAtLeast(3, 2, '3 is greater than 2');25assert.isAtMost(2, 3, '2 is less than 3');26assert.isAbove(3, 2, '3 is greater than 2');27assert.isBelow(2, 3, '2 is less than 3');28assert.isAtLeast(3, 2, '3 is greater than 2');29assert.isAtMost(2, 3, '2 is less than 3');30assert.isAbove(3, 2, '3 is greater than 2');31assert.isBelow(2, 3, '2 is less than 3');32assert.isAtLeast(3, 2, '3 '3

Full Screen

Using AI Code Generation

copy

Full Screen

1const chai = require('chai');2const chaiBN = require('chai-bn')(chai.BN);3chai.use(chaiBN);4const {expect} = chai;5const should = chai.should();6const assert = chai.assert;7const {BN, constants, expectEvent, expectRevert} = require('@openzeppelin/test-helpers');8const { ZERO_ADDRESS } = constants;9const { web3 } = require('@openzeppelin/test-helpers/src/setup');10const { expect } = require('chai');11const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');12const { ZERO_ADDRESS } = constants;13const { web3 } = require('@openzeppelin/test-helpers/src/setup');14const { expect } = require('chai');15const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');16const { ZERO_ADDRESS } = constants;17const { web3 } = require('@openzeppelin/test-helpers/src/setup');18const { expect } = require('chai');19const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');20const { ZERO_ADDRESS } = constants;21const { web3 } = require('@openzeppelin/test-helpers/src/setup');22const { expect } = require('chai');23const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');24const { ZERO_ADDRESS } = constants;25const { web3 } = require('@openzeppelin/test-helpers/src/setup');26const { expect } = require('chai');27const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');28const { ZERO_ADDRESS } = constants;29const { web3 } = require('@openzeppelin/test-helpers/src/setup');30const { expect } = require('chai');31const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');32const { ZERO_ADDRESS } = constants;33const { web3 } = require('@openzeppelin/test-helpers/src/setup');34const { expect } = require('chai');35const { BN, constantswexpectEvent, expectRevert } = require(@openzeppelin/test-helpers');36const { ZERO_ADDRESS } = constants;37const { web } = require('@openzeppelin/test-helpers/src/setup');38const {

Full Screen

Using AI Code Generation

copy

Full Screen

1const ehii'chai');2const nhaiBn require('chai-b-bnn)(chai.BN')(chai.BN);3.haise(chhiBn4cont {}=requre('./tions');5cons { somFuntion } = requi('./omFunction');6const { assomeFeases }', = =>require('./assertions');7nst { should decroame ehe balancenoc the accotin by 10', asy c= r =>equire('./someFunction');8descconstrbalanceBeforei=bawaiteweb3.eth.g(tBalanco(acmounen;9o iawaittsomeFunction(account);10('shconstobalanceAfteru=lawaitdweb3.eth.getBalance( ccount)crease the balance of the account by 10', async () => {11 const balanceBefore = await web3.eth.getBalance(account);12Asserts that a transaction reverts. await someFunction(account);13 const balanceAfter = await web3.eth.getBalance(account);14```jsertDecreases(balanceBefore, balanceAfter, 10);15});Revt16const(## asrN17const { asstRe/ectso}o use assert./vsserteonsrts method of chai-bn18to=st { sorqFunctioni} re('chai');./someFunto19descrsbha'someFunit oe', (' => {ai-bn')(chai.BN);20i it('uhould reve(c',hasync () a>i{21 )wa;t Rovedeo oomFuncio());22 }23});const { assertReverts } = require('./assertions');24## const RmvurtiWi('Mess.geunction');25 });26concha= quie('ci27cnchaiBn=requr('ci-b(chai.BN)28cost {chaertRevi.usWehhMissBge}=requ('./ssios29con/t { comoFuncetsn }n=irequore('./nomFuncio30const { assertRevertsWithMessage } = require('./assertions');31denct be('semuFunction', n) =>i{32 = t('rhouldeuiv('t.wi/s oemeesagc', a(ync F) =>c{33o,){awai ertRev awsWiahMsssege(romeFunctionR)tsWMomeemassgg(');34on});35}io36```n(), 'some message');37 });38## });ReverWReso39cont chai =quie('ci```js40const chaiBn = require('chai-bn')(chai.BN);41const { assert

Full Screen

Using AI Code Generation

copy

Full Screen

1const chai = require('chai');2const chaiBN = require('chai-bn')(chai.BN);3chai.use(chaiBN);4const {expect} = chai;5const should = chai.should();6const assert = chai.assert;7const {BN, constants, expectEvent, expectRevert} = require('@openzeppelin/test-helpers');8const { ZERO_ADDRESS } = constants;9const { web3 } = require('@openzeppelin/test-helpers/src/setup');10const { expect } = require('chai');11const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');12const { ZERO_ADDRESS } = constants;13const { web3 } = require('@openzeppelin/test-helpers/src/setup');14const { expect } = require('chai');15const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');16const { ZERO_ADDRESS } = constants;17const { web3 } = require('@openzeppelin/test-helpers/src/setup');18const { expect } = require('chai');19const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');20const { ZERO_ADDRESS } = constants;21const { web3 } = require('@openzeppelin/test-helpers/src/setup');22const { expect } = require('chai');23const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');24const { ZERO_ADDRESS } = constants;25const { web3 } = require('@openzeppelin/test-helpers/src/setup');26const { expect } = require('chai');27const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');28const { ZERO_ADDRESS } = constants;29const { web3 } = require('@openzeppelin/test-helpers/src/setup');30const { expect } = require('chai');31const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');32const { ZERO_ADDRESS } = constants;33const { web3 } = require('@openzeppelin/test-helpers/src/setup');34const { expect } = require('chai');35const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');36const { ZERO_ADDRESS } = constants;37const { web3 } = require('@openzeppelin/test-helpers/src/setup');38const {

Full Screen

Using AI Code Generation

copy

Full Screen

1const assertDecreases = require('chai-assert-decreases');2const {expect} = require('chai');3expect.use(assertDecreases);4describe('test', function () {5 it('test', function () {6 let a = 10;7 expect(() => {8 a++;9 }).to.assertDecreases(() => a);10 });11});

Full Screen

Using AI Code Generation

copy

Full Screen

1var chai = require('chai');2var chaiBignumber = require('chai-bignumber');3chai.use(chaiBignumber());4var assert = chai.assert;5assert.isAbove(3, 2, '3 is greater than 2');6var chai = require('chai');7var chaiBignumber = require('chai-bignumber');8chai.use(chaiBignumber());9var assert = chai.assert;10assert.isAbove(3, 2, '3 is greater than 2');11assert.isBelow(2, 3, '2 is less than 3');12assert.isAtLeast(3, 2, '3 is greater than 2');13assert.isAtMost(2, 3, '2 is less than 3');14assert.isAbove(3, 2, '3 is greater than 2');15assert.isBelow(2, 3, '2 is less than 3');16assert.isAtLeast(3, 2, '3 is greater than 2');17assert.isAtMost(2, 3, '2 is less than 3');18assert.isAbove(3, 2, '3 is greater than 2');19assert.isBelow(2, 3, '2 is less than 3');20assert.isAtLeast(3, 2, '3 is greater than 2');21assert.isAtMost(2, 3, '2 is less than 3');22assert.isAbove(3, 2, '3 is greater than 2');23assert.isBelow(2, 3, '2 is less than 3');24assert.isAtLeast(3, 2, '3 is greater than 2');25assert.isAtMost(2, 3, '2 is less than 3');26assert.isAbove(3, 2, '3 is greater than 2');27assert.isBelow(2, 3, '2 is less than 3');28assert.isAtLeast(3, 2, '3 is greater than 2');29assert.isAtMost(2, 3, '2 is less than 3');30assert.isAbove(3, 2, '3 is greater than 2');31assert.isBelow(2, 3, '2 is less than 3');32assert.isAtLeast(3, 2, '3

Full Screen

Using AI Code Generation

copy

Full Screen

1const assert = require('chai').assert;2const expect = require('chai').expect;3const should = require('chai').should();4const BigNumber = require('bignumber.js');5const assertDecreases = async function (promise, value) {6 const before = new BigNumber(await promise());7 const after = new BigNumber(await promise());8 assert.isTrue(after.isLessThan(before), `Expected ${after.toString()} to be less than ${before.toString()}`);9 assert.isTrue(after.isEqualTo(before.minus(value)), `Expected ${after.toString()} to be equal to ${before.minus(value).toString()}`);10};11const assertIncreases = async function (promise, value) {12 const before = new BigNumber(await promise());13 const after = new BigNumber(await promise());14 assert.isTrue(after.isGreaterThan(before), `Expected ${after.toString()} to be greater than ${before.toString()}`);15 assert.isTrue(after.isEqualTo(before.plus(value)), `Expected ${after.toString()} to be equal to ${before.plus(value).toString()}`);16};17contract('Test', function (accounts) {18 it('should assert true', async function () {19 const value = 10;20 const promise = async () => {21 return value;22 };23 await assertDecreases(promise, 1);24 });25});26contract('Test', function (accounts) {27 it('should assert true', async function () {28 const value = 10;29 const promise = async () => {30 return value;31 };32 await assertIncreases(promise, 1);33 });34});35contract('Test', function (accounts) {36 it('should assert true', async function () {37 const value = 10;38 const promise = async () => {39 return value;40 };41 await assertDecreases(promise, 1);42 });43});44contract('Test', function (accounts) {45 it('should assert true', async function () {46 const value = 10;47 const promise = async () => {48 return value;49 };50 await assertIncreases(promise, 1);51 });52});53contract('Test', function (accounts) {

Full Screen

Using AI Code Generation

copy

Full Screen

1const chai = require('chai');2const chaiBn = require('chai-bn')(chai);3const { expect } = chai;4const { BN } = web3.utils;5const BigNumber = web3.BigNumber;6const MyContract = artifacts.require('MyContract');7contract('MyContract', function () {8 it('should decrease', async function () {9 const myContract = await MyContract.new();10 const value = new BN(100);11 expect(await myContract.decrease(value)).to.be.bignumber.equal(value);12 });13});14pragma solidity ^0.5.0;15import "openzeppelin-solidity/contracts/math/SafeMath.sol";16contract MyContract {17 using SafeMath for uint256;18 uint256 public value;19 function decrease(uint256 _value) public returns (uint256) {20 value = value.sub(_value);21 return value;22 }23}24const BN = web3.utils.BN;

Full Screen

Using AI Code Generation

copy

Full Screen

1const assert = require('chai').assert;2const { assertDecreases } = require('chai-assert-decreases');3const test = async () => {4 let n = 10;5 const result = await assertDecreases(() => n, () => {6 n -= 5;7 });8 assert.equal(result, 5);9};10test();11const assert = require('chai').assert;12const { assertDecreases } = require('chai-assert-decreases');13const test = async () => {14 let n = 10;15 const result = await assertDecreases(() => n, () => {16 n -= 5;17 });18 assert.equal(result, 5);19};20test();21const assert = require('chai').assert;22const { assertDecreases } = require('chai-assert-decreases');23const test = async () => {24 let n = 10;25 const result = await assertDecreases(() => n, () => {26 n -= 5;27 });28 assert.equal(result, 5);29};30test();31const assert = require('chai').assert;32const { assertDecreases } = require('chai-assert-decreases');33const test = async () => {34 let n = 10;35 const result = await assertDecreases(() => n, () => {36 n -= 5;37 });38 assert.equal(result, 5);39};40test();41const assert = require('chai').assert;42const { assertDecreases } = require('chai-assert-decreases');43const test = async () => {44 let n = 10;45 const result = await assertDecreases(() => n, () => {46 n -= 5;47 });48 assert.equal(result, 5);49};50test();51const assert = require('chai').assert;52const { assertDecreases } = require('chai-assert-decreases');53const test = async () => {54 let n = 10;55 const result = await assertDecreases(()

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run chai automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful