Test results

    The code...

        
       <script type="text/javascript">
       
            assert( true, "Test suite is up and running" );
       
            /* test 1: does function exist? */
            assert( typeof someFunction != "undefined", "Function 'someFunction' is valid" );
       
            /* test 2: does function return a string? */
            function helloWorld() { return 'hello world'; }  
       
            assert( helloWorld() == "hello world", "Function 'helloWorld' returns correct string" );
            
            /* test 3: does ninja.attack return a object? */
            var ninja = {
                            attack:function(steps) {
                            
                                // return object
                                var result = { 'type': 'katana', 'points': 0 };
                               
                                // any step?
                                if (steps != undefined) {
                                    
                                    // calculate damage
                                    var dmg = Math.floor( Math.random() * 10 * steps );
                                    
                                    // save and return
                                   result.points = dmg;
                                   return result;
                                    
                                } else {
                                
                                    // no step taken    
                                    result.points = 1;
                                    return result;
                                
                                }
                                
                            }
            };
            
            assert( typeof ninja.attack() == 'object', 'ninja.attack returns an object' );
            
            /* test 4: does ninja.attack(2).points return a number? */
            var pattern1 = new RegExp("[^0-9]"); // match all except numbers
            assert( pattern1.test( ninja.attack(2).points ) == false, "ninja.attack(2).points returns a number (regexp match)" );
            
            /* test 5; does ninja.attack(3).type return a string? */
            assert( typeof ninja.attack(3).type == "string", "ninja.attack(3).type returns a string (typeof)" );
            
            /* test 6: does ninja.attack(2).points return a number? */
            assert( typeof ninja.attack(2).points == "number", "ninja.attack(2).points returns a number (typeof)" );
            
       </script>
       
    

    The assert-function

    
        TEST_RESULT_ID_LIST = "results";
        TEST_PASS_LI_CLASS = "passed";
        TEST_FAIL_LI_CLASS = "failed";
        
        /**
         * Run a test and output the result in a LI-tag
         * specified by TEST_RESULT_ID_LIST
         *
         * @param bool value (testresult)
         * @param string desc (description of test)
        */
        function assert( value, desc ) {
            var li = document.createElement("li");
            li.className = value ? TEST_PASS_LI_CLASS : TEST_FAIL_LI_CLASS;
            li.appendChild( document.createTextNode( desc ) );
            document.getElementById( TEST_RESULT_ID_LIST ).appendChild( li );
        }