Files
fuelboard-editor/tests/strings-format.test.js

55 lines
2.6 KiB
JavaScript

// FuelBoard Copy Editor — strings-format round-trip tests (node)
const fs = require("fs");
const path = require("path");
const { parseStrings, serializeStrings, escapeStr, unescapeStr } = require(
path.join(__dirname, "..", "strings-format.js"));
const REAL = "/Users/apt/workspace/fuelboard/FuelBoard/en.lproj/Localizable.strings";
const text = fs.readFileSync(REAL, "utf8");
let failures = 0;
function check(name, cond, extra) {
console.log((cond ? "PASS" : "FAIL") + " " + name + (extra ? " [" + extra + "]" : ""));
if (!cond) failures++;
}
// 1. Round-trip: parse -> serialize must reproduce the file byte-for-byte.
const blocks = parseStrings(text);
const out = serializeStrings(blocks);
check("round-trip byte-identical", out === text,
out === text ? "" : `len ${out.length} vs ${text.length}, first diff: ` + firstDiff(out, text));
function firstDiff(a, b) {
const n = Math.min(a.length, b.length);
for (let i = 0; i < n; i++) if (a[i] !== b[i]) return `${i}: ${JSON.stringify(a.slice(i, i + 40))} vs ${JSON.stringify(b.slice(i, i + 40))}`;
return "length differs";
}
// 2. Content stats.
const entries = blocks.filter(b => b.type === "entry");
const comments = blocks.filter(b => b.type === "comment");
const raws = blocks.filter(b => b.type === "raw");
check("107 entries", entries.length === 107, `got ${entries.length}`);
check("no unparseable raw lines", raws.length === 0, `got ${raws.length}`);
check("comment blocks preserved (incl. 4-line header)", comments.length === 9, `got ${comments.length}`);
check("sections detectable (8 banners)", comments.filter(c => /^\/\* .+ \*\/$/.test(c.raw.trim())).length === 8);
// 3. Escape round-trips on tricky values (as found in the file).
const tricky = ['a\\"b', "it\\'s", "a\\\\b", 'say "hi"', "line1\nline2", "tab\there", "plain"];
for (const t of tricky) {
const esc = escapeStr(t);
const back = unescapeStr(esc);
check(`escape round-trip ${JSON.stringify(t)}`, back === t, `esc=${JSON.stringify(esc)} back=${JSON.stringify(back)}`);
}
// 4. Edit a value -> serialize -> the file lint must pass and contain the edit.
const edited = blocks.map(b => b.type === "entry" && b.key === "Welcome to FuelBoard"
? { ...b, value: 'Welcome to FuelBoard — "edited" today \\\\ path' }
: b);
const editedText = serializeStrings(edited);
check("edited value written", editedText.includes('"Welcome to FuelBoard — \\"edited\\" today \\\\\\\\ path";'),
editedText.split("\n").find(l => l.startsWith('"Welcome to FuelBoard')));
fs.writeFileSync("/tmp/fb_strings_edited.strings", editedText);
console.log("edited file written to /tmp/fb_strings_edited.strings");
process.exit(failures ? 1 : 0);