**Ìܼ¡ [#z93ea414]
#contents
** Jsunit [#f47075b4]
|¥«¥Æ¥´¥ê¡¼|Jsunit|
|Í¥Àè½ç°Ì|»êµÞ|
|¥¤¥Æ¥ì¡¼¥·¥ç¥ó|[[¥¤¥Æ¥ì¡¼¥·¥ç¥ó1]]|
|¾õÂÖ|´°Î»|
|´°Î»Í½ÄêÆü|2007/10/5|
|¹©¿ô||
|Âбþ¼Ô|À¶¸¶|
----
**»È¤¤Êý [#b783e9c7]
¥Æ¥¹¥È¥±¡¼¥¹¤ÎºîÀ®
jsUnitCore.js¤ò¥í¡¼¥É¤¹¤ë
-¡Ötest¡×¤Ç»Ï¤Þ¤ë¥á¥½¥Ã¥É¤òºîÀ®¤·¥Æ¥¹¥ÈÆâÍƤòµ½Ò¤¹¤ë
¥Æ¥¹¥ÈÆâÍƤǤϰʲ¼¤Îassertion´Ø¿ô¤¬»ÈÍѤǤ¤ë
|´Ø¿ô|ÀâÌÀ|
|assert([comment], booleanValue)|booleanValue¤¬True¤«|
|assertTrue([comment], booleanValue)|assert¤ÈƱ°Õ|
|assertFalse([comment], booleanValue)|booleanValue¤¬False¤«|
|assertEquals([comment], value1, value2)|value2==value1¤«|
|assertNotEquals([comment], value1, value2)|value2¡ª=value1¤«|
|assertNull([comment], value)|value==Null¤«|
|assertNotNull([comment], value)|value!=Null¤«|
|assertUndefined([comment], value)|value==Undefined¤«|
|assertNotUndefined([comment], value)|value!=Undefined¤«|
|assertNaN([comment], value)|value==NaN¤«|
|assertNotNaN([comment], value)|value!=NaN¤«|
|fail(comment)|ɬ¤º¥¨¥é¡¼¤Ë¤¹¤ë|
-¥Æ¥¹¥È¥±¡¼¥¹¤Î¼Â¹Ô
testRunner.html¤ò¥Ö¥é¥¦¥¶¤Çµ¯Æ°¤·¡¢¥Æ¥¹¥È¥±¡¼¥¹¤òµ½Ò¤·¤¿¥Õ¥¡¥¤¥ë¤òÁªÂò¤·Run¤Ç¼Â¹Ô
-¥Æ¥¹¥È¥±¡¼¥¹¥Õ¥¡¥¤¥ë¤¬Ê£¿ô¤¢¤ê¡¢°ìÅ٤Ǽ¹Ԥ¹¤ë¾ì¹ç
TestSuite¤òºîÀ®
suite()¥á¥½¥Ã¥É¤òºîÀ®¤·¡¢¥á¥½¥Ã¥ÉÆâ¤ÇjsUnitTestSuite¥¯¥é¥¹¤òÀ¸À®¤·¡¢³Æ¥Æ¥¹¥È¥±¡¼¥¹¥Õ¥¡¥¤¥ë¤òÄɲ乤ë
**ºîÀ®¤·¤¿¥Æ¥¹¥È¥±¡¼¥¹ [#v21c768b]
º£²ó¤Ï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>
-test-suite.html
<html>
<head>
<script language="javascript" src="../../jsunit/jsunit/app/jsUnitCore.js"></script>
<script language="javascript" type="text/javascript">
function suite(){
var newsuite = new top.jsUnitTestSuite();
newsuite.addTestPage("../../training/js/test-div.html");
newsuite.addTestPage("../../training/js/test-pytha.html");
return newsuite;
}
</script>
</head>
<body>
</body>
</html>
**setUp() tearDown() [#mb0aeea9]
-setUp()
ÍÑ°Õ¤¹¤ëJsUnit¥Æ¥¹¥È¥á¥½¥Ã¥É¤¬¼Â¹Ô¤µ¤ì¤ëÁ°¤ËËè²ó¸Æ¤Ó½Ð¤µ¤ì¤ë¡£
¥á¥½¥Ã¥Éñ°Ì¤Î½é´ü ²½½èÍýÅù¤òµ½Ò¡£
-tearDown()
ÍÑ°Õ¤¹¤ëJsUnit¥Æ¥¹¥È¥á¥½¥Ã¥É¤¬¼Â¹Ô¤µ¤ì¤¿¸å¤ËËè²ó¸Æ¤Ó½Ð¤µ¤ì¤ë¡£
¥á¥½¥Ã¥Éñ°Ì¤Î½ªÎ»½èÍýÅù¤òµ½Ò¡£
**CalcClass.js [#b56e73da]
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';
}
}
}
**¥Æ¥¹¥È¥±¡¼¥¹ [#z32fa059]
<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));
}
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>