清原研修/12 http://www.ark-web.jp/sandbox/wiki/446.html

[edit]

目次

[edit]

Jsunit

カテゴリーJsunit
優先順位至急
イテレーションイテレーション1
状態完了
完了予定日2007/10/5
工数
対応者清原

[edit]

使い方

テストケースの作成
jsUnitCore.jsをロードする

TestSuiteを作成
suite()メソッドを作成し、メソッド内でjsUnitTestSuiteクラスを生成し、各テストケースファイルを追加する

[edit]

作成したテストケース

今回はtest-div.htmlとtest-pytha.htmlの2つのテストケースを用意した
以下はtest-pytha.html

<html>
<head>
<script language="javascript"  src="../../jsunit/jsunit/app/jsUnitCore.js"></script>
<script language="javascript" src="Calc.js"></script>
</head>
<body>
<script language="javascript">
  function testAbnormalpytha(){
      var max =999999999999999999......(ものスゴく巨大な数字)
      assertEquals('10:x = positiveInfinity y = 5','InfinityError',pythagoras(max,5));
      assertEquals('11:x = 49 y =  negativeInfinity','InfinityError',pythagoras(49,-(max)))
      assertEquals('12:NotNumber','NotNumber',pythagoras('r',9));
      assertEquals('13:Empty','ArgumentEmptyError',pythagoras('',5));
      assertEquals('14:NotNumber','NotNumber',pythagoras('0.00.3',5));
  }
  function testNormalpytha(){
      assertEquals("17:x = -2.2 y = 3",Math.sqrt(13.84),pythagoras(-2.2,3)); 
      assertEquals('18:x = 4 y = 2',Math.sqrt(20),pythagoras(4,2));
      assertEquals('19:x = 3 y = 5',Math.sqrt(34),pythagoras(3,5));
      assertEquals('20:x = 10 y = 9',Math.sqrt(181),pythagoras(10,9));
      assertEquals('21:x = 2 y = -6',Math.sqrt(40),pythagoras(2,-6));
  }
</script>
</body>
</html>
[edit]

setUp() tearDown()

[edit]

CalcClass.js

Calc = function(){
}
Calc.prototype ={
    setSubmitEvent: function(){
        var formObj = document.getElementById('form1');
        Event.observe(formObj,'submit',function(){
            var divObj = document.getElementById('div');
            var x = document.getElementById('x');
            var y = document.getElementById('y');
            divObj.childNodes[1].childNodes[0].nodeValue = calc.division(x.value,y.value);
            divObj.childNodes[4].childNodes[0].nodeValue = calc.pythagoras(x.value,y.value);
            return false;
        });
    },
    division: function(x,y){
        try{
            if(!this.isEmpty(x,y) && this.isNumber(x,y) && !this.isZero(x,y) && this.isInfinity(x,y)){
                return (x/y);
            }
        }catch(e){
            return e;
        }    
    },
    pythagoras: function(x,y){
        try{
            if(!this.isEmpty(x,y) && this.isNumber(x,y) && this.isInfinity(x,y)){
                var c = (x * x) + (y * y);
                return Math.sqrt(c);
            }
        }catch(e){
            return e;
        }
    },
    isNumber: function(x,y){
        if (isNaN(x) || isNaN(y)){
            throw 'NotNumber';
        }else{
            return true;
        }
    },
    isEmpty: function(x,y){
        if((x == '' || y == '')){
            throw 'ArgumentEmptyError';
        }else{
            return false;
        }
    },
    isZero: function(x,y){
        if(x == 0 || y == 0){
            throw 'ZeroDivisionError';
        }else{
            return false;
        }
    },
    isInfinity: function(x,y){
        if(isFinite(x) && isFinite(y)){
            return true;
        }else{
            throw 'InfinityError';
        }    
    }
}
[edit]

テストケース

<html>
<head>
<meta content="text/html; charset=UTF-8">
</head>
<body>
<script language="JavaScript" src="../../jsunit/jsunit/app/jsUnitCore.js"></script>
<script language="JavaScript" src="./prototype.js"></script>
<script language="JavaScript" src="./CalcClass.js"></script>
<script language="JavaScript" type="text/javascript">
    function setUp(){
        calc = new Calc();
        max =99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999     
    }
    function tearDown(){
        alert("ok");
    }
function testAbnormalDiv(){
        assertEquals('18:x = positiveInfinity y = 5','InfinityError',calc.division(max,5));
        assertEquals('19:x = 49 y = negativeInfinity','InfinityError',calc.division(49,-(max)))
        assertEquals('20:NotNumber','NotNumber',calc.division('r',9));
        assertEquals('21:Empty','ArgumentEmptyError',calc.division('',5));
        assertEquals('22:NotNumber','NotNumber',calc.division('0.00.3',5));
        assertEquals('23:ZeroErro','ZeroDivisionError',calc.division('0',45));
        assertEquals('24:ZeroErro','ZeroDivisionError',calc.division(23,'0'));
    }
    function testNormalDiv(){ 
        assertEquals('27:x = 40 y = 5',8,calc.division(40,5));
        assertEquals('28:x = 8.4 y = 4',2.1,calc.division(8.4,4));
        assertEquals('29:x = -50 y = 5',-10,calc.division(-50,5)); 
        assertEquals('30:x = 0.3 y = 0.3',1,calc.division(0.3,0.3));
    }
    function testAbnormalpytha(){    
        assertEquals('33:x = 49 y = negativeInfinity','InfinityError',calc.pythagoras(49,-(max)))
        assertEquals('34:NotNumber','NotNumber',calc.pythagoras('r',9));
        assertEquals('35:Empty','ArgumentEmptyError',calc.pythagoras('',5));
        assertEquals('36:NotNumber','NotNumber',calc.pythagoras('0.00.3',5));
    }   
    function testNormalpytha(){
        assertEquals("39:x = -2.2 y = 3",Math.sqrt(13.84),calc.pythagoras(-2.2,3)); 
        assertEquals('40:x = 4 y = 2',Math.sqrt(20),calc.pythagoras(4,2));
        assertEquals('41:x = 3 y = 5',Math.sqrt(34),calc.pythagoras(3,5));
        assertEquals('42:x = 10 y = 9',Math.sqrt(181),calc.pythagoras(10,9));
        assertEquals('43:x = 2 y = -6',Math.sqrt(40),calc.pythagoras(2,-6));
    }
</script>
</body>
</html>

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2007-10-09 (火) 14:41:59 (1686d)

アークウェブのサービスやソリューションはこちら