aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test-util.js
blob: c97776a87573a6bc4cdf9cd822da74e5c8df940d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*global exports: false, require: false */
/*jslint plusplus: false */
// TODO: add some failing tests as well
"use strict";
var util = require("util");

// 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");

};