libwreport  2.4
tests.h
00001 /*
00002  * wreport/tests - Unit test utilities
00003  *
00004  * Copyright (C) 2005--2010  ARPA-SIM <urpsim@smr.arpa.emr.it>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License.
00009  *
00010  * This program 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
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00018  *
00019  * Author: Enrico Zini <enrico@enricozini.com>
00020  */
00021 
00022 #include <wibble/tests.h>
00023 #include <wreport/varinfo.h>
00024 #include <wreport/var.h>
00025 #include <string>
00026 #include <iostream>
00027 #include <cstdlib>
00028 #include <cstdio>
00029 
00030 namespace wreport {
00031 namespace tests {
00032 
00033 #define ensure_contains(x, y) wreport::tests::impl_ensure_contains(wibble::tests::Location(__FILE__, __LINE__, #x " == " #y), (x), (y))
00034 #define inner_ensure_contains(x, y) wreport::tests::impl_ensure_contains(wibble::tests::Location(loc, __FILE__, __LINE__, #x " == " #y), (x), (y))
00035 static inline void impl_ensure_contains(const wibble::tests::Location& loc, const std::string& haystack, const std::string& needle)
00036 {
00037     if( haystack.find(needle) == std::string::npos )
00038     {
00039         std::stringstream ss;
00040         ss << "'" << haystack << "' does not contain '" << needle << "'";
00041         throw tut::failure(loc.msg(ss.str()));
00042     }
00043 }
00044 
00045 #define ensure_not_contains(x, y) arki::tests::impl_ensure_not_contains(wibble::tests::Location(__FILE__, __LINE__, #x " == " #y), (x), (y))
00046 #define inner_ensure_not_contains(x, y) arki::tests::impl_ensure_not_contains(wibble::tests::Location(loc, __FILE__, __LINE__, #x " == " #y), (x), (y))
00047 static inline void impl_ensure_not_contains(const wibble::tests::Location& loc, const std::string& haystack, const std::string& needle)
00048 {
00049     if( haystack.find(needle) != std::string::npos )
00050     {
00051         std::stringstream ss;
00052         ss << "'" << haystack << "' must not contain '" << needle << "'";
00053         throw tut::failure(loc.msg(ss.str()));
00054     }
00055 }
00056 
00057 #define ensure_varcode_equals(x, y) wreport::tests::_ensure_varcode_equals(wibble::tests::Location(__FILE__, __LINE__, #x " == " #y), (x), (y))
00058 #define inner_ensure_varcode_equals(x, y) wreport::tests::_ensure_varcode_equals(wibble::tests::Location(loc, __FILE__, __LINE__, #x " == " #y), (x), (y))
00059 static inline void _ensure_varcode_equals(const wibble::tests::Location& loc, Varcode actual, Varcode expected)
00060 {
00061     if( expected != actual )
00062     {
00063         char buf[40];
00064         snprintf(buf, 40, "expected %01d%02d%03d actual %01d%02d%03d",
00065                 WR_VAR_F(expected), WR_VAR_X(expected), WR_VAR_Y(expected),
00066                 WR_VAR_F(actual), WR_VAR_X(actual), WR_VAR_Y(actual));
00067         throw tut::failure(loc.msg(buf));
00068     }
00069 }
00070 
00071 #define ensure_var_equals(x, y) wreport::tests::_ensure_var_equals(wibble::tests::Location(__FILE__, __LINE__, #x " == " #y), (x), (y))
00072 #define inner_ensure_var_equals(x, y) wreport::tests::_ensure_var_equals(wibble::tests::Location(loc, __FILE__, __LINE__, #x " == " #y), (x), (y))
00073 static inline void _ensure_var_equals(const wibble::tests::Location& loc, const Var& var, int val)
00074 {
00075     inner_ensure_equals(var.enqi(), val);
00076 }
00077 static inline void _ensure_var_equals(const wibble::tests::Location& loc, const Var& var, double val)
00078 {
00079     inner_ensure_equals(var.enqd(), val);
00080 }
00081 static inline void _ensure_var_equals(const wibble::tests::Location& loc, const Var& var, const std::string& val)
00082 {
00083     inner_ensure_equals(std::string(var.enqc()), val);
00084 }
00085 
00086 #define ensure_var_undef(x) wreport::tests::_ensure_var_undef(wibble::tests::Location(__FILE__, __LINE__, #x " is undef"), (x))
00087 #define inner_ensure_var_undef(x) wreport::tests::_ensure_var_undef(wibble::tests::Location(loc, __FILE__, __LINE__, #x " is undef"), (x))
00088 static inline void _ensure_var_undef(const wibble::tests::Location& loc, const Var& var)
00089 {
00090     inner_ensure_equals(var.value(), (const char*)0);
00091 }
00092 
00094 class LocalEnv
00095 {
00097     std::string key;
00099     std::string oldVal;
00100 public:
00105     LocalEnv(const std::string& key, const std::string& val)
00106         : key(key)
00107     {
00108         const char* v = getenv(key.c_str());
00109         oldVal = v == NULL ? "" : v;
00110         setenv(key.c_str(), val.c_str(), 1);
00111     }
00112     ~LocalEnv()
00113     {
00114         setenv(key.c_str(), oldVal.c_str(), 1);
00115     }
00116 };
00117 
00118 } // namespace tests
00119 } // namespace wreport
00120 
00121 // vim:set ts=4 sw=4: