Merge pull request from timvandermeij/unit-test-primitives

Improve unit test coverage for primitives
This commit is contained in:
Tim van der Meij 2020-06-07 18:58:17 +02:00 committed by GitHub
commit 4c36dadfe2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -16,30 +16,34 @@
import { import {
Cmd, Cmd,
Dict, Dict,
EOF,
isCmd, isCmd,
isDict, isDict,
isEOF,
isName, isName,
isRef, isRef,
isRefsEqual, isRefsEqual,
isStream,
Name, Name,
Ref, Ref,
RefSet, RefSet,
} from "../../src/core/primitives.js"; } from "../../src/core/primitives.js";
import { StringStream } from "../../src/core/stream.js";
import { XRefMock } from "./test_utils.js"; import { XRefMock } from "./test_utils.js";
describe("primitives", function () { describe("primitives", function () {
describe("Name", function () { describe("Name", function () {
it("should retain the given name", function () { it("should retain the given name", function () {
var givenName = "Font"; const givenName = "Font";
var name = Name.get(givenName); const name = Name.get(givenName);
expect(name.name).toEqual(givenName); expect(name.name).toEqual(givenName);
}); });
it("should create only one object for a name and cache it", function () { it("should create only one object for a name and cache it", function () {
var firstFont = Name.get("Font"); const firstFont = Name.get("Font");
var secondFont = Name.get("Font"); const secondFont = Name.get("Font");
var firstSubtype = Name.get("Subtype"); const firstSubtype = Name.get("Subtype");
var secondSubtype = Name.get("Subtype"); const secondSubtype = Name.get("Subtype");
expect(firstFont).toBe(secondFont); expect(firstFont).toBe(secondFont);
expect(firstSubtype).toBe(secondSubtype); expect(firstSubtype).toBe(secondSubtype);
@ -49,16 +53,16 @@ describe("primitives", function () {
describe("Cmd", function () { describe("Cmd", function () {
it("should retain the given cmd name", function () { it("should retain the given cmd name", function () {
var givenCmd = "BT"; const givenCmd = "BT";
var cmd = Cmd.get(givenCmd); const cmd = Cmd.get(givenCmd);
expect(cmd.cmd).toEqual(givenCmd); expect(cmd.cmd).toEqual(givenCmd);
}); });
it("should create only one object for a command and cache it", function () { it("should create only one object for a command and cache it", function () {
var firstBT = Cmd.get("BT"); const firstBT = Cmd.get("BT");
var secondBT = Cmd.get("BT"); const secondBT = Cmd.get("BT");
var firstET = Cmd.get("ET"); const firstET = Cmd.get("ET");
var secondET = Cmd.get("ET"); const secondET = Cmd.get("ET");
expect(firstBT).toBe(secondBT); expect(firstBT).toBe(secondBT);
expect(firstET).toBe(secondET); expect(firstET).toBe(secondET);
@ -67,23 +71,23 @@ describe("primitives", function () {
}); });
describe("Dict", function () { describe("Dict", function () {
var checkInvalidHasValues = function (dict) { const checkInvalidHasValues = function (dict) {
expect(dict.has()).toBeFalsy(); expect(dict.has()).toBeFalsy();
expect(dict.has("Prev")).toBeFalsy(); expect(dict.has("Prev")).toBeFalsy();
}; };
var checkInvalidKeyValues = function (dict) { const checkInvalidKeyValues = function (dict) {
expect(dict.get()).toBeUndefined(); expect(dict.get()).toBeUndefined();
expect(dict.get("Prev")).toBeUndefined(); expect(dict.get("Prev")).toBeUndefined();
expect(dict.get("Decode", "D")).toBeUndefined(); expect(dict.get("Decode", "D")).toBeUndefined();
expect(dict.get("FontFile", "FontFile2", "FontFile3")).toBeUndefined(); expect(dict.get("FontFile", "FontFile2", "FontFile3")).toBeUndefined();
}; };
var emptyDict, dictWithSizeKey, dictWithManyKeys; let emptyDict, dictWithSizeKey, dictWithManyKeys;
var storedSize = 42; const storedSize = 42;
var testFontFile = "file1"; const testFontFile = "file1";
var testFontFile2 = "file2"; const testFontFile2 = "file2";
var testFontFile3 = "file3"; const testFontFile3 = "file3";
beforeAll(function (done) { beforeAll(function (done) {
emptyDict = new Dict(); emptyDict = new Dict();
@ -103,6 +107,15 @@ describe("primitives", function () {
emptyDict = dictWithSizeKey = dictWithManyKeys = null; emptyDict = dictWithSizeKey = dictWithManyKeys = null;
}); });
it("should allow assigning an XRef table after creation", function () {
const dict = new Dict(null);
expect(dict.xref).toEqual(null);
const xref = new XRefMock([]);
dict.assignXref(xref);
expect(dict.xref).toEqual(xref);
});
it("should return invalid values for unknown keys", function () { it("should return invalid values for unknown keys", function () {
checkInvalidHasValues(emptyDict); checkInvalidHasValues(emptyDict);
checkInvalidKeyValues(emptyDict); checkInvalidKeyValues(emptyDict);
@ -147,7 +160,7 @@ describe("primitives", function () {
}); });
it("should asynchronously fetch unknown keys", function (done) { it("should asynchronously fetch unknown keys", function (done) {
var keyPromises = [ const keyPromises = [
dictWithManyKeys.getAsync("Size"), dictWithManyKeys.getAsync("Size"),
dictWithSizeKey.getAsync("FontFile", "FontFile2", "FontFile3"), dictWithSizeKey.getAsync("FontFile", "FontFile2", "FontFile3"),
]; ];
@ -164,7 +177,7 @@ describe("primitives", function () {
}); });
it("should asynchronously fetch correct values for multiple stored keys", function (done) { it("should asynchronously fetch correct values for multiple stored keys", function (done) {
var keyPromises = [ const keyPromises = [
dictWithManyKeys.getAsync("FontFile3"), dictWithManyKeys.getAsync("FontFile3"),
dictWithManyKeys.getAsync("FontFile2", "FontFile3"), dictWithManyKeys.getAsync("FontFile2", "FontFile3"),
dictWithManyKeys.getAsync("FontFile", "FontFile2", "FontFile3"), dictWithManyKeys.getAsync("FontFile", "FontFile2", "FontFile3"),
@ -183,12 +196,12 @@ describe("primitives", function () {
}); });
it("should callback for each stored key", function () { it("should callback for each stored key", function () {
var callbackSpy = jasmine.createSpy("spy on callback in dictionary"); const callbackSpy = jasmine.createSpy("spy on callback in dictionary");
dictWithManyKeys.forEach(callbackSpy); dictWithManyKeys.forEach(callbackSpy);
expect(callbackSpy).toHaveBeenCalled(); expect(callbackSpy).toHaveBeenCalled();
var callbackSpyCalls = callbackSpy.calls; const callbackSpyCalls = callbackSpy.calls;
expect(callbackSpyCalls.argsFor(0)).toEqual(["FontFile", testFontFile]); expect(callbackSpyCalls.argsFor(0)).toEqual(["FontFile", testFontFile]);
expect(callbackSpyCalls.argsFor(1)).toEqual(["FontFile2", testFontFile2]); expect(callbackSpyCalls.argsFor(1)).toEqual(["FontFile2", testFontFile2]);
expect(callbackSpyCalls.argsFor(2)).toEqual(["FontFile3", testFontFile3]); expect(callbackSpyCalls.argsFor(2)).toEqual(["FontFile3", testFontFile3]);
@ -196,9 +209,9 @@ describe("primitives", function () {
}); });
it("should handle keys pointing to indirect objects, both sync and async", function (done) { it("should handle keys pointing to indirect objects, both sync and async", function (done) {
var fontRef = Ref.get(1, 0); const fontRef = Ref.get(1, 0);
var xref = new XRefMock([{ ref: fontRef, data: testFontFile }]); const xref = new XRefMock([{ ref: fontRef, data: testFontFile }]);
var fontDict = new Dict(xref); const fontDict = new Dict(xref);
fontDict.set("FontFile", fontRef); fontDict.set("FontFile", fontRef);
expect(fontDict.getRaw("FontFile")).toEqual(fontRef); expect(fontDict.getRaw("FontFile")).toEqual(fontRef);
@ -218,15 +231,15 @@ describe("primitives", function () {
}); });
it("should handle arrays containing indirect objects", function () { it("should handle arrays containing indirect objects", function () {
var minCoordRef = Ref.get(1, 0), const minCoordRef = Ref.get(1, 0);
maxCoordRef = Ref.get(2, 0); const maxCoordRef = Ref.get(2, 0);
var minCoord = 0, const minCoord = 0;
maxCoord = 1; const maxCoord = 1;
var xref = new XRefMock([ const xref = new XRefMock([
{ ref: minCoordRef, data: minCoord }, { ref: minCoordRef, data: minCoord },
{ ref: maxCoordRef, data: maxCoord }, { ref: maxCoordRef, data: maxCoord },
]); ]);
var xObjectDict = new Dict(xref); const xObjectDict = new Dict(xref);
xObjectDict.set("BBox", [minCoord, maxCoord, minCoordRef, maxCoordRef]); xObjectDict.set("BBox", [minCoord, maxCoord, minCoordRef, maxCoordRef]);
expect(xObjectDict.get("BBox")).toEqual([ expect(xObjectDict.get("BBox")).toEqual([
@ -244,31 +257,31 @@ describe("primitives", function () {
}); });
it("should get all key names", function () { it("should get all key names", function () {
var expectedKeys = ["FontFile", "FontFile2", "FontFile3"]; const expectedKeys = ["FontFile", "FontFile2", "FontFile3"];
var keys = dictWithManyKeys.getKeys(); const keys = dictWithManyKeys.getKeys();
expect(keys.sort()).toEqual(expectedKeys); expect(keys.sort()).toEqual(expectedKeys);
}); });
it("should create only one object for Dict.empty", function () { it("should create only one object for Dict.empty", function () {
var firstDictEmpty = Dict.empty; const firstDictEmpty = Dict.empty;
var secondDictEmpty = Dict.empty; const secondDictEmpty = Dict.empty;
expect(firstDictEmpty).toBe(secondDictEmpty); expect(firstDictEmpty).toBe(secondDictEmpty);
expect(firstDictEmpty).not.toBe(emptyDict); expect(firstDictEmpty).not.toBe(emptyDict);
}); });
it("should correctly merge dictionaries", function () { it("should correctly merge dictionaries", function () {
var expectedKeys = ["FontFile", "FontFile2", "FontFile3", "Size"]; const expectedKeys = ["FontFile", "FontFile2", "FontFile3", "Size"];
var fontFileDict = new Dict(); const fontFileDict = new Dict();
fontFileDict.set("FontFile", "Type1 font file"); fontFileDict.set("FontFile", "Type1 font file");
var mergedDict = Dict.merge(null, [ const mergedDict = Dict.merge(null, [
dictWithManyKeys, dictWithManyKeys,
dictWithSizeKey, dictWithSizeKey,
fontFileDict, fontFileDict,
]); ]);
var mergedKeys = mergedDict.getKeys(); const mergedKeys = mergedDict.getKeys();
expect(mergedKeys.sort()).toEqual(expectedKeys); expect(mergedKeys.sort()).toEqual(expectedKeys);
expect(mergedDict.get("FontFile")).toEqual(testFontFile); expect(mergedDict.get("FontFile")).toEqual(testFontFile);
@ -276,46 +289,77 @@ describe("primitives", function () {
}); });
describe("Ref", function () { describe("Ref", function () {
it("should get a string representation", function () {
const nonZeroRef = Ref.get(4, 2);
expect(nonZeroRef.toString()).toEqual("4R2");
// If the generation number is 0, a shorter representation is used.
const zeroRef = Ref.get(4, 0);
expect(zeroRef.toString()).toEqual("4R");
});
it("should retain the stored values", function () { it("should retain the stored values", function () {
var storedNum = 4; const storedNum = 4;
var storedGen = 2; const storedGen = 2;
var ref = Ref.get(storedNum, storedGen); const ref = Ref.get(storedNum, storedGen);
expect(ref.num).toEqual(storedNum); expect(ref.num).toEqual(storedNum);
expect(ref.gen).toEqual(storedGen); expect(ref.gen).toEqual(storedGen);
}); });
it("should create only one object for a reference and cache it", function () {
const firstRef = Ref.get(4, 2);
const secondRef = Ref.get(4, 2);
const firstOtherRef = Ref.get(5, 2);
const secondOtherRef = Ref.get(5, 2);
expect(firstRef).toBe(secondRef);
expect(firstOtherRef).toBe(secondOtherRef);
expect(firstRef).not.toBe(firstOtherRef);
});
}); });
describe("RefSet", function () { describe("RefSet", function () {
it("should have a stored value", function () { it("should have a stored value", function () {
var ref = Ref.get(4, 2); const ref = Ref.get(4, 2);
var refset = new RefSet(); const refset = new RefSet();
refset.put(ref); refset.put(ref);
expect(refset.has(ref)).toBeTruthy(); expect(refset.has(ref)).toBeTruthy();
}); });
it("should not have an unknown value", function () { it("should not have an unknown value", function () {
var ref = Ref.get(4, 2); const ref = Ref.get(4, 2);
var refset = new RefSet(); const refset = new RefSet();
expect(refset.has(ref)).toBeFalsy(); expect(refset.has(ref)).toBeFalsy();
refset.put(ref); refset.put(ref);
var anotherRef = Ref.get(2, 4); const anotherRef = Ref.get(2, 4);
expect(refset.has(anotherRef)).toBeFalsy(); expect(refset.has(anotherRef)).toBeFalsy();
}); });
}); });
describe("isEOF", function () {
it("handles non-EOF", function () {
const nonEOF = "foo";
expect(isEOF(nonEOF)).toEqual(false);
});
it("handles EOF", function () {
expect(isEOF(EOF)).toEqual(true);
});
});
describe("isName", function () { describe("isName", function () {
it("handles non-names", function () { it("handles non-names", function () {
var nonName = {}; const nonName = {};
expect(isName(nonName)).toEqual(false); expect(isName(nonName)).toEqual(false);
}); });
it("handles names", function () { it("handles names", function () {
var name = Name.get("Font"); const name = Name.get("Font");
expect(isName(name)).toEqual(true); expect(isName(name)).toEqual(true);
}); });
it("handles names with name check", function () { it("handles names with name check", function () {
var name = Name.get("Font"); const name = Name.get("Font");
expect(isName(name, "Font")).toEqual(true); expect(isName(name, "Font")).toEqual(true);
expect(isName(name, "Subtype")).toEqual(false); expect(isName(name, "Subtype")).toEqual(false);
}); });
@ -323,17 +367,17 @@ describe("primitives", function () {
describe("isCmd", function () { describe("isCmd", function () {
it("handles non-commands", function () { it("handles non-commands", function () {
var nonCmd = {}; const nonCmd = {};
expect(isCmd(nonCmd)).toEqual(false); expect(isCmd(nonCmd)).toEqual(false);
}); });
it("handles commands", function () { it("handles commands", function () {
var cmd = Cmd.get("BT"); const cmd = Cmd.get("BT");
expect(isCmd(cmd)).toEqual(true); expect(isCmd(cmd)).toEqual(true);
}); });
it("handles commands with cmd check", function () { it("handles commands with cmd check", function () {
var cmd = Cmd.get("BT"); const cmd = Cmd.get("BT");
expect(isCmd(cmd, "BT")).toEqual(true); expect(isCmd(cmd, "BT")).toEqual(true);
expect(isCmd(cmd, "ET")).toEqual(false); expect(isCmd(cmd, "ET")).toEqual(false);
}); });
@ -341,18 +385,18 @@ describe("primitives", function () {
describe("isDict", function () { describe("isDict", function () {
it("handles non-dictionaries", function () { it("handles non-dictionaries", function () {
var nonDict = {}; const nonDict = {};
expect(isDict(nonDict)).toEqual(false); expect(isDict(nonDict)).toEqual(false);
}); });
it("handles empty dictionaries with type check", function () { it("handles empty dictionaries with type check", function () {
var dict = Dict.empty; const dict = Dict.empty;
expect(isDict(dict)).toEqual(true); expect(isDict(dict)).toEqual(true);
expect(isDict(dict, "Page")).toEqual(false); expect(isDict(dict, "Page")).toEqual(false);
}); });
it("handles dictionaries with type check", function () { it("handles dictionaries with type check", function () {
var dict = new Dict(); const dict = new Dict();
dict.set("Type", Name.get("Page")); dict.set("Type", Name.get("Page"));
expect(isDict(dict, "Page")).toEqual(true); expect(isDict(dict, "Page")).toEqual(true);
expect(isDict(dict, "Contents")).toEqual(false); expect(isDict(dict, "Contents")).toEqual(false);
@ -361,27 +405,39 @@ describe("primitives", function () {
describe("isRef", function () { describe("isRef", function () {
it("handles non-refs", function () { it("handles non-refs", function () {
var nonRef = {}; const nonRef = {};
expect(isRef(nonRef)).toEqual(false); expect(isRef(nonRef)).toEqual(false);
}); });
it("handles refs", function () { it("handles refs", function () {
var ref = Ref.get(1, 0); const ref = Ref.get(1, 0);
expect(isRef(ref)).toEqual(true); expect(isRef(ref)).toEqual(true);
}); });
}); });
describe("isRefsEqual", function () { describe("isRefsEqual", function () {
it("should handle Refs pointing to the same object", function () { it("should handle Refs pointing to the same object", function () {
var ref1 = Ref.get(1, 0); const ref1 = Ref.get(1, 0);
var ref2 = Ref.get(1, 0); const ref2 = Ref.get(1, 0);
expect(isRefsEqual(ref1, ref2)).toEqual(true); expect(isRefsEqual(ref1, ref2)).toEqual(true);
}); });
it("should handle Refs pointing to different objects", function () { it("should handle Refs pointing to different objects", function () {
var ref1 = Ref.get(1, 0); const ref1 = Ref.get(1, 0);
var ref2 = Ref.get(2, 0); const ref2 = Ref.get(2, 0);
expect(isRefsEqual(ref1, ref2)).toEqual(false); expect(isRefsEqual(ref1, ref2)).toEqual(false);
}); });
}); });
describe("isStream", function () {
it("handles non-streams", function () {
const nonStream = {};
expect(isStream(nonStream)).toEqual(false);
});
it("handles streams", function () {
const stream = new StringStream("foo");
expect(isStream(stream)).toEqual(true);
});
});
}); });