Code Monkey home page Code Monkey logo

bn-chai's Introduction

bn-chai

bn-chai extends Chai with assertions about bn.js decimals.

Installation

npm install -s bn-chai

Setup

var chai = require('chai');
var expect = chai.expect;
var BN = require('bn.js');
var bnChai = require('bn-chai');
chai.use(bnChai(BN));

Usage

When comparing two bn.js decimals bn1 and bn2, instead of doing:

expect(bn1.eq(bn2)).to.be.true;

You can do this instead:

expect(bn1).to.eq.BN(bn2);

This plugin is also handy when comparing a bn.js decimal to an inline constant, so instead of writing:

expect(bn1.eq(new BN('1'))).to.be.true;

You can simply write:

expect(bn1).to.eq.BN(1);

Which is simpler and more readable.

Assertions

eq

Asserts that the target is equal to the given object.

expect(new BN('1')).to.eq.BN(1);
expect(new BN('1')).not.to.eq.BN(0);

lt

Asserts that the target is less than the given object.

expect(new BN('0')).to.be.lt.BN(1);
expect(new BN('1')).not.to.be.lt.BN(1);

lte

Asserts that the target is less than or equal to the given object.

expect(new BN('0')).to.be.lte.BN(1);
expect(new BN('1')).to.be.lte.BN(1);
expect(new BN('2')).not.to.be.lte.BN(1);

gt

Asserts that the target is greater than the given object.

expect(new BN('1')).to.be.gt.BN(0);
expect(new BN('1')).not.to.be.gt.BN(1);

gte

Asserts that the target is greater than or equal to the given object.

expect(new BN('1')).to.be.gte.BN(0);
expect(new BN('1')).to.be.gte.BN(1);
expect(new BN('1')).not.to.be.gte.BN(2);

negative

Asserts that the target is negative.

expect(new BN('-1')).to.be.negative;
expect(new BN('1')).not.to.be.negative;

even

Asserts that the target is even.

expect(new BN('2')).to.be.even;
expect(new BN('1')).not.to.be.even;

odd

Asserts that the target is odd.

expect(new BN('1')).to.be.odd;
expect(new BN('2')).not.to.be.odd;

zero

Asserts that the target is equal to 0.

expect(new BN('0')).to.be.zero;
expect(new BN('1')).not.to.be.zero;

Mixing BN, numbers and strings

You can mix BN with numbers and strings freely:

expect(new BN('1')).to.eq.BN(new BN('1'));
expect(new BN('1')).to.eq.BN('1');
expect(new BN('1')).to.eq.BN(1);

expect('1').to.eq.BN(new BN('1'));
expect('1').to.eq.BN('1');
expect('1').to.eq.BN(1);

expect(1).to.eq.BN(new BN('1'));
expect(1).to.eq.BN('1');
expect(1).to.eq.BN(1);

Executing example tests

Example unit tests making use of this plugin can be executed by running the following commands:

cd test
npm run test; npm run failTest

bn-chai's People

Contributors

mischi avatar rzadp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

bn-chai's Issues

Does this work with should.js?

I am using this library after following the upgrade guidelines for the new truffle V5 release, see here: https://github.com/trufflesuite/truffle/releases?after=truffle-code-utils%401.1.2-beta.0

Example code:

const bnChai = require('bn-chai');

// Setup should and bnChai as a plugin
require('chai')
  .use(require('chai-as-promised'))
  .use(bnChai(web3.utils.BN)) // web3 is provided by the test runner in truffle
  .should();

// sample assertion 
myValue.should.be.eq.BN(0);
// gives the following failure: AssertionError: expected 0 to be below 0

When I one of my tests it always outs the following AssertionError: expected 0 to be below 0

Am I using it wrong?

All the equality assertions come out the same way by looking ?

Extend equal etc. instead of adding BN property

Using chai.Assertion.overwriteMethod() it is possible to extend methods like equal, so that we could write

let a = new BN(1);
let b = new BN(1);
expect(a).to.equal(b); //or
a.should.equal(b);

I wrote a minimum viable plugin:

module.exports = function(BN) {
  return function (chai, utils) {
    chai.Assertion.overwriteMethod('equal', function(_super) {
      return function(exp) {
        var obj = utils.flag(this, 'object');
        if (BN.isBN(obj)) {
          chai.assert(obj.eq(exp), "BNs don't equal.");
        } else {
          _super.apply(this, arguments);
        }
      }
    });
  };
};

This extends equal to BNs. However, I don't know how to display the message that was passed and, ideally, to show a diff.
It may also make sense to wrap the expected exp into a new BN(exp) to be more forgiving when passing non-BNs.
Probably, it is cleaner to use something like let test = new Assertion(...); test.assert(...), which I saw in the chai sourcecode, instead of chai.assert(). However, I didn't understand yet how to fill those ...s.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.