begin process at 2010 02 10 15:53:25
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Divers

 > GÉNÉRATEUR DE UNIT TEST

GÉNÉRATEUR DE UNIT TEST


 Information sur la source

Note :
Aucune note
Catégorie :Divers Classé sous :unit, test, débug Niveau :Expert Date de création :25/03/2008 Date de mise à jour :25/03/2008 17:12:45 Vu / téléchargé :2 803 / 51

Auteur : le_duche

Ecrire un message privé
Commentaire sur cette source (2)
Ajouter un commentaire et/ou une note

 Description

Inspiré des unit test que fait eclipse en java.

Cette source vous permettra de générer des fichiers unittest.h et unittest.cpp à joindre à tous vos projets pour tester vos classes. Elle permet aussi de générer des fichiers de unit test préformatés pour chacune de vos classes. Il ne reste alors qu'à écrire les tests à l'aide des fonction assertTrue(bool), assertFalse(bool) ou assertEquals(const T&,const T&).
Il suffit alors de runner le test qui s'occupe de l'affichage.

Un exemple complet est fournit dans le zip.

Pour des questions ou des notifications de bug n'hésitez pas à me mailer !

Bon amusement ! :-)

Source

  • /*
  • * UnitTestGenerator : main.cpp
  • *
  • * Created by Christophe Dumeunier on 25/03/08.
  • * Copyright 2008 Christophe Dumeunier. All rights reserved.
  • * This application is free; you can redistribute it and/or modify
  • * it under the terms of the GNU General Public License as published by
  • * the Free Software Foundation; either version 2 of the License, or
  • * (at your option) any later version.
  • *
  • * Contact: legrandduche@gmail.com (in french if possible)
  • *
  • */
  • #include <iostream>
  • #include <fstream>
  • #include <list>
  • using namespace std;
  • void createUnitTestLib()
  • {
  • ofstream f1("unittest.h");
  • f1 << "/*" << endl
  • << " * unittest.h" << endl
  • << " * " << endl
  • << " * Created by Christophe Dumeunier on 25/03/08." << endl
  • << " * Generated by UnitTestGenerator." << endl
  • << " * Copyright 2008 Christophe Dumeunier. All rights reserved." << endl
  • << " * This file is free; you can redistribute it and/or modify" << endl
  • << " * it under the terms of the GNU General Public License as published by" << endl
  • << " * the Free Software Foundation; either version 2 of the License, or" << endl
  • << " * (at your option) any later version." << endl
  • << " * " << endl
  • << " * Contact: legrandduche@gmail.com (in french if possible)" << endl
  • << " * " << endl
  • << " */" << endl
  • << endl
  • << "#ifndef UNITTEST_H" << endl
  • << "#define UNITTEST_H" << endl
  • << endl
  • << "#include <iostream>" << endl
  • << "#include <string.h>" << endl
  • << "#include <list>" << endl
  • << "using namespace std;" << endl
  • << endl
  • << "class UnitTest" << endl
  • << "{" << endl
  • << " private:" << endl
  • << " int nbr_valid;" << endl
  • << " int nbr_unvalid;" << endl
  • << " list<int> failure;" << endl
  • << " " << endl
  • << " public:" << endl
  • << " UnitTest();" << endl
  • << " ~UnitTest();" << endl
  • << " " << endl
  • << " void fail();" << endl
  • << " void success();" << endl
  • << " " << endl
  • << " void assertTrue(bool);" << endl
  • << " void assertFalse(bool);" << endl
  • << " template <class T> void assertEquals(const T& value,const T& test)" << endl
  • << " {" << endl
  • << " if(value==test)" << endl
  • << " success();" << endl
  • << " else" << endl
  • << " fail();" << endl
  • << " }" << endl
  • << " template <class T> void assertNotEquals(const T& value,const T& test)" << endl
  • << " {" << endl
  • << " if(!(value == test))" << endl
  • << " success();" << endl
  • << " else" << endl
  • << " fail();" << endl
  • << " }" << endl
  • << " void assertNull(void*);" << endl
  • << " void assertNotNull(void*);" << endl
  • << " void assertSame(void*,void*);" << endl
  • << " void assertNotSame(void*,void*);" << endl
  • << " " << endl
  • << " bool valid() const;" << endl
  • << " void print(const string&);" << endl
  • << " void reinit();" << endl
  • << "};" << endl
  • << endl
  • << "#endif" << endl;
  • f1.close();
  • ofstream f2("unittest.cpp");
  • f2 << "/*" << endl
  • << " * unittest.cpp" << endl
  • << " * " << endl
  • << " * Created by Christophe Dumeunier on 25/03/08." << endl
  • << " * Generated by UnitTestGenerator." << endl
  • << " * Copyright 2008 Christophe Dumeunier. All rights reserved." << endl
  • << " * This file is free; you can redistribute it and/or modify" << endl
  • << " * it under the terms of the GNU General Public License as published by" << endl
  • << " * the Free Software Foundation; either version 2 of the License, or" << endl
  • << " * (at your option) any later version." << endl
  • << " * " << endl
  • << " * Contact: legrandduche@gmail.com (in french if possible)" << endl
  • << " * " << endl
  • << " */" << endl
  • << endl
  • << "#include \"unittest.h\"" << endl
  • << endl
  • << "UnitTest::UnitTest()" << endl
  • << "{" << endl
  • << " nbr_valid = 0;" << endl
  • << " nbr_unvalid = 0;" << endl
  • << "}" << endl
  • << endl
  • << "UnitTest::~UnitTest()" << endl
  • << "{" << endl
  • << "}" << endl
  • << endl
  • << "void UnitTest::fail()" << endl
  • << "{" << endl
  • << " nbr_unvalid++;" << endl
  • << " failure.push_back(nbr_valid + nbr_unvalid);" << endl
  • << "}" << endl
  • << endl
  • << "void UnitTest::success()" << endl
  • << "{" << endl
  • << " nbr_valid++;" << endl
  • << "}" << endl
  • << endl
  • << "void UnitTest::assertTrue(bool test)" << endl
  • << "{" << endl
  • << " if(test)" << endl
  • << " success();" << endl
  • << " else" << endl
  • << " fail();" << endl
  • << "}" << endl
  • << endl
  • << "void UnitTest::assertFalse(bool test)" << endl
  • << "{" << endl
  • << " if(!test)" << endl
  • << " success();" << endl
  • << " else" << endl
  • << " fail();" << endl
  • << "}" << endl
  • << endl
  • << "void UnitTest::assertNull(void* ptr)" << endl
  • << "{" << endl
  • << " if(ptr == NULL)" << endl
  • << " success();" << endl
  • << " else" << endl
  • << " fail();" << endl
  • << "}" << endl
  • << endl
  • << "void UnitTest::assertNotNull(void* ptr)" << endl
  • << "{" << endl
  • << " if(!(ptr == NULL))" << endl
  • << " success();" << endl
  • << " else" << endl
  • << " fail();" << endl
  • << "}" << endl
  • << endl
  • << "void UnitTest::assertSame(void* ptr1,void* ptr2)" << endl
  • << "{" << endl
  • << " if(ptr1 == ptr2)" << endl
  • << " success();" << endl
  • << " else" << endl
  • << " fail();" << endl
  • << "}" << endl
  • << endl
  • << "void UnitTest::assertNotSame(void* ptr1,void* ptr2)" << endl
  • << "{" << endl
  • << " if(!(ptr1 == ptr2))" << endl
  • << " success();" << endl
  • << " else" << endl
  • << " fail();" << endl
  • << "}" << endl
  • << endl
  • << "bool UnitTest::valid() const" << endl
  • << "{" << endl
  • << " if(nbr_unvalid == 0)" << endl
  • << " return true;" << endl
  • << " else" << endl
  • << " return false;" << endl
  • << "}" << endl
  • << endl
  • << "void UnitTest::print(const string& s)" << endl
  • << "{" << endl
  • << " cout << \" Valid Test for \" << s << \" : [\" << nbr_valid << \"/\" << nbr_valid + nbr_unvalid << \"].\";" << endl
  • << " if(nbr_unvalid == 0)" << endl
  • << " cout << \" Test is ok.\" << endl;" << endl
  • << " else" << endl
  • << " {" << endl
  • << " cout << \" TEST IS WRONG !!!\" << endl" << endl
  • << " << \" Failure(s) on test(s) \";" << endl
  • << " list<int>::iterator it;" << endl
  • << " for(it=failure.begin();it!=failure.end();it++)" << endl
  • << " cout << \" \" << (*it);" << endl
  • << " cout << \".\" << endl;" << endl
  • << " }" << endl
  • << "}" << endl
  • << endl
  • << "void UnitTest::reinit()" << endl
  • << "{" << endl
  • << " nbr_valid = 0;" << endl
  • << " nbr_unvalid = 0;" << endl
  • << " failure.clear();" << endl
  • << "}" << endl;
  • f2.close();
  • }
  • void createNewUnitTest(const string& file_name,const string& class_name,list<string>& function_list)
  • {
  • string min_class_name = class_name;
  • if((min_class_name[0] >= 'A') && (min_class_name[0] <= 'Z'))
  • min_class_name[0] += (int)'a' - (int)'A';
  • string all_maj_class_name = class_name;
  • for(int i=0;i<all_maj_class_name.length();i++)
  • {
  • if((all_maj_class_name[i] >= 'a') && (all_maj_class_name[i] <= 'z'))
  • all_maj_class_name[i] -= (int)'a' - (int)'A';
  • }
  • string unittest_hfile_name = min_class_name + ".unittest.h";
  • string unittest_cppfile_name = min_class_name + ".unittest.cpp";
  • ofstream f1(unittest_hfile_name.c_str());
  • f1 << "/*" << endl
  • << " * " << unittest_hfile_name << endl
  • << " * " << endl
  • << " * Generated by UnitTestGenerator." << endl
  • << " * Copyright 2008 Christophe Dumeunier. All rights reserved." << endl
  • << " * This file is free; you can redistribute it and/or modify" << endl
  • << " * it under the terms of the GNU General Public License as published by" << endl
  • << " * the Free Software Foundation; either version 2 of the License, or" << endl
  • << " * (at your option) any later version." << endl
  • << " * " << endl
  • << " * Contact: legrandduche@gmail.com (in french if possible)" << endl
  • << " * " << endl
  • << " */" << endl
  • << endl
  • << "#ifndef " << all_maj_class_name << "_UNITTEST_H" << endl
  • << "#define " << all_maj_class_name << "_UNITTEST_H" << endl
  • << endl
  • << "#include \"unittest.h\"" << endl
  • << "#include \"" << file_name << "\"" << endl
  • << endl
  • << "class " << class_name << "UnitTest : private UnitTest" << endl
  • << "{" << endl
  • << " private:" << endl
  • << " //add your variables" << endl
  • << " " << endl
  • << " public:" << endl
  • << " " << class_name << "UnitTest();" << endl
  • << " ~" << class_name << "UnitTest();" << endl
  • << " " << endl
  • << " bool runTest();" << endl
  • << " " << endl
  • << " private:" << endl
  • << " void setUp();" << endl
  • << " void tearDown();" << endl
  • << " " << endl;
  • list<string>::iterator it;
  • for(it=function_list.begin();it!=function_list.end();it++)
  • {
  • string name = (*it);
  • int left_parenth = name.find("(");
  • int length = name.length();
  • name.erase(left_parenth,length-left_parenth);
  • f1 << " void " << name << "_";
  • string params = (*it);
  • params.erase(length-1,1);
  • params.erase(0,left_parenth+1);
  • params += ",";
  • if(params == ",")
  • params = "void,";
  • while(params.length() > 0)
  • {
  • int comma = params.find(",");
  • int length_param = params.length();
  • string param = params;
  • param.erase(comma,length_param-comma);
  • if((param[0] >= 'a') && (param[0] <= 'z'))
  • param[0] -= (int)'a' - (int)'A';
  • while(param.find("*") != string::npos)
  • {
  • int paramFindStar = param.find("*");
  • param.replace(paramFindStar,1,"Ptr");
  • }
  • while(param.find("&") != string::npos)
  • {
  • int paramFindAnd = param.find("&");
  • param.replace(paramFindAnd,1,"Ref");
  • }
  • f1 << param;
  • params.erase(0,comma+1);
  • }
  • f1 << "();" << endl;
  • }
  • f1 << "};" << endl
  • << endl
  • << "typedef void (" << class_name << "UnitTest::*" << class_name << "UnitTestMemberFunctionPointer)(void);" << endl
  • << endl
  • << "#endif" << endl;
  • f1.close();
  • ofstream f2(unittest_cppfile_name.c_str());
  • f2 << "/*" << endl
  • << " * " << unittest_cppfile_name << endl
  • << " * " << endl
  • << " * Generated by UnitTestGenerator." << endl
  • << " * Copyright 2008 Christophe Dumeunier. All rights reserved." << endl
  • << " * This file is free; you can redistribute it and/or modify" << endl
  • << " * it under the terms of the GNU General Public License as published by" << endl
  • << " * the Free Software Foundation; either version 2 of the License, or" << endl
  • << " * (at your option) any later version." << endl
  • << " * " << endl
  • << " * Contact: legrandduche@gmail.com (in french if possible)" << endl
  • << " * " << endl
  • << " */" << endl
  • << endl
  • << "#include \"" << unittest_hfile_name << "\"" << endl
  • << endl
  • << class_name << "UnitTest::" << class_name << "UnitTest()" << endl
  • << "{" << endl
  • << " //construct your variables" << endl
  • << "}" << endl
  • << endl
  • << class_name << "UnitTest::~" << class_name << "UnitTest()" << endl
  • << "{" << endl
  • << " //destruct your variables" << endl
  • << "}" << endl
  • << endl
  • << "bool " << class_name << "UnitTest::runTest()" << endl
  • << "{" << endl
  • << " const int run_vector_size = " << (int)function_list.size() << ";" << endl
  • << " " << class_name << "UnitTestMemberFunctionPointer run_vector[run_vector_size] =" << endl
  • << " {" << endl;
  • for(it=function_list.begin();it!=function_list.end();it++)
  • {
  • string name = (*it);
  • int left_parenth = name.find("(");
  • int length = name.length();
  • name.erase(left_parenth,length-left_parenth);
  • if(it != function_list.begin())
  • f2 << "," << endl;
  • f2 << " &" << class_name << "UnitTest::" << name << "_";
  • string params = (*it);
  • params.erase(length-1,1);
  • params.erase(0,left_parenth+1);
  • params += ",";
  • if(params == ",")
  • params = "void,";
  • while(params.length() > 0)
  • {
  • int comma = params.find(",");
  • int length_param = params.length();
  • string param = params;
  • param.erase(comma,length_param-comma);
  • if((param[0] >= 'a') && (param[0] <= 'z'))
  • param[0] -= (int)'a' - (int)'A';
  • while(param.find("*") != string::npos)
  • {
  • int paramFindStar = param.find("*");
  • param.replace(paramFindStar,1,"Ptr");
  • }
  • while(param.find("&") != string::npos)
  • {
  • int paramFindAnd = param.find("&");
  • param.replace(paramFindAnd,1,"Ref");
  • }
  • f2 << param;
  • params.erase(0,comma+1);
  • }
  • }
  • f2 << endl
  • << " };" << endl
  • << " string string_run_vector[run_vector_size] =" << endl
  • << " {" << endl;
  • for(it=function_list.begin();it!=function_list.end();it++)
  • {
  • if(it != function_list.begin())
  • f2 << "," << endl;
  • f2 << " \"" << class_name << "::" << (*it) << "\"";
  • }
  • f2 << endl
  • << " };" << endl
  • << " bool validTest = true;" << endl
  • << " cout << \" ------------------------------------------------------------------------------\" << endl" << endl
  • << " << \" Unit Test of class " << class_name << " -- by Christophe Dumeunier\" << endl" << endl
  • << " << \" ------------------------------------------------------------------------------\" << endl;" << endl
  • << " for(int i=0;i<run_vector_size;i++)" << endl
  • << " {" << endl
  • << " setUp();" << endl
  • << " (this->*run_vector[i])();" << endl
  • << " tearDown();" << endl
  • << " validTest = validTest && valid();" << endl
  • << " print(string_run_vector[i]);" << endl
  • << " reinit();" << endl
  • << " }" << endl
  • << " cout << \" ------------------------------------------------------------------------------\" << endl;" << endl
  • << " return validTest;" << endl
  • << "}" << endl
  • << endl
  • << "void " << class_name << "UnitTest::setUp()" << endl
  • << "{" << endl
  • << " //prepare each test" << endl
  • << "}" << endl
  • << endl
  • << "void " << class_name << "UnitTest::tearDown()" << endl
  • << "{" << endl
  • << " //terminate each test" << endl
  • << "}" << endl;
  • for(it=function_list.begin();it!=function_list.end();it++)
  • {
  • string name = (*it);
  • int left_parenth = name.find("(");
  • int length = name.length();
  • name.erase(left_parenth,length-left_parenth);
  • f2 << endl
  • << "void " << class_name << "UnitTest::" << name << "_";
  • string params = (*it);
  • params.erase(length-1,1);
  • params.erase(0,left_parenth+1);
  • params += ",";
  • if(params == ",")
  • params = "void,";
  • while(params.length() > 0)
  • {
  • int comma = params.find(",");
  • int length_param = params.length();
  • string param = params;
  • param.erase(comma,length_param-comma);
  • if((param[0] >= 'a') && (param[0] <= 'z'))
  • param[0] -= (int)'a' - (int)'A';
  • while(param.find("*") != string::npos)
  • {
  • int paramFindStar = param.find("*");
  • param.replace(paramFindStar,1,"Ptr");
  • }
  • while(param.find("&") != string::npos)
  • {
  • int paramFindAnd = param.find("&");
  • param.replace(paramFindAnd,1,"Ref");
  • }
  • f2 << param;
  • params.erase(0,comma+1);
  • }
  • f2 << "()" << endl
  • << "{" << endl
  • << " //make your tests" << endl
  • << "}" << endl;
  • }
  • f2.close();
  • }
  • int main(int sys_nbr_args,char** sys_args)
  • {
  • cout << endl
  • << endl
  • << " ------------------------------------------------------------------------------" << endl
  • << " Welcome on the UnitTest Generator by Christophe Dumeunier." << endl
  • << " Copyright 2008 Christophe Dumeunier. All rights reserved." << endl
  • << " This application is a free software; you can redistribute it and/or modify" << endl
  • << " it under the terms of the GNU General Public License as published by" << endl
  • << " the Free Software Foundation; either version 2 of the License, or" << endl
  • << " (at your option) any later version." << endl
  • << " ------------------------------------------------------------------------------" << endl
  • << endl
  • << endl;
  • int user;
  • bool finish = false;
  • while(!finish)
  • {
  • cout << endl
  • << " 1. Create unittest.h and unittest.cpp files" << endl
  • << " 2. Create a new UnitTest on a class" << endl
  • << " 3. Quit" << endl
  • << endl;
  • cin >> user;
  • if(user == 1)
  • {
  • createUnitTestLib();
  • cout << "done." << endl;
  • }
  • else if(user == 2)
  • {
  • string file_name;
  • string class_name;
  • list<string> function_list;
  • cout << " Enter the file name (containing the class, for example: example.h)" << endl;
  • cin >> file_name;
  • cout << " Enter the class name (for example : Example)" << endl;
  • cin >> class_name;
  • bool finish2 = false;
  • while(!finish2)
  • {
  • string function_name = "";
  • cout << " Add a member function of class " << class_name << " (for example : int myFunction(char,int))" << endl
  • << " -- \"()\" to terminate --" << endl;
  • bool line_readed = false;
  • while(!line_readed)
  • {
  • string temporary_str;
  • cin >> temporary_str;
  • function_name += temporary_str;
  • if(function_name[function_name.length()-1] == ')')
  • line_readed = true;
  • else
  • function_name += ",";
  • }
  • if(function_name == "()")
  • finish2 = true;
  • else
  • function_list.push_back(function_name);
  • }
  • createNewUnitTest(file_name,class_name,function_list);
  • cout << "done." << endl;
  • }
  • else if(user == 3)
  • {
  • finish = true;
  • cout << "Bye !" << endl;
  • }
  • else
  • {
  • cout << "Try again..." << endl;
  • }
  • }
  • cout << endl
  • << endl;
  • return 0;
  • }
