Skip to content

Commit

Permalink
feat:add some detail
Browse files Browse the repository at this point in the history
  • Loading branch information
chenxuan520 committed May 15, 2024
1 parent 11f52e4 commit 94a3989
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 104 deletions.
2 changes: 1 addition & 1 deletion src/hpp/cppweb.h
Original file line number Diff line number Diff line change
Expand Up @@ -2282,11 +2282,11 @@ class ClientTcpIp{
error=NULL;
isHttps=false;
sock=socket(AF_INET,SOCK_STREAM,0);
addrC.sin_family=AF_INET;//af_inet IPv4
}
ClientTcpIp(const std::string& connectIP,unsigned short connectPort):ClientTcpIp(){
this->connectIP=connectIP;
addrC.sin_addr.s_addr=inet_addr(connectIP.c_str());
addrC.sin_family=AF_INET;//af_inet IPv4
addrC.sin_port=htons(connectPort);
#ifdef CPPWEB_OPENSSL
ssl=NULL;
Expand Down
2 changes: 0 additions & 2 deletions src/test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,3 @@ INIT(init){
TEST(TestForHeader, RunTest){
MUST_EQUAL(1, 1);
}

RUN
250 changes: 149 additions & 101 deletions src/test/test.h
Original file line number Diff line number Diff line change
@@ -1,121 +1,169 @@
/***********************************************
* Author: [email protected]
* change time:2023-04-14 20:56:46
* description: this is a simple C++ testing framework
***********************************************/
* Author: [email protected]
* change time:2023-04-14 20:56:46
* description: this is a simple C++ testing framework
* download: use `wget https://gitee.com/chenxuan520/cpptest/raw/master/test.h`
***********************************************/
#pragma once
#include <cstdlib>
#include <functional>
#include <iostream>
#include <regex>
#include <string>
#include <utility>
#include <vector>
#include <functional>

//base class
class _test_base{
// base class
class _test_base {
public:
bool result=true;
static std::vector<std::pair<_test_base*, std::string>> test_arr;
static int success;
static int fail;
virtual void TestBody(){};
bool result_ = true;
static std::string regex_filt_;
static std::vector<std::pair<_test_base *, std::string>> test_arr_;
static int success_;
static int fail_;
virtual void TestBody(){};
};
int _test_base::success=0;
int _test_base::fail=0;
std::vector<std::pair<_test_base*, std::string>> _test_base::test_arr;
int _test_base::success_ = 0;
int _test_base::fail_ = 0;
std::string _test_base::regex_filt_ = "";
std::vector<std::pair<_test_base *, std::string>> _test_base::test_arr_;

// std out or stderr
#define TESTSTDOUT(text) std::cout<<text<<std::endl;
#define TESTSTDERR(text) std::cerr<<text<<std::endl;
#define _TESTSTDOUT_(text) std::cout << text << std::endl;
#define _TESTSTDERR_(text) std::cerr << text << std::endl;

// print kinds of color
#define _TESTRED_(text) "\033[31m" << text << "\033[0m"
#define _TESTGREEN_(text) "\033[32m" << text << "\033[0m"
#define _TESTYELLOW_(text) "\033[33m" << text << "\033[0m"
#define _TESTBLUE_(text) "\033[34m" << text << "\033[0m"
#define _TESTCAR_(text) "\033[35m" << text << "\033[0m"
#define _TESTCYAN_(text) "\033[36m" << text << "\033[0m"

// setting macro
#define REGEX_FILT_TEST(test_regex) _test_base::regex_filt_ = test_regex;

// util macro
#define _CONNECTSTR_(...) #__VA_ARGS__
#define _CLASS_FAIL_ \
this->fail_++; \
this->success_--; \
this->result_ = false
#define _FILE_LINE_MSG_ __FILE__ << ":" << __LINE__

//print kinds of color
#define TESTRED(text) "\033[31m"<<text<<"\033[0m"
#define TESTGREEN(text) "\033[32m"<<text<<"\033[0m"
#define TESTYELLOW(text) "\033[33m"<<text<<"\033[0m"
#define TESTBLUE(text) "\033[34m"<<text<<"\033[0m"
#define TESTCAR(text) "\033[35m"<<text<<"\033[0m"
#define TESTCYAN(text) "\033[36m"<<text<<"\033[0m"
// test name
#define _TEST_NAME__CREATE_(test_group, test_name) \
test_group##test_name##_create
#define _TEST_NAME_(test_group, test_name) test_group##test_name##_test
#define _TEST_INIT_NAME__CREATE_(init_name) init_name##_init##_create
#define _TEST_INIT_NAME_(init_name) init_name##_init
#define _TEST_END_NAME__CREATE_(end_name) end_name##_end##_create
#define _TEST_END_NAME_(end_name) end_name##_end

//util macro
#define CPPWEBCONNECTSTR(...) #__VA_ARGS__
#define _CLASS_FAIL_ this->fail++;this->success--;this->result=false
#define _FILE_LINE_MSG_ __FILE__<<":"<<__LINE__
// test init function,run before all test example
#define INIT(init_name) \
int _TEST_INIT_NAME_(init_name)(); \
auto _TEST_INIT_NAME__CREATE_(init_name) = _TEST_INIT_NAME_(init_name)(); \
int _TEST_INIT_NAME_(init_name)()

//test name
#define CPPWEB_TEST_NAME_CREATE(test_group,test_name) test_group##test_name##_create
#define CPPWEB_TEST_NAME(test_group,test_name) test_group##test_name##_cppweb
#define CPPWEB_TEST_INIT_NAME_CREATE(init_name) init_name##_init##_create
#define CPPWEB_TEST_INIT_NAME(init_name) init_name##_init
#define CPPWEB_TEST_END_NAME_CREATE(end_name) end_name##_end##_create
#define CPPWEB_TEST_END_NAME(end_name) end_name##_end
// test end function,run after all test example
#define END(end_name) \
class _TEST_END_NAME_(end_name) { \
public: \
~_TEST_END_NAME_(end_name)(); \
}; \
_TEST_END_NAME_(end_name) _TEST_END_NAME__CREATE_(end_name); \
_TEST_END_NAME_(end_name)::~_TEST_END_NAME_(end_name)()

//test init function,run before all test example
#define INIT(init_name) \
int CPPWEB_TEST_INIT_NAME(init_name)(); \
auto CPPWEB_TEST_INIT_NAME_CREATE(init_name)=CPPWEB_TEST_INIT_NAME(init_name)(); \
int CPPWEB_TEST_INIT_NAME(init_name)()
// test function for users
#define TEST(test_group, test_name) \
class _TEST_NAME_(test_group, test_name) : public _test_base { \
public: \
_TEST_NAME_(test_group, test_name)() { \
test_arr_.push_back({this, _CONNECTSTR_(test_group test_name)}); \
this->success_++; \
} \
void TestBody(); \
}; \
_TEST_NAME_(test_group, test_name) \
_TEST_NAME__CREATE_(test_group, test_name); \
void _TEST_NAME_(test_group, test_name)::TestBody()

//test end function,run after all test example
#define END(end_name) \
class CPPWEB_TEST_END_NAME(end_name){ \
public: \
~CPPWEB_TEST_END_NAME(end_name)(); \
}; \
CPPWEB_TEST_END_NAME(end_name) CPPWEB_TEST_END_NAME_CREATE(end_name); \
CPPWEB_TEST_END_NAME(end_name)::~CPPWEB_TEST_END_NAME(end_name)()
// for use default name for create test example
#define TEST_DEFAULT TEST(DefaultTest, __COUNTER__)

//test function for users
#define TEST(test_group,test_name) \
class CPPWEB_TEST_NAME(test_group,test_name):public _test_base{ \
public: \
CPPWEB_TEST_NAME(test_group,test_name)(){ \
test_arr.push_back({this,CPPWEBCONNECTSTR(test_group test_name)}); \
this->success++; \
} \
void TestBody(); \
}; \
CPPWEB_TEST_NAME(test_group,test_name) CPPWEB_TEST_NAME_CREATE(test_group,test_name); \
void CPPWEB_TEST_NAME(test_group,test_name)::TestBody()
// some function for debug and judge
#define SKIP() \
_TESTSTDOUT_(_TESTYELLOW_("[SKIP] in [" << _FILE_LINE_MSG_ << "] : ")); \
return;
#define DEBUG(text) \
_TESTSTDOUT_( \
_TESTYELLOW_("[DEBUG] in [" << _FILE_LINE_MSG_ << "] : " << text))
#define ERROR(text) \
_TESTSTDERR_(_TESTCAR_("[ERROR] in [" << _FILE_LINE_MSG_ << "] : " << text)) \
_CLASS_FAIL_
#define FATAL(text) \
_TESTSTDERR_(_TESTRED_("[FATAL] in [" << _FILE_LINE_MSG_ << "] : " << text)) \
_CLASS_FAIL_; \
return;
#define PANIC(text) \
_TESTSTDERR_( \
_TESTRED_("[PANIC] in [" << _FILE_LINE_MSG_ << "] : " << text)); \
exit(-1);
#define EXPECT_EQ(result, expect) \
if (result != expect) { \
FATAL(_CONNECTSTR_(result want get expect but get) << " " << result) \
}
#define MUST_EQUAL(result, expect) \
if (result != expect) { \
FATAL(result << " " << _CONNECTSTR_(!= expect)); \
}
#define MUST_TRUE(flag, text) \
if (!(flag)) { \
FATAL(text); \
}

//some function for debug and judge
#define SKIP() TESTSTDOUT(TESTYELLOW("[SKIP] in ["<<_FILE_LINE_MSG_<<"] : "));return;
#define DEBUG(text) TESTSTDOUT(TESTYELLOW("[DEBUG] in ["<<_FILE_LINE_MSG_<<"] : "<<text))
#define ERROR(text) TESTSTDERR(TESTCAR("[ERROR] in ["<<_FILE_LINE_MSG_<<"] : "<<text))\
_CLASS_FAIL_
#define FATAL(text) TESTSTDERR(TESTRED("[FATAL] in ["<<_FILE_LINE_MSG_<<"] : "<<text))\
_CLASS_FAIL_;return;
#define PANIC(text) TESTSTDERR(TESTRED("[PANIC] in ["<<_FILE_LINE_MSG_<<"] : "<<text));exit(-1);
#define EXPECT_EQ(result,expect)\
if (result!=expect){FATAL(CPPWEBCONNECTSTR(result want get expect but get)<<" "<<result)}
#define MUST_EQUAL(result,expect)\
if(result!=expect){FATAL(result<<" "<<CPPWEBCONNECTSTR(!= expect));}
#define MUST_TRUE(flag,text)\
if(!(flag)){FATAL(text);}
// for argc message
static void (*__test_argc_funcpr__)(int argc, char *argv[]) = nullptr;
static void __test_deal_argc__(int argc, char **argv);
#define ARGC_FUNC \
static auto __test_temp_argc__ = \
(__test_argc_funcpr__ = __test_deal_argc__); \
void __test_deal_argc__(int argc, char *argv[])

//for argc message
static void (*__test_argc_funcpr__)(int argc,char* argv[])=nullptr;
static void __test_deal_argc__(int argc,char** argv);
#define ARGC_FUNC static auto __test__temp__argc__=(__test_argc_funcpr__=__test_deal_argc__);\
void __test_deal_argc__(int argc,char* argv[])
// the main function
#define RUN \
int main(int argc, char *argv[]) { \
if (__test_argc_funcpr__ != nullptr) { \
__test_argc_funcpr__(argc, argv); \
} \
_test_base base; \
for (int i = 0; i < base.test_arr_.size(); i++) { \
if (base.regex_filt_ != "") { \
std::regex pattern(base.regex_filt_); \
if (!std::regex_search(base.test_arr_[i].second, pattern)) { \
continue; \
} \
} \
_TESTSTDOUT_(_TESTCYAN_("Runing:" << base.test_arr_[i].second)); \
base.test_arr_[i].first->TestBody(); \
if (base.test_arr_[i].first->result_) { \
_TESTSTDOUT_(_TESTGREEN_("Result:PASS")); \
} else { \
_TESTSTDOUT_(_TESTRED_("Result:Fail")); \
} \
std::cout << std::endl; \
} \
if (base.regex_filt_ != "") { \
_TESTSTDOUT_(_TESTBLUE_("Regex Filt:" << base.regex_filt_) << std::endl) \
} \
_TESTSTDOUT_(_TESTBLUE_("Total Run:" << base.success_ + base.fail_)) \
_TESTSTDOUT_(_TESTBLUE_("Success Run:" << base.success_)) \
_TESTSTDERR_(_TESTBLUE_("Fail Run:" << base.fail_)) \
return base.fail_; \
}

//the main function
#define RUN \
int main(int argc,char* argv[]){ \
if (__test_argc_funcpr__!=nullptr) { \
__test_argc_funcpr__(argc,argv); \
} \
_test_base base; \
for (int i = 0; i < base.test_arr.size(); i++) { \
TESTSTDOUT(TESTCYAN("Runing:"<<base.test_arr[i].second)); \
base.test_arr[i].first->TestBody(); \
if(base.test_arr[i].first->result){ \
TESTSTDOUT(TESTGREEN("Result:PASS")); \
}else{ \
TESTSTDOUT(TESTRED("Result:Fail")); \
} \
std::cout<<std::endl; \
} \
TESTSTDOUT(TESTBLUE("Total Run:"<<base.success+base.fail)) \
TESTSTDOUT(TESTBLUE("Success Run:"<<base.success)) \
TESTSTDERR(TESTBLUE("Fail Run:"<<base.fail)) \
return base.fail; \
}
// default run auto
#ifndef TEST_CUSTOM_MAIN
RUN
#endif

0 comments on commit 94a3989

Please sign in to comment.