Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

TestCase.cpp

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // Lamp : Open source game middleware
00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //------------------------------------------------------------------------------
00019 
00020 /** @file
00021  * テストケース実装
00022  * @author Junpee
00023  */
00024 
00025 #include <cstdio>
00026 #include <exception>
00027 #include <string>
00028 #include <LampBasic.h>
00029 #include "LampUnit/TestCase.h"
00030 #include "LampUnit/TestResult.h"
00031 #include "LampUnit/TestFailure.h"
00032 
00033 namespace LampUnit{
00034 
00035 //------------------------------------------------------------------------------
00036 // コンストラクタ
00037 TestCase::TestCase(Lamp::String name) : name_(name), result_(NULL){
00038 }
00039 //------------------------------------------------------------------------------
00040 // コンストラクタ
00041 TestCase::TestCase() : name_(""), result_(NULL){
00042 }
00043 //------------------------------------------------------------------------------
00044 // テストを実行する
00045 void TestCase::run(TestResult* result){
00046     result_ = result;
00047     result_->startTest(this);
00048 
00049     try{
00050         // テストのセットアップ
00051         setUp();
00052 
00053         // テストの実行
00054         try{
00055             runTest();
00056         }catch(std::exception& exception){
00057             TestFail(exception.what());
00058         }catch(...){
00059             TestFail("runTest() catchされていない例外が発生しました。");
00060         }
00061 
00062         // テストの後始末
00063         try{
00064             tearDown();
00065         }catch (...){
00066             TestFail("tearDown() catchされていない例外が発生しました。");
00067         }
00068 
00069     }catch(...){
00070         TestFail("setUp() catchされていない例外が発生しました。");
00071     }
00072 
00073     result_->endTest(this);
00074     result_ = NULL;
00075 }
00076 //------------------------------------------------------------------------------
00077 // 失敗の追加
00078 void TestCase::addFailure(
00079     const Lamp::String& message, const char* file, int line){
00080     TestFailure failure(this, message, file, line);
00081     result_->addFailure(failure);
00082 }
00083 //------------------------------------------------------------------------------
00084 // 文字列の比較
00085 bool TestCase::equals(const char* leftValue, const char* rightValue){
00086     if(leftValue == rightValue){ return true; }
00087     if((leftValue == NULL) || (rightValue == NULL)){ return false; }
00088     return (std::strcmp(leftValue, rightValue) == 0);
00089 }
00090 //------------------------------------------------------------------------------
00091 // 値が同じでないメッセージ
00092 Lamp::String TestCase::notEqualMessage(int expected, int actual){
00093     Lamp::String result;
00094     result.format(
00095         "期待される値は %d ですが実際の値は %d でした。",
00096         expected, actual);
00097     return result;
00098 }
00099 //------------------------------------------------------------------------------
00100 // 値が同じでないメッセージ
00101 Lamp::String TestCase::notEqualMessage(
00102     u_int expected, u_int actual){
00103     Lamp::String result;
00104     result.format(
00105         "期待される値は %u ですが実際の値は %u でした。",
00106         expected, actual);
00107     return result;
00108 }
00109 //------------------------------------------------------------------------------
00110 // 値が同じでないメッセージ
00111 Lamp::String TestCase::notEqualMessage(
00112     float expected, float actual, float epsilon){
00113     Lamp::String result;
00114     result.format(
00115         "期待される値は %.8f(0x%x) で、許容誤差は%.8fですが"
00116         "実際の値は %.8f(0x%x) でした。",
00117         expected, *(u_int*)&expected, epsilon, actual, *(u_int*)&actual);
00118     return result;
00119 }
00120 //------------------------------------------------------------------------------
00121 // 値が同じでないメッセージ
00122 Lamp::String TestCase::notEqualMessage(
00123     double expected, double actual, double epsilon){
00124     Lamp::String result;
00125     result.format(
00126         "期待される値は %.16f(0x%I64x) で、許容誤差は%.16fですが"
00127         "実際の値は %.16f(0x%I64x) でした。",
00128         expected, *(long long*)&expected, epsilon, actual, *(long long*)&actual);
00129     return result;
00130 }
00131 //------------------------------------------------------------------------------
00132 // 文字列が同じでないメッセージ
00133 Lamp::String TestCase::notEqualMessage(const char* expected, const char* actual){
00134     Lamp::String result("期待される文字列は ");
00135     if(expected == NULL){
00136         result.append("<NULL>");
00137     }else{
00138         result.append("\"").append(expected).append("\"");
00139     }
00140     result.append(" ですが実際の文字列は ");
00141     if(actual == NULL){
00142         result.append("<NULL>");
00143     }else{
00144         result.append("\"").append(actual).append("\"");
00145     }
00146     result.append(" でした。");
00147     return result;
00148 }
00149 //------------------------------------------------------------------------------
00150 // 値が同じメッセージ
00151 Lamp::String TestCase::equalMessage(int notExpected, int actual){
00152     Lamp::String result;
00153     result.format(
00154         "期待さない値は %d ですが実際の値は %d でした。",
00155         notExpected, actual);
00156     return result;
00157 }
00158 //------------------------------------------------------------------------------
00159 // 値が同じメッセージ
00160 Lamp::String TestCase::equalMessage(
00161     u_int notExpected, u_int actual){
00162     Lamp::String result;
00163     result.format(
00164         "期待さない値は %u ですが実際の値は %u でした。",
00165         notExpected, actual);
00166     return result;
00167 }
00168 //------------------------------------------------------------------------------
00169 // 値が同じメッセージ
00170 Lamp::String TestCase::equalMessage(
00171     float notExpected, float actual, float epsilon){
00172     Lamp::String result;
00173     result.format(
00174         "期待されない値は %.8f(0x%x) で、許容誤差は%.8fですが"
00175         "実際の値は %.8f(0x%x) でした。",
00176         notExpected, *(u_int*)&notExpected, epsilon, actual, *(u_int*)&actual);
00177     return result;
00178 }
00179 //------------------------------------------------------------------------------
00180 // 値が同じメッセージ
00181 Lamp::String TestCase::equalMessage(
00182     double notExpected, double actual, double epsilon){
00183     Lamp::String result;
00184     result.format(
00185         "期待されない値は %.16f(0x%I64x) で、許容誤差は%.16fですが"
00186         "実際の値は %.16f(0x%I64x) でした。",
00187         notExpected, *(long long*)&notExpected, epsilon,
00188         actual, *(long long*)&actual);
00189     return result;
00190 }
00191 //------------------------------------------------------------------------------
00192 // 文字列が同じメッセージ
00193 Lamp::String TestCase::equalMessage(const char* notExpected, const char* actual){
00194     Lamp::String result("期待されない文字列は ");
00195     if(notExpected == NULL){
00196         result.append("<NULL>");
00197     }else{
00198         result.append("\"").append(notExpected).append("\"");
00199     }
00200     result.append(" ですが実際の文字列は ");
00201     if(actual == NULL){
00202         result.append("<NULL>");
00203     }else{
00204         result.append("\"").append(actual).append("\"");
00205     }
00206     result.append(" でした。");
00207     return result;
00208 }
00209 //------------------------------------------------------------------------------
00210 } // End of namespace LampUnit
00211 //------------------------------------------------------------------------------

Generated on Wed Mar 16 10:29:54 2005 for LampUnit by doxygen 1.3.2