From db727eaf1ca85f519fabbbf3b43be1bd3319c1d0 Mon Sep 17 00:00:00 2001 From: Alex Pounds Date: Tue, 20 Oct 2015 23:30:51 -0400 Subject: [PATCH] Extra tests for the 'binary' exercise, as discussed in https://github.com/exercism/x-common/issues/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. --- binary/binary-test.spec.js | 16 +++++++++++++--- binary/example.js | 6 +++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/binary/binary-test.spec.js b/binary/binary-test.spec.js index c674b074..63ed387c 100644 --- a/binary/binary-test.spec.js +++ b/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); }); @@ -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); }); -}); \ No newline at end of file +}); diff --git a/binary/example.js b/binary/example.js index e4c24002..8174186e 100644 --- a/binary/example.js +++ b/binary/example.js @@ -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 () {