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
36namespace wreport {
37struct Var;
38
39namespace tests {
40
42std::filesystem::path datafile(const std::filesystem::path& fname);
43
52std::filesystem::path path_from_env(const char* varname, 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
66void track_bulletin(Bulletin& b, const char* tag, const std::filesystem::path& fname);
67
68template<typename BULLETIN>
69std::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
84template<typename BULLETIN>
85std::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
100template<typename BULLETIN>
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
112void assert_var_equal(const Var& actual, const Var& expected);
113void assert_var_not_equal(const Var& actual, const Var& expected);
114template<typename Val>
115void assert_var_value_equal(const Var& actual, Val expected);
116template<typename Val>
117void assert_var_value_not_equal(const Var& actual, Val expected);
118
119
120struct 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
134inline ActualVar actual(const wreport::Var& actual) { return ActualVar(actual); }
135
136struct ActualVarcode : public Actual<Varcode>
137{
138 using Actual::Actual;
139
140 void operator==(Varcode expected) const;
141 void operator!=(Varcode expected) const;
142};
143
144inline ActualVarcode actual_varcode(Varcode actual) { return ActualVarcode(actual); }
145
146}
147}
148
149#endif
A physical variable.
Definition var.h:25
Report an error when parsing informations.
Definition error.h:202
Base class for DB-All.e exceptions.
Definition error.h:60
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:121
Definition tests.h:137
Definition utils/tests.h:320
Definition tests.h:102
Implement fast access to information about WMO variables.