Nickolay - Test.Run-0.10

Documentation | Source
Role('Test.Run.Test.More', {
    
    methods : {
        
        like : function (string, regex, desc) {
            if (regex instanceof RegExp) 
                this.ok(string.match(regex), desc)
            else
                this.ok(string.indexOf(regex) != -1, desc)
        },
        
        
        unlike : function(string, regex, desc) {
            if (regex instanceof RegExp) 
                this.ok(!string.match(regex), desc)
            else
                this.ok(string.indexOf(regex) == -1, desc)
        },
        
        
        throwsOk : function () {
            this.throws_ok.apply(this, arguments)
        },
        
        
        livesOk : function () {
            this.lives_ok.apply(this, arguments)
        },
        
        
        throws_ok : function(func, expected, desc) {
            if (typeof func != 'function') throw 'throws_ok accepts a function as 1st argument'
            
            var e = this.topScope.__EXCEPTION_CATCHER__(func)
            
            if (e instanceof this.topScope.Error)
                //IE uses non-standard 'description' property for error msg
                e = e.message || e.description
            
            this.like('' + e, expected, desc + ' (got [' + e + '], expected [' + expected + '])')
        },
        
        
        lives_ok : function (func, desc) {
            if (typeof func != 'function') throw 'lives_ok accepts a function as 1st argument'
            
            var e = this.topScope.__EXCEPTION_CATCHER__(func)
            
            if (e) 
                this.fail(desc)
            else
                this.pass(desc)
        },
        
        
        isaOk : function (value, className, desc) {
            this.isa_ok(value, className, desc)
        },
        
        
        isa_ok : function (value, className, desc) {
            try {
                if (typeof className == 'string') className = eval(className)
            } catch (e) {
                this.fail("Exception [" + e + "] caught, when evaluting the class name [" + className + "]")
            }
            
            this.ok(value instanceof className, desc)
        },
        
        
        typeOf : function (object) {
            return Object.prototype.toString.call(object).replace(/^\[object /, '').replace(/\]$/, '')
        },
        
        
        countKeys : function (object) {
            var counter = 0
            
            Joose.O.eachOwn(object, function () {
                counter++
            })
            
            return counter
        },
        
        
        compareObjects : function (obj1, obj2) {
            if (obj1 == obj2) return true
            
            var type1 = this.typeOf(obj1)
            var type2 = this.typeOf(obj2)
            
            if (type1 != type2) return false
            
            if (type1 == 'Array')
                if (obj1.length != obj2.length) 
                    return false
                else {
                    for (var i = 0; i < obj1.length; i++)
                        if (!this.compareObjects(obj1[ i ], obj2[ i ])) return false
                    
                    return true
                }
            
            if (type1 == 'Object')
                if (this.countKeys(obj1) != this.countKeys(obj2)) 
                    return false
                else {
                    var res = Joose.O.eachOwn(obj1, function (value, name) {
                        
                        if (!this.compareObjects(value, obj2[ name ])) return false
                    })
                    
                    return res === false ? false : true
                }
        }, 
        
        
        isDeeply : function (obj1, obj2, desc) {
            this.ok(this.compareObjects(obj1, obj2), desc)
        },
        
        
        is_deeply : function (obj1, obj2, desc) {
            this.ok(this.compareObjects(obj1, obj2), desc)
        }
        
        
    }
        
})
//eof Test.Run.Test.More