Improve unit test coverage for primitives

This commit includes unit tests for:

- `isEOF`
- `isStream`
- `Ref`'s string representation and caching
- `Dict`'s XRef assignment
This commit is contained in:
Tim van der Meij 2020-06-07 15:06:54 +02:00
parent 2bd0690fdd
commit 550a38f1ba
No known key found for this signature in database
GPG Key ID: 8C3FD2925A5F2762

View File

@ -16,15 +16,19 @@
import {
Cmd,
Dict,
EOF,
isCmd,
isDict,
isEOF,
isName,
isRef,
isRefsEqual,
isStream,
Name,
Ref,
RefSet,
} from "../../src/core/primitives.js";
import { StringStream } from "../../src/core/stream.js";
import { XRefMock } from "./test_utils.js";
describe("primitives", function () {
@ -103,6 +107,15 @@ describe("primitives", function () {
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 () {
checkInvalidHasValues(emptyDict);
checkInvalidKeyValues(emptyDict);
@ -276,6 +289,15 @@ describe("primitives", 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 () {
const storedNum = 4;
const storedGen = 2;
@ -283,6 +305,17 @@ describe("primitives", function () {
expect(ref.num).toEqual(storedNum);
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 () {
@ -303,6 +336,17 @@ describe("primitives", function () {
});
});
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 () {
it("handles non-names", function () {
const nonName = {};
@ -384,4 +428,16 @@ describe("primitives", function () {
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);
});
});
});