As we learned in Unit Tested, when you require that developers — especially those "certain" developers — write more unit tests, you'll get exactly what you ask for: more unit tests.

Johnny Biggg, whose company recently mandated this, knows this all too well. Although the ratio of testing-to-functional code went up, the quality (or lack thereof) remained about the same. Well, that is, unless you consider how often arrays can fail in JavaScript.

function doTest()
{
    var oTest = new Object()
    var oTest2 = new Object()
    var oFn1 = function(b){alert("fn1: " + b)}
    var oFn2 = function(c){alert("fn2: " + c)}
    var oFn3 = function(d){alert("fn3: " + d)}
    
    var a = new Array()
    a.push("hello")
    a.push("goodbye")
    a.push(1)
    a.push(2)
    a.push(true)
    a.push(oTest)
    a.push(oTest2)
    a.push(oFn1)
    a.push(oFn2)
    
    if (!(a.contains("hello") && a.contains("goodbye")))
    {
        alert("failed test: string not found, but should be")
        return false
    }
    if (a.contains("byebye"))
    {
        alert("failed test: string found, but should not be")
        return false
    }
    if (!(a.contains(1) && a.contains(2)))
    {
        alert("failed test: number not found, but should be")
        return false
    }
    if (a.contains(3))
    {
        alert("failed test: number found, but should not be")
        return false
    }
    if (!(a.contains(true)))
    {
        alert("failed test: boolean not found, but should be")
        return false
    }
    if (a.contains(false))
    {
        alert("failed test: boolean found, but should not be")
        return false
    }
    if (!(a.contains(oTest) && a.contains(oTest2)))
    {
        alert("failed test: object not found, but should be")
        return false
    }
    if (a.contains(new Object()))
    {
        alert("failed test: object found, but should not be")
        return false
    }
    if (!(a.contains(oFn1) && a.contains(oFn2)))
    {
        alert("failed test: function not found, but should be")
        return false
    }
    if (a.contains(oFn3))
    {
        alert("failed test: function found, but should not be")
        return false
    }
    if (a.indexOf("hello")!=0)
    {
        alert("failed test: index not correct for string")
        return false
    }
    if (a.indexOf(1)!=2)
    {
        alert("failed test: index not correct for number")
        return false
    }
    if (a.indexOf(true)!=4)
    {
        alert("failed test: index not correct for boolean")
        return false
    }
    if (a.indexOf(oTest)!=5)
    {
        alert("failed test: index not correct for object")
        return false
    }
    if (a.indexOf(oFn1)!=7)
    {
        alert("failed test: index not correct for function")
        return false
    }
    alert("passed all tests")
}
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!