translations integration test

This commit is contained in:
fewieden 2018-02-15 23:22:52 +01:00
parent d709a44960
commit 20244c4fb5
2 changed files with 129 additions and 118 deletions

View File

@ -0,0 +1,129 @@
const fs = require("fs");
const path = require("path");
const chai = require("chai");
const expect = chai.expect;
const mlog = require("mocha-logger");
const translations = require("../../translations/translations.js");
const helmet = require("helmet");
const {JSDOM} = require("jsdom");
const express = require("express");
describe("Translations", function() {
let server;
before(function() {
const app = express();
app.use(helmet());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
next();
});
app.use("/translations", express.static(path.join(__dirname, "..", "..", "translations")));
server = app.listen(3000);
});
after(function() {
server.close();
});
it("should have a translation file in the specified path", function() {
for(let language in translations) {
const file = fs.statSync(translations[language]);
expect(file.isFile()).to.be.equal(true);
}
});
const mmm = {
name: "TranslationTest",
file(file) {
return `http://localhost:3000/${file}`;
}
};
describe("Parsing language files through the Translator class", function() {
for(let language in translations) {
it(`should parse ${language}`, function(done) {
const dom = new JSDOM(`<script>var translations = ${JSON.stringify(translations)}; var Log = {log: function(){}};</script>\
<script src="${path.join(__dirname, "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
resources: "usable" });
dom.window.onload = function() {
const {Translator} = dom.window;
Translator.load(mmm, translations[language], false, function() {
expect(Translator.translations[mmm.name]).to.be.an('object');
expect(Object.keys(Translator.translations[mmm.name]).length).to.be.at.least(1);
done();
});
};
});
}
});
describe("Same keys", function() {
let base;
before(function(done) {
const dom = new JSDOM(`<script>var translations = ${JSON.stringify(translations)}; var Log = {log: function(){}};</script>\
<script src="${path.join(__dirname, "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
resources: "usable" });
dom.window.onload = function() {
const {Translator} = dom.window;
Translator.load(mmm, translations.en, false, function() {
base = Object.keys(Translator.translations[mmm.name]).sort();
done();
});
};
});
for (let language in translations) {
if (language === "en") {
continue;
}
describe(`Translation keys of ${language}`, function() {
let keys;
before(function(done){
const dom = new JSDOM(`<script>var translations = ${JSON.stringify(translations)}; var Log = {log: function(){}};</script>\
<script src="${path.join(__dirname, "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
resources: "usable" });
dom.window.onload = function() {
const {Translator} = dom.window;
Translator.load(mmm, translations[language], false, function() {
keys = Object.keys(Translator.translations[mmm.name]).sort();
done();
});
};
});
it(`${language} keys should be in base`, function() {
keys.forEach(function(key) {
expect(base.indexOf(key)).to.be.at.least(0);
});
});
it(`${language} should contain all base keys`, function() {
// TODO: when all translations are fixed, use
// expect(keys).to.deep.equal(base);
// instead of the try-catch-block
try {
expect(keys).to.deep.equal(base);
} catch(e) {
if (e instanceof chai.AssertionError) {
const diff = base.filter(key => !keys.includes(key));
mlog.pending(`Missing Translations for language ${language}: ${diff}`);
this.skip();
} else {
throw e;
}
}
})
});
}
});
});

View File

@ -1,118 +0,0 @@
var fs = require("fs");
var path = require("path");
var chai = require("chai");
var expect = chai.expect;
var mlog = require("mocha-logger");
describe("Translations have the same keys as en.js", function() {
var translations = require("../../../translations/translations.js");
var base = JSON.parse(stripComments(fs.readFileSync("translations/en.json", "utf8")));
var baseKeys = Object.keys(base).sort();
Object.keys(translations).forEach(function(tr) {
var fileName = translations[tr];
var fileContent = stripComments(fs.readFileSync(fileName, "utf8"));
var fileTranslations = JSON.parse(fileContent);
var fileKeys = Object.keys(fileTranslations).sort();
it(fileName + " keys should be in base", function() {
fileKeys.forEach(function(key) {
expect( baseKeys.indexOf(key) ).to.be.at.least(0);
});
});
it(fileName + " should contain all base keys", function() {
var test = this;
baseKeys.forEach(function(key) {
// TODO: when all translations are fixed, use
// expect(fileKeys).to.deep.equal(baseKeys);
// instead of the try-catch-block
try {
expect(fileKeys).to.deep.equal(baseKeys);
} catch(e) {
if (e instanceof chai.AssertionError) {
diff = baseKeys.filter(function(x) { return fileKeys.indexOf(x) < 0 });
mlog.pending("Missing Translations for language " + tr + ": ", diff);
test.skip();
} else {
throw e;
}
}
});
});
});
});
// Copied from js/translator.js
function stripComments(str, opts) {
// strip comments copied from: https://github.com/sindresorhus/strip-json-comments
var singleComment = 1;
var multiComment = 2;
function stripWithoutWhitespace() {
return "";
}
function stripWithWhitespace(str, start, end) {
return str.slice(start, end).replace(/\S/g, " ");
}
opts = opts || {};
var currentChar;
var nextChar;
var insideString = false;
var insideComment = false;
var offset = 0;
var ret = "";
var strip = opts.whitespace === false ? stripWithoutWhitespace : stripWithWhitespace;
for (var i = 0; i < str.length; i++) {
currentChar = str[i];
nextChar = str[i + 1];
if (!insideComment && currentChar === "\"") {
var escaped = str[i - 1] === "\\" && str[i - 2] !== "\\";
if (!escaped) {
insideString = !insideString;
}
}
if (insideString) {
continue;
}
if (!insideComment && currentChar + nextChar === "//") {
ret += str.slice(offset, i);
offset = i;
insideComment = singleComment;
i++;
} else if (insideComment === singleComment && currentChar + nextChar === "\r\n") {
i++;
insideComment = false;
ret += strip(str, offset, i);
offset = i;
continue;
} else if (insideComment === singleComment && currentChar === "\n") {
insideComment = false;
ret += strip(str, offset, i);
offset = i;
} else if (!insideComment && currentChar + nextChar === "/*") {
ret += str.slice(offset, i);
offset = i;
insideComment = multiComment;
i++;
continue;
} else if (insideComment === multiComment && currentChar + nextChar === "*/") {
i++;
insideComment = false;
ret += strip(str, offset, i + 1);
offset = i + 1;
continue;
}
}
return ret + (insideComment ? strip(str.substr(offset)) : str.substr(offset));
}