Skip to content

Commit

Permalink
Full test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Cody Dalton committed Apr 24, 2021
1 parent 72b596a commit ad986c2
Show file tree
Hide file tree
Showing 6 changed files with 1,858 additions and 12 deletions.
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# dependencies
/node_modules

# reports
/coverage
/.nyc_output

# distributions
/dist

# system files
.DS_Store

/.env
4 changes: 4 additions & 0 deletions .mocharc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
full-trace: true
spec: '**/*.spec.js'
bail: true
slow: 2000
25 changes: 13 additions & 12 deletions index.js → enigma.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* Class Declaration
*/
class Enigma {

constructor(
Expand All @@ -23,15 +26,15 @@ class Enigma {
const pairs = this.plugboardSettings.split(' ');
pairs.forEach(
pair => {
const first = pair[0].toUpperCase();
const second = pair[1].toUpperCase();

this.plugboard[first] = second;
this.plugboard[second] = first;
if(pair[0]) {
const first = pair[0].toUpperCase();
const second = pair[1].toUpperCase();

this.plugboard[first] = second;
this.plugboard[second] = first;
}
}
);

console.log(this.plugboard)
}

encrypt(text) {
Expand Down Expand Up @@ -120,8 +123,6 @@ class Enigma {
}
}

const aEnigma = new Enigma([ 5, 10, 9 ], 'ea bc df hi km zt xp ln');
const bEnigma = new Enigma([ 5, 10, 9 ], 'ea bc df hi km zt xp ln');

console.log(aEnigma.encrypt('HITHISISANENCRYPTEDSENTENCE'))
console.log(bEnigma.encrypt('FLYANJAHZGWMQNZQOJYRUGBZJGG'))
module.exports = {
Enigma
};
39 changes: 39 additions & 0 deletions enigma.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const { expect } = require('chai');
const { Enigma } = require('./enigma');

describe('Class: Enigma', () => {

it('should correctly encrypt a string', () => {

const encrypted = 'FLYANJAHZGWMQNZQOJYRUGBZJGG';
const decrypted = 'hi this is an encrypted sentence';

const aEnigma = new Enigma([ 5, 10, 9 ], 'ea bc df hi km zt xp ln');

expect(aEnigma.encrypt(decrypted)).to.equal(encrypted);
});

it('should correctly decrypt a string', () => {

const encrypted = 'FLYANJAHZGWMQNZQOJYRUGBZJGG';
const decrypted = 'HITHISISANENCRYPTEDSENTENCE';

const aEnigma = new Enigma([ 5, 10, 9 ], 'ea bc df hi km zt xp ln');

expect(aEnigma.encrypt(encrypted)).to.equal(decrypted);
});

it('should default rotor settings to [0, 0, 0]', () => {
const aEnigma = new Enigma();

expect(aEnigma.rotorSettings).to.deep.equal([0, 0, 0]);
});

it('should roll over max rotors to [0, 0, 1]', () => {
const aEnigma = new Enigma([ 25, 25, 25 ], 'ea bc df hi km zt xp ln');

aEnigma.encrypt('A');

expect(aEnigma.rotorSettings).to.deep.equal([0, 0, 0]);
});
});
Loading

0 comments on commit ad986c2

Please sign in to comment.