From 5a99f483b18f9c6abd28dc02e31c06c1b3c862f9 Mon Sep 17 00:00:00 2001 From: Michael Krotscheck Date: Fri, 28 Mar 2014 16:57:32 -0700 Subject: [PATCH] Added unit tests for string_util. Tests are good. These put some sane rules around our random string generation utilities, and provides demo code for writing convenience test methods that can be applied to the global testing environment. Change-Id: Ib03382068bf3165addc4d96595850b6b0c3087cb --- .jshintrc | 1 + karma-unit.conf.js | 1 + src/app/util/helpers/string_util.js | 17 ++-- test/unit/custom_matchers.js | 57 +++++++++++++ test/unit/util/helpers/string_util_test.js | 93 ++++++++++++++++++++++ test/unit/util/module_test.js | 27 +++++++ 6 files changed, 189 insertions(+), 7 deletions(-) create mode 100644 test/unit/custom_matchers.js create mode 100644 test/unit/util/helpers/string_util_test.js create mode 100644 test/unit/util/module_test.js diff --git a/.jshintrc b/.jshintrc index bb85a928..13332331 100644 --- a/.jshintrc +++ b/.jshintrc @@ -58,6 +58,7 @@ "spyOn": false, "runs": false, "waitsFor": false, + "jasmine": false, // functional test constants "browser": false, diff --git a/karma-unit.conf.js b/karma-unit.conf.js index 29b3c7d0..1bcc9c48 100644 --- a/karma-unit.conf.js +++ b/karma-unit.conf.js @@ -39,6 +39,7 @@ module.exports = function (config) { './bower_components/angular-mocks/angular-mocks.js', './dist/js/storyboard.js', './dist/js/templates.js', + './test/unit/custom_matchers.js', './test/unit/**/*.js' ], diff --git a/src/app/util/helpers/string_util.js b/src/app/util/helpers/string_util.js index 9f4a6609..3f09ee20 100644 --- a/src/app/util/helpers/string_util.js +++ b/src/app/util/helpers/string_util.js @@ -24,6 +24,11 @@ angular.module('sb.util').factory('StringUtil', function () { 'use strict'; + var defaultLength = 32; // MD5 length. Seems decent. + var alphaNumeric = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + + 'abcdefghijklmnopqrstuvwxyz' + + '0123456789'; + return { /** * Helper to generate a random alphanumeric string for the state @@ -33,12 +38,7 @@ angular.module('sb.util').factory('StringUtil', * @returns {string} A random alphanumeric string. */ randomAlphaNumeric: function (length) { - var possible = - 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + - 'abcdefghijklmnopqrstuvwxyz' + - '0123456789'; - - return this.random(length, possible); + return this.random(length, alphaNumeric); }, /** @@ -51,11 +51,14 @@ angular.module('sb.util').factory('StringUtil', * characters. */ random: function (length, characters) { + length = length || defaultLength; + characters = characters || alphaNumeric; + var text = ''; for (var i = 0; i < length; i++) { text += characters.charAt(Math.floor( - Math.random() * characters.length)); + Math.random() * characters.length)); } return text; diff --git a/test/unit/custom_matchers.js b/test/unit/custom_matchers.js new file mode 100644 index 00000000..df4c13a7 --- /dev/null +++ b/test/unit/custom_matchers.js @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2014 Hewlett-Packard Development Company, L.P. + * + * Licensed under the Apache License, Version 2.0 (the 'License'); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +/** + * A list of custom matchers to simplify our unit tests. + */ +beforeEach(function () { + 'use strict'; + + // Make sure our custom matchers are registered on every run. + jasmine.addMatchers({ + + /** + * This custom matcher compares the actual value with a string of + * characters, which must all be contained within the actual string. If + * the actual value contains anything other than those characters, the + * test will fail. + */ + toOnlyContain: function () { + + return { + compare: function (actual, expected) { + var result = { + pass: true, + message: '' + }; + + for (var i = 0; i < actual.length; i++) { + var currentChar = actual.charAt(i); + var charIndex = expected.indexOf(currentChar); + if (charIndex === -1) { + result.pass = false; + result.message = 'String "' + actual + + '" may only contain "' + expected + '".'; + break; + } + + } + return result; + } + }; + } + }); +}); \ No newline at end of file diff --git a/test/unit/util/helpers/string_util_test.js b/test/unit/util/helpers/string_util_test.js new file mode 100644 index 00000000..18fdf196 --- /dev/null +++ b/test/unit/util/helpers/string_util_test.js @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2014 Hewlett-Packard Development Company, L.P. + * + * Licensed under the Apache License, Version 2.0 (the 'License'); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +/** + * Unit test for the util framework. + */ +describe('StringUtil', function () { + 'use strict'; + + beforeEach(function () { + + // Load the module + module('sb.util'); + }); + + // Make sure the utilities exist. + it('should exist', function () { + inject(function (StringUtil) { + expect(StringUtil).toBeDefined(); + }); + }); + + // Test random() method. + it('should have a random() method', function () { + inject(function (StringUtil) { + var len = 10; + var chars = 'abcde'; + + // The method must exist. + expect(StringUtil.random).toBeDefined(); + + // Invoking the method, with no parameters, should work. + var result = StringUtil.random(); + expect(result).toBeDefined(); + expect(result.length).toEqual(32); // MD5 length + expect(result).not.toEqual(StringUtil.random()); + + // Invoking the method, with a length but no characters, + // should work + var result2 = StringUtil.random(len); + expect(result2).toBeDefined(); + expect(result2.length).toEqual(len); + expect(result2).not.toEqual(StringUtil.random(len)); + + // Invoking the method, with a length and characters, should work. + var result3 = StringUtil.random(len, chars); + expect(result3).toBeDefined(); + expect(result3.length).toEqual(len); + expect(result3).not.toEqual(StringUtil.random(len, chars)); + expect(result3).toOnlyContain(chars); + }); + }); + + // Test randomAlphaNumeric + it('should have a randomAlphaNumeric() method', function () { + inject(function (StringUtil) { + var len = 10; + var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + + 'abcdefghijklmnopqrstuvwxyz' + + '0123456789'; + + // The method must exist. + expect(StringUtil.randomAlphaNumeric).toBeDefined(); + + // Invoking the method, with no parameters, should work. + var result = StringUtil.randomAlphaNumeric(); + expect(result).toBeDefined(); + expect(result.length).toEqual(32); // MD5 length + expect(result).not.toEqual(StringUtil.random()); + expect(result).toOnlyContain(chars); + + // Invoking the method, with a length, should work + var result2 = StringUtil.random(len); + expect(result2).toBeDefined(); + expect(result2.length).toEqual(len); + expect(result2).not.toEqual(StringUtil.random(len)); + expect(result2).toOnlyContain(chars); + }); + }); +}); diff --git a/test/unit/util/module_test.js b/test/unit/util/module_test.js new file mode 100644 index 00000000..95a3a4ee --- /dev/null +++ b/test/unit/util/module_test.js @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2014 Hewlett-Packard Development Company, L.P. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +/** + * Unit test for the util framework. + */ +describe('sb.util', function () { + 'use strict'; + + it('should exist', function () { + var module = angular.module('sb.util'); + expect(module).toBeTruthy(); + }); +});