libwreport  2.9
tests.h
1 /*
2  * wreport/tests - Unit test utilities
3  *
4  * Copyright (C) 2005--2013 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 #ifndef WREPORT_TESTS
22 #define WREPORT_TESTS
23 
24 #include <wibble/tests.h>
25 #include <wreport/varinfo.h>
26 #include <wreport/var.h>
27 #include <string>
28 #include <iostream>
29 #include <cstdlib>
30 #include <cstdio>
31 
32 namespace wreport {
33 namespace tests {
34 
35 #ifndef ensure_contains
36 #define ensure_contains(x, y) wreport::tests::impl_ensure_contains(wibble::tests::Location(__FILE__, __LINE__, #x " == " #y), (x), (y))
37 #define inner_ensure_contains(x, y) wreport::tests::impl_ensure_contains(wibble::tests::Location(loc, __FILE__, __LINE__, #x " == " #y), (x), (y))
38 static inline void impl_ensure_contains(const wibble::tests::Location& loc, const std::string& haystack, const std::string& needle)
39 {
40  if( haystack.find(needle) == std::string::npos )
41  {
42  std::stringstream ss;
43  ss << "'" << haystack << "' does not contain '" << needle << "'";
44  throw tut::failure(loc.msg(ss.str()));
45  }
46 }
47 
48 #define ensure_not_contains(x, y) arki::tests::impl_ensure_not_contains(wibble::tests::Location(__FILE__, __LINE__, #x " == " #y), (x), (y))
49 #define inner_ensure_not_contains(x, y) arki::tests::impl_ensure_not_contains(wibble::tests::Location(loc, __FILE__, __LINE__, #x " == " #y), (x), (y))
50 static inline void impl_ensure_not_contains(const wibble::tests::Location& loc, const std::string& haystack, const std::string& needle)
51 {
52  if( haystack.find(needle) != std::string::npos )
53  {
54  std::stringstream ss;
55  ss << "'" << haystack << "' must not contain '" << needle << "'";
56  throw tut::failure(loc.msg(ss.str()));
57  }
58 }
59 #endif
60 
61 #define ensure_varcode_equals(x, y) wreport::tests::_ensure_varcode_equals(wibble::tests::Location(__FILE__, __LINE__, #x " == " #y), (x), (y))
62 #define inner_ensure_varcode_equals(x, y) wreport::tests::_ensure_varcode_equals(wibble::tests::Location(loc, __FILE__, __LINE__, #x " == " #y), (x), (y))
63 static inline void _ensure_varcode_equals(const wibble::tests::Location& loc, Varcode actual, Varcode expected)
64 {
65  if( expected != actual )
66  {
67  char buf[40];
68  snprintf(buf, 40, "expected %01d%02d%03d actual %01d%02d%03d",
69  WR_VAR_F(expected), WR_VAR_X(expected), WR_VAR_Y(expected),
70  WR_VAR_F(actual), WR_VAR_X(actual), WR_VAR_Y(actual));
71  throw tut::failure(loc.msg(buf));
72  }
73 }
74 
75 #define ensure_var_equals(x, y) wreport::tests::_ensure_var_equals(wibble::tests::Location(__FILE__, __LINE__, #x " == " #y), (x), (y))
76 #define inner_ensure_var_equals(x, y) wreport::tests::_ensure_var_equals(wibble::tests::Location(loc, __FILE__, __LINE__, #x " == " #y), (x), (y))
77 static inline void _ensure_var_equals(const wibble::tests::Location& loc, const Var& var, int val)
78 {
79  inner_ensure_equals(var.enqi(), val);
80 }
81 static inline void _ensure_var_equals(const wibble::tests::Location& loc, const Var& var, double val)
82 {
83  inner_ensure_equals(var.enqd(), val);
84 }
85 static inline void _ensure_var_equals(const wibble::tests::Location& loc, const Var& var, const std::string& val)
86 {
87  inner_ensure_equals(std::string(var.enqc()), val);
88 }
89 
90 #define ensure_var_undef(x) wreport::tests::_ensure_var_undef(wibble::tests::Location(__FILE__, __LINE__, #x " is undef"), (x))
91 #define inner_ensure_var_undef(x) wreport::tests::_ensure_var_undef(wibble::tests::Location(loc, __FILE__, __LINE__, #x " is undef"), (x))
92 static inline void _ensure_var_undef(const wibble::tests::Location& loc, const Var& var)
93 {
94  inner_ensure_equals(var.value(), (const char*)0);
95 }
96 
98 class LocalEnv
99 {
101  std::string key;
103  std::string oldVal;
104 public:
109  LocalEnv(const std::string& key, const std::string& val)
110  : key(key)
111  {
112  const char* v = getenv(key.c_str());
113  oldVal = v == NULL ? "" : v;
114  setenv(key.c_str(), val.c_str(), 1);
115  }
116  ~LocalEnv()
117  {
118  setenv(key.c_str(), oldVal.c_str(), 1);
119  }
120 };
121 
122 #ifdef wassert
123 struct TestVarEqual
125 {
126  Var avar;
127  Var evar;
128  bool inverted;
129 
130  TestVarEqual(const Var& actual, const Var& expected, bool inverted=false) : avar(actual), evar(expected), inverted(inverted) {}
131  TestVarEqual operator!() { return TestVarEqual(avar, evar, !inverted); }
132 
133  void check(WIBBLE_TEST_LOCPRM) const;
134 };
135 
136 struct ActualVar : public wibble::tests::Actual<Var>
137 {
138  ActualVar(const Var& actual) : wibble::tests::Actual<Var>(actual) {}
139 
140  TestVarEqual operator==(const Var& expected) const { return TestVarEqual(actual, expected); }
141  TestVarEqual operator!=(const Var& expected) const { return TestVarEqual(actual, expected, true); }
142 };
143 #endif
144 
145 }
146 }
147 
148 #ifdef wassert
149 namespace wibble {
150 namespace tests {
151 
152 inline wreport::tests::ActualVar actual(const wreport::Var& actual) { return wreport::tests::ActualVar(actual); }
153 
154 }
155 }
156 #endif
157 
158 #endif