mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 19:53:36 +00:00
strip comments unit test
This commit is contained in:
parent
d775bc9d7e
commit
d709a44960
@ -218,10 +218,10 @@ var Translator = (function() {
|
|||||||
for (var first in translations) {break;}
|
for (var first in translations) {break;}
|
||||||
|
|
||||||
if (first) {
|
if (first) {
|
||||||
Log.log("Loading core translation fallback file: " + translations[first]);
|
Log.log("Loading core translation fallback file: " + translations[first]);
|
||||||
loadJSON(translations[first], function(translations) {
|
loadJSON(translations[first], function(translations) {
|
||||||
self.coreTranslationsFallback = translations;
|
self.coreTranslationsFallback = translations;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
13
tests/configs/data/StripComments.json
Normal file
13
tests/configs/data/StripComments.json
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
// Escaped
|
||||||
|
"FOO\"BAR": "Today",
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The following lines
|
||||||
|
* represent cardinal directions
|
||||||
|
*/
|
||||||
|
"N": "N",
|
||||||
|
"E": "E",
|
||||||
|
"S": "S",
|
||||||
|
"W": "W"
|
||||||
|
}
|
@ -2,6 +2,7 @@ const chai = require("chai");
|
|||||||
const expect = chai.expect;
|
const expect = chai.expect;
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
|
const helmet = require("helmet");
|
||||||
const {JSDOM} = require("jsdom");
|
const {JSDOM} = require("jsdom");
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
|
|
||||||
@ -10,12 +11,12 @@ describe("Translator", function() {
|
|||||||
|
|
||||||
before(function() {
|
before(function() {
|
||||||
const app = express();
|
const app = express();
|
||||||
|
app.use(helmet());
|
||||||
app.get("/translations/:file", function(req, res) {
|
app.use(function (req, res, next) {
|
||||||
res.status(200)
|
res.header("Access-Control-Allow-Origin", "*");
|
||||||
.header("Access-Control-Allow-Origin", "*")
|
next();
|
||||||
.json(require(path.join(__dirname, "..", "..", "..", "tests", "configs", "data", req.params.file)));
|
|
||||||
});
|
});
|
||||||
|
app.use("/translations", express.static(path.join(__dirname, "..", "..", "..", "tests", "configs", "data")));
|
||||||
|
|
||||||
server = app.listen(3000);
|
server = app.listen(3000);
|
||||||
});
|
});
|
||||||
@ -180,6 +181,26 @@ describe("Translator", function() {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should strip comments", function(done) {
|
||||||
|
const dom = new JSDOM(`<script>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;
|
||||||
|
const file = "StripComments.json";
|
||||||
|
|
||||||
|
Translator.load(mmm, file, false, function() {
|
||||||
|
expect(Translator.translations[mmm.name]).to.be.deep.equal({
|
||||||
|
"FOO\"BAR": "Today",
|
||||||
|
"N": "N",
|
||||||
|
"E": "E",
|
||||||
|
"S": "S",
|
||||||
|
"W": "W"
|
||||||
|
});
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
it("should not load translations, if module fallback exists", function(done) {
|
it("should not load translations, if module fallback exists", function(done) {
|
||||||
const dom = new JSDOM(`<script>var Log = {log: function(){}};</script><script src="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
|
const dom = new JSDOM(`<script>var Log = {log: function(){}};</script><script src="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
|
||||||
resources: "usable" });
|
resources: "usable" });
|
||||||
@ -209,7 +230,7 @@ describe("Translator", function() {
|
|||||||
describe("loadCoreTranslations", function() {
|
describe("loadCoreTranslations", function() {
|
||||||
it("should load core translations and fallback", function(done) {
|
it("should load core translations and fallback", function(done) {
|
||||||
const dom = new JSDOM(`<script>var translations = {en: "http://localhost:3000/translations/en.json"}; var Log = {log: function(){}};</script>\
|
const dom = new JSDOM(`<script>var translations = {en: "http://localhost:3000/translations/en.json"}; var Log = {log: function(){}};</script>\
|
||||||
<script src="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
|
<script src="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
|
||||||
resources: "usable" });
|
resources: "usable" });
|
||||||
dom.window.onload = function() {
|
dom.window.onload = function() {
|
||||||
const {Translator} = dom.window;
|
const {Translator} = dom.window;
|
||||||
@ -226,7 +247,7 @@ describe("Translator", function() {
|
|||||||
|
|
||||||
it("should load core fallback if language cannot be found", function(done) {
|
it("should load core fallback if language cannot be found", function(done) {
|
||||||
const dom = new JSDOM(`<script>var translations = {en: "http://localhost:3000/translations/en.json"}; var Log = {log: function(){}};</script>\
|
const dom = new JSDOM(`<script>var translations = {en: "http://localhost:3000/translations/en.json"}; var Log = {log: function(){}};</script>\
|
||||||
<script src="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
|
<script src="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
|
||||||
resources: "usable" });
|
resources: "usable" });
|
||||||
dom.window.onload = function() {
|
dom.window.onload = function() {
|
||||||
const {Translator} = dom.window;
|
const {Translator} = dom.window;
|
||||||
@ -242,37 +263,36 @@ describe("Translator", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("loadCoreTranslationsFallback", function() {
|
describe("loadCoreTranslationsFallback", function() {
|
||||||
it("should load core translations fallback", function(done) {
|
it("should load core translations fallback", function(done) {
|
||||||
const dom = new JSDOM(`<script>var translations = {en: "http://localhost:3000/translations/en.json"}; var Log = {log: function(){}};</script>\
|
const dom = new JSDOM(`<script>var translations = {en: "http://localhost:3000/translations/en.json"}; var Log = {log: function(){}};</script>\
|
||||||
<script src="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
|
<script src="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
|
||||||
resources: "usable" });
|
resources: "usable" });
|
||||||
dom.window.onload = function() {
|
dom.window.onload = function() {
|
||||||
const {Translator} = dom.window;
|
const {Translator} = dom.window;
|
||||||
Translator.loadCoreTranslationsFallback();
|
Translator.loadCoreTranslationsFallback();
|
||||||
|
|
||||||
const en = require(path.join(__dirname, "..", "..", "..", "tests", "configs", "data", "en.json"));
|
const en = require(path.join(__dirname, "..", "..", "..", "tests", "configs", "data", "en.json"));
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
expect(Translator.coreTranslationsFallback).to.be.deep.equal(en);
|
expect(Translator.coreTranslationsFallback).to.be.deep.equal(en);
|
||||||
done();
|
done();
|
||||||
}, 500);
|
}, 500);
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should load core fallback if language cannot be found", function(done) {
|
it("should load core fallback if language cannot be found", function(done) {
|
||||||
const dom = new JSDOM(`<script>var translations = {}; var Log = {log: function(){}};</script>\
|
const dom = new JSDOM(`<script>var translations = {}; var Log = {log: function(){}};</script>\
|
||||||
<script src="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
|
<script src="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
|
||||||
resources: "usable" });
|
resources: "usable" });
|
||||||
dom.window.onload = function() {
|
dom.window.onload = function() {
|
||||||
const {Translator} = dom.window;
|
const {Translator} = dom.window;
|
||||||
Translator.loadCoreTranslations();
|
Translator.loadCoreTranslations();
|
||||||
|
|
||||||
const en = require(path.join(__dirname, "..", "..", "..", "tests", "configs", "data", "en.json"));
|
setTimeout(function() {
|
||||||
setTimeout(function() {
|
expect(Translator.coreTranslationsFallback).to.be.deep.equal({});
|
||||||
expect(Translator.coreTranslationsFallback).to.be.deep.equal({});
|
done();
|
||||||
done();
|
}, 500);
|
||||||
}, 500);
|
};
|
||||||
};
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user