mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-25 08:23:13 +09:00
Refactor getFactorioLocale spec
Fix the incorrect error validation, be more lenient about empty values and convert the checks to use async functions instead of error prone callbacks. Remove excessive getFactorioLocale checks in master.spec.
This commit is contained in:
1 parent
ab2a377ca5
commit
940a7b9180
2 files changed
+59
-70
No files matched your search
@@ -1,74 +1,74 @@
|
|||||||
const assert = require("assert");
|
const assert = require("assert");
|
||||||
const getFactorioLocale = require("./getFactorioLocale");
|
const util = require("util");
|
||||||
|
|
||||||
describe("getFactorioLocale.js", () => {
|
const getFactorioLocale = require("./getFactorioLocale");
|
||||||
describe("asObject()", () => {
|
const asObjectAsync = util.promisify(getFactorioLocale.asObject);
|
||||||
it("requires 2 strings and a function as parameters", () => {
|
|
||||||
let validate = (err) => {
|
describe("getFactorioLocale.js", function() {
|
||||||
return err == "Error: wrong parameters provided"
|
describe("asObject()", function() {
|
||||||
|
it("requires 2 strings and a function as parameters", function() {
|
||||||
|
function validate(err) {
|
||||||
|
return err.message == "Error: wrong parameters provided"
|
||||||
}
|
}
|
||||||
let message = "getFactorioLocale is responding incorrectly to invalid arguments"
|
let message = "getFactorioLocale is responding incorrectly to invalid arguments"
|
||||||
let callback = (err,object) => true;
|
let callback = (err,object) => true;
|
||||||
|
|
||||||
assert.throws(() => getFactorioLocale.asObject(), validate, message);
|
assert.throws(() => getFactorioLocale.asObject(), validate, message);
|
||||||
assert.throws(() => getFactorioLocale.asObject("hello", "I forgot my callback"), validate, message);
|
assert.throws(() => getFactorioLocale.asObject("hello", "I forgot my callback"), validate, message);
|
||||||
assert.throws(() => getFactorioLocale.asObject(undefined, undefined, callback), validate, message);
|
assert.throws(() => getFactorioLocale.asObject(undefined, undefined, callback), validate, message);
|
||||||
|
|
||||||
// the proper way to do it
|
// the proper way to do it
|
||||||
assert.doesNotThrow(() => getFactorioLocale.asObject("factorio_0.15.27", "en", callback), validate, message);
|
getFactorioLocale.asObject("factorio_0.15.27", "en", callback);
|
||||||
});
|
});
|
||||||
it("returns a nested object with base game locale information", (done) => {
|
it("returns a nested object with base game locale information", async function() {
|
||||||
getFactorioLocale.asObject("factorio", "en", callback);
|
let object = await asObjectAsync("factorio", "en");
|
||||||
|
|
||||||
function callback(err, object){
|
assert.equal(typeof object, "object");
|
||||||
assert.equal(typeof object, "object");
|
assert.equal(object["entity-name"]["fish"], "Fish");
|
||||||
assert.equal(object["entity-name"]["fish"], "Fish");
|
assert.equal(object["entity-name"]["small-lamp"], "Lamp");
|
||||||
assert.equal(object["entity-name"]["small-lamp"], "Lamp");
|
let empty_keys = 0;
|
||||||
Object.keys(object).forEach(key => {
|
Object.keys(object).forEach(key => {
|
||||||
// first level of the nested object is always an object
|
// first level of the nested object is always an object
|
||||||
assert.equal(typeof object[key], "object");
|
assert.equal(typeof object[key], "object");
|
||||||
|
|
||||||
Object.keys(object[key]).forEach(key2 => {
|
Object.keys(object[key]).forEach(key2 => {
|
||||||
// second level of the nested object is always a string, nearly always truthy
|
// second level of the nested object is always a string, nearly always truthy
|
||||||
if(key2 != "so-long-and-thanks-for-all-the-fish"){
|
if (!object[key][key2]) {
|
||||||
assert.ok(object[key][key2]);
|
empty_keys++;
|
||||||
assert.equal(typeof object[key][key2], "string");
|
}
|
||||||
}
|
assert.equal(typeof object[key][key2], "string");
|
||||||
});
|
|
||||||
});
|
});
|
||||||
done();
|
|
||||||
}
|
});
|
||||||
|
assert(empty_keys < 10, `Got unusually high amount (${empty_keys}) of empty keys`);
|
||||||
});
|
});
|
||||||
it("has some duplicate keys", (done) => {
|
it("has some duplicate keys", async function() {
|
||||||
getFactorioLocale.asObject("factorio", "en", callback);
|
let object = await asObjectAsync("factorio", "en");
|
||||||
|
|
||||||
function callback(err, object){
|
let arrayOfKeys = [];
|
||||||
let arrayOfKeys = [];
|
// collect all keys in a single array
|
||||||
// collect all keys in a single array
|
Object.keys(object).forEach(key => {
|
||||||
Object.keys(object).forEach(key => {
|
arrayOfKeys.push(key);
|
||||||
arrayOfKeys.push(key);
|
Object.keys(object[key]).forEach(key2 => {
|
||||||
Object.keys(object[key]).forEach(key2 => {
|
arrayOfKeys.push(key2);
|
||||||
arrayOfKeys.push(key2);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
// check for duplicates
|
|
||||||
let duplicateKeys = [];
|
// check for duplicates
|
||||||
let i = 0;
|
let duplicateKeys = [];
|
||||||
arrayOfKeys.forEach(key => {
|
let i = 0;
|
||||||
let o = 0;
|
arrayOfKeys.forEach(key => {
|
||||||
arrayOfKeys.forEach(key2 => {
|
let o = 0;
|
||||||
if(i != o && key == key2){
|
arrayOfKeys.forEach(key2 => {
|
||||||
duplicateKeys.push(key);
|
if(i != o && key == key2){
|
||||||
}
|
duplicateKeys.push(key);
|
||||||
o++
|
}
|
||||||
});
|
o++
|
||||||
i++;
|
|
||||||
});
|
});
|
||||||
// we got 704 but this is a very unexact number
|
i++;
|
||||||
assert(duplicateKeys.length > 10)
|
});
|
||||||
done();
|
// we got 704 but this is a very unexact number
|
||||||
}
|
assert(duplicateKeys.length > 10)
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
+2
-13
@@ -26,22 +26,11 @@ describe('Master server endpoint testing', function() {
|
|||||||
it('should get the basegame factorio locale', async function() {
|
it('should get the basegame factorio locale', async function() {
|
||||||
let res = await get('/api/getFactorioLocale');
|
let res = await get('/api/getFactorioLocale');
|
||||||
let object = res.body;
|
let object = res.body;
|
||||||
// test that it is actual factorio locale, copied from getFactorioLocale.spec.js
|
|
||||||
|
// test that it is looks like a factorio locale
|
||||||
assert.equal(typeof object, "object");
|
assert.equal(typeof object, "object");
|
||||||
assert.equal(object["entity-name"]["fish"], "Fish");
|
assert.equal(object["entity-name"]["fish"], "Fish");
|
||||||
assert.equal(object["entity-name"]["small-lamp"], "Lamp");
|
assert.equal(object["entity-name"]["small-lamp"], "Lamp");
|
||||||
Object.keys(object).forEach(key => {
|
|
||||||
// first level of the nested object is always an object
|
|
||||||
assert.equal(typeof object[key], "object");
|
|
||||||
|
|
||||||
Object.keys(object[key]).forEach(key2 => {
|
|
||||||
// second level of the nested object is always a string, nearly always truthy
|
|
||||||
if(key2 != "so-long-and-thanks-for-all-the-fish") {
|
|
||||||
assert.ok(object[key][key2], `${key}.${key2} is falsy (${object[key][key2]})`);
|
|
||||||
assert.equal(typeof object[key][key2], "string");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
// describe("#GET /api/")
|
// describe("#GET /api/")
|
||||||
|
|||||||
Reference in new issue
Block a user