libwreport  3.38
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 <filesystem>
30 #include <string>
31 #include <vector>
32 #include <memory>
33 #include <iostream>
34 #include <cstdlib>
35 
36 namespace wreport {
37 struct Var;
38 
39 namespace tests {
40 
42 std::filesystem::path datafile(const std::filesystem::path& fname);
43 
52 std::filesystem::path path_from_env(const char* varname, const char* deflt = nullptr);
53 
59 std::string slurpfile(const std::filesystem::path& name);
60 
64 std::vector<std::filesystem::path> all_test_files(const std::string& encoding);
65 
66 void track_bulletin(Bulletin& b, const char* tag, const std::filesystem::path& fname);
67 
68 template<typename BULLETIN>
69 std::unique_ptr<BULLETIN> decode_checked(const std::string& buf, const char* name)
70 {
71  try {
72  return BULLETIN::decode(buf, name);
73  } catch (wreport::error_parse& e) {
74  try {
75  auto h = BULLETIN::decode_header(buf, name);
76  h->print_structured(stderr);
77  } catch (wreport::error& e) {
78  std::cerr << "Dump interrupted: " << e.what();
79  }
80  throw;
81  }
82 }
83 
84 template<typename BULLETIN>
85 std::unique_ptr<BULLETIN> decode_checked(const std::string& buf, const char* name, FILE* verbose)
86 {
87  try {
88  return BULLETIN::decode_verbose(buf, verbose, name);
89  } catch (wreport::error_parse& e1) {
90  try {
91  auto h = BULLETIN::decode_header(buf, name);
92  h->print_structured(stderr);
93  } catch (wreport::error& e2) {
94  std::cerr << "Dump interrupted: " << e2.what();
95  }
96  throw;
97  }
98 }
99 
100 template<typename BULLETIN>
101 struct TestCodec
102 {
103  std::filesystem::path fname;
104  std::function<void(const BULLETIN&)> check_contents = [](const BULLETIN&) noexcept {};
105 
106  explicit TestCodec(const std::filesystem::path& fname) : fname(fname) {}
107  virtual ~TestCodec() {}
108 
109  void run();
110 };
111 
112 void assert_var_equal(const Var& actual, const Var& expected);
113 void assert_var_not_equal(const Var& actual, const Var& expected);
114 template<typename Val>
115 void assert_var_value_equal(const Var& actual, Val expected);
116 template<typename Val>
117 void assert_var_value_not_equal(const Var& actual, Val expected);
118 
119 
120 struct ActualVar : public Actual<Var>
121 {
122  explicit ActualVar(const Var& actual) : Actual<Var>(actual) {}
123 
124  void operator==(const Var& expected) const { assert_var_equal(_actual, expected); }
125  void operator!=(const Var& expected) const { assert_var_not_equal(_actual, expected); }
126  template<typename Val>
127  void operator==(Val expected) const { assert_var_value_equal(_actual, expected); }
128  template<typename Val>
129  void operator!=(Val expected) const { assert_var_value_not_equal(_actual, expected); }
130  void isset() const;
131  void isunset() const;
132 };
133 
134 inline ActualVar actual(const wreport::Var& actual) { return ActualVar(actual); }
135 
136 struct ActualVarcode : public Actual<Varcode>
137 {
138  using Actual::Actual;
139 
140  void operator==(Varcode expected) const;
141  void operator!=(Varcode expected) const;
142 };
143 
144 inline ActualVarcode actual_varcode(Varcode actual) { return ActualVarcode(actual); }
145 
146 }
147 }
148 
149 #endif
Definition: tests.h:101
Report an error when parsing informations.
Definition: error.h:201
A physical variable.
Definition: var.h:24
Definition: utils/tests.h:319
const char * what() const noexcept override=0
Error message.
uint16_t Varcode
Holds the WMO variable code of a variable.
Definition: fwd.h:12
Base class for DB-All.e exceptions.
Definition: error.h:59
Definition: tests.h:120
String functions.
Definition: benchmark.h:13
Implement fast access to information about WMO variables.
Definition: tests.h:136