libwreport  2.8
tests.h
1 /*
2  * wreport/tests - Unit test utilities
3  *
4  * Copyright (C) 2005--2010 ARPA-SIM <urpsim@smr.arpa.emr.it>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Author: Enrico Zini <enrico@enricozini.com>
20  */
21 
22 #include <wibble/tests.h>
23 #include <wreport/varinfo.h>
24 #include <wreport/var.h>
25 #include <string>
26 #include <iostream>
27 #include <cstdlib>
28 #include <cstdio>
29 
30 namespace wreport {
31 namespace tests {
32 
33 #define ensure_contains(x, y) wreport::tests::impl_ensure_contains(wibble::tests::Location(__FILE__, __LINE__, #x " == " #y), (x), (y))
34 #define inner_ensure_contains(x, y) wreport::tests::impl_ensure_contains(wibble::tests::Location(loc, __FILE__, __LINE__, #x " == " #y), (x), (y))
35 static inline void impl_ensure_contains(const wibble::tests::Location& loc, const std::string& haystack, const std::string& needle)
36 {
37  if( haystack.find(needle) == std::string::npos )
38  {
39  std::stringstream ss;
40  ss << "'" << haystack << "' does not contain '" << needle << "'";
41  throw tut::failure(loc.msg(ss.str()));
42  }
43 }
44 
45 #define ensure_not_contains(x, y) arki::tests::impl_ensure_not_contains(wibble::tests::Location(__FILE__, __LINE__, #x " == " #y), (x), (y))
46 #define inner_ensure_not_contains(x, y) arki::tests::impl_ensure_not_contains(wibble::tests::Location(loc, __FILE__, __LINE__, #x " == " #y), (x), (y))
47 static inline void impl_ensure_not_contains(const wibble::tests::Location& loc, const std::string& haystack, const std::string& needle)
48 {
49  if( haystack.find(needle) != std::string::npos )
50  {
51  std::stringstream ss;
52  ss << "'" << haystack << "' must not contain '" << needle << "'";
53  throw tut::failure(loc.msg(ss.str()));
54  }
55 }
56 
57 #define ensure_varcode_equals(x, y) wreport::tests::_ensure_varcode_equals(wibble::tests::Location(__FILE__, __LINE__, #x " == " #y), (x), (y))
58 #define inner_ensure_varcode_equals(x, y) wreport::tests::_ensure_varcode_equals(wibble::tests::Location(loc, __FILE__, __LINE__, #x " == " #y), (x), (y))
59 static inline void _ensure_varcode_equals(const wibble::tests::Location& loc, Varcode actual, Varcode expected)
60 {
61  if( expected != actual )
62  {
63  char buf[40];
64  snprintf(buf, 40, "expected %01d%02d%03d actual %01d%02d%03d",
65  WR_VAR_F(expected), WR_VAR_X(expected), WR_VAR_Y(expected),
66  WR_VAR_F(actual), WR_VAR_X(actual), WR_VAR_Y(actual));
67  throw tut::failure(loc.msg(buf));
68  }
69 }
70 
71 #define ensure_var_equals(x, y) wreport::tests::_ensure_var_equals(wibble::tests::Location(__FILE__, __LINE__, #x " == " #y), (x), (y))
72 #define inner_ensure_var_equals(x, y) wreport::tests::_ensure_var_equals(wibble::tests::Location(loc, __FILE__, __LINE__, #x " == " #y), (x), (y))
73 static inline void _ensure_var_equals(const wibble::tests::Location& loc, const Var& var, int val)
74 {
75  inner_ensure_equals(var.enqi(), val);
76 }
77 static inline void _ensure_var_equals(const wibble::tests::Location& loc, const Var& var, double val)
78 {
79  inner_ensure_equals(var.enqd(), val);
80 }
81 static inline void _ensure_var_equals(const wibble::tests::Location& loc, const Var& var, const std::string& val)
82 {
83  inner_ensure_equals(std::string(var.enqc()), val);
84 }
85 
86 #define ensure_var_undef(x) wreport::tests::_ensure_var_undef(wibble::tests::Location(__FILE__, __LINE__, #x " is undef"), (x))
87 #define inner_ensure_var_undef(x) wreport::tests::_ensure_var_undef(wibble::tests::Location(loc, __FILE__, __LINE__, #x " is undef"), (x))
88 static inline void _ensure_var_undef(const wibble::tests::Location& loc, const Var& var)
89 {
90  inner_ensure_equals(var.value(), (const char*)0);
91 }
92 
94 class LocalEnv
95 {
97  std::string key;
99  std::string oldVal;
100 public:
105  LocalEnv(const std::string& key, const std::string& val)
106  : key(key)
107  {
108  const char* v = getenv(key.c_str());
109  oldVal = v == NULL ? "" : v;
110  setenv(key.c_str(), val.c_str(), 1);
111  }
112  ~LocalEnv()
113  {
114  setenv(key.c_str(), oldVal.c_str(), 1);
115  }
116 };
117 
118 } // namespace tests
119 } // namespace wreport
120 
121 // vim:set ts=4 sw=4: