libwreport  3.40
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 <cstdlib>
25 #include <filesystem>
26 #include <iostream>
27 #include <memory>
28 #include <string>
29 #include <vector>
30 #include <wreport/bulletin.h>
31 #include <wreport/fwd.h>
32 #include <wreport/notes.h>
33 #include <wreport/tests.h>
34 #include <wreport/utils/tests.h>
35 #include <wreport/varinfo.h>
36 
37 namespace wreport {
38 namespace tests {
39 
41 std::filesystem::path datafile(const std::filesystem::path& fname);
42 
51 std::filesystem::path path_from_env(const char* varname,
52  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 
72 void dump_jsonl(const Bulletin& bulletin, std::stringstream& out);
73 
74 void track_bulletin(Bulletin& b, const char* tag,
75  const std::filesystem::path& fname);
76 
77 template <typename BULLETIN>
78 std::unique_ptr<BULLETIN> decode_checked(const std::string& buf,
79  const char* name)
80 {
81  try
82  {
83  return BULLETIN::decode(buf, name);
84  }
85  catch (wreport::error_parse& e)
86  {
87  try
88  {
89  auto h = BULLETIN::decode_header(buf, name);
90  h->print_structured(stderr);
91  }
92  catch (wreport::error& e)
93  {
94  std::cerr << "Dump interrupted: " << e.what();
95  }
96  throw;
97  }
98 }
99 
100 template <typename BULLETIN>
101 std::unique_ptr<BULLETIN> decode_checked(const std::string& buf,
102  const char* name, FILE* verbose)
103 {
104  try
105  {
106  return BULLETIN::decode_verbose(buf, verbose, name);
107  }
108  catch (wreport::error_parse& e1)
109  {
110  try
111  {
112  auto h = BULLETIN::decode_header(buf, name);
113  h->print_structured(stderr);
114  }
115  catch (wreport::error& e2)
116  {
117  std::cerr << "Dump interrupted: " << e2.what();
118  }
119  throw;
120  }
121 }
122 
123 template <typename BULLETIN> struct TestCodec
124 {
125  std::filesystem::path fname;
126  std::function<void(const BULLETIN&)> check_contents =
127  [](const BULLETIN&) noexcept {};
128 
129  explicit TestCodec(const std::filesystem::path& fname) : fname(fname) {}
130  virtual ~TestCodec() {}
131 
132  void run();
133 };
134 
135 void assert_var_equal(const Var& actual, const Var& expected);
136 void assert_var_not_equal(const Var& actual, const Var& expected);
137 template <typename Val>
138 void assert_var_value_equal(const Var& actual, Val expected);
139 template <typename Val>
140 void assert_var_value_not_equal(const Var& actual, Val expected);
141 
142 struct ActualVar : public Actual<Var>
143 {
144  explicit ActualVar(const Var& actual) : Actual<Var>(actual) {}
145 
146  void operator==(const Var& expected) const
147  {
148  assert_var_equal(_actual, expected);
149  }
150  void operator!=(const Var& expected) const
151  {
152  assert_var_not_equal(_actual, expected);
153  }
154  template <typename Val> void operator==(Val expected) const
155  {
156  assert_var_value_equal(_actual, expected);
157  }
158  template <typename Val> void operator!=(Val expected) const
159  {
160  assert_var_value_not_equal(_actual, expected);
161  }
162  void isset() const;
163  void isunset() const;
164 };
165 
166 inline ActualVar actual(const wreport::Var& actual)
167 {
168  return ActualVar(actual);
169 }
170 
171 struct ActualVarcode : public Actual<Varcode>
172 {
173  using Actual::Actual;
174 
175  void operator==(Varcode expected) const;
176  void operator!=(Varcode expected) const;
177 };
178 
179 inline ActualVarcode actual_varcode(Varcode actual)
180 {
181  return ActualVarcode(actual);
182 }
183 
184 } // namespace tests
185 } // namespace wreport
186 
187 #endif
Definition: tests.h:123
Report an error when parsing informations.
Definition: error.h:200
A physical variable.
Definition: var.h:24
Definition: utils/tests.h:338
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:60
Definition: tests.h:142
String functions.
Definition: benchmark.h:13
Implement fast access to information about WMO variables.
Definition: tests.h:171