JavaScript 自定义单元测试

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <script>
        'use strict';
        var yyTester = (function(){
            var testFunctions = [],
                testDesc = [];

            function describe (desc, func) {
                if (testFunctions.indexOf(func) < 0){
                    testFunctions.push(func);
                    testDesc.push(desc);
                }
            }

            function undescribe (func) {
                var i = testFunctions.indexOf(func);
                if (i >= 0){
                    testFunctions.splice(i, 1);
                    testDesc.splice(i, 1);
                }
            }

            function runTest(){
                for (var i = 0, l = testFunctions.length; i < l; ++i){
                    console.log(testDesc[i]);
                    testFunctions[i]();
                }
            }

            return {
                $describe : describe , 
                $undescribe: undescribe,
                $runTest: runTest
            };
        })();
            
        (function(){
            yyTester.$describe('数组测试', function(){
                var a = [];
                var aValue = 1;
                a.push(aValue);
                console.assert(a.length == 1);
                console.assert(aValue == a[a.length - 1]);
                console.assert(a.indexOf(aValue) == a.length - 1);
                a.pop();
                console.assert(a.length == 0);
            });
            
            yyTester.$runTest();
        })();
        </script>
    </head>
    <body>
    </body>
</html>

 

posted @ 2015-04-15 08:05  一花一世界,一叶一乾坤  阅读(157)  评论(0编辑  收藏  举报