diff options
author | Matěj Cepl <mcepl@redhat.com> | 2010-05-31 22:19:07 +0200 |
---|---|---|
committer | Matěj Cepl <mcepl@redhat.com> | 2010-05-31 22:19:07 +0200 |
commit | 93570dc0f18dbe255cf453c947c5d521acae9c69 (patch) | |
tree | fe3cdd73196087d6c988a0fcc05cd5bbf2768998 | |
parent | 475c2df048fa5c42ba913ceeba3e6201938031f7 (diff) | |
download | bugzilla-triage-93570dc0f18dbe255cf453c947c5d521acae9c69.tar.gz |
* Added hopefully a full coverage of unit tests for:
util.valToArray
util.addCSVValue
util.removeCSVValue
* Cleaning up to make jslint happy for */util*.js
-rw-r--r-- | lib/util.js | 45 | ||||
-rw-r--r-- | tests/test-util.js | 93 |
2 files changed, 115 insertions, 23 deletions
diff --git a/lib/util.js b/lib/util.js index 3416b7b..c57cb1b 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,4 +1,5 @@ -/*global exports, require */ +/*global exports: false, require: false */ +/*jslint onevar: false */ // Released under the MIT/X11 license // http://www.opensource.org/licenses/mit-license.php "use strict"; @@ -27,7 +28,7 @@ var urlMod = require("url"); * Son.prototype.constructor = Son; * </pre> */ -exports.heir = function heir (p) { +exports.heir = function heir(p) { function f() {}; f.prototype = p.prototype; return new f(); @@ -41,7 +42,7 @@ exports.heir = function heir (p) { * @param list list * @return position of the string in the list, or -1 if none found. */ -var isInList = exports.isInList = function isInList (mbr, list) { +var isInList = exports.isInList = function isInList(mbr, list) { if (!list) { return false; } @@ -54,7 +55,7 @@ var isInList = exports.isInList = function isInList (mbr, list) { * @param date * @return string with the formatted date */ -exports.getISODate = function getISODate (dateStr) { +exports.getISODate = function getISODate(dateStr) { function pad(n) { return n < 10 ? '0' + n : n; } @@ -84,11 +85,17 @@ var valToArray = exports.valToArray = function valToArray(val) { * @return String with merged lists */ exports.addCSVValue = function addCSVValue(str, value) { - var parts = (str.trim().length > 0 ? str.split(",") : []); - if (!isInList(value,parts)) { + var parts = (str.trim().length > 0 ? str.split(/,\s*/) : []); + if (!value) { + return str; + } + if (!isInList(value, parts)) { var newValue = valToArray(value); parts = parts.concat(newValue); } + // this is necessary to get comma-space separated string even when + // value is an array already + parts = parts.join(",").split(","); return parts.join(", "); }; @@ -101,12 +108,12 @@ exports.addCSVValue = function addCSVValue(str, value) { */ exports.removeCSVValue = function removeCSVValue(str, value) { str = str.trim(); - var parts = str ? str.split(",") : []; - var valueArr = value instanceof Array ? value : value.split(","); - parts = parts.filter(function (e,i,a) { - return (isInList(e,valueArr)); + var parts = str ? str.split(/,\s*/) : []; + var valueArr = value instanceof Array ? value : value.split(",\s*"); + parts = parts.filter(function (e, i, a) { + return (!isInList(e, valueArr)); }); - return parts.join(","); + return parts.join(", "); }; /** @@ -114,8 +121,9 @@ exports.removeCSVValue = function removeCSVValue(str, value) { * * @param url String with the investigated URL * @return String with hostname + * @depreceated */ -exports.getHost = function getHost (url) { +exports.getHost = function getHost(url) { var uri = urlMod.parse(url); return uri.host; }; @@ -123,10 +131,7 @@ exports.getHost = function getHost (url) { /** * */ -var getBugNo = exports.getBugNo = function getBugNo (url) { -// var bugNoTitle = this.doc.querySelector("#title > p").textContent.trim(); -// console.log("bugNoTitle = " + bugNoTitle); -// this.bugNo = this.getBugNo(this.doc.location.toString()); +var getBugNo = exports.getBugNo = function getBugNo(url) { var re = new RegExp(".*id=([0-9]+).*$"); var bugNo = null; var reResult = re.exec(url); @@ -145,7 +150,7 @@ var getBugNo = exports.getBugNo = function getBugNo (url) { * @param what optional Object argument representing this for this call * @return none */ -var loadText = exports.loadText = function loadText (URL, cb_function, what) { +var loadText = exports.loadText = function loadText(URL, cb_function, what) { if (what === undefined) { // missing optional argument what = this; } @@ -153,8 +158,8 @@ var loadText = exports.loadText = function loadText (URL, cb_function, what) { var req = new xhrMod.XMLHttpRequest(); req.open("GET", URL, true); req.onreadystatechange = function (aEvt) { - if (req.readyState == 4) { - if (req.status == 200) { + if (req.readyState === 4) { + if (req.status === 200) { cb_function.call(what, req.responseText); } else { throw "Getting " + URL + "failed!"; @@ -173,7 +178,7 @@ var loadText = exports.loadText = function loadText (URL, cb_function, what) { * @param what optional Object argument representing this for this call * @return none */ -exports.loadJSON = function loadJSON (URL, cb_function, what) { +exports.loadJSON = function loadJSON(URL, cb_function, what) { if (what === undefined) { // missing optional argument what = this; } diff --git a/tests/test-util.js b/tests/test-util.js index 5af8eee..c97776a 100644 --- a/tests/test-util.js +++ b/tests/test-util.js @@ -1,7 +1,94 @@ +/*global exports: false, require: false */ +/*jslint plusplus: false */ +// TODO: add some failing tests as well +"use strict"; var util = require("util"); -exports.ensureGetHost = function(test) { - var uri = util.getHost("https://bugzilla.redhat.com/show_bug.cgi?id=597141"); - test.assertEqual(uri, "bugzilla.redhat.com", "checking right hostname from URL"); +// shamelessly stolen from +// http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256BFB0077DFFD +var areArraysEqual = function areArraysEqual(array1, array2) { + var temp = [], i = 0, key = ""; + if ((!array1[0]) || (!array2[0])) { // If either is not an array + return false; + } + if (array1.length !== array2.length) { + return false; + } + // Put all the elements from array1 into a "tagged" array + for (i = 0; i < array1.length; i++) { + key = (typeof array1[i]) + "~" + array1[i]; + // Use "typeof" so a number 1 isn't equal to a string "1". + if (temp[key]) { + temp[key]++; + } else { + temp[key] = 1; + } + // temp[key] = # of occurrences of the value (so an element could appear multiple times) + } + // Go through array2 - if same tag missing in "tagged" array, not equal + for (i = 0; i < array2.length; i++) { + key = (typeof array2[i]) + "~" + array2[i]; + if (temp[key]) { + if (temp[key] === 0) { + return false; + } else { + temp[key]--; + } + // Subtract to keep track of # of appearances in array2 + } else { // Key didn't appear in array1, arrays are not equal. + return false; + } + } + return true; }; +// testing util.valToArray +exports.ensureValToArray = function (test) { + test.assert(areArraysEqual(util.valToArray("a"), ["a"]), + "conversion of a string to an array"); +}; + +// testing util.addCSVValue +exports.ensureCSVAddedToNull = function (test) { + test.assertEqual(util.addCSVValue("", "b"), "b", + "adding a string to empty string"); +}; + +exports.ensureCSVAddedNull = function (test) { + test.assertEqual(util.addCSVValue("a", ""), "a", + "adding nothing to a string"); +}; + +exports.ensureCSVAddedString = function (test) { + test.assertEqual(util.addCSVValue("a", "b"), "a, b", + "adding one string to another one"); +}; + +exports.ensureCSVAddedArray = function (test) { + test.assertEqual(util.addCSVValue("a", ["b", "c"]), "a, b, c", + "adding array to a string"); +}; + +exports.ensureCSVAddedArray2Array = function (test) { + test.assertEqual(util.addCSVValue("a, b", ["c", "d"]), "a, b, c, d", + "adding one array to another"); +}; + +// testing util.removeCSVValue +exports.ensureCSVRemoveSimple = function (test) { + test.assertEqual(util.removeCSVValue("a, b", "b"), "a", + "removing one string from an array"); + +}; + +exports.ensureCSVRemoveNonMember = function (test) { + test.assertEqual(util.removeCSVValue("a, b", "c"), "a, b", + "removing a string from an array of which it isn't a member"); + +}; + +exports.ensureCSVRemoveEmpty = function (test) { + test.assertEqual(util.removeCSVValue("", "c"), "", + "removing a string from an empty array"); + +}; |