Repository: mozilla/pdf.js
File: test/unit/primitives_spec.js (JavaScript)

Code before:

      dict.setIfArray("c", [1, 2, 3]);
      expect(dict.get("c")).toEqual([1, 2, 3]);

      dict.setIfArray("d", new Uint8Array([4, 5, 6]));
      expect(dict.get("d")).toEqual(new Uint8Array([4, 5, 6]));

      dict.setIfArray("e", "not an array");
      expect(dict.has("e")).toBeFalse();

      dict.setIfDefined("f", "defined");
      expect(dict.get("f")).toEqual("defined");

      dict.setIfDefined("g", undefined);
      expect(dict.has("g")).toBeFalse();

      dict.setIfDefined("h", null);
      expect(dict.has("h")).toBeFalse();

      dict.setIfName("i", Name.get("name"));
      expect(dict.get("i")).toEqual(Name.get("name"));

      dict.setIfName("j", "name");
      expect(dict.get("j")).toEqual(Name.get("name"));
    });
  });

  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);


Review comment (suggestion):
Similar to the other methods it'd be good to also add a test for the "unhappy" flow here, so the case where the input is both not a string and not a `Name` instance (such as `undefined` or `null`).

Code after:

      dict.setIfNumber("b", "not a number");
      expect(dict.has("b")).toBeFalse();

      dict.setIfArray("c", [1, 2, 3]);
      expect(dict.get("c")).toEqual([1, 2, 3]);

      dict.setIfArray("d", new Uint8Array([4, 5, 6]));
      expect(dict.get("d")).toEqual(new Uint8Array([4, 5, 6]));

      dict.setIfArray("e", "not an array");
      expect(dict.has("e")).toBeFalse();

      dict.setIfDefined("f", "defined");
      expect(dict.get("f")).toEqual("defined");

      dict.setIfDefined("g", undefined);
      expect(dict.has("g")).toBeFalse();

      dict.setIfDefined("h", null);
      expect(dict.has("h")).toBeFalse();

      dict.setIfName("i", Name.get("name"));
      expect(dict.get("i")).toEqual(Name.get("name"));

      dict.setIfName("j", "name");
      expect(dict.get("j")).toEqual(Name.get("name"));

      dict.setIfName("k", 1234);
      expect(dict.has("k")).toBeFalse();
    });
  });

  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);
    });