aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test-util.js
blob: 2104a2e5bbd1ec43e7c44c77064ef85eda2bd837 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*global exports: false, require: false */
/*jslint plusplus: false */
// TODO: add some failing tests as well
"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;
};

// testing util.heir
exports.ensureHeir = function (test) {
    var fedlimid = {}, naoise = {};

    function Father(x) {
        this.family = x;
    }
    
    Father.prototype.getFamily = function getFamily() {
        return this.family;
    };

    function Son(x, w) {
        Father.call(this, x);
        this.wife = w;
    }
        
    Son.prototype = util.heir(Father);
    Son.prototype.constructor = Son;
    
    Son.prototype.getWife = function getWife() {
        return this.wife;
    };
    
    Son.prototype.getFamily = function getFamily() {
        var upFamily =
            Father.prototype.getFamily.call(this);
        return upFamily + ", " + this.wife;
    };
    
    // for curious and non-Celtic
    // http://en.wikipedia.org/wiki/Deirdre :)
    fedlimid = new Father("mac Daill");
    naoise = new Son("Usnech", "Deirdre");
    
    test.assertEqual(fedlimid.getFamily(), "mac Daill",
        "checking creation of new simple object");
    
    test.assertEqual(naoise.getWife(), "Deirdre",
        "checking creation of new daughter object");

    test.assertEqual(naoise.getFamily(), "Usnech, Deirdre",
        "checking creation of new overloaded method");
};


// testing util.isInList
exports.ensureIsInListTrue = function (test) {
    test.assert(util.isInList("a", ["a"]),
        "conversion of a string to an array");
};

exports.ensureIsInListFalse = function (test) {
    test.assert(!util.isInList("b", ["a"]),
        "conversion of a string to an array");
};

exports.ensureIsInListEmpty = function (test) {
    test.assert(!util.isInList("b", []),
        "conversion of a string to an array");
};

exports.ensureIsInListNoMember = function (test) {
    test.assert(!util.isInList("", ["x"]),
        "conversion of a string to an array");
};

// testing util.getISODate
exports.ensureGetISODate = function (test) {
    test.assertEqual(util.getISODate("Mon May 31 2010 23:29:09 GMT+0200 (CET)"),
        "2010-05-31", "conversion of a Date to ISO-formatted String");
};

// testing util.valToArray
exports.ensureValToArrayString = function (test) {
    test.assert(areArraysEqual(util.valToArray("a"), ["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.ensureValToArrayArray = 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");

};

// also checking a tolerancy against different ways of writing arrays
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");

};

// testing util.getBugNo
exports.ensureGetBugNo = function (test) {
    var bugNo = util.getBugNo("https://bugzilla.redhat.com/show_bug.cgi?id=597141");
    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);
//    });
//};

//// 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]);
//    });
//};