libwreport 3.34
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
35namespace wreport {
36struct Var;
37
38namespace tests {
39
41std::string datafile(const std::string& fname);
42
48std::string slurpfile(const std::string& name);
49
53std::vector<std::string> all_test_files(const std::string& encoding);
54
55void track_bulletin(Bulletin& b, const char* tag, const char* fname);
56
57template<typename BULLETIN>
58std::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
73template<typename BULLETIN>
74std::unique_ptr<BULLETIN> decode_checked(const std::string& buf, const char* name, FILE* verbose)
75{
76 try {
77 return BULLETIN::decode_verbose(buf, verbose, name);
78 } catch (wreport::error_parse& e) {
79 try {
80 auto h = BULLETIN::decode_header(buf, name);
81 h->print_structured(stderr);
82 } catch (wreport::error& e) {
83 std::cerr << "Dump interrupted: " << e.what();
84 }
85 throw;
86 }
87}
88
89template<typename BULLETIN>
91{
92 std::string fname;
93 std::function<void(const BULLETIN&)> check_contents = [](const BULLETIN&) {};
94
95 TestCodec(const std::string& fname) : fname(fname) {}
96 virtual ~TestCodec() {}
97
98 void run();
99};
100
101void assert_var_equal(const Var& actual, const Var& expected);
102void assert_var_not_equal(const Var& actual, const Var& expected);
103template<typename Val>
104void assert_var_value_equal(const Var& actual, Val expected);
105template<typename Val>
106void assert_var_value_not_equal(const Var& actual, Val expected);
107
108
109struct ActualVar : public Actual<Var>
110{
111 ActualVar(const Var& actual) : Actual<Var>(actual) {}
112
113 void operator==(const Var& expected) const { assert_var_equal(_actual, expected); }
114 void operator!=(const Var& expected) const { assert_var_not_equal(_actual, expected); }
115 template<typename Val>
116 void operator==(Val expected) const { assert_var_value_equal(_actual, expected); }
117 template<typename Val>
118 void operator!=(Val expected) const { assert_var_value_not_equal(_actual, expected); }
119 void isset() const;
120 void isunset() const;
121};
122
123inline ActualVar actual(const wreport::Var& actual) { return ActualVar(actual); }
124
125struct ActualVarcode : public Actual<Varcode>
126{
127 using Actual::Actual;
128
129 void operator==(Varcode expected) const;
130 void operator!=(Varcode expected) const;
131};
132
133inline ActualVarcode actual_varcode(Varcode actual) { return ActualVarcode(actual); }
134
135}
136}
137
138#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
virtual const char * what() const noexcept=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:110
Definition: tests.h:126
Definition: utils/tests.h:318
Definition: tests.h:91
Implement fast access to information about WMO variables.