From d70e07fb90715807f3585aa60edf631778e8ae6e Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 3 Aug 2016 16:37:04 +0200 Subject: [PATCH] Add more unit-tests for `primitives.js` --- test/unit/primitives_spec.js | 141 ++++++++++++++++++++++++++++++++++- 1 file changed, 139 insertions(+), 2 deletions(-) diff --git a/test/unit/primitives_spec.js b/test/unit/primitives_spec.js index be9354c47..68cd86542 100644 --- a/test/unit/primitives_spec.js +++ b/test/unit/primitives_spec.js @@ -1,15 +1,46 @@ /* globals expect, it, describe, beforeEach, Name, Dict, Ref, RefSet, Cmd, - jasmine, isDict, isRefsEqual */ + jasmine, isName, isCmd, isDict, isRef, isRefsEqual */ 'use strict'; describe('primitives', function() { + function XRefMock(array) { + this.map = Object.create(null); + for (var elem in array) { + var obj = array[elem]; + var ref = obj.ref, data = obj.data; + this.map[ref.toString()] = data; + } + } + XRefMock.prototype = { + fetch: function (ref) { + return this.map[ref.toString()]; + }, + fetchIfRef: function (obj) { + if (!isRef(obj)) { + return obj; + } + return this.fetch(obj); + }, + }; + describe('Name', function() { it('should retain the given name', function() { var givenName = 'Font'; var name = Name.get(givenName); expect(name.name).toEqual(givenName); }); + + it('should create only one object for a name and cache it', function () { + var firstFont = Name.get('Font'); + var secondFont = Name.get('Font'); + var firstSubtype = Name.get('Subtype'); + var secondSubtype = Name.get('Subtype'); + + expect(firstFont).toBe(secondFont); + expect(firstSubtype).toBe(secondSubtype); + expect(firstFont).not.toBe(firstSubtype); + }); }); describe('Cmd', function() { @@ -24,6 +55,7 @@ describe('primitives', function() { var secondBT = Cmd.get('BT'); var firstET = Cmd.get('ET'); var secondET = Cmd.get('ET'); + expect(firstBT).toBe(secondBT); expect(firstET).toBe(secondET); expect(firstBT).not.toBe(firstET); @@ -116,6 +148,62 @@ describe('primitives', function() { expect(callbackSpyCalls.argsFor(2)).toEqual(['FontFile3', testFontFile3]); expect(callbackSpyCalls.count()).toEqual(3); }); + + it('should handle keys pointing to indirect objects', function () { + var fontRef = new Ref(1, 0); + var xref = new XRefMock([ + { ref: fontRef, data: testFontFile, } + ]); + var fontDict = new Dict(xref); + fontDict.set('FontFile', fontRef); + + expect(fontDict.getRaw('FontFile')).toEqual(fontRef); + expect(fontDict.get('FontFile')).toEqual(testFontFile); + }); + + it('should handle arrays containing indirect objects', function () { + var minCoordRef = new Ref(1, 0), maxCoordRef = new Ref(2, 0); + var minCoord = 0, maxCoord = 1; + var xref = new XRefMock([ + { ref: minCoordRef, data: minCoord, }, + { ref: maxCoordRef, data: maxCoord, } + ]); + var xObjectDict = new Dict(xref); + xObjectDict.set('BBox', [minCoord, maxCoord, minCoordRef, maxCoordRef]); + + expect(xObjectDict.get('BBox')).toEqual( + [minCoord, maxCoord, minCoordRef, maxCoordRef]); + expect(xObjectDict.getArray('BBox')).toEqual( + [minCoord, maxCoord, minCoord, maxCoord]); + }); + + it('should get all key names', function () { + var expectedKeys = ['FontFile', 'FontFile2', 'FontFile3']; + var keys = dictWithManyKeys.getKeys(); + + expect(keys.sort()).toEqual(expectedKeys); + }); + + it('should create only one object for Dict.empty', function () { + var firstDictEmpty = Dict.empty; + var secondDictEmpty = Dict.empty; + + expect(firstDictEmpty).toBe(secondDictEmpty); + expect(firstDictEmpty).not.toBe(emptyDict); + }); + + it('should correctly merge dictionaries', function () { + var expectedKeys = ['FontFile', 'FontFile2', 'FontFile3', 'Size']; + + var fontFileDict = new Dict(); + fontFileDict.set('FontFile', 'Type1 font file'); + var mergedDict = Dict.merge(null, + [dictWithManyKeys, dictWithSizeKey, fontFileDict]); + var mergedKeys = mergedDict.getKeys(); + + expect(mergedKeys.sort()).toEqual(expectedKeys); + expect(mergedDict.get('FontFile')).toEqual(testFontFile); + }); }); describe('Ref', function() { @@ -146,9 +234,45 @@ describe('primitives', function() { }); }); + describe('isName', function () { + it('handles non-names', function () { + var nonName = {}; + expect(isName(nonName)).toEqual(false); + }); + + it('handles names', function () { + var name = Name.get('Font'); + expect(isName(name)).toEqual(true); + }); + }); + + describe('isCmd', function () { + it('handles non-commands', function () { + var nonCmd = {}; + expect(isCmd(nonCmd)).toEqual(false); + }); + + it('handles commands', function () { + var cmd = Cmd.get('BT'); + expect(isCmd(cmd)).toEqual(true); + }); + + it('handles commands with cmd check', function () { + var cmd = Cmd.get('BT'); + expect(isCmd(cmd, 'BT')).toEqual(true); + expect(isCmd(cmd, 'ET')).toEqual(false); + }); + }); + describe('isDict', function() { + it('handles non-dictionaries', function () { + var nonDict = {}; + expect(isDict(nonDict)).toEqual(false); + }); + it('handles empty dictionaries with type check', function() { - var dict = new Dict(); + var dict = Dict.empty; + expect(isDict(dict)).toEqual(true); expect(isDict(dict, 'Page')).toEqual(false); }); @@ -156,6 +280,19 @@ describe('primitives', function() { var dict = new Dict(); dict.set('Type', Name.get('Page')); expect(isDict(dict, 'Page')).toEqual(true); + expect(isDict(dict, 'Contents')).toEqual(false); + }); + }); + + describe('isRef', function () { + it ('handles non-refs', function () { + var nonRef = {}; + expect(isRef(nonRef)).toEqual(false); + }); + + it ('handles refs', function () { + var ref = new Ref(1, 0); + expect(isRef(ref)).toEqual(true); }); });