libwreport  3.6
tests.h
1 /*
2  * wreport/test-utils-wreport - Unit test utilities, not included in the library
3  *
4  * Copyright (C) 2005--2011 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_UTILS
22 #define WREPORT_TESTS_UTILS
23 
24 #include <wreport/utils/tests.h>
25 #include <wreport/varinfo.h>
26 #include <wreport/bulletin.h>
27 #include <wreport/tests.h>
28 #include <wreport/notes.h>
29 #include <string>
30 #include <vector>
31 #include <memory>
32 #include <iostream>
33 #include <cstdlib>
34 
35 namespace wreport {
36 struct Var;
37 
38 namespace tests {
39 
41 std::string datafile(const std::string& fname);
42 
48 std::string slurpfile(const std::string& name);
49 
53 std::vector<std::string> all_test_files(const std::string& encoding);
54 
55 void track_bulletin(Bulletin& b, const char* tag, const char* fname);
56 
57 template<typename BULLETIN>
58 std::unique_ptr<BULLETIN> decode_checked(const std::string& buf, const char* name)
59 {
60  try {
61  return BULLETIN::decode(buf, name);
62  } catch (wreport::error_parse& e) {
63  try {
64  auto h = BULLETIN::decode_header(buf, name);
65  h->print_structured(stderr);
66  } catch (wreport::error& e) {
67  std::cerr << "Dump interrupted: " << e.what();
68  }
69  throw;
70  }
71 }
72 
73 template<typename BULLETIN>
74 struct TestCodec
75 {
76  std::string fname;
77  std::function<void(const BULLETIN&)> check_contents = [](const BULLETIN&) {};
78 
79  TestCodec(const std::string& fname) : fname(fname) {}
80  virtual ~TestCodec() {}
81 
82  void run();
83 };
84 
85 void assert_var_equal(const Var& actual, const Var& expected);
86 void assert_var_not_equal(const Var& actual, const Var& expected);
87 template<typename Val>
88 void assert_var_value_equal(const Var& actual, Val expected);
89 template<typename Val>
90 void assert_var_value_not_equal(const Var& actual, Val expected);
91 
92 
93 struct ActualVar : public Actual<Var>
94 {
95  ActualVar(const Var& actual) : Actual<Var>(actual) {}
96 
97  void operator==(const Var& expected) const { assert_var_equal(_actual, expected); }
98  void operator!=(const Var& expected) const { assert_var_not_equal(_actual, expected); }
99  template<typename Val>
100  void operator==(Val expected) const { assert_var_value_equal(_actual, expected); }
101  template<typename Val>
102  void operator!=(Val expected) const { assert_var_value_not_equal(_actual, expected); }
103  void isset() const;
104  void isunset() const;
105 };
106 
107 inline ActualVar actual(const wreport::Var& actual) { return ActualVar(actual); }
108 
109 struct ActualVarcode : public Actual<Varcode>
110 {
111  using Actual::Actual;
112 
113  void operator==(Varcode expected) const;
114  void operator!=(Varcode expected) const;
115 };
116 
117 inline ActualVarcode actual_varcode(Varcode actual) { return ActualVarcode(actual); }
118 
119 }
120 }
121 
122 #endif
Definition: tests.h:74
Base class for DB-All.e exceptions.
Definition: error.h:59
A physical variable.
Definition: var.h:23
Definition: utils/tests.h:267
Report an error when parsing informations.
Definition: error.h:192
uint16_t Varcode
Holds the WMO variable code of a variable.
Definition: varinfo.h:57
Definition: tests.h:93
String functions.
Definition: benchmark.h:13
Implement fast access to information about WMO variables.
virtual const char * what() const noexcept=0
Error message.
Definition: tests.h:109