diff --git a/src/hpp/cppweb.h b/src/hpp/cppweb.h index 2b302ec..3528e84 100644 --- a/src/hpp/cppweb.h +++ b/src/hpp/cppweb.h @@ -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; diff --git a/src/test/main.cpp b/src/test/main.cpp index 0abe923..6bfbc3d 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -12,5 +12,3 @@ INIT(init){ TEST(TestForHeader, RunTest){ MUST_EQUAL(1, 1); } - -RUN diff --git a/src/test/test.h b/src/test/test.h index 27e1ad1..130bd05 100644 --- a/src/test/test.h +++ b/src/test/test.h @@ -1,121 +1,169 @@ /*********************************************** -* Author: chenxuan-1607772321@qq.com -* change time:2023-04-14 20:56:46 -* description: this is a simple C++ testing framework -***********************************************/ + * Author: chenxuan-1607772321@qq.com + * 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 +#include #include +#include +#include #include #include -#include -//base class -class _test_base{ +// base class +class _test_base { public: - bool result=true; - static std::vector> test_arr; - static int success; - static int fail; - virtual void TestBody(){}; + bool result_ = true; + static std::string regex_filt_; + static std::vector> test_arr_; + static int success_; + static int fail_; + virtual void TestBody(){}; }; -int _test_base::success=0; -int _test_base::fail=0; -std::vector> _test_base::test_arr; +int _test_base::success_ = 0; +int _test_base::fail_ = 0; +std::string _test_base::regex_filt_ = ""; +std::vector> _test_base::test_arr_; // std out or stderr -#define TESTSTDOUT(text) std::cout<fail_++; \ + this->success_--; \ + this->result_ = false +#define _FILE_LINE_MSG_ __FILE__ << ":" << __LINE__ -//print kinds of color -#define TESTRED(text) "\033[31m"<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_<<"] : "<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:"<TestBody(); \ - if(base.test_arr[i].first->result){ \ - TESTSTDOUT(TESTGREEN("Result:PASS")); \ - }else{ \ - TESTSTDOUT(TESTRED("Result:Fail")); \ - } \ - std::cout<