Skip to content

Commit

Permalink
Improve unit test coverage for primitives
Browse files Browse the repository at this point in the history
This commit includes unit tests for:

- `isEOF`
- `isStream`
- `Ref`'s string representation and caching
- `Dict`'s XRef assignment
  • Loading branch information
timvandermeij committed Jun 7, 2020
1 parent 2bd0690 commit 4caa10b
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions test/unit/primitives_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -103,6 +107,15 @@ describe("primitives", function () {
emptyDict = dictWithSizeKey = dictWithManyKeys = null;
});

it("should allow assing 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);
Expand Down Expand Up @@ -276,13 +289,33 @@ 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;
const ref = Ref.get(storedNum, storedGen);
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 () {
Expand All @@ -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 = {};
Expand Down Expand Up @@ -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);
});
});
});

0 comments on commit 4caa10b

Please sign in to comment.