aboutsummaryrefslogblamecommitdiffstats
path: root/tests/test-util.js
blob: a9d0e6a9be11f534bd9a28ce9703b2de5300960c (plain) (tree)
1
2
3
4
5
6
7
8
9



                                          

                           
                                                                                                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
 












































                                                           




















                                                  




















                                                                         





                                                                                
                          
                                                  

                                                          


                                              




                                                         
 




                                                            
 
































                                                                        
                                                                     
                                                    
                                                             








                                                                      






                                                                                    
                          







                                                                          
 








                                                                             
 
                          









                                                                 
/*global exports: false, require: false */
/*jslint plusplus: false */
// TODO: add some failing tests as well
"use strict";
var util = require("util");

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";
var pushkinTestString = "Byl pozdní večer první máj!\n\nАРИОН.\n\nНас было много на челне;\nИные парус напрягали,\nДругие дружно упирали\nВ глубь мощны веслы. В тишине\nНа руль склонясь, наш кормщик умный\nВ молчаньи правил грузный чолн;\nА я — беспечной веры полн —\nПловцам я пел … Вдруг лоно волн\nИзмял с налету вихорь шумный …\nПогиб и кормщик и пловец! –\nЛишь я, таинственный певец,\nНа берег выброшен грозою,\nЯ гимны прежние пою\nИ ризу влажную мою\nСушу на солнце под скалою.\n"

// 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.filterByRegexp
exports.ensureFilterByRegexp = function (test) {
    var list = [
        {
            "regexp": "test(ing|ed)",
            "addr": "correct" 
        },
        {
            "regexp": "ba.*d",
            "addr": true
        }
    ];
    
    test.assertEqual(util.filterByRegexp(list,"testing"),"correct",
        "simple testing of filterByRegexp");
    test.assertEqual(util.filterByRegexp(list,"unknown value"),"",
        "simple testing of filterByRegexp with non-found address");
    test.assert(util.filterByRegexp(list,"baaad"),
        "simple testing of filterByRegexp with non-string return value");
};

// 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.assertEqual(JSON.stringify(util.valToArray("a")),
            JSON.stringify(["a"]),
        "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.assertEqual(JSON.stringify(util.valToArray(["a"])),
            JSON.stringify(["a"]),
        "non-conversion of 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");
};

//// testing util.loadText
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();
    });
};

// exports.ensureLoadTextUnicode = function (test) {
//     var url = "http://matej.ceplovi.cz/progs/data/pushkin.txt", text = "";
//     test.waitUntilDone();
//     util.loadText(url, function (txt) {
//         console.log(txt);
//         //test.assertEqual(txt, pushkinTestString);
//         test.done();
//     });
// };

//// testing util.loadJSON
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();
    });
};