From 7c2ccbc44ecf7d6c8a577abe42333e383d23e407 Mon Sep 17 00:00:00 2001 From: Casper Warden <216465704+casperwardensl@users.noreply.github.com> Date: Tue, 19 Dec 2017 00:44:24 +0000 Subject: [PATCH] Unit tests for UUID class --- dist/tests/uuid.d.ts | 1 + dist/tests/uuid.js | 86 ++++++++++++++++++++++++++++++++ dist/tests/uuid.js.map | 1 + lib/tests/uuid.ts | 109 +++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- test/basic.js | 12 ----- 6 files changed, 198 insertions(+), 13 deletions(-) create mode 100644 dist/tests/uuid.d.ts create mode 100644 dist/tests/uuid.js create mode 100644 dist/tests/uuid.js.map create mode 100644 lib/tests/uuid.ts delete mode 100644 test/basic.js diff --git a/dist/tests/uuid.d.ts b/dist/tests/uuid.d.ts new file mode 100644 index 0000000..d490e0e --- /dev/null +++ b/dist/tests/uuid.d.ts @@ -0,0 +1 @@ +import 'mocha'; diff --git a/dist/tests/uuid.js b/dist/tests/uuid.js new file mode 100644 index 0000000..e271310 --- /dev/null +++ b/dist/tests/uuid.js @@ -0,0 +1,86 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +require("mocha"); +const validator = require("validator"); +const assert = require("assert"); +const UUID_1 = require("../classes/UUID"); +describe('UUID', () => { + describe('random', () => { + it('should generate a random, valid v4 UUID', () => { + const uuid = UUID_1.UUID.random().toString(); + const secondUUID = UUID_1.UUID.random().toString(); + if (typeof uuid !== 'string') { + assert.fail('Returned UUID is not a string'); + } + if (!validator.isUUID(uuid)) { + assert.fail('Returned string is not a valid UUID'); + } + if (uuid === '00000000-0000-0000-0000-000000000000') { + assert.fail('Random UUID should not be zero'); + } + if (typeof secondUUID !== 'string') { + assert.fail('Returned second UUID is not a string'); + } + if (!validator.isUUID(secondUUID)) { + assert.fail('Second UUID is not a valid UUID'); + } + if (secondUUID === '00000000-0000-0000-0000-000000000000') { + assert.fail('Random UUID should not be zero'); + } + if (uuid === secondUUID) { + assert.fail('Two random UUIDs match! (Not random)'); + } + assert.ok(true); + }); + }); + describe('zero', () => { + it('should generate a zeroed, valid v4 UUID', () => { + const uuid = UUID_1.UUID.zero().toString(); + if (typeof uuid !== 'string') { + assert.fail('Returned UUID is not a string'); + } + if (!validator.isUUID(uuid)) { + assert.fail('Returned string is not a valid UUID'); + } + if (uuid !== '00000000-0000-0000-0000-000000000000') { + assert.fail('UUID is not zero'); + } + assert.ok(true); + }); + }); + describe('encode/decode', () => { + it('should correctly decode a 16-byte UUID from a buffer', () => { + const buf = Buffer.from('00004af668bb6fe34893881408f586c5657c4e1c9910', 'hex'); + const uuid = new UUID_1.UUID(buf, 2); + const str = uuid.toString(); + if (typeof str !== 'string') { + assert.fail('Returned UUID is not a string'); + } + if (!validator.isUUID(str)) { + assert.fail('Returned string is not a valid UUID'); + } + if (str !== '4af668bb-6fe3-4893-8814-08f586c5657c') { + assert.fail('UUID decoded incorrectly'); + } + assert.ok(true); + }); + it('should correct encode a UUID into the correct position in a buffer', () => { + const buf = Buffer.alloc(22); + const uuid = new UUID_1.UUID('4af668bb-6fe3-4893-8814-08f586c5657c'); + uuid.writeToBuffer(buf, 2); + const bufCmp = Buffer.from('00004af668bb6fe34893881408f586c5657c00000000', 'hex'); + if (buf.compare(bufCmp) !== 0) { + assert.fail('Encoded buffer does not match expected output'); + } + const result = uuid.toString(); + if (typeof result !== 'string') { + assert.fail('Returned UUID is not a string'); + } + if (!validator.isUUID(result)) { + assert.fail('Returned string is not a valid UUID'); + } + assert.ok(true); + }); + }); +}); +//# sourceMappingURL=uuid.js.map \ No newline at end of file diff --git a/dist/tests/uuid.js.map b/dist/tests/uuid.js.map new file mode 100644 index 0000000..516316e --- /dev/null +++ b/dist/tests/uuid.js.map @@ -0,0 +1 @@ +{"version":3,"file":"uuid.js","sourceRoot":"","sources":["../../lib/tests/uuid.ts"],"names":[],"mappings":";;AAAA,iBAAe;AACf,uCAAuC;AACvC,iCAAiC;AACjC,0CAAqC;AAErC,QAAQ,CAAC,MAAM,EA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ad0 --- /dev/null +++ b/lib/tests/uuid.ts @@ -0,0 +1,109 @@ +import 'mocha'; +import * as validator from 'validator'; +import * as assert from 'assert'; +import {UUID} from '../classes/UUID'; + +describe('UUID', () => +{ + describe('random', () => + { + it ('should generate a random, valid v4 UUID', () => + { + const uuid = UUID.random().toString(); + const secondUUID = UUID.random().toString(); + + if (typeof uuid !== 'string') + { + assert.fail('Returned UUID is not a string'); + } + if (!validator.isUUID(uuid)) + { + assert.fail('Returned string is not a valid UUID'); + } + if (uuid === '00000000-0000-0000-0000-000000000000') + { + assert.fail('Random UUID should not be zero'); + } + if (typeof secondUUID !== 'string') + { + assert.fail('Returned second UUID is not a string'); + } + if (!validator.isUUID(secondUUID)) + { + assert.fail('Second UUID is not a valid UUID'); + } + if (secondUUID === '00000000-0000-0000-0000-000000000000') + { + assert.fail('Random UUID should not be zero'); + } + if (uuid === secondUUID) + { + assert.fail('Two random UUIDs match! (Not random)'); + } + assert.ok(true); + }); + }); + describe('zero', () => + { + it ('should generate a zeroed, valid v4 UUID', () => + { + const uuid = UUID.zero().toString(); + if (typeof uuid !== 'string') + { + assert.fail('Returned UUID is not a string'); + } + if (!validator.isUUID(uuid)) + { + assert.fail('Returned string is not a valid UUID'); + } + if (uuid !== '00000000-0000-0000-0000-000000000000') + { + assert.fail('UUID is not zero') + } + assert.ok(true); + }); + }); + describe('encode/decode', () => + { + it ('should correctly decode a 16-byte UUID from a buffer', () => + { + const buf = Buffer.from('00004af668bb6fe34893881408f586c5657c4e1c9910', 'hex'); + const uuid = new UUID(buf, 2); + const str = uuid.toString(); + if (typeof str !== 'string') + { + assert.fail('Returned UUID is not a string'); + } + if (!validator.isUUID(str)) + { + assert.fail('Returned string is not a valid UUID'); + } + if (str !== '4af668bb-6fe3-4893-8814-08f586c5657c') + { + assert.fail('UUID decoded incorrectly'); + } + assert.ok(true); + }); + it ('should correct encode a UUID into the correct position in a buffer', () => + { + const buf = Buffer.alloc(22); + const uuid = new UUID('4af668bb-6fe3-4893-8814-08f586c5657c'); + uuid.writeToBuffer(buf, 2); + const bufCmp = Buffer.from('00004af668bb6fe34893881408f586c5657c00000000', 'hex'); + if (buf.compare(bufCmp) !== 0) + { + assert.fail('Encoded buffer does not match expected output'); + } + const result = uuid.toString(); + if (typeof result !== 'string') + { + assert.fail('Returned UUID is not a string'); + } + if (!validator.isUUID(result)) + { + assert.fail('Returned string is not a valid UUID'); + } + assert.ok(true); + }); + }); +}); diff --git a/package.json b/package.json index fb9fcbe..75c38ef 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "scripts": { "setup": "npm install", "prepublish": "npm run build", - "test-only": "mocha test/", + "test-only": "mocha dist/tests/", "pretest": "npm run build", "test": "npm run test-only", "build": "tsc --removeComments", diff --git a/test/basic.js b/test/basic.js deleted file mode 100644 index 83d9c93..0000000 --- a/test/basic.js +++ /dev/null @@ -1,12 +0,0 @@ -const assert = require('assert'); - -describe('Array', function() -{ - describe('#indexOf()', function () - { - it('should return -1 when the value is not present', function () - { - assert.equal([1, 2, 3].indexOf(4), -1); - }); - }); -}); \ No newline at end of file