/*
 *  UnitTestGenerator : main.cpp
 *  
 *  Created by Christophe Dumeunier on 25/03/08.
 *  Copyright 2008 Christophe Dumeunier. All rights reserved.
 *  This application is free; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *  
 *  Contact: legrandduche@gmail.com (in french if possible)
 *
 */

#include <iostream>
#include <fstream>
#include <list>
using namespace std;

void createUnitTestLib()
{
	ofstream f1("unittest.h");
	f1 << "/*" << endl
		 << " *  unittest.h" << endl
		 << " *  " << endl
		 << " *  Created by Christophe Dumeunier on 25/03/08." << endl
		 << " *  Generated by UnitTestGenerator." << endl
		 << " *  Copyright 2008 Christophe Dumeunier. All rights reserved." << endl
		 << " *  This file is free; you can redistribute it and/or modify" << endl
		 << " *  it under the terms of the GNU General Public License as published by" << endl
		 << " *  the Free Software Foundation; either version 2 of the License, or" << endl
		 << " *  (at your option) any later version." << endl
		 << " *  " << endl
		 << " *  Contact: legrandduche@gmail.com (in french if possible)" << endl
		 << " *  " << endl
		 << " */" << endl
		 << endl
		 << "#ifndef UNITTEST_H" << endl
		 << "#define UNITTEST_H" << endl
		 << endl
		 << "#include <iostream>" << endl
		 << "#include <string.h>" << endl
		 << "#include <list>" << endl
		 << "using namespace std;" << endl
		 << endl
		 << "class UnitTest" << endl
		 << "{" << endl
		 << "  private:" << endl
		 << "    int nbr_valid;" << endl
		 << "    int nbr_unvalid;" << endl
		 << "    list<int> failure;" << endl
		 << "    " << endl
		 << "  public:" << endl
		 << "    UnitTest();" << endl
		 << "    ~UnitTest();" << endl
		 << "    " << endl
		 << "    void fail();" << endl
		 << "    void success();" << endl
		 << "    " << endl
		 << "    void assertTrue(bool);" << endl
		 << "    void assertFalse(bool);" << endl
		 << "    template <class T> void assertEquals(const T& value,const T& test)" << endl
		 << "    {" << endl
		 << "      if(value==test)" << endl
		 << "        success();" << endl
		 << "      else" << endl
		 << "        fail();" << endl
		 << "    }" << endl
		 << "    template <class T> void assertNotEquals(const T& value,const T& test)" << endl
		 << "    {" << endl
		 << "      if(!(value == test))" << endl
		 << "        success();" << endl
		 << "      else" << endl
		 << "        fail();" << endl
		 << "    }" << endl
		 << "    void assertNull(void*);" << endl
		 << "    void assertNotNull(void*);" << endl
		 << "    void assertSame(void*,void*);" << endl
		 << "    void assertNotSame(void*,void*);" << endl
		 << "    " << endl
		 << "    bool valid() const;" << endl
		 << "    void print(const string&);" << endl
		 << "    void reinit();" << endl
		 << "};" << endl
		 << endl
		 << "#endif" << endl;
	f1.close();
	
	ofstream f2("unittest.cpp");
	f2 << "/*" << endl
		 << " *  unittest.cpp" << endl
		 << " *  " << endl
		 << " *  Created by Christophe Dumeunier on 25/03/08." << endl
		 << " *  Generated by UnitTestGenerator." << endl
		 << " *  Copyright 2008 Christophe Dumeunier. All rights reserved." << endl
		 << " *  This file is free; you can redistribute it and/or modify" << endl
		 << " *  it under the terms of the GNU General Public License as published by" << endl
		 << " *  the Free Software Foundation; either version 2 of the License, or" << endl
		 << " *  (at your option) any later version." << endl
		 << " *  " << endl
		 << " *  Contact: legrandduche@gmail.com (in french if possible)" << endl
		 << " *  " << endl
		 << " */" << endl
		 << endl
		 << "#include \"unittest.h\"" << endl
		 << endl
		 << "UnitTest::UnitTest()" << endl
		 << "{" << endl
		 << "  nbr_valid = 0;" << endl
		 << "  nbr_unvalid = 0;" << endl
		 << "}" << endl
		 << endl
		 << "UnitTest::~UnitTest()" << endl
		 << "{" << endl
		 << "}" << endl
		 << endl
		 << "void UnitTest::fail()" << endl
		 << "{" << endl
		 << "  nbr_unvalid++;" << endl
		 << "  failure.push_back(nbr_valid + nbr_unvalid);" << endl
		 << "}" << endl
		 << endl
		 << "void UnitTest::success()" << endl
		 << "{" << endl
		 << "  nbr_valid++;" << endl
		 << "}" << endl
		 << endl
		 << "void UnitTest::assertTrue(bool test)" << endl
		 << "{" << endl
		 << "  if(test)" << endl
		 << "    success();" << endl
		 << "  else" << endl
		 << "    fail();" << endl
		 << "}" << endl
		 << endl
		 << "void UnitTest::assertFalse(bool test)" << endl
		 << "{" << endl
		 << "  if(!test)" << endl
		 << "    success();" << endl
		 << "  else"  << endl
		 << "    fail();" << endl
		 << "}" << endl
		 << endl
		 << "void UnitTest::assertNull(void* ptr)" << endl
		 << "{" << endl
		 << "  if(ptr == NULL)" << endl
		 << "    success();" << endl
		 << "  else" << endl
		 << "    fail();" << endl
		 << "}" << endl
		 << endl
		 << "void UnitTest::assertNotNull(void* ptr)" << endl
		 << "{" << endl
		 << "  if(!(ptr == NULL))" << endl
		 << "    success();" << endl
		 << "  else" << endl
		 << "    fail();" << endl
		 << "}" << endl
		 << endl
		 << "void UnitTest::assertSame(void* ptr1,void* ptr2)" << endl
		 << "{" << endl
		 << "  if(ptr1 == ptr2)" << endl
		 << "    success();" << endl
		 << "  else" << endl
		 << "    fail();" << endl
		 << "}" << endl
		 << endl
		 << "void UnitTest::assertNotSame(void* ptr1,void* ptr2)" << endl
		 << "{" << endl
		 << "  if(!(ptr1 == ptr2))" << endl
		 << "    success();" << endl
		 << "  else" << endl
		 << "    fail();" << endl
		 << "}" << endl
		 << endl
		 << "bool UnitTest::valid() const" << endl
		 << "{" << endl
		 << "  if(nbr_unvalid == 0)" << endl
		 << "    return true;" << endl
		 << "  else" << endl
		 << "    return false;" << endl
		 << "}" << endl
		 << endl
		 << "void UnitTest::print(const string& s)" << endl
		 << "{" << endl
		 << "  cout << \"  Valid Test for \" << s << \" : [\" << nbr_valid << \"/\" << nbr_valid + nbr_unvalid << \"].\";" << endl
		 << "  if(nbr_unvalid == 0)" << endl
		 << "    cout << \" Test is ok.\" << endl;" << endl
		 << "  else" << endl
		 << "  {" << endl
		 << "    cout << \" TEST IS WRONG !!!\" << endl" << endl
		 << "         << \"    Failure(s) on test(s) \";" << endl
		 << "    list<int>::iterator it;" << endl
		 << "    for(it=failure.begin();it!=failure.end();it++)" << endl
		 << "      cout << \" \" << (*it);" << endl
		 << "    cout << \".\" << endl;" << endl
		 << "  }" << endl
		 << "}" << endl
		 << endl
		 << "void UnitTest::reinit()" << endl
		 << "{" << endl
		 << "  nbr_valid = 0;" << endl
		 << "  nbr_unvalid = 0;" << endl
		 << "  failure.clear();" << endl
		 << "}" << endl;
	f2.close();
}

