aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/util.js5
-rw-r--r--tests/test-util.js101
2 files changed, 35 insertions, 71 deletions
diff --git a/lib/util.js b/lib/util.js
index dbadef8..b6691af 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -74,7 +74,10 @@ exports.getISODate = function getISODate(dateStr) {
* the result will be untouched actual argument of the call.
*/
var valToArray = exports.valToArray = function valToArray(val) {
- return (val instanceof Array) ? val : [val];
+ var isArr = val &&
+ val.constructor &&
+ val.constructor.name === "Array";
+ return isArr ? val : [val];
};
/**
diff --git a/tests/test-util.js b/tests/test-util.js
index 2104a2e..49450c9 100644
--- a/tests/test-util.js
+++ b/tests/test-util.js
@@ -4,49 +4,7 @@
"use strict";
var util = require("util");
-var pushkinTestString = "Byl pozdní večer první máj!\n\n" +
- "Нас было много на челне;\nИные парус напрягали,\nДругие дружно упирали\n\n" +
- "В глубь мощны веслы. В тишине\nНа руль склонясь, наш кормщик умный\n" +
- "В молчаньи правил грузный чолн;\nА я — беспечной веры полн —\n" +
- "Пловцам я пел....";
-
-// 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;
-};
+var testString = "When in the Course of human events it becomes necessary for one people to dissolve the political bands which have connected them with another and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.\n";
// testing util.heir
exports.ensureHeir = function (test) {
@@ -93,7 +51,6 @@ exports.ensureHeir = function (test) {
"checking creation of new overloaded method");
};
-
// testing util.isInList
exports.ensureIsInListTrue = function (test) {
test.assert(util.isInList("a", ["a"]),
@@ -123,21 +80,22 @@ exports.ensureGetISODate = function (test) {
// testing util.valToArray
exports.ensureValToArrayString = function (test) {
- test.assert(areArraysEqual(util.valToArray("a"), ["a"]),
+ test.assertEqual(JSON.stringify(util.valToArray("a")),
+ JSON.stringify(["a"]),
"conversion of a string to an array");
};
-// TODO: somehow cannot pass array as a parameter
-// waiting on https://bugzilla.mozilla.org/show_bug.cgi?id=569273
-//exports.ensureValToArrayEmpty = function (test) {
-// test.assert(areArraysEqual(util.valToArray(""), [""]),
-// "conversion of a string to an array");
-//};
+exports.ensureValToArrayEmpty = function (test) {
+ test.assertEqual(JSON.stringify(util.valToArray("")),
+ JSON.stringify([""]),
+ "conversion of an empty string to an array");
+};
-//exports.ensureValToArrayArray = function (test) {
-// test.assert(areArraysEqual(util.valToArray("a"), ["a"]),
-// "conversion of a string to an array");
-//};
+exports.ensureValToArrayArray = function (test) {
+ test.assertEqual(JSON.stringify(util.valToArray(["a"])),
+ JSON.stringify(["a"]),
+ "non-conversion of an array");
+};
// testing util.addCSVValue
exports.ensureCSVAddedToNull = function (test) {
@@ -191,21 +149,24 @@ exports.ensureGetBugNo = function (test) {
test.assertEqual(bugNo, 597141, "getting bug number");
};
-// TODO: waiting on https://bugzilla.mozilla.org/show_bug.cgi?id=569271
//// testing util.loadText
-//exports.ensureLoadText = function (test) {
-// var url = "http://www.ceplovi.cz/matej/progs/data/pushkin.txt";
-// var text = "";
-// util.loadText(url,function(txt) {
-// test.assertEqual(txt,pushkinTestString);
-// });
-//};
+exports.ensureLoadText = function (test) {
+ var url = "http://www.ceplovi.cz/matej/progs/data/doi.txt", text = "";
+ test.waitUntilDone();
+ util.loadText(url, function (txt) {
+ test.assertEqual(txt, testString);
+ test.done();
+ });
+};
//// testing util.loadJSON
-//exports.ensureLoadJSON = function (test) {
-// var url = "http://www.ceplovi.cz/matej/progs/data/test.json";
-// var date = {};
-// util.loadJSON(url,function(data) {
-// test.assertEqual(data,[1,2,3]);
-// });
-//};
+exports.ensureLoadJSON = function (test) {
+ var url = "http://www.ceplovi.cz/matej/progs/data/test.json",
+ date = {};
+ test.waitUntilDone();
+ util.loadJSON(url, function (data) {
+ test.assertEqual(JSON.stringify(data),
+ JSON.stringify([1, 2, 3]));
+ test.done();
+ });
+};