diff --git a/src/obj.js b/src/obj.js index 453014a91..bdb0ba58a 100644 --- a/src/obj.js +++ b/src/obj.js @@ -8,8 +8,7 @@ var Name = (function NameClosure() { this.name = name; } - Name.prototype = { - }; + Name.prototype = {}; return Name; })(); @@ -19,9 +18,7 @@ var Cmd = (function CmdClosure() { this.cmd = cmd; } - Cmd.prototype = { - }; - + Cmd.prototype = {}; var cmdCache = {}; @@ -80,8 +77,7 @@ var Ref = (function RefClosure() { this.gen = gen; } - Ref.prototype = { - }; + Ref.prototype = {}; return Ref; })(); diff --git a/test/unit/obj_spec.js b/test/unit/obj_spec.js index 4f1a0b57a..02e268fd4 100644 --- a/test/unit/obj_spec.js +++ b/test/unit/obj_spec.js @@ -3,14 +3,129 @@ 'use strict'; -describe("obj", function() { +describe('obj', function() { - describe("Name", function() { - it("should retain the given name", function() { - var givenName = "Font"; + describe('Name', function() { + it('should retain the given name', function() { + var givenName = 'Font'; var name = new Name(givenName); expect(name.name).toEqual(givenName); }); }); + + describe('Cmd', function() { + it('should retain the given cmd name', function() { + var givenCmd = 'BT'; + var cmd = new Cmd(givenCmd); + expect(cmd.cmd).toEqual(givenCmd); + }); + + it('should create only one object for a command and cache it', function() { + var firstBT = Cmd.get('BT'); + 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); + }); + }); + + describe('Dict', function() { + var checkInvalidHasValues = function(dict) { + expect(dict.has()).toBeFalsy(); + expect(dict.has('Prev')).toBeFalsy(); + }; + + var checkInvalidKeyValues = function(dict) { + expect(dict.get()).toBeUndefined(); + expect(dict.get('Prev')).toBeUndefined(); + expect(dict.get('Decode', 'D')).toBeUndefined(); + + // Note that the getter with three arguments breaks the pattern here. + expect(dict.get('FontFile', 'FontFile2', 'FontFile3')).toBeNull(); + }; + + var emptyDict, dictWithSizeKey, dictWithManyKeys; + var storedSize = 42; + var testFontFile = 'file1'; + var testFontFile2 = 'file2'; + var testFontFile3 = 'file3'; + + beforeEach(function() { + emptyDict = new Dict(); + + dictWithSizeKey = new Dict(); + dictWithSizeKey.set('Size', storedSize); + + dictWithManyKeys = new Dict(); + dictWithManyKeys.set('FontFile', testFontFile); + dictWithManyKeys.set('FontFile2', testFontFile2); + dictWithManyKeys.set('FontFile3', testFontFile3); + }); + + it('should return invalid values for unknown keys', function() { + checkInvalidHasValues(emptyDict); + checkInvalidKeyValues(emptyDict); + }); + + it('should return correct value for stored Size key', function() { + expect(dictWithSizeKey.has('Size')).toBeTruthy(); + + expect(dictWithSizeKey.get('Size')).toEqual(storedSize); + expect(dictWithSizeKey.get('Prev', 'Size')).toEqual(storedSize); + expect(dictWithSizeKey.get('Prev', 'Root', 'Size')).toEqual(storedSize); + }); + + it('should return invalid values for unknown keys when Size key is stored', + function() { + checkInvalidHasValues(dictWithSizeKey); + checkInvalidKeyValues(dictWithSizeKey); + }); + + it('should return correct value for stored Size key with undefined value', + function() { + var dict = new Dict(); + dict.set('Size'); + + expect(dict.has('Size')).toBeTruthy(); + + checkInvalidKeyValues(dict); + }); + + it('should return correct values for multiple stored keys', function() { + expect(dictWithManyKeys.has('FontFile')).toBeTruthy(); + expect(dictWithManyKeys.has('FontFile2')).toBeTruthy(); + expect(dictWithManyKeys.has('FontFile3')).toBeTruthy(); + + expect(dictWithManyKeys.get('FontFile3')).toEqual(testFontFile3); + expect(dictWithManyKeys.get('FontFile2', 'FontFile3')) + .toEqual(testFontFile2); + expect(dictWithManyKeys.get('FontFile', 'FontFile2', 'FontFile3')) + .toEqual(testFontFile); + }); + + it('should callback for each stored key', function() { + var callbackSpy = jasmine.createSpy('spy on callback in dictionary'); + + dictWithManyKeys.forEach(callbackSpy); + + expect(callbackSpy).wasCalled(); + expect(callbackSpy.argsForCall[0]).toEqual(['FontFile', testFontFile]); + expect(callbackSpy.argsForCall[1]).toEqual(['FontFile2', testFontFile2]); + expect(callbackSpy.argsForCall[2]).toEqual(['FontFile3', testFontFile3]); + expect(callbackSpy.callCount).toEqual(3); + }); + }); + + describe('Ref', function() { + it('should retain the stored values', function() { + var storedNum = 4; + var storedGen = 2; + var ref = new Ref(storedNum, storedGen); + expect(ref.num).toEqual(storedNum); + expect(ref.gen).toEqual(storedGen); + }); + }); });