void createNewUnitTest(const string& file_name,const string& class_name,list<string>& function_list)
{
	string min_class_name = class_name;
	if((min_class_name[0] >= 'A') && (min_class_name[0] <= 'Z'))
		min_class_name[0] += (int)'a' - (int)'A';
	string all_maj_class_name = class_name;
	for(int i=0;i<all_maj_class_name.length();i++)
	{
		if((all_maj_class_name[i] >= 'a') && (all_maj_class_name[i] <= 'z'))
			all_maj_class_name[i] -= (int)'a' - (int)'A';
	}
	string unittest_hfile_name = min_class_name + ".unittest.h";
	string unittest_cppfile_name = min_class_name + ".unittest.cpp";

	ofstream f1(unittest_hfile_name.c_str());
	f1 << "/*" << endl
		 << " *  " << unittest_hfile_name << endl
		 << " *  " << endl
		 << " *  Generated by UnitTestGenerator." << endl
		 << " *  Copyright 2008 Christophe Dumeunier. All rights reserved." << endl
		 << " *  This file is free; you can redistribute it and/or modify" << endl
		 << " *  it under the terms of the GNU General Public License as published by" << endl
		 << " *  the Free Software Foundation; either version 2 of the License, or" << endl
		 << " *  (at your option) any later version." << endl
		 << " *  " << endl
		 << " *  Contact: legrandduche@gmail.com (in french if possible)" << endl
		 << " *  " << endl
		 << " */" << endl
		 << endl
		 << "#ifndef " << all_maj_class_name << "_UNITTEST_H" << endl
		 << "#define " << all_maj_class_name << "_UNITTEST_H" << endl
		 << endl
		 << "#include \"unittest.h\"" << endl
		 << "#include \"" << file_name << "\"" << endl
		 << endl
		 << "class " << class_name << "UnitTest : private UnitTest" << endl
		 << "{" << endl
		 << "  private:" << endl
		 << "    //add your variables" << endl
		 << "    " << endl
		 << "  public:" << endl
		 << "    " << class_name << "UnitTest();" << endl
		 << "    ~" << class_name << "UnitTest();" << endl
		 << "    " << endl
		 << "    bool runTest();" << endl
		 << "    " << endl
		 << "  private:" << endl
		 << "    void setUp();" << endl
		 << "    void tearDown();" << endl
		 << "    " << endl;
	list<string>::iterator it;
	for(it=function_list.begin();it!=function_list.end();it++)
	{
		string name = (*it);
		int left_parenth = name.find("(");
		int length = name.length();
		name.erase(left_parenth,length-left_parenth);
		f1 << "    void " << name << "_";
		string params = (*it);
		params.erase(length-1,1);
		params.erase(0,left_parenth+1);
		params += ",";
		if(params == ",")
			params = "void,";
		while(params.length() > 0)
		{
			int comma = params.find(",");
			int length_param = params.length();
			string param = params;
			param.erase(comma,length_param-comma);
			if((param[0] >= 'a') && (param[0] <= 'z'))
				param[0] -= (int)'a' - (int)'A';
			while(param.find("*") != string::npos)
			{
				int paramFindStar = param.find("*");
				param.replace(paramFindStar,1,"Ptr");
			}
			while(param.find("&") != string::npos)
			{
				int paramFindAnd = param.find("&");
				param.replace(paramFindAnd,1,"Ref");
			}
			f1 << param;
			params.erase(0,comma+1);
		}
		f1 << "();" << endl;
	}
	f1 << "};" << endl
	   << endl
		 << "typedef void (" << class_name << "UnitTest::*" << class_name << "UnitTestMemberFunctionPointer)(void);" << endl
		 << endl
		 << "#endif" << endl;
	f1.close();
	
	
	
	ofstream f2(unittest_cppfile_name.c_str());
	f2 << "/*" << endl
		 << " *  " << unittest_cppfile_name << endl
		 << " *  " << endl
		 << " *  Generated by UnitTestGenerator." << endl
		 << " *  Copyright 2008 Christophe Dumeunier. All rights reserved." << endl
		 << " *  This file is free; you can redistribute it and/or modify" << endl
		 << " *  it under the terms of the GNU General Public License as published by" << endl
		 << " *  the Free Software Foundation; either version 2 of the License, or" << endl
		 << " *  (at your option) any later version." << endl
		 << " *  " << endl
		 << " *  Contact: legrandduche@gmail.com (in french if possible)" << endl
		 << " *  " << endl
		 << " */" << endl
		 << endl
		 << "#include \"" << unittest_hfile_name << "\"" << endl
		 << endl
		 << class_name << "UnitTest::" << class_name << "UnitTest()" << endl
		 << "{" << endl
		 << "  //construct your variables" << endl
		 << "}" << endl
		 << endl
		 << class_name << "UnitTest::~" << class_name << "UnitTest()" << endl
		 << "{" << endl
		 << "  //destruct your variables" << endl
		 << "}" << endl
		 << endl
		 << "bool " << class_name << "UnitTest::runTest()" << endl
		 << "{" << endl
		 << "  const int run_vector_size = " << (int)function_list.size() << ";" << endl
		 << "  " << class_name << "UnitTestMemberFunctionPointer run_vector[run_vector_size] =" << endl
		 << "  {" << endl;
	for(it=function_list.begin();it!=function_list.end();it++)
	{
		string name = (*it);
		int left_parenth = name.find("(");
		int length = name.length();
		name.erase(left_parenth,length-left_parenth);
		if(it != function_list.begin())
			f2 << "," << endl;
		f2 << "    &" << class_name << "UnitTest::" << name << "_";
		string params = (*it);
		params.erase(length-1,1);
		params.erase(0,left_parenth+1);
		params += ",";
		if(params == ",")
			params = "void,";
		while(params.length() > 0)
		{
			int comma = params.find(",");
			int length_param = params.length();
			string param = params;
			param.erase(comma,length_param-comma);
			if((param[0] >= 'a') && (param[0] <= 'z'))
				param[0] -= (int)'a' - (int)'A';
			while(param.find("*") != string::npos)
			{
				int paramFindStar = param.find("*");
				param.replace(paramFindStar,1,"Ptr");
			}
			while(param.find("&") != string::npos)
			{
				int paramFindAnd = param.find("&");
				param.replace(paramFindAnd,1,"Ref");
			}
			f2 << param;
			params.erase(0,comma+1);
		}
	}
	f2 << endl
	   << "  };" << endl
		 << "  string string_run_vector[run_vector_size] =" << endl
		 << "  {" << endl;
	for(it=function_list.begin();it!=function_list.end();it++)
	{
		if(it != function_list.begin())
			f2 << "," << endl;
		f2 << "    \"" << class_name << "::" << (*it) << "\"";
	}
	f2 << endl
	   << "  };" << endl
		 << "  bool validTest = true;" << endl
		 << "  cout << \" ------------------------------------------------------------------------------\" << endl" << endl
		 << "       << \"  Unit Test of class " << class_name << "  --  by Christophe Dumeunier\" << endl" << endl
		 << "       << \" ------------------------------------------------------------------------------\" << endl;" << endl
		 << "  for(int i=0;i<run_vector_size;i++)" << endl
		 << "  {" << endl
		 << "    setUp();" << endl
		 << "    (this->*run_vector[i])();" << endl
		 << "    tearDown();" << endl
		 << "    validTest = validTest && valid();" << endl
		 << "    print(string_run_vector[i]);" << endl
		 << "    reinit();" << endl
		 << "  }" << endl
		 << "  cout << \" ------------------------------------------------------------------------------\" << endl;" << endl
		 << "  return validTest;" << endl
		 << "}" << endl
		 << endl
		 << "void " << class_name << "UnitTest::setUp()" << endl
		 << "{" << endl
		 << "  //prepare each test" << endl
		 << "}" << endl
		 << endl
		 << "void " << class_name << "UnitTest::tearDown()" << endl
		 << "{" << endl
		 << "  //terminate each test" << endl
		 << "}" << endl;
	for(it=function_list.begin();it!=function_list.end();it++)
	{
		string name = (*it);
		int left_parenth = name.find("(");
		int length = name.length();
		name.erase(left_parenth,length-left_parenth);
		f2 << endl
		   << "void " << class_name << "UnitTest::" << name << "_";
		string params = (*it);
		params.erase(length-1,1);
		params.erase(0,left_parenth+1);
		params += ",";
		if(params == ",")
			params = "void,";
		while(params.length() > 0)
		{
			int comma = params.find(",");
			int length_param = params.length();
			string param = params;
			param.erase(comma,length_param-comma);
			if((param[0] >= 'a') && (param[0] <= 'z'))
				param[0] -= (int)'a' - (int)'A';
			while(param.find("*") != string::npos)
			{
				int paramFindStar = param.find("*");
				param.replace(paramFindStar,1,"Ptr");
			}
			while(param.find("&") != string::npos)
			{
				int paramFindAnd = param.find("&");
				param.replace(paramFindAnd,1,"Ref");
			}
			f2 << param;
			params.erase(0,comma+1);
		}
		f2 << "()" << endl
		   << "{" << endl
			 << "  //make your tests" << endl
			 << "}" << endl;
	}
	f2.close();
}

