exiftool-vendored
Version:
Efficient, cross-platform access to ExifTool
190 lines • 8.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const StrEnum_1 = require("./StrEnum");
describe("StrEnum", () => {
const Examples = (0, StrEnum_1.strEnum)("a", "b", "c");
it(".values", () => {
const expected = ["a", "b", "c"];
(0, chai_1.expect)(Examples.values).to.eql(expected);
});
const fixtures = [
{ v: null, idx: undefined, exp: false },
{ v: undefined, idx: undefined, exp: false },
{ v: "", idx: undefined, exp: false },
{ v: "a", idx: 0, exp: true },
{ v: "b", idx: 1, exp: true },
{ v: "c", idx: 2, exp: true },
{ v: "d", idx: undefined, exp: false },
];
for (const { v, exp } of fixtures) {
it(`.has(${v}) -> ${exp}`, () => {
(0, chai_1.expect)(Examples.has(v)).to.eql(exp);
});
it(`.includes(${v}) -> ${exp}`, () => {
(0, chai_1.expect)(Examples.includes(v)).to.eql(exp);
});
it(`.getCI(${v?.toUpperCase()}) -> ${exp}`, () => {
(0, chai_1.expect)(Examples.getCI(v?.toUpperCase())).to.eql(exp ? v : undefined);
});
}
for (const { v, idx } of fixtures) {
it(`.indexOf(${v}) -> ${idx}`, () => {
(0, chai_1.expect)(Examples.indexOf(v)).to.eql(idx);
});
}
for (const { v, exp } of fixtures) {
it(`.toValid(${v}) -> ${exp ? v : "undefined"}`, () => {
(0, chai_1.expect)(Examples.toValid(v)).to.eql(exp ? v : undefined);
(0, chai_1.expect)(Examples.toValid(v) ?? "default").to.eql(exp ? v : "default");
});
it(`.firstValid(${v}) -> ${exp ? v : "undefined"}`, () => {
(0, chai_1.expect)(Examples.firstValid()).to.eql(undefined);
(0, chai_1.expect)(Examples.firstValid(v, "invalid")).to.eql(exp ? v : undefined);
(0, chai_1.expect)(Examples.firstValid("invalid", v)).to.eql(exp ? v : undefined);
(0, chai_1.expect)(Examples.firstValid("invalid", v) ?? "default").to.eql(exp ? v : "default");
});
}
for (const { v, exp } of fixtures) {
const f = (ea) => `f(${ea})`;
const expF = exp ? f(v) : undefined;
it(`.map(${v}) -> ${expF}`, () => {
(0, chai_1.expect)(Examples.mapValid(v, f)).to.eql(expF);
});
}
it(".omit()", () => {
// type assertions:
{
const s = Examples.omit("a", "b");
(0, chai_1.expect)(s).to.eql(["c"]);
}
{
const s = Examples.omit("b");
(0, chai_1.expect)(s).to.eql(["a", "c"]);
}
{
const s = Examples.omit("a", "b", "c");
(0, chai_1.expect)(s).to.eql([]);
}
});
it(".pick()", () => {
// type assertions:
{
const s = Examples.pick("a");
(0, chai_1.expect)(s).to.eql(["a"]);
}
{
const s = Examples.pick("a", "c");
(0, chai_1.expect)(s).to.eql(["a", "c"]);
}
{
const s = Examples.pick("a", "b", "c");
(0, chai_1.expect)(s).to.eql(["a", "b", "c"]);
}
{
const s = Examples.pick();
(0, chai_1.expect)(s).to.eql([]);
}
});
it(".ordinal()", () => {
for (const { v, idx } of fixtures) {
const expected = idx ?? Examples.values.length;
(0, chai_1.expect)(Examples.ordinal(v)).to.eql(expected);
}
});
describe(".cmp()", () => {
it("compares two valid enum values", () => {
(0, chai_1.expect)(Examples.cmp("a", "b")).to.eql(-1);
(0, chai_1.expect)(Examples.cmp("b", "a")).to.eql(1);
(0, chai_1.expect)(Examples.cmp("b", "b")).to.eql(0);
});
it("returns undefined for invalid values", () => {
(0, chai_1.expect)(Examples.cmp("a", "d")).to.eql(undefined);
(0, chai_1.expect)(Examples.cmp("d", "a")).to.eql(undefined);
(0, chai_1.expect)(Examples.cmp(null, "a")).to.eql(undefined);
(0, chai_1.expect)(Examples.cmp("a", null)).to.eql(undefined);
});
});
describe(".lt()", () => {
it("compares two valid enum values", () => {
(0, chai_1.expect)(Examples.lt("a", "b")).to.eql(true);
(0, chai_1.expect)(Examples.lt("b", "a")).to.eql(false);
(0, chai_1.expect)(Examples.lt("a", "a")).to.eql(false);
(0, chai_1.expect)(Examples.lt("c", "b")).to.eql(false);
(0, chai_1.expect)(Examples.lt("b", "c")).to.eql(true);
});
});
describe(".next()", () => {
it("returns the same value for valid inputs", () => {
(0, chai_1.expect)(Examples.next("a")).to.eql("a");
(0, chai_1.expect)(Examples.next("b")).to.eql("b");
});
it("returns undefined for invalid inputs", () => {
(0, chai_1.expect)(Examples.next("d")).to.eql(undefined);
(0, chai_1.expect)(Examples.next(null)).to.eql(undefined);
(0, chai_1.expect)(Examples.next(undefined)).to.eql(undefined);
});
});
describe(".toReversed()", () => {
it("returns a new StrEnum with values in reversed order", () => {
const reversed = Examples.toReversed();
(0, chai_1.expect)(reversed.values).to.eql(["c", "b", "a"]);
// Verify the returned object is a proper StrEnum
(0, chai_1.expect)(reversed.has("b")).to.eql(true);
(0, chai_1.expect)(reversed.indexOf("c")).to.eql(0);
(0, chai_1.expect)(reversed.indexOf("b")).to.eql(1);
(0, chai_1.expect)(reversed.indexOf("a")).to.eql(2);
});
});
describe("iterator functionality", () => {
it("should be iterable with for...of loops", () => {
const result = [];
for (const value of Examples) {
result.push(value);
}
(0, chai_1.expect)(result).to.eql(["a", "b", "c"]);
});
it("should support array destructuring", () => {
const [first, second, third] = Examples;
(0, chai_1.expect)(first).to.eql("a");
(0, chai_1.expect)(second).to.eql("b");
(0, chai_1.expect)(third).to.eql("c");
});
it("should work with Array.from()", () => {
const result = Array.from(Examples);
(0, chai_1.expect)(result).to.eql(["a", "b", "c"]);
});
it("should work with spread operator", () => {
const result = [...Examples];
(0, chai_1.expect)(result).to.eql(["a", "b", "c"]);
});
it("should support iterator protocol manually", () => {
const iterator = Examples[Symbol.iterator]();
let result = iterator.next();
(0, chai_1.expect)(result.value).to.eql("a");
(0, chai_1.expect)(result.done).to.eql(false);
result = iterator.next();
(0, chai_1.expect)(result.value).to.eql("b");
(0, chai_1.expect)(result.done).to.eql(false);
result = iterator.next();
(0, chai_1.expect)(result.value).to.eql("c");
(0, chai_1.expect)(result.done).to.eql(false);
result = iterator.next();
(0, chai_1.expect)(result.done).to.eql(true);
});
it("should work with other iterator-consuming functions", () => {
const map = new Map(Examples.values.map((v, i) => [v, i]));
const mapFromIterator = new Map([...Examples].map((v, i) => [v, i]));
(0, chai_1.expect)(mapFromIterator).to.deep.equal(map);
const set = new Set(Examples.values);
const setFromIterator = new Set(Examples);
(0, chai_1.expect)(setFromIterator).to.deep.equal(set);
});
});
describe("Symbol.toStringTag", () => {
it("should have correct string tag for debugging", () => {
(0, chai_1.expect)(Object.prototype.toString.call(Examples)).to.eql("[object StrEnum]");
});
});
});
//# sourceMappingURL=StrEnum.spec.js.map