mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-19 15:33:12 +09:00
Refactor getFactorioLocale
Move getFactorioLocale to the new lib/factorio namespace and refactor it to use async functions instead of callbacks. Split tests into integration with factorio and testing of the function itself. Also removes the cacheing from the function itself as this breaks the isolated testing of it.
This commit is contained in:
1 parent
a01b971ba2
commit
93480c8b29
8 files changed
+125
-132
No files matched your search
@@ -0,0 +1,4 @@
|
||||
; Test data for getLocale
|
||||
[test]
|
||||
key-a=1
|
||||
key-b=2
|
||||
@@ -0,0 +1,62 @@
|
||||
const assert = require("assert").strict;
|
||||
const path = require("path");
|
||||
|
||||
const factorio = require("lib/factorio");
|
||||
|
||||
|
||||
describe("Integration of lib/factorio/locale", function() {
|
||||
describe("getLocale()", function() {
|
||||
let locale;
|
||||
before(async function() {
|
||||
locale = await factorio.getLocale(path.join("factorio", "data"), "en");
|
||||
});
|
||||
|
||||
it("returns a nested object with base game locale information", async function() {
|
||||
assert.equal(typeof locale, "object");
|
||||
assert.equal(locale["entity-name"]["fish"], "Fish");
|
||||
assert.equal(locale["entity-name"]["small-lamp"], "Lamp");
|
||||
let empty_keys = 0;
|
||||
Object.keys(locale).forEach(key => {
|
||||
// first level of the nested object is always an object
|
||||
assert.equal(typeof locale[key], "object");
|
||||
|
||||
Object.keys(locale[key]).forEach(key2 => {
|
||||
// second level of the nested object is always a string, nearly always truthy
|
||||
if (!locale[key][key2]) {
|
||||
empty_keys++;
|
||||
}
|
||||
assert.equal(typeof locale[key][key2], "string");
|
||||
});
|
||||
|
||||
});
|
||||
assert(empty_keys < 10, `Got unusually high amount (${empty_keys}) of empty keys`);
|
||||
});
|
||||
|
||||
it("has some duplicate keys", async function() {
|
||||
let arrayOfKeys = [];
|
||||
// collect all keys in a single array
|
||||
Object.keys(locale).forEach(key => {
|
||||
arrayOfKeys.push(key);
|
||||
Object.keys(locale[key]).forEach(key2 => {
|
||||
arrayOfKeys.push(key2);
|
||||
});
|
||||
});
|
||||
|
||||
// check for duplicates
|
||||
let duplicateKeys = [];
|
||||
let i = 0;
|
||||
arrayOfKeys.forEach(key => {
|
||||
let o = 0;
|
||||
arrayOfKeys.forEach(key2 => {
|
||||
if(i != o && key == key2){
|
||||
duplicateKeys.push(key);
|
||||
}
|
||||
o++
|
||||
});
|
||||
i++;
|
||||
});
|
||||
// we got 704 but this is a very unexact number
|
||||
assert(duplicateKeys.length > 10)
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,18 @@
|
||||
const assert = require("assert").strict;
|
||||
const path = require("path");
|
||||
|
||||
const factorio = require("lib/factorio");
|
||||
|
||||
|
||||
describe("lib/factorio/locale", function() {
|
||||
describe("getLocale()", function() {
|
||||
it("verifies types of the arguments", async function() {
|
||||
await assert.rejects(factorio.getLocale(1, "a"), new TypeError("dataDir must be a string"));
|
||||
await assert.rejects(factorio.getLocale("a", 1), new TypeError("languageCode must be a string"));
|
||||
});
|
||||
it("returns a nested object with locale definitions", async function() {
|
||||
let locale = await factorio.getLocale(path.join("test", "file", "factorio", "data"), "en");
|
||||
assert.deepEqual(locale, { test: { "key-a": "1", "key-b": "2" } });
|
||||
});
|
||||
});;
|
||||
});
|
||||
@@ -1,74 +0,0 @@
|
||||
const assert = require("assert");
|
||||
const util = require("util");
|
||||
|
||||
const getFactorioLocale = require("lib/getFactorioLocale");
|
||||
const asObjectAsync = util.promisify(getFactorioLocale.asObject);
|
||||
|
||||
describe("getFactorioLocale.js", function() {
|
||||
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 callback = (err,object) => true;
|
||||
|
||||
assert.throws(() => getFactorioLocale.asObject(), validate, message);
|
||||
assert.throws(() => getFactorioLocale.asObject("hello", "I forgot my callback"), validate, message);
|
||||
assert.throws(() => getFactorioLocale.asObject(undefined, undefined, callback), validate, message);
|
||||
|
||||
// the proper way to do it
|
||||
getFactorioLocale.asObject("factorio_0.15.27", "en", callback);
|
||||
});
|
||||
it("returns a nested object with base game locale information", async function() {
|
||||
let object = await asObjectAsync("factorio", "en");
|
||||
|
||||
assert.equal(typeof object, "object");
|
||||
assert.equal(object["entity-name"]["fish"], "Fish");
|
||||
assert.equal(object["entity-name"]["small-lamp"], "Lamp");
|
||||
let empty_keys = 0;
|
||||
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 (!object[key][key2]) {
|
||||
empty_keys++;
|
||||
}
|
||||
assert.equal(typeof object[key][key2], "string");
|
||||
});
|
||||
|
||||
});
|
||||
assert(empty_keys < 10, `Got unusually high amount (${empty_keys}) of empty keys`);
|
||||
});
|
||||
it("has some duplicate keys", async function() {
|
||||
let object = await asObjectAsync("factorio", "en");
|
||||
|
||||
let arrayOfKeys = [];
|
||||
// collect all keys in a single array
|
||||
Object.keys(object).forEach(key => {
|
||||
arrayOfKeys.push(key);
|
||||
Object.keys(object[key]).forEach(key2 => {
|
||||
arrayOfKeys.push(key2);
|
||||
});
|
||||
});
|
||||
|
||||
// check for duplicates
|
||||
let duplicateKeys = [];
|
||||
let i = 0;
|
||||
arrayOfKeys.forEach(key => {
|
||||
let o = 0;
|
||||
arrayOfKeys.forEach(key2 => {
|
||||
if(i != o && key == key2){
|
||||
duplicateKeys.push(key);
|
||||
}
|
||||
o++
|
||||
});
|
||||
i++;
|
||||
});
|
||||
// we got 704 but this is a very unexact number
|
||||
assert(duplicateKeys.length > 10)
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user