From 84d6a121af3849f3654cbef9efb7fde050baa1ea Mon Sep 17 00:00:00 2001 From: Kalervo Kujala Date: Wed, 28 Dec 2011 23:20:04 +0200 Subject: [PATCH 1/2] Add basic unit tests for obj.js. --- src/obj.js | 10 +--- test/unit/obj_spec.js | 132 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 131 insertions(+), 11 deletions(-) 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..f886904e5 100644 --- a/test/unit/obj_spec.js +++ b/test/unit/obj_spec.js @@ -3,14 +3,138 @@ '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() { + beforeEach(function() { + this.checkInvalidHasValues = function(dict, key) { + expect(dict.has()).toBeFalsy(); + expect(dict.has(key)).toBeFalsy(); + }; + this.checkInvalidKeyValues = function(dict, key) { + expect(dict.get()).toBeUndefined(); + expect(dict.get(key)).toBeUndefined(); + expect(dict.get('Prev', 'Root')).toBeUndefined(); + + // Note that the getter with three arguments breaks the pattern here. + expect(dict.get('Encrypt', 'Info', 'ID')).toBeNull(); + }; + }); + + it('should return invalid values for unknown keys', function() { + var dict = new Dict(); + this.checkInvalidHasValues(dict, 'Size'); + this.checkInvalidKeyValues(dict, 'Size'); + }); + + it('should return correct value for stored Size key', function() { + var dict = new Dict(); + var storedSize = 42; + dict.set('Size', storedSize); + + expect(dict.has('Size')).toBeTruthy(); + + expect(dict.get('Size')).toEqual(storedSize); + expect(dict.get('Prev', 'Size')).toEqual(storedSize); + expect(dict.get('Prev', 'Root', 'Size')).toEqual(storedSize); + }); + + it('should return invalid values for unknown keys when Size key is stored', + function() { + var dict = new Dict(); + var storedSize = 42; + dict.set('Size', storedSize); + + this.checkInvalidHasValues(dict, 'Prev'); + this.checkInvalidKeyValues(dict, 'Prev'); + }); + + 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(); + + this.checkInvalidKeyValues(dict, 'Size'); + }); + + it('should return correct values for multiple stored keys', function() { + var dict = new Dict(); + var storedSize = 42; + var storedPrev = 4; + var storedID = '[<06316BB1DF26984E89AFA8619B8722ED>' + + '<5E07F5458646544588630F1FD634FF2C>]'; + dict.set('Size', storedSize); + dict.set('Prev', storedPrev); + dict.set('ID', storedID); + + expect(dict.has('Size')).toBeTruthy(); + expect(dict.has('Prev')).toBeTruthy(); + expect(dict.has('ID')).toBeTruthy(); + + expect(dict.get('ID')).toEqual(storedID); + expect(dict.get('Prev', 'ID')).toEqual(storedPrev); + expect(dict.get('Size', 'Prev', 'ID')).toEqual(storedSize); + }); + + it('should callback for each stored key', function() { + var dict = new Dict(); + var storedSize = 42; + var storedPrev = 4; + var storedID = '[<06316BB1DF26984E89AFA8619B8722ED>' + + '<5E07F5458646544588630F1FD634FF2C>]'; + dict.set('Size', storedSize); + dict.set('Prev', storedPrev); + dict.set('ID', storedID); + + var callbackSpy = jasmine.createSpy('spy on callback in dictionary'); + + dict.forEach(callbackSpy); + + expect(callbackSpy).wasCalled(); + expect(callbackSpy.argsForCall[0]).toEqual(['Size', storedSize]); + expect(callbackSpy.argsForCall[1]).toEqual(['Prev', storedPrev]); + expect(callbackSpy.argsForCall[2]).toEqual(['ID', storedID]); + expect(callbackSpy.callCount).toEqual(3); + }); + }); + + describe('Ref', function() { + it('should retain the stored values', function() { + var storedNum = 42; + var storedGen = "fortytwo"; + var ref = new Ref(storedNum, storedGen); + expect(ref.num).toEqual(storedNum); + expect(ref.gen).toEqual(storedGen); + }); + }); }); From 0e2fb810c5c6cd5afeabc4edbbd3a4553e742854 Mon Sep 17 00:00:00 2001 From: Kalervo Kujala Date: Thu, 29 Dec 2011 14:06:06 +0200 Subject: [PATCH 2/2] Refactor obj.js unit tests to be aligned with code. --- test/unit/obj_spec.js | 113 +++++++++++++++++++----------------------- 1 file changed, 52 insertions(+), 61 deletions(-) diff --git a/test/unit/obj_spec.js b/test/unit/obj_spec.js index f886904e5..02e268fd4 100644 --- a/test/unit/obj_spec.js +++ b/test/unit/obj_spec.js @@ -20,7 +20,6 @@ describe('obj', function() { 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'); @@ -33,47 +32,55 @@ describe('obj', function() { }); describe('Dict', function() { - beforeEach(function() { - this.checkInvalidHasValues = function(dict, key) { - expect(dict.has()).toBeFalsy(); - expect(dict.has(key)).toBeFalsy(); - }; - this.checkInvalidKeyValues = function(dict, key) { - expect(dict.get()).toBeUndefined(); - expect(dict.get(key)).toBeUndefined(); - expect(dict.get('Prev', 'Root')).toBeUndefined(); + var checkInvalidHasValues = function(dict) { + expect(dict.has()).toBeFalsy(); + expect(dict.has('Prev')).toBeFalsy(); + }; - // Note that the getter with three arguments breaks the pattern here. - expect(dict.get('Encrypt', 'Info', 'ID')).toBeNull(); - }; + 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() { - var dict = new Dict(); - this.checkInvalidHasValues(dict, 'Size'); - this.checkInvalidKeyValues(dict, 'Size'); + checkInvalidHasValues(emptyDict); + checkInvalidKeyValues(emptyDict); }); it('should return correct value for stored Size key', function() { - var dict = new Dict(); - var storedSize = 42; - dict.set('Size', storedSize); + expect(dictWithSizeKey.has('Size')).toBeTruthy(); - expect(dict.has('Size')).toBeTruthy(); - - expect(dict.get('Size')).toEqual(storedSize); - expect(dict.get('Prev', 'Size')).toEqual(storedSize); - expect(dict.get('Prev', 'Root', 'Size')).toEqual(storedSize); + 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() { - var dict = new Dict(); - var storedSize = 42; - dict.set('Size', storedSize); - - this.checkInvalidHasValues(dict, 'Prev'); - this.checkInvalidKeyValues(dict, 'Prev'); + checkInvalidHasValues(dictWithSizeKey); + checkInvalidKeyValues(dictWithSizeKey); }); it('should return correct value for stored Size key with undefined value', @@ -83,54 +90,38 @@ describe('obj', function() { expect(dict.has('Size')).toBeTruthy(); - this.checkInvalidKeyValues(dict, 'Size'); + checkInvalidKeyValues(dict); }); it('should return correct values for multiple stored keys', function() { - var dict = new Dict(); - var storedSize = 42; - var storedPrev = 4; - var storedID = '[<06316BB1DF26984E89AFA8619B8722ED>' + - '<5E07F5458646544588630F1FD634FF2C>]'; - dict.set('Size', storedSize); - dict.set('Prev', storedPrev); - dict.set('ID', storedID); + expect(dictWithManyKeys.has('FontFile')).toBeTruthy(); + expect(dictWithManyKeys.has('FontFile2')).toBeTruthy(); + expect(dictWithManyKeys.has('FontFile3')).toBeTruthy(); - expect(dict.has('Size')).toBeTruthy(); - expect(dict.has('Prev')).toBeTruthy(); - expect(dict.has('ID')).toBeTruthy(); - - expect(dict.get('ID')).toEqual(storedID); - expect(dict.get('Prev', 'ID')).toEqual(storedPrev); - expect(dict.get('Size', 'Prev', 'ID')).toEqual(storedSize); + 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 dict = new Dict(); - var storedSize = 42; - var storedPrev = 4; - var storedID = '[<06316BB1DF26984E89AFA8619B8722ED>' + - '<5E07F5458646544588630F1FD634FF2C>]'; - dict.set('Size', storedSize); - dict.set('Prev', storedPrev); - dict.set('ID', storedID); - var callbackSpy = jasmine.createSpy('spy on callback in dictionary'); - dict.forEach(callbackSpy); + dictWithManyKeys.forEach(callbackSpy); expect(callbackSpy).wasCalled(); - expect(callbackSpy.argsForCall[0]).toEqual(['Size', storedSize]); - expect(callbackSpy.argsForCall[1]).toEqual(['Prev', storedPrev]); - expect(callbackSpy.argsForCall[2]).toEqual(['ID', storedID]); + 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 = 42; - var storedGen = "fortytwo"; + var storedNum = 4; + var storedGen = 2; var ref = new Ref(storedNum, storedGen); expect(ref.num).toEqual(storedNum); expect(ref.gen).toEqual(storedGen);