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
37namespace wreport {
38namespace tests {
39
41std::filesystem::path datafile(const std::filesystem::path& fname);
42
51std::filesystem::path path_from_env(const char* varname,
52 const char* deflt = nullptr);
53
59std::string slurpfile(const std::filesystem::path& name);
60
64std::vector<std::filesystem::path> all_test_files(const std::string& encoding);
65
72void dump_jsonl(const Bulletin& bulletin, std::stringstream& out);
73
74void track_bulletin(Bulletin& b, const char* tag,
75 const std::filesystem::path& fname);
76
77template <typename BULLETIN>
78std::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
100template <typename BULLETIN>
101std::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
123template <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
135void assert_var_equal(const Var& actual, const Var& expected);
136void assert_var_not_equal(const Var& actual, const Var& expected);
137template <typename Val>
138void assert_var_value_equal(const Var& actual, Val expected);
139template <typename Val>
140void assert_var_value_not_equal(const Var& actual, Val expected);
141
142struct 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
166inline ActualVar actual(const wreport::Var& actual)
167{
168 return ActualVar(actual);
169}
170
171struct ActualVarcode : public Actual<Varcode>
172{
173 using Actual::Actual;
174
175 void operator==(Varcode expected) const;
176 void operator!=(Varcode expected) const;
177};
178
179inline ActualVarcode actual_varcode(Varcode actual)
180{
181 return ActualVarcode(actual);
182}
183
184} // namespace tests
185} // namespace wreport
186
187#endif
A physical variable.
Definition var.h:25
Report an error when parsing informations.
Definition error.h:201
Base class for DB-All.e exceptions.
Definition error.h:61
const char * what() const noexcept override=0
Error message.
String functions.
Definition benchmark.h:13
uint16_t Varcode
Holds the WMO variable code of a variable.
Definition fwd.h:12
Definition tests.h:143
Definition tests.h:172
Definition utils/tests.h:339
Definition tests.h:124
Implement fast access to information about WMO variables.