Skip to content

Commit

Permalink
Test for duplicates (swapagarwal#251)
Browse files Browse the repository at this point in the history
Close swapagarwal#209 

* Wrote test for duplicate translation entries by parsing the library code

* Removed duplicate translation entry ("forbidden" -> 403)
  • Loading branch information
Theys96 authored Jul 13, 2020
1 parent 5b71582 commit 0175ae7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
1 change: 0 additions & 1 deletion geeksay.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ const translations = {
"death": "BSOD",
"wait": "load",
"waiting": "loading",
"forbidden": "403",
"unauthorized": "401",
"nothing": "void",
}
Expand Down
37 changes: 37 additions & 0 deletions test/geeksay.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const should = require('should');
const esprima = require('esprima');
const fs = require('fs');
const geeksay = require('../geeksay')

describe('words', () => {
Expand Down Expand Up @@ -99,3 +101,38 @@ describe('inputs', () => {
should.equal(geeksay([1, 2, 3, 4, '100']), '1 10 11 100 1100100');
});
});

describe('code checks', () => {
it('duplicate translation entries', () => {
const libfile = '../geeksay.js' // Library file location
const varname = 'translations' // Variable to check for duplicate entries

var code = fs.readFileSync(__dirname + '/' + libfile, 'utf8')
var ast = esprima.parse(code);

// Find 'translations' variable and extract entries' keys
var duplicates;
for (const i in ast.body) {
let decl = ast.body[i]
if (decl.type == 'VariableDeclaration') {
for (const j in decl.declarations) {
if (decl.declarations[j].id.name == varname) {
translations = decl.declarations[j];
entries = translations.init.properties.map(prop => prop.key.value);
duplicates = entries.filter((item, index) => entries.indexOf(item) != index);
}
}
}
}

// Case if 'translations' variable was not found and 'duplicates' is therefore undefined
if (typeof duplicates == "undefined") {
throw Error("Variable \'" + varname + "\' was not found in library file (" + libfile + ").");
}

// The number of duplicates should be 0
should.equal(duplicates.length, 0,
"Found duplicate " + duplicates.length + " entries: " + duplicates.map(entry => '"'+entry+'"').join(', ') );

});
});

0 comments on commit 0175ae7

Please sign in to comment.