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
This commit is contained in:
Michael Krotscheck 2014-03-28 16:57:32 -07:00
parent f07bd3b54e
commit 5a99f483b1
6 changed files with 189 additions and 7 deletions

View File

@ -58,6 +58,7 @@
"spyOn": false, "spyOn": false,
"runs": false, "runs": false,
"waitsFor": false, "waitsFor": false,
"jasmine": false,
// functional test constants // functional test constants
"browser": false, "browser": false,

View File

@ -39,6 +39,7 @@ module.exports = function (config) {
'./bower_components/angular-mocks/angular-mocks.js', './bower_components/angular-mocks/angular-mocks.js',
'./dist/js/storyboard.js', './dist/js/storyboard.js',
'./dist/js/templates.js', './dist/js/templates.js',
'./test/unit/custom_matchers.js',
'./test/unit/**/*.js' './test/unit/**/*.js'
], ],

View File

@ -24,6 +24,11 @@ angular.module('sb.util').factory('StringUtil',
function () { function () {
'use strict'; 'use strict';
var defaultLength = 32; // MD5 length. Seems decent.
var alphaNumeric = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
'abcdefghijklmnopqrstuvwxyz' +
'0123456789';
return { return {
/** /**
* Helper to generate a random alphanumeric string for the state * 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. * @returns {string} A random alphanumeric string.
*/ */
randomAlphaNumeric: function (length) { randomAlphaNumeric: function (length) {
var possible = return this.random(length, alphaNumeric);
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
'abcdefghijklmnopqrstuvwxyz' +
'0123456789';
return this.random(length, possible);
}, },
/** /**
@ -51,6 +51,9 @@ angular.module('sb.util').factory('StringUtil',
* characters. * characters.
*/ */
random: function (length, characters) { random: function (length, characters) {
length = length || defaultLength;
characters = characters || alphaNumeric;
var text = ''; var text = '';
for (var i = 0; i < length; i++) { for (var i = 0; i < length; i++) {

View File

@ -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;
}
};
}
});
});

View File

@ -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);
});
});
});

View File

@ -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();
});
});