Skip to content

Commit

Permalink
Extra tests for the 'binary' exercise, as discussed in exercism/probl…
Browse files Browse the repository at this point in the history
…em-specifications#95

This expands the JavaScript test for the 'binary' exercise to include
the same examples as the Ruby equivalent. The example solution has also
been updated so it passes the new tests.
  • Loading branch information
creature committed Oct 21, 2015
1 parent bac8157 commit c307ab5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
16 changes: 13 additions & 3 deletions binary/binary_test.spec.js
@@ -1,8 +1,11 @@
var Binary = require('./binary');

describe('binary', function() {
it('0 is decimal 0', function() {
expect(new Binary('0').toDecimal()).toEqual(0);
});

it('1 is decimal 1', function() {
xit('1 is decimal 1', function() {
expect(new Binary('1').toDecimal()).toEqual(1);
});

Expand Down Expand Up @@ -30,8 +33,15 @@ describe('binary', function() {
expect(new Binary('10001101000').toDecimal()).toEqual(1128);
});

xit('carrot is decimal 0', function() {
xit('00011111 is decimal 31', function() {
expect(new Binary('00011111').toDecimal()).toEqual(31);
});

xit('invalid inputs are decimal 0', function() {
expect(new Binary('carrot').toDecimal()).toEqual(0);
expect(new Binary('012').toDecimal()).toEqual(0);
expect(new Binary('10nope').toDecimal()).toEqual(0);
expect(new Binary('nope10').toDecimal()).toEqual(0);
});

});
});
6 changes: 5 additions & 1 deletion binary/example.js
Expand Up @@ -3,7 +3,11 @@
module.exports = Binary;

function Binary(binary) {
this.binary = parseInt(binary, 2);
if (binary.match(/^[01]*$/)) {
this.binary = parseInt(binary, 2);
} else {
this.binary = 0;
}
}

Binary.prototype.toDecimal = function () {
Expand Down

0 comments on commit c307ab5

Please sign in to comment.