Fix existing tests, update to jest

This commit is contained in:
David Edmondson 2021-08-26 16:16:38 -07:00
parent 7bfceb1690
commit 1a8bcf66cc
7 changed files with 656 additions and 650 deletions

@ -5,12 +5,6 @@ import { RunningScript } from "../../src/Script/RunningScript";
import { Script } from "../../src/Script/Script"; import { Script } from "../../src/Script/Script";
import { SourceFileFlags } from "../../src/SourceFile/SourceFileFlags"; import { SourceFileFlags } from "../../src/SourceFile/SourceFileFlags";
import { expect } from "chai";
console.log("Beginning Netscript Dynamic RAM Calculation/Generation Tests");
console.log("Beginning Netscript Static RAM Calculation/Generation Tests");
const ScriptBaseCost = RamCostConstants.ScriptBaseRamCost; const ScriptBaseCost = RamCostConstants.ScriptBaseRamCost;
describe("Netscript Dynamic RAM Calculation/Generation Tests", function() { describe("Netscript Dynamic RAM Calculation/Generation Tests", function() {
@ -27,12 +21,13 @@ describe("Netscript Dynamic RAM Calculation/Generation Tests", function() {
// Tests numeric equality, allowing for floating point imprecision // Tests numeric equality, allowing for floating point imprecision
function testEquality(val, expected) { function testEquality(val, expected) {
expect(val).to.be.within(expected - 100 * Number.EPSILON, expected + 100 * Number.EPSILON); expect(val).toBeGreaterThanOrEqual(expected - 100 * Number.EPSILON);
expect(val).toBeLessThanOrEqual(expected + 100 * Number.EPSILON);
} }
// Runs a Netscript function and properly catches it if it returns promise // Runs a Netscript function and properly catches it if it returns promise
function runPotentiallyAsyncFunction(fn) { function runPotentiallyAsyncFunction(fn) {
let res = fn(); const res = fn();
if (res instanceof Promise) { if (res instanceof Promise) {
res.catch(() => undefined); res.catch(() => undefined);
} }
@ -47,9 +42,9 @@ describe("Netscript Dynamic RAM Calculation/Generation Tests", function() {
* including the namespace(s). e.g. ["gang", "getMemberNames"] * including the namespace(s). e.g. ["gang", "getMemberNames"]
*/ */
async function testNonzeroDynamicRamCost(fnDesc) { async function testNonzeroDynamicRamCost(fnDesc) {
if (!Array.isArray(fnDesc)) { expect.fail("Non-array passed to testNonzeroDynamicRamCost()"); } if (!Array.isArray(fnDesc)) { throw new Error("Non-array passed to testNonzeroDynamicRamCost()"); }
const expected = getRamCost(...fnDesc); const expected = getRamCost(...fnDesc);
expect(expected).to.be.above(0); expect(expected).toBeGreaterThan(0);
const code = `${fnDesc.join(".")}();` const code = `${fnDesc.join(".")}();`
@ -72,7 +67,7 @@ describe("Netscript Dynamic RAM Calculation/Generation Tests", function() {
let curr = scope[fnDesc[0]]; let curr = scope[fnDesc[0]];
for (let i = 1; i < fnDesc.length; ++i) { for (let i = 1; i < fnDesc.length; ++i) {
if (curr == null) { if (curr == null) {
expect.fail(`Invalid function specified: [${fnDesc}]`); throw new Error(`Invalid function specified: [${fnDesc}]`);
} }
if (typeof curr === "function") { if (typeof curr === "function") {
@ -92,13 +87,13 @@ describe("Netscript Dynamic RAM Calculation/Generation Tests", function() {
runPotentiallyAsyncFunction(curr); runPotentiallyAsyncFunction(curr);
} catch(e) {} } catch(e) {}
} else { } else {
expect.fail(`Invalid function specified: [${fndesc}]`); throw new Error(`Invalid function specified: [${fnDesc}]`);
} }
const fnName = fnDesc[fnDesc.length - 1]; const fnName = fnDesc[fnDesc.length - 1];
testEquality(workerScript.dynamicRamUsage - ScriptBaseCost, expected); testEquality(workerScript.dynamicRamUsage - ScriptBaseCost, expected);
testEquality(workerScript.dynamicRamUsage, runningScript.ramUsage); testEquality(workerScript.dynamicRamUsage, runningScript.ramUsage);
expect(workerScript.dynamicLoadedFns).to.have.property(fnName); expect(workerScript.dynamicLoadedFns).toHaveProperty(fnName);
} }
/** /**
@ -110,9 +105,9 @@ describe("Netscript Dynamic RAM Calculation/Generation Tests", function() {
* including the namespace(s). e.g. ["gang", "getMemberNames"] * including the namespace(s). e.g. ["gang", "getMemberNames"]
*/ */
async function testZeroDynamicRamCost(fnDesc) { async function testZeroDynamicRamCost(fnDesc) {
if (!Array.isArray(fnDesc)) { expect.fail("Non-array passed to testZeroDynamicRamCost()"); } if (!Array.isArray(fnDesc)) { throw new Error("Non-array passed to testZeroDynamicRamCost()"); }
const expected = getRamCost(...fnDesc); const expected = getRamCost(...fnDesc);
expect(expected).to.equal(0); expect(expected).toEqual(0);
const code = `${fnDesc.join(".")}();` const code = `${fnDesc.join(".")}();`
@ -135,7 +130,7 @@ describe("Netscript Dynamic RAM Calculation/Generation Tests", function() {
let curr = scope[fnDesc[0]]; let curr = scope[fnDesc[0]];
for (let i = 1; i < fnDesc.length; ++i) { for (let i = 1; i < fnDesc.length; ++i) {
if (curr == null) { if (curr == null) {
expect.fail(`Invalid function specified: [${fnDesc}]`); throw new Error(`Invalid function specified: [${fnDesc}]`);
} }
if (typeof curr === "function") { if (typeof curr === "function") {
@ -155,20 +150,20 @@ describe("Netscript Dynamic RAM Calculation/Generation Tests", function() {
runPotentiallyAsyncFunction(curr); runPotentiallyAsyncFunction(curr);
} catch(e) {} } catch(e) {}
} else { } else {
expect.fail(`Invalid function specified: [${fndesc}]`); throw new Error(`Invalid function specified: [${fndesc}]`);
} }
testEquality(workerScript.dynamicRamUsage, ScriptBaseCost); testEquality(workerScript.dynamicRamUsage, ScriptBaseCost);
testEquality(workerScript.dynamicRamUsage, runningScript.ramUsage); testEquality(workerScript.dynamicRamUsage, runningScript.ramUsage);
} }
before(function() { beforeEach(function() {
for (let i = 0; i < SourceFileFlags.length; ++i) { for (let i = 0; i < SourceFileFlags.length; ++i) {
SourceFileFlags[i] = 3; SourceFileFlags[i] = 3;
} }
}); });
describe("Basic Functions", async function() { describe("Basic Functions", function() {
it("hack()", async function() { it("hack()", async function() {
const f = ["hack"]; const f = ["hack"];
await testNonzeroDynamicRamCost(f); await testNonzeroDynamicRamCost(f);
@ -570,14 +565,14 @@ describe("Netscript Dynamic RAM Calculation/Generation Tests", function() {
}); });
}); });
describe("Advanced Functions", async function() { describe("Advanced Functions", function() {
it("getBitNodeMultipliers()", async function() { it("getBitNodeMultipliers()", async function() {
const f = ["getBitNodeMultipliers"]; const f = ["getBitNodeMultipliers"];
await testNonzeroDynamicRamCost(f); await testNonzeroDynamicRamCost(f);
}); });
}); });
describe("TIX API", async function() { describe("TIX API", function() {
it("getStockSymbols()", async function() { it("getStockSymbols()", async function() {
const f = ["getStockSymbols"]; const f = ["getStockSymbols"];
await testNonzeroDynamicRamCost(f); await testNonzeroDynamicRamCost(f);
@ -664,7 +659,7 @@ describe("Netscript Dynamic RAM Calculation/Generation Tests", function() {
}); });
}); });
describe("Singularity Functions", async function() { describe("Singularity Functions", function() {
it("universityCourse()", async function() { it("universityCourse()", async function() {
const f = ["universityCourse"]; const f = ["universityCourse"];
await testNonzeroDynamicRamCost(f); await testNonzeroDynamicRamCost(f);
@ -831,7 +826,7 @@ describe("Netscript Dynamic RAM Calculation/Generation Tests", function() {
}); });
}); });
describe("Bladeburner API", async function() { describe("Bladeburner API", function() {
it("getContractNames()", async function() { it("getContractNames()", async function() {
const f = ["bladeburner", "getContractNames"]; const f = ["bladeburner", "getContractNames"];
await testNonzeroDynamicRamCost(f); await testNonzeroDynamicRamCost(f);
@ -1003,7 +998,7 @@ describe("Netscript Dynamic RAM Calculation/Generation Tests", function() {
}); });
}); });
describe("Gang API", async function() { describe("Gang API", function() {
it("getMemberNames()", async function() { it("getMemberNames()", async function() {
const f = ["gang", "getMemberNames"]; const f = ["gang", "getMemberNames"];
await testNonzeroDynamicRamCost(f); await testNonzeroDynamicRamCost(f);
@ -1085,7 +1080,7 @@ describe("Netscript Dynamic RAM Calculation/Generation Tests", function() {
}); });
}); });
describe("Coding Contract API", async function() { describe("Coding Contract API", function() {
it("attempt()", async function() { it("attempt()", async function() {
const f = ["codingcontract", "attempt"]; const f = ["codingcontract", "attempt"];
await testNonzeroDynamicRamCost(f); await testNonzeroDynamicRamCost(f);
@ -1112,7 +1107,7 @@ describe("Netscript Dynamic RAM Calculation/Generation Tests", function() {
}); });
}); });
describe("Sleeve API", async function() { describe("Sleeve API", function() {
it("getNumSleeves()", async function() { it("getNumSleeves()", async function() {
const f = ["sleeve", "getNumSleeves"]; const f = ["sleeve", "getNumSleeves"];
await testNonzeroDynamicRamCost(f); await testNonzeroDynamicRamCost(f);

@ -1,8 +1,5 @@
import { getRamCost, RamCostConstants } from "../../src/Netscript/RamCostGenerator"; import { getRamCost, RamCostConstants } from "../../src/Netscript/RamCostGenerator";
import { calculateRamUsage } from "../../src/Script/RamCalculations" import { calculateRamUsage } from "../../src/Script/RamCalculations"
import { expect } from "chai";
console.log("Beginning Netscript Static RAM Calculation/Generation Tests");
const ScriptBaseCost = RamCostConstants.ScriptBaseRamCost; const ScriptBaseCost = RamCostConstants.ScriptBaseRamCost;
const HacknetNamespaceCost = RamCostConstants.ScriptHacknetNodesRamCost; const HacknetNamespaceCost = RamCostConstants.ScriptHacknetNodesRamCost;
@ -10,7 +7,8 @@ const HacknetNamespaceCost = RamCostConstants.ScriptHacknetNodesRamCost;
describe("Netscript Static RAM Calculation/Generation Tests", function() { describe("Netscript Static RAM Calculation/Generation Tests", function() {
// Tests numeric equality, allowing for floating point imprecision // Tests numeric equality, allowing for floating point imprecision
function testEquality(val, expected) { function testEquality(val, expected) {
expect(val).to.be.within(expected - 100 * Number.EPSILON, expected + 100 * Number.EPSILON); expect(val).toBeGreaterThanOrEqual(expected - 100 * Number.EPSILON);
expect(val).toBeLessThanOrEqual(expected + 100 * Number.EPSILON);
} }
/** /**
@ -24,7 +22,7 @@ describe("Netscript Static RAM Calculation/Generation Tests", function() {
async function expectNonZeroRamCost(fnDesc) { async function expectNonZeroRamCost(fnDesc) {
if (!Array.isArray(fnDesc)) { expect.fail("Non-array passed to expectNonZeroRamCost()"); } if (!Array.isArray(fnDesc)) { expect.fail("Non-array passed to expectNonZeroRamCost()"); }
const expected = getRamCost(...fnDesc); const expected = getRamCost(...fnDesc);
expect(expected).to.be.above(0); expect(expected).toBeGreaterThan(0);
const code = fnDesc.join(".") + "(); "; const code = fnDesc.join(".") + "(); ";
@ -33,7 +31,7 @@ describe("Netscript Static RAM Calculation/Generation Tests", function() {
const multipleCallsCode = code.repeat(3); const multipleCallsCode = code.repeat(3);
const multipleCallsCalculated = await calculateRamUsage(multipleCallsCode, []); const multipleCallsCalculated = await calculateRamUsage(multipleCallsCode, []);
expect(multipleCallsCalculated).to.equal(calculated); expect(multipleCallsCalculated).toEqual(calculated);
} }
/** /**
@ -47,7 +45,7 @@ describe("Netscript Static RAM Calculation/Generation Tests", function() {
async function expectZeroRamCost(fnDesc) { async function expectZeroRamCost(fnDesc) {
if (!Array.isArray(fnDesc)) { expect.fail("Non-array passed to expectZeroRamCost()"); } if (!Array.isArray(fnDesc)) { expect.fail("Non-array passed to expectZeroRamCost()"); }
const expected = getRamCost(...fnDesc); const expected = getRamCost(...fnDesc);
expect(expected).to.equal(0); expect(expected).toEqual(0);
const code = fnDesc.join(".") + "(); "; const code = fnDesc.join(".") + "(); ";
@ -55,10 +53,10 @@ describe("Netscript Static RAM Calculation/Generation Tests", function() {
testEquality(calculated, ScriptBaseCost); testEquality(calculated, ScriptBaseCost);
const multipleCallsCalculated = await calculateRamUsage(code, []); const multipleCallsCalculated = await calculateRamUsage(code, []);
expect(multipleCallsCalculated).to.equal(ScriptBaseCost); expect(multipleCallsCalculated).toEqual(ScriptBaseCost);
} }
describe("Basic Functions", async function() { describe("Basic Functions", function() {
it("hack()", async function() { it("hack()", async function() {
const f = ["hack"]; const f = ["hack"];
await expectNonZeroRamCost(f); await expectNonZeroRamCost(f);
@ -460,14 +458,14 @@ describe("Netscript Static RAM Calculation/Generation Tests", function() {
}); });
}); });
describe("Advanced Functions", async function() { describe("Advanced Functions", function() {
it("getBitNodeMultipliers()", async function() { it("getBitNodeMultipliers()", async function() {
const f = ["getBitNodeMultipliers"]; const f = ["getBitNodeMultipliers"];
await expectNonZeroRamCost(f); await expectNonZeroRamCost(f);
}); });
}); });
describe("Hacknet Node API", async function() { describe("Hacknet Node API", function() {
// The Hacknet Node API RAM cost is a bit different because // The Hacknet Node API RAM cost is a bit different because
// it's just a one-time cost to access the 'hacknet' namespace. // it's just a one-time cost to access the 'hacknet' namespace.
// Otherwise, all functions cost 0 RAM // Otherwise, all functions cost 0 RAM
@ -490,7 +488,7 @@ describe("Netscript Static RAM Calculation/Generation Tests", function() {
] ]
it("should have zero RAM cost for all functions", function() { it("should have zero RAM cost for all functions", function() {
for (const fn of apiFunctions) { for (const fn of apiFunctions) {
expect(getRamCost("hacknet", fn)).to.equal(0); expect(getRamCost("hacknet", fn)).toEqual(0);
} }
}); });
@ -505,7 +503,7 @@ describe("Netscript Static RAM Calculation/Generation Tests", function() {
}); });
}); });
describe("TIX API", async function() { describe("TIX API", function() {
it("getStockSymbols()", async function() { it("getStockSymbols()", async function() {
const f = ["getStockSymbols"]; const f = ["getStockSymbols"];
await expectNonZeroRamCost(f); await expectNonZeroRamCost(f);
@ -602,7 +600,7 @@ describe("Netscript Static RAM Calculation/Generation Tests", function() {
}); });
}); });
describe("Singularity Functions", async function() { describe("Singularity Functions", function() {
it("universityCourse()", async function() { it("universityCourse()", async function() {
const f = ["universityCourse"]; const f = ["universityCourse"];
await expectNonZeroRamCost(f); await expectNonZeroRamCost(f);
@ -769,7 +767,7 @@ describe("Netscript Static RAM Calculation/Generation Tests", function() {
}); });
}); });
describe("Bladeburner API", async function() { describe("Bladeburner API", function() {
it("getContractNames()", async function() { it("getContractNames()", async function() {
const f = ["bladeburner", "getContractNames"]; const f = ["bladeburner", "getContractNames"];
await expectNonZeroRamCost(f); await expectNonZeroRamCost(f);
@ -941,7 +939,7 @@ describe("Netscript Static RAM Calculation/Generation Tests", function() {
}); });
}); });
describe("Gang API", async function() { describe("Gang API", function() {
it("getMemberNames()", async function() { it("getMemberNames()", async function() {
const f = ["gang", "getMemberNames"]; const f = ["gang", "getMemberNames"];
await expectNonZeroRamCost(f); await expectNonZeroRamCost(f);
@ -1023,7 +1021,7 @@ describe("Netscript Static RAM Calculation/Generation Tests", function() {
}); });
}); });
describe("Coding Contract API", async function() { describe("Coding Contract API", function() {
it("attempt()", async function() { it("attempt()", async function() {
const f = ["codingcontract", "attempt"]; const f = ["codingcontract", "attempt"];
await expectNonZeroRamCost(f); await expectNonZeroRamCost(f);
@ -1050,7 +1048,7 @@ describe("Netscript Static RAM Calculation/Generation Tests", function() {
}); });
}); });
describe("Sleeve API", async function() { describe("Sleeve API", function() {
it("getNumSleeves()", async function() { it("getNumSleeves()", async function() {
const f = ["sleeve", "getNumSleeves"]; const f = ["sleeve", "getNumSleeves"];
await expectNonZeroRamCost(f); await expectNonZeroRamCost(f);

File diff suppressed because it is too large Load Diff

@ -0,0 +1,33 @@
import { convertTimeMsToTimeElapsedString } from "../utils/StringHelperFunctions";
describe("StringHelperFunctions Tests", function () {
it("transforms strings", () => {
expect(convertTimeMsToTimeElapsedString(1000)).toEqual("1 seconds");
expect(convertTimeMsToTimeElapsedString(5 * 60 * 1000 + 34 * 1000)).toEqual(
"5 minutes 34 seconds",
);
expect(
convertTimeMsToTimeElapsedString(
2 * 60 * 60 * 24 * 1000 + 5 * 60 * 1000 + 34 * 1000,
),
).toEqual("2 days 5 minutes 34 seconds");
expect(
convertTimeMsToTimeElapsedString(
2 * 60 * 60 * 24 * 1000 + 5 * 60 * 1000 + 34 * 1000,
true,
),
).toEqual("2 days 5 minutes 34.000 seconds");
expect(
convertTimeMsToTimeElapsedString(
2 * 60 * 60 * 24 * 1000 + 5 * 60 * 1000 + 34 * 1000 + 123,
true,
),
).toEqual("2 days 5 minutes 34.123 seconds");
expect(
convertTimeMsToTimeElapsedString(
2 * 60 * 60 * 24 * 1000 + 5 * 60 * 1000 + 34 * 1000 + 123.888,
true,
),
).toEqual("2 days 5 minutes 34.123 seconds");
})
});

@ -1,11 +0,0 @@
import { expect } from "chai";
import { convertTimeMsToTimeElapsedString } from "../utils/StringHelperFunctions";
describe("StringHelperFunctions Tests", function() {
expect(convertTimeMsToTimeElapsedString(1000)).to.equal("1 seconds");
expect(convertTimeMsToTimeElapsedString(5*60*1000+34*1000)).to.equal("5 minutes 34 seconds");
expect(convertTimeMsToTimeElapsedString(2*60*60*24*1000+5*60*1000+34*1000)).to.equal("2 days 5 minutes 34 seconds");
expect(convertTimeMsToTimeElapsedString(2*60*60*24*1000+5*60*1000+34*1000, true)).to.equal("2 days 5 minutes 34.000 seconds");
expect(convertTimeMsToTimeElapsedString(2*60*60*24*1000+5*60*1000+34*1000+123, true)).to.equal("2 days 5 minutes 34.123 seconds");
expect(convertTimeMsToTimeElapsedString(2*60*60*24*1000+5*60*1000+34*1000+123.888, true)).to.equal("2 days 5 minutes 34.123 seconds");
})

@ -0,0 +1,295 @@
import * as dirHelpers from "../../src/Terminal/DirectoryHelpers";
describe("Terminal Directory Tests", function() {
describe("removeLeadingSlash()", function() {
const removeLeadingSlash = dirHelpers.removeLeadingSlash;
it("should remove first slash in a string", function() {
expect(removeLeadingSlash("/")).toEqual("");
expect(removeLeadingSlash("/foo.txt")).toEqual("foo.txt");
expect(removeLeadingSlash("/foo/file.txt")).toEqual("foo/file.txt");
});
it("should only remove one slash", function() {
expect(removeLeadingSlash("///")).toEqual("//");
expect(removeLeadingSlash("//foo")).toEqual("/foo");
});
it("should do nothing for a string that doesn't start with a slash", function() {
expect(removeLeadingSlash("foo.txt")).toEqual("foo.txt");
expect(removeLeadingSlash("foo/test.txt")).toEqual("foo/test.txt");
});
it("should not fail on an empty string", function() {
expect(removeLeadingSlash.bind(null, "")).not.toThrow();
expect(removeLeadingSlash("")).toEqual("");
});
});
describe("removeTrailingSlash()", function() {
const removeTrailingSlash = dirHelpers.removeTrailingSlash;
it("should remove last slash in a string", function() {
expect(removeTrailingSlash("/")).toEqual("");
expect(removeTrailingSlash("foo.txt/")).toEqual("foo.txt");
expect(removeTrailingSlash("foo/file.txt/")).toEqual("foo/file.txt");
});
it("should only remove one slash", function() {
expect(removeTrailingSlash("///")).toEqual("//");
expect(removeTrailingSlash("foo//")).toEqual("foo/");
});
it("should do nothing for a string that doesn't end with a slash", function() {
expect(removeTrailingSlash("foo.txt")).toEqual("foo.txt");
expect(removeTrailingSlash("foo/test.txt")).toEqual("foo/test.txt");
});
it("should not fail on an empty string", function() {
expect(removeTrailingSlash.bind(null, "")).not.toThrow();
expect(removeTrailingSlash("")).toEqual("");
});
});
describe("isValidFilename()", function() {
const isValidFilename = dirHelpers.isValidFilename;
it("should return true for valid filenames", function() {
expect(isValidFilename("test.txt")).toEqual(true);
expect(isValidFilename("123.script")).toEqual(true);
expect(isValidFilename("foo123.b")).toEqual(true);
expect(isValidFilename("my_script.script")).toEqual(true);
expect(isValidFilename("my-script.script")).toEqual(true);
expect(isValidFilename("_foo.lit")).toEqual(true);
expect(isValidFilename("mult.periods.script")).toEqual(true);
expect(isValidFilename("mult.per-iods.again.script")).toEqual(true);
expect(isValidFilename("BruteSSH.exe-50%-INC")).toEqual(true);
expect(isValidFilename("DeepscanV1.exe-1.01%-INC")).toEqual(true);
expect(isValidFilename("DeepscanV2.exe-1.00%-INC")).toEqual(true);
expect(isValidFilename("AutoLink.exe-1.%-INC")).toEqual(true);
});
it("should return false for invalid filenames", function() {
expect(isValidFilename("foo")).toEqual(false);
expect(isValidFilename("my script.script")).toEqual(false);
expect(isValidFilename("a^.txt")).toEqual(false);
expect(isValidFilename("b#.lit")).toEqual(false);
expect(isValidFilename("lib().js")).toEqual(false);
expect(isValidFilename("foo.script_")).toEqual(false);
expect(isValidFilename("foo._script")).toEqual(false);
expect(isValidFilename("foo.hyphened-ext")).toEqual(false);
expect(isValidFilename("")).toEqual(false);
expect(isValidFilename("AutoLink-1.%-INC.exe")).toEqual(false);
expect(isValidFilename("AutoLink.exe-1.%-INC.exe")).toEqual(false);
expect(isValidFilename("foo%.exe")).toEqual(false);
expect(isValidFilename("-1.00%-INC")).toEqual(false);
});
});
describe("isValidDirectoryName()", function() {
const isValidDirectoryName = dirHelpers.isValidDirectoryName;
it("should return true for valid directory names", function() {
expect(isValidDirectoryName("a")).toEqual(true);
expect(isValidDirectoryName("foo")).toEqual(true);
expect(isValidDirectoryName("foo-dir")).toEqual(true);
expect(isValidDirectoryName("foo_dir")).toEqual(true);
expect(isValidDirectoryName(".a")).toEqual(true);
expect(isValidDirectoryName("1")).toEqual(true);
expect(isValidDirectoryName("a1")).toEqual(true);
expect(isValidDirectoryName(".a1")).toEqual(true);
expect(isValidDirectoryName("._foo")).toEqual(true);
expect(isValidDirectoryName("_foo")).toEqual(true);
});
it("should return false for invalid directory names", function() {
expect(isValidDirectoryName("")).toEqual(false);
expect(isValidDirectoryName("foo.dir")).toEqual(false);
expect(isValidDirectoryName("1.")).toEqual(false);
expect(isValidDirectoryName("foo.")).toEqual(false);
expect(isValidDirectoryName("dir#")).toEqual(false);
expect(isValidDirectoryName("dir!")).toEqual(false);
expect(isValidDirectoryName("dir*")).toEqual(false);
expect(isValidDirectoryName(".")).toEqual(false);
});
});
describe("isValidDirectoryPath()", function() {
const isValidDirectoryPath = dirHelpers.isValidDirectoryPath;
it("should return false for empty strings", function() {
expect(isValidDirectoryPath("")).toEqual(false);
});
it("should return true only for the forward slash if the string has length 1", function() {
expect(isValidDirectoryPath("/")).toEqual(true);
expect(isValidDirectoryPath(" ")).toEqual(false);
expect(isValidDirectoryPath(".")).toEqual(false);
expect(isValidDirectoryPath("a")).toEqual(false);
});
it("should return true for valid directory paths", function() {
expect(isValidDirectoryPath("/a")).toEqual(true);
expect(isValidDirectoryPath("/dir/a")).toEqual(true);
expect(isValidDirectoryPath("/dir/foo")).toEqual(true);
expect(isValidDirectoryPath("/.dir/foo-dir")).toEqual(true);
expect(isValidDirectoryPath("/.dir/foo_dir")).toEqual(true);
expect(isValidDirectoryPath("/.dir/.a")).toEqual(true);
expect(isValidDirectoryPath("/dir1/1")).toEqual(true);
expect(isValidDirectoryPath("/dir1/a1")).toEqual(true);
expect(isValidDirectoryPath("/dir1/.a1")).toEqual(true);
expect(isValidDirectoryPath("/dir_/._foo")).toEqual(true);
expect(isValidDirectoryPath("/dir-/_foo")).toEqual(true);
});
it("should return false if the path does not have a leading slash", function() {
expect(isValidDirectoryPath("a")).toEqual(false);
expect(isValidDirectoryPath("dir/a")).toEqual(false);
expect(isValidDirectoryPath("dir/foo")).toEqual(false);
expect(isValidDirectoryPath(".dir/foo-dir")).toEqual(false);
expect(isValidDirectoryPath(".dir/foo_dir")).toEqual(false);
expect(isValidDirectoryPath(".dir/.a")).toEqual(false);
expect(isValidDirectoryPath("dir1/1")).toEqual(false);
expect(isValidDirectoryPath("dir1/a1")).toEqual(false);
expect(isValidDirectoryPath("dir1/.a1")).toEqual(false);
expect(isValidDirectoryPath("dir_/._foo")).toEqual(false);
expect(isValidDirectoryPath("dir-/_foo")).toEqual(false);
});
it("should accept dot notation", function() {
expect(isValidDirectoryPath("/dir/./a")).toEqual(true);
expect(isValidDirectoryPath("/dir/../foo")).toEqual(true);
expect(isValidDirectoryPath("/.dir/./foo-dir")).toEqual(true);
expect(isValidDirectoryPath("/.dir/../foo_dir")).toEqual(true);
expect(isValidDirectoryPath("/.dir/./.a")).toEqual(true);
expect(isValidDirectoryPath("/dir1/1/.")).toEqual(true);
expect(isValidDirectoryPath("/dir1/a1/..")).toEqual(true);
expect(isValidDirectoryPath("/dir1/.a1/..")).toEqual(true);
expect(isValidDirectoryPath("/dir_/._foo/.")).toEqual(true);
expect(isValidDirectoryPath("/./dir-/_foo")).toEqual(true);
expect(isValidDirectoryPath("/../dir-/_foo")).toEqual(true);
});
});
describe("isValidFilePath()", function() {
const isValidFilePath = dirHelpers.isValidFilePath;
it("should return false for strings that are too short", function() {
expect(isValidFilePath("/a")).toEqual(false);
expect(isValidFilePath("a.")).toEqual(false);
expect(isValidFilePath(".a")).toEqual(false);
expect(isValidFilePath("/.")).toEqual(false);
});
it("should return true for arguments that are just filenames", function() {
expect(isValidFilePath("test.txt")).toEqual(true);
expect(isValidFilePath("123.script")).toEqual(true);
expect(isValidFilePath("foo123.b")).toEqual(true);
expect(isValidFilePath("my_script.script")).toEqual(true);
expect(isValidFilePath("my-script.script")).toEqual(true);
expect(isValidFilePath("_foo.lit")).toEqual(true);
expect(isValidFilePath("mult.periods.script")).toEqual(true);
expect(isValidFilePath("mult.per-iods.again.script")).toEqual(true);
});
it("should return true for valid filepaths", function() {
expect(isValidFilePath("/foo/test.txt")).toEqual(true);
expect(isValidFilePath("/../123.script")).toEqual(true);
expect(isValidFilePath("/./foo123.b")).toEqual(true);
expect(isValidFilePath("/dir/my_script.script")).toEqual(true);
expect(isValidFilePath("/dir1/dir2/dir3/my-script.script")).toEqual(true);
expect(isValidFilePath("/dir1/dir2/././../_foo.lit")).toEqual(true);
expect(isValidFilePath("/.dir1/./../.dir2/mult.periods.script")).toEqual(true);
expect(isValidFilePath("/_dir/../dir2/mult.per-iods.again.script")).toEqual(true);
});
it("should return false for strings that end with a slash", function() {
expect(isValidFilePath("/foo/")).toEqual(false);
expect(isValidFilePath("foo.txt/")).toEqual(false);
expect(isValidFilePath("/")).toEqual(false);
expect(isValidFilePath("/_dir/")).toEqual(false);
});
it("should return false for invalid arguments", function() {
expect(isValidFilePath(null)).toEqual(false);
expect(isValidFilePath()).toEqual(false);
expect(isValidFilePath(5)).toEqual(false);
expect(isValidFilePath({})).toEqual(false);
})
});
describe("getFirstParentDirectory()", function() {
const getFirstParentDirectory = dirHelpers.getFirstParentDirectory;
it("should return the first parent directory in a filepath", function() {
expect(getFirstParentDirectory("/dir1/foo.txt")).toEqual("dir1/");
expect(getFirstParentDirectory("/dir1/dir2/dir3/dir4/foo.txt")).toEqual("dir1/");
expect(getFirstParentDirectory("/_dir1/dir2/foo.js")).toEqual("_dir1/");
});
it("should return '/' if there is no first parent directory", function() {
expect(getFirstParentDirectory("")).toEqual("/");
expect(getFirstParentDirectory(" ")).toEqual("/");
expect(getFirstParentDirectory("/")).toEqual("/");
expect(getFirstParentDirectory("//")).toEqual("/");
expect(getFirstParentDirectory("foo.script")).toEqual("/");
expect(getFirstParentDirectory("/foo.txt")).toEqual("/");
});
});
describe("getAllParentDirectories()", function() {
const getAllParentDirectories = dirHelpers.getAllParentDirectories;
it("should return all parent directories in a filepath", function() {
expect(getAllParentDirectories("/")).toEqual("/");
expect(getAllParentDirectories("/home/var/foo.txt")).toEqual("/home/var/");
expect(getAllParentDirectories("/home/var/")).toEqual("/home/var/");
expect(getAllParentDirectories("/home/var/test/")).toEqual("/home/var/test/");
});
it("should return an empty string if there are no parent directories", function() {
expect(getAllParentDirectories("foo.txt")).toEqual("");
});
});
describe("isInRootDirectory()", function() {
const isInRootDirectory = dirHelpers.isInRootDirectory;
it("should return true for filepaths that refer to a file in the root directory", function() {
expect(isInRootDirectory("a.b")).toEqual(true);
expect(isInRootDirectory("foo.txt")).toEqual(true);
expect(isInRootDirectory("/foo.txt")).toEqual(true);
});
it("should return false for filepaths that refer to a file that's NOT in the root directory", function() {
expect(isInRootDirectory("/dir/foo.txt")).toEqual(false);
expect(isInRootDirectory("dir/foo.txt")).toEqual(false);
expect(isInRootDirectory("/./foo.js")).toEqual(false);
expect(isInRootDirectory("../foo.js")).toEqual(false);
expect(isInRootDirectory("/dir1/dir2/dir3/foo.txt")).toEqual(false);
});
it("should return false for invalid inputs (inputs that aren't filepaths)", function() {
expect(isInRootDirectory(null)).toEqual(false);
expect(isInRootDirectory(undefined)).toEqual(false);
expect(isInRootDirectory("")).toEqual(false);
expect(isInRootDirectory(" ")).toEqual(false);
expect(isInRootDirectory("a")).toEqual(false);
expect(isInRootDirectory("/dir")).toEqual(false);
expect(isInRootDirectory("/dir/")).toEqual(false);
expect(isInRootDirectory("/dir/foo")).toEqual(false);
});
});
describe("evaluateDirectoryPath()", function() {
//const evaluateDirectoryPath = dirHelpers.evaluateDirectoryPath;
// TODO
});
describe("evaluateFilePath()", function() {
//const evaluateFilePath = dirHelpers.evaluateFilePath;
// TODO
})
});

@ -1,299 +0,0 @@
import * as dirHelpers from "../../src/Terminal/DirectoryHelpers";
import { expect } from "chai";
console.log("Beginning Terminal Directory Tests");
describe("Terminal Directory Tests", function() {
describe("removeLeadingSlash()", function() {
const removeLeadingSlash = dirHelpers.removeLeadingSlash;
it("should remove first slash in a string", function() {
expect(removeLeadingSlash("/")).to.equal("");
expect(removeLeadingSlash("/foo.txt")).to.equal("foo.txt");
expect(removeLeadingSlash("/foo/file.txt")).to.equal("foo/file.txt");
});
it("should only remove one slash", function() {
expect(removeLeadingSlash("///")).to.equal("//");
expect(removeLeadingSlash("//foo")).to.equal("/foo");
});
it("should do nothing for a string that doesn't start with a slash", function() {
expect(removeLeadingSlash("foo.txt")).to.equal("foo.txt");
expect(removeLeadingSlash("foo/test.txt")).to.equal("foo/test.txt");
});
it("should not fail on an empty string", function() {
expect(removeLeadingSlash.bind(null, "")).to.not.throw();
expect(removeLeadingSlash("")).to.equal("");
});
});
describe("removeTrailingSlash()", function() {
const removeTrailingSlash = dirHelpers.removeTrailingSlash;
it("should remove last slash in a string", function() {
expect(removeTrailingSlash("/")).to.equal("");
expect(removeTrailingSlash("foo.txt/")).to.equal("foo.txt");
expect(removeTrailingSlash("foo/file.txt/")).to.equal("foo/file.txt");
});
it("should only remove one slash", function() {
expect(removeTrailingSlash("///")).to.equal("//");
expect(removeTrailingSlash("foo//")).to.equal("foo/");
});
it("should do nothing for a string that doesn't end with a slash", function() {
expect(removeTrailingSlash("foo.txt")).to.equal("foo.txt");
expect(removeTrailingSlash("foo/test.txt")).to.equal("foo/test.txt");
});
it("should not fail on an empty string", function() {
expect(removeTrailingSlash.bind(null, "")).to.not.throw();
expect(removeTrailingSlash("")).to.equal("");
});
});
describe("isValidFilename()", function() {
const isValidFilename = dirHelpers.isValidFilename;
it("should return true for valid filenames", function() {
expect(isValidFilename("test.txt")).to.equal(true);
expect(isValidFilename("123.script")).to.equal(true);
expect(isValidFilename("foo123.b")).to.equal(true);
expect(isValidFilename("my_script.script")).to.equal(true);
expect(isValidFilename("my-script.script")).to.equal(true);
expect(isValidFilename("_foo.lit")).to.equal(true);
expect(isValidFilename("mult.periods.script")).to.equal(true);
expect(isValidFilename("mult.per-iods.again.script")).to.equal(true);
expect(isValidFilename("BruteSSH.exe-50%-INC")).to.equal(true);
expect(isValidFilename("DeepscanV1.exe-1.01%-INC")).to.equal(true);
expect(isValidFilename("DeepscanV2.exe-1.00%-INC")).to.equal(true);
expect(isValidFilename("AutoLink.exe-1.%-INC")).to.equal(true);
});
it("should return false for invalid filenames", function() {
expect(isValidFilename("foo")).to.equal(false);
expect(isValidFilename("my script.script")).to.equal(false);
expect(isValidFilename("a^.txt")).to.equal(false);
expect(isValidFilename("b#.lit")).to.equal(false);
expect(isValidFilename("lib().js")).to.equal(false);
expect(isValidFilename("foo.script_")).to.equal(false);
expect(isValidFilename("foo._script")).to.equal(false);
expect(isValidFilename("foo.hyphened-ext")).to.equal(false);
expect(isValidFilename("")).to.equal(false);
expect(isValidFilename("AutoLink-1.%-INC.exe")).to.equal(false);
expect(isValidFilename("AutoLink.exe-1.%-INC.exe")).to.equal(false);
expect(isValidFilename("foo%.exe")).to.equal(false);
expect(isValidFilename("-1.00%-INC")).to.equal(false);
});
});
describe("isValidDirectoryName()", function() {
const isValidDirectoryName = dirHelpers.isValidDirectoryName;
it("should return true for valid directory names", function() {
expect(isValidDirectoryName("a")).to.equal(true);
expect(isValidDirectoryName("foo")).to.equal(true);
expect(isValidDirectoryName("foo-dir")).to.equal(true);
expect(isValidDirectoryName("foo_dir")).to.equal(true);
expect(isValidDirectoryName(".a")).to.equal(true);
expect(isValidDirectoryName("1")).to.equal(true);
expect(isValidDirectoryName("a1")).to.equal(true);
expect(isValidDirectoryName(".a1")).to.equal(true);
expect(isValidDirectoryName("._foo")).to.equal(true);
expect(isValidDirectoryName("_foo")).to.equal(true);
});
it("should return false for invalid directory names", function() {
expect(isValidDirectoryName("")).to.equal(false);
expect(isValidDirectoryName("foo.dir")).to.equal(false);
expect(isValidDirectoryName("1.")).to.equal(false);
expect(isValidDirectoryName("foo.")).to.equal(false);
expect(isValidDirectoryName("dir#")).to.equal(false);
expect(isValidDirectoryName("dir!")).to.equal(false);
expect(isValidDirectoryName("dir*")).to.equal(false);
expect(isValidDirectoryName(".")).to.equal(false);
});
});
describe("isValidDirectoryPath()", function() {
const isValidDirectoryPath = dirHelpers.isValidDirectoryPath;
it("should return false for empty strings", function() {
expect(isValidDirectoryPath("")).to.equal(false);
});
it("should return true only for the forward slash if the string has length 1", function() {
expect(isValidDirectoryPath("/")).to.equal(true);
expect(isValidDirectoryPath(" ")).to.equal(false);
expect(isValidDirectoryPath(".")).to.equal(false);
expect(isValidDirectoryPath("a")).to.equal(false);
});
it("should return true for valid directory paths", function() {
expect(isValidDirectoryPath("/a")).to.equal(true);
expect(isValidDirectoryPath("/dir/a")).to.equal(true);
expect(isValidDirectoryPath("/dir/foo")).to.equal(true);
expect(isValidDirectoryPath("/.dir/foo-dir")).to.equal(true);
expect(isValidDirectoryPath("/.dir/foo_dir")).to.equal(true);
expect(isValidDirectoryPath("/.dir/.a")).to.equal(true);
expect(isValidDirectoryPath("/dir1/1")).to.equal(true);
expect(isValidDirectoryPath("/dir1/a1")).to.equal(true);
expect(isValidDirectoryPath("/dir1/.a1")).to.equal(true);
expect(isValidDirectoryPath("/dir_/._foo")).to.equal(true);
expect(isValidDirectoryPath("/dir-/_foo")).to.equal(true);
});
it("should return false if the path does not have a leading slash", function() {
expect(isValidDirectoryPath("a")).to.equal(false);
expect(isValidDirectoryPath("dir/a")).to.equal(false);
expect(isValidDirectoryPath("dir/foo")).to.equal(false);
expect(isValidDirectoryPath(".dir/foo-dir")).to.equal(false);
expect(isValidDirectoryPath(".dir/foo_dir")).to.equal(false);
expect(isValidDirectoryPath(".dir/.a")).to.equal(false);
expect(isValidDirectoryPath("dir1/1")).to.equal(false);
expect(isValidDirectoryPath("dir1/a1")).to.equal(false);
expect(isValidDirectoryPath("dir1/.a1")).to.equal(false);
expect(isValidDirectoryPath("dir_/._foo")).to.equal(false);
expect(isValidDirectoryPath("dir-/_foo")).to.equal(false);
});
it("should accept dot notation", function() {
expect(isValidDirectoryPath("/dir/./a")).to.equal(true);
expect(isValidDirectoryPath("/dir/../foo")).to.equal(true);
expect(isValidDirectoryPath("/.dir/./foo-dir")).to.equal(true);
expect(isValidDirectoryPath("/.dir/../foo_dir")).to.equal(true);
expect(isValidDirectoryPath("/.dir/./.a")).to.equal(true);
expect(isValidDirectoryPath("/dir1/1/.")).to.equal(true);
expect(isValidDirectoryPath("/dir1/a1/..")).to.equal(true);
expect(isValidDirectoryPath("/dir1/.a1/..")).to.equal(true);
expect(isValidDirectoryPath("/dir_/._foo/.")).to.equal(true);
expect(isValidDirectoryPath("/./dir-/_foo")).to.equal(true);
expect(isValidDirectoryPath("/../dir-/_foo")).to.equal(true);
});
});
describe("isValidFilePath()", function() {
const isValidFilePath = dirHelpers.isValidFilePath;
it("should return false for strings that are too short", function() {
expect(isValidFilePath("/a")).to.equal(false);
expect(isValidFilePath("a.")).to.equal(false);
expect(isValidFilePath(".a")).to.equal(false);
expect(isValidFilePath("/.")).to.equal(false);
});
it("should return true for arguments that are just filenames", function() {
expect(isValidFilePath("test.txt")).to.equal(true);
expect(isValidFilePath("123.script")).to.equal(true);
expect(isValidFilePath("foo123.b")).to.equal(true);
expect(isValidFilePath("my_script.script")).to.equal(true);
expect(isValidFilePath("my-script.script")).to.equal(true);
expect(isValidFilePath("_foo.lit")).to.equal(true);
expect(isValidFilePath("mult.periods.script")).to.equal(true);
expect(isValidFilePath("mult.per-iods.again.script")).to.equal(true);
});
it("should return true for valid filepaths", function() {
expect(isValidFilePath("/foo/test.txt")).to.equal(true);
expect(isValidFilePath("/../123.script")).to.equal(true);
expect(isValidFilePath("/./foo123.b")).to.equal(true);
expect(isValidFilePath("/dir/my_script.script")).to.equal(true);
expect(isValidFilePath("/dir1/dir2/dir3/my-script.script")).to.equal(true);
expect(isValidFilePath("/dir1/dir2/././../_foo.lit")).to.equal(true);
expect(isValidFilePath("/.dir1/./../.dir2/mult.periods.script")).to.equal(true);
expect(isValidFilePath("/_dir/../dir2/mult.per-iods.again.script")).to.equal(true);
});
it("should return false for strings that end with a slash", function() {
expect(isValidFilePath("/foo/")).to.equal(false);
expect(isValidFilePath("foo.txt/")).to.equal(false);
expect(isValidFilePath("/")).to.equal(false);
expect(isValidFilePath("/_dir/")).to.equal(false);
});
it("should return false for invalid arguments", function() {
expect(isValidFilePath(null)).to.equal(false);
expect(isValidFilePath()).to.equal(false);
expect(isValidFilePath(5)).to.equal(false);
expect(isValidFilePath({})).to.equal(false);
})
});
describe("getFirstParentDirectory()", function() {
const getFirstParentDirectory = dirHelpers.getFirstParentDirectory;
it("should return the first parent directory in a filepath", function() {
expect(getFirstParentDirectory("/dir1/foo.txt")).to.equal("dir1/");
expect(getFirstParentDirectory("/dir1/dir2/dir3/dir4/foo.txt")).to.equal("dir1/");
expect(getFirstParentDirectory("/_dir1/dir2/foo.js")).to.equal("_dir1/");
});
it("should return '/' if there is no first parent directory", function() {
expect(getFirstParentDirectory("")).to.equal("/");
expect(getFirstParentDirectory(" ")).to.equal("/");
expect(getFirstParentDirectory("/")).to.equal("/");
expect(getFirstParentDirectory("//")).to.equal("/");
expect(getFirstParentDirectory("foo.script")).to.equal("/");
expect(getFirstParentDirectory("/foo.txt")).to.equal("/");
});
});
describe("getAllParentDirectories()", function() {
const getAllParentDirectories = dirHelpers.getAllParentDirectories;
it("should return all parent directories in a filepath", function() {
expect(getAllParentDirectories("/")).to.equal("/");
expect(getAllParentDirectories("/home/var/foo.txt")).to.equal("/home/var/");
expect(getAllParentDirectories("/home/var/")).to.equal("/home/var/");
expect(getAllParentDirectories("/home/var/test/")).to.equal("/home/var/test/");
});
it("should return an empty string if there are no parent directories", function() {
expect(getAllParentDirectories("foo.txt")).to.equal("");
});
});
describe("isInRootDirectory()", function() {
const isInRootDirectory = dirHelpers.isInRootDirectory;
it("should return true for filepaths that refer to a file in the root directory", function() {
expect(isInRootDirectory("a.b")).to.equal(true);
expect(isInRootDirectory("foo.txt")).to.equal(true);
expect(isInRootDirectory("/foo.txt")).to.equal(true);
});
it("should return false for filepaths that refer to a file that's NOT in the root directory", function() {
expect(isInRootDirectory("/dir/foo.txt")).to.equal(false);
expect(isInRootDirectory("dir/foo.txt")).to.equal(false);
expect(isInRootDirectory("/./foo.js")).to.equal(false);
expect(isInRootDirectory("../foo.js")).to.equal(false);
expect(isInRootDirectory("/dir1/dir2/dir3/foo.txt")).to.equal(false);
});
it("should return false for invalid inputs (inputs that aren't filepaths)", function() {
expect(isInRootDirectory(null)).to.equal(false);
expect(isInRootDirectory(undefined)).to.equal(false);
expect(isInRootDirectory("")).to.equal(false);
expect(isInRootDirectory(" ")).to.equal(false);
expect(isInRootDirectory("a")).to.equal(false);
expect(isInRootDirectory("/dir")).to.equal(false);
expect(isInRootDirectory("/dir/")).to.equal(false);
expect(isInRootDirectory("/dir/foo")).to.equal(false);
});
});
describe("evaluateDirectoryPath()", function() {
//const evaluateDirectoryPath = dirHelpers.evaluateDirectoryPath;
// TODO
});
describe("evaluateFilePath()", function() {
//const evaluateFilePath = dirHelpers.evaluateFilePath;
// TODO
})
});