int main(int sys_nbr_args,char** sys_args)
{
	cout << endl
       << endl
			 << " ------------------------------------------------------------------------------" << endl
			 << "  Welcome on the UnitTest Generator by Christophe Dumeunier." << endl
			 << "  Copyright 2008 Christophe Dumeunier. All rights reserved." << endl
			 << "  This application is a free software; you can redistribute it and/or modify" << endl
			 << "  it under the terms of the GNU General Public License as published by" << endl
			 << "  the Free Software Foundation; either version 2 of the License, or" << endl
			 << "  (at your option) any later version." << endl
			 << " ------------------------------------------------------------------------------" << endl
			 << endl
			 << endl;
	int user;
	bool finish = false;
	while(!finish)
	{
		cout << endl
				 << "  1. Create unittest.h and unittest.cpp files" << endl
				 << "  2. Create a new UnitTest on a class" << endl
				 << "  3. Quit" << endl
				 << endl;
		cin >> user;
		if(user == 1)
		{
			createUnitTestLib();
			cout << "done." << endl;
		}
		else if(user == 2)
		{
			string file_name;
			string class_name;
			list<string> function_list;
			cout << "  Enter the file name (containing the class, for example: example.h)" << endl;
			cin >> file_name;
			cout << "  Enter the class name (for example : Example)" << endl;
			cin >> class_name;
			bool finish2 = false;
			while(!finish2)
			{
				string function_name = "";
				cout << "  Add a member function of class " << class_name << " (for example : int myFunction(char,int))" << endl
				     << "  -- \"()\" to terminate --" << endl;
				bool line_readed = false;
				while(!line_readed)
				{
					string temporary_str;
					cin >> temporary_str;
					function_name += temporary_str;
					if(function_name[function_name.length()-1] == ')')
						line_readed = true;
					else
						function_name += ",";
				}
				if(function_name == "()")
					finish2 = true;
				else
					function_list.push_back(function_name);
			}
			createNewUnitTest(file_name,class_name,function_list);
			cout << "done." << endl;
		}
		else if(user == 3)
		{
			finish = true;
			cout << "Bye !" << endl;
		}
		else
		{
			cout << "Try again..." << endl;
		}
	}
	cout << endl
	     << endl;
	return 0;
}

 Conclusion

à venir:
* parseur pour éviter à l'utilisateur de taper les fonctions à la main (et ainsi des risquer des erreur dans le fichier généré)
* possibilité d'ajouter des unittest à un fichier
* interface plus sympa que la console ( peut-être un jour... un motivé pour le faire ? :-D )

 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip


 Historique

25 mars 2008 12:13:57 :
ajout du .zip
25 mars 2008 17:12:45 :
ajout des possibilités de test assertNotEquals assertSame assertNotSame assertNull assertNotNull

 Sources du même auteur

Source avec Zip CLASSE MOMENT V2.0
Source avec Zip GESTION DE LA DATE DE L'HEURE ET DU TEMPS
Source avec Zip EXEMPLE SDL : FAIRE COURIR ET FREINER SONIC
Source avec Zip QUELQUES FONCTIONS POUR FACILITER LA PRISE EN MAIN DE LA SDL
Source avec Zip RÉSOLUTION ET CRÉATIONS DE SUDOKUS

 Sources de la même categorie

Source avec Zip CALLOCATOR par troctsch
Source avec Zip HEAPCOLLECTOR par troctsch
Source avec Zip GÉNÉRE UN TABLEAU DE CARACTÉRE AU FORMAT C CONTENANT LE BYTE... par kertimanoff
FRACTIONS EGYPTIENNES par lptm974
BITOPERATOR par FrancoisGauthier

 Sources en rapport avec celle ci

Source avec Zip Source avec une capture Source .NET (Dotnet) TEST AUDIO par grandcalibre
Source avec une capture COMPARAISON TRI + TEST CPU par lucastok2
FICHIER_EXISTE par aarriba59

Commentaires et avis

Commentaire de le_duche le 25/03/2008 00:29:08

Il n'y a pas de zip, et donc pas d'exemple. Désolé, je ne sais pas pourquoi mon fichier zip n'est pas accepté.

Commentaire de le_duche le 25/03/2008 12:15:22

Et voilà le problème du zip réglé ! :-)

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

turboc++ icones .exe??? [ par idk ] Comment faire pour plus avoir cette icone horrible, (carré blanc avec du bleu en haut!)Dans le makefile faut faire quoi!voici le makefile d'un program Exporter une classe dans une DLL... [ par Clovis ] Salut,Je voudrais pouvoir exporter dans une DLL, si c'est possible, les fonctions et les objets du listing suivant. Car, je voudrais, par la suite pou HEEELLLLPPPP!!!!!!!!!!detruire un handle de com [ par mavric ] salut je fais un prog de com serie ds mon prog j'ouvre une com avec la fonction suivante :hcom=createfile("com2"...........puis je verifie si l'ouvert HEEELLLLPPPP!!!!!!!!!!detruire un handle de com [ par mavric ] salut je fais un prog de com serie ds mon prog j'ouvre une com avec la fonction suivante :hcom=createfile("com2"...........puis je verifie si l'ouvert HELP!!!!!!!!!!!!!! => comparaison de BMP [ par mavric ] bonjour ,je cherche a comparer 2 fichiers bmp ,mais de facon optimisé , pour le moment mon prog mais environ 20 sec et il faudrait qui mette moins de Switch et char [ par Stoomm ] Bonjour,Si j'ai :char test[10];strcpy (test, "monchar ";Si je souhaite faire un switch : Je fais comment ???switch (test){...}Il aime pas ! Quelqu'un Hexadécimal HELP ! [ par Xentor ] Bonjour,Mon problème est le suivant : je vient d'effectuer un AND sur une variable, et celle ci, bien que conservant son type unsigned int se met en h econio.h petit bug [ par cognac ] Bonjour,J'ai intallé econio.h (mais pas econio.ccp), je fais le petit test que voilà:#include &lt;econio.h&gt;#include &lt;iostream.h&gt;int main(){te pb avec ADO [ par Frederyck ] Voilà g un pb, j'utilise dans mon appli une bdd Access 97 via ADO et donc g inclut la commande:#import "C:\Program Files\Fichiers communs\System\ado\m Convertion "15"(string) --> 15(int) [ par crocejf2000 ] (Re)SalutIl faut que je test ma chaine et que je la convertisse si je peut. Avec atoi("12"), ca me donne bien 12, mais si je fait un atoi("12hach"), c


Nos sponsors


Sondage...

CalendriCode

Février 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,640 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales