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:
parent
f07bd3b54e
commit
5a99f483b1
@ -58,6 +58,7 @@
|
||||
"spyOn": false,
|
||||
"runs": false,
|
||||
"waitsFor": false,
|
||||
"jasmine": false,
|
||||
|
||||
// functional test constants
|
||||
"browser": false,
|
||||
|
@ -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'
|
||||
],
|
||||
|
||||
|
@ -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;
|
||||
|
57
test/unit/custom_matchers.js
Normal file
57
test/unit/custom_matchers.js
Normal 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;
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
});
|
93
test/unit/util/helpers/string_util_test.js
Normal file
93
test/unit/util/helpers/string_util_test.js
Normal 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);
|
||||
});
|
||||
});
|
||||
});
|
27
test/unit/util/module_test.js
Normal file
27
test/unit/util/module_test.js
Normal 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();
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user