xref: /netbsd-src/external/bsd/atf/dist/atf-c++/tests.hpp (revision d877c4c3c02304002c0642d7f34a58d07138d6a9)
1 //
2 // Automated Testing Framework (atf)
3 //
4 // Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 // 1. Redistributions of source code must retain the above copyright
11 //    notice, this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 //    notice, this list of conditions and the following disclaimer in the
14 //    documentation and/or other materials provided with the distribution.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 #if !defined(_ATF_CXX_TESTS_HPP_)
31 #define _ATF_CXX_TESTS_HPP_
32 
33 #include <map>
34 #include <string>
35 
36 extern "C" {
37 #include <atf-c/tc.h>
38 #include <atf-c/tcr.h>
39 }
40 
41 #include <atf-c++/fs.hpp>
42 #include <atf-c++/utils.hpp>
43 
44 namespace atf {
45 namespace tests {
46 
47 // ------------------------------------------------------------------------
48 // The "vars_map" class.
49 // ------------------------------------------------------------------------
50 
51 typedef std::map< std::string, std::string > vars_map;
52 
53 // ------------------------------------------------------------------------
54 // The "tcr" class.
55 // ------------------------------------------------------------------------
56 
57 //!
58 //! \brief Holds the results of a test case's execution.
59 //!
60 //! The tcr class holds the information that describes the results of a
61 //! test case's execution.  This is composed of an exit code and a reason
62 //! for that exit code.
63 //!
64 //! TODO: Complete documentation for this class.  Not done yet because it
65 //! is worth to investigate if this class could be rewritten as several
66 //! different classes, one for each status.
67 //!
68 class tcr {
69     atf_tcr_t m_tcr;
70 
71 public:
72     typedef atf_tcr_state_t state;
73 
74     static const state passed_state;
75     static const state failed_state;
76     static const state skipped_state;
77 
78     tcr(state);
79     tcr(state, const std::string&);
80     tcr(const tcr&);
81     ~tcr(void);
82 
83     state get_state(void) const;
84     const std::string get_reason(void) const;
85 
86     tcr& operator=(const tcr&);
87 };
88 
89 // ------------------------------------------------------------------------
90 // The "tc" class.
91 // ------------------------------------------------------------------------
92 
93 class tc : public atf::utils::noncopyable {
94     std::string m_ident;
95     atf_map_t m_config;
96     atf_tc_t m_tc;
97 
98 protected:
99     virtual void head(void) = 0;
100     virtual void body(void) const = 0;
101     virtual void cleanup(void) const;
102 
103     void require_prog(const std::string&) const;
104 
105     static void wrap_head(atf_tc_t *);
106     static void wrap_body(const atf_tc_t *);
107     static void wrap_cleanup(const atf_tc_t *);
108 
109 public:
110     tc(const std::string&);
111     virtual ~tc(void);
112 
113     void init(const vars_map&);
114 
115     const std::string get_config_var(const std::string&) const;
116     const std::string get_config_var(const std::string&, const std::string&)
117         const;
118     const std::string get_md_var(const std::string&) const;
119     bool has_config_var(const std::string&) const;
120     bool has_md_var(const std::string&) const;
121     void set_md_var(const std::string&, const std::string&);
122 
123     tcr run(const fs::path&) const;
124 
125     /* To be called from the child process only. */
126     static void pass(void);
127     static void fail(const std::string&);
128     static void skip(const std::string&);
129 };
130 
131 } // namespace tests
132 } // namespace atf
133 
134 #endif // !defined(_ATF_CXX_TESTS_HPP_)
135