codemod 2010-2016 to 2010-present
[hiphop-php.git] / hphp / test / ext / test_base.h
blob2260c36695a6d32fdc9e0d2edd9f65bff8f6b781
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_TEST_BASE_H_
18 #define incl_HPHP_TEST_BASE_H_
20 #include "hphp/compiler/hphp.h"
21 #include "hphp/runtime/base/type-conversions.h"
23 #include "hphp/test/ext/test.h"
24 #include "hphp/util/text-util.h"
26 #include <cassert>
27 #include <exception>
28 #include <string>
30 using namespace HPHP;
31 ///////////////////////////////////////////////////////////////////////////////
33 struct TestBase {
34 TestBase();
35 virtual ~TestBase() {}
37 virtual bool RunTests(const std::string &which) = 0;
39 int fail_count;
40 int skip_count;
41 int pass_count;
42 std::string error_messages;
43 std::string test_name;
45 protected:
46 bool Count(bool result);
47 bool CountSkip();
49 bool VerifySame(const char *exp1, const char *exp2,
50 const Variant& v1, const Variant& v2);
51 bool VerifyClose(const char *exp1, const char *exp2,
52 double v1, double v2);
53 bool array_value_exists(const Variant& var, const Variant& value);
55 template<class T>
56 bool runTestImpl(T test, const std::string& which, const std::string& name) {
57 bool ret = true;
58 auto pass = [&] { printf("%s passed\n", name.c_str()); };
59 auto fail = [&] { printf("%s failed\n", name.c_str()); ret = false; };
61 if (which.empty() || which == name) {
62 SCOPE_EXIT { fflush(nullptr); };
63 test_name = name;
64 try {
65 if (test()) {
66 if (!Test::s_quiet) {
67 pass();
69 } else {
70 fail();
72 } catch (const std::exception& e) {
73 fprintf(stderr, "%s threw %s: '%s'\n",
74 name.c_str(), typeid(e).name(), e.what());
75 fail();
76 } catch (...) {
77 fprintf(stderr, "%s threw unknown object\n", name.c_str());
78 fail();
81 return ret;
85 template <bool value>
86 struct WithOption {
87 explicit WithOption(bool& option) :
88 m_option(&option), m_save(option) {
89 option = value;
91 ~WithOption() { *m_option = m_save; }
92 private:
93 bool *m_option;
94 const bool m_save;
97 typedef WithOption<true> WithOpt;
98 typedef WithOption<false> WithNoOpt;
100 ///////////////////////////////////////////////////////////////////////////////
101 // macros
103 #define RUN_TEST(test) do { \
104 if (!runTestImpl([=] { return test(); }, which, #test)) ret = false; \
105 } while(false)
107 #define LOG_TEST_ERROR(...) do { \
108 std::string msg; \
109 string_printf(msg, __VA_ARGS__); \
110 printf("%s\n", msg.c_str()); \
111 error_messages += "\n\n" + msg; \
112 } while (false)
114 #define SKIP(reason) \
115 LOG_TEST_ERROR("%s skipped [%s]", __FUNCTION__, #reason); \
116 return CountSkip(); \
118 #define VERIFY(exp) \
119 if (!toBoolean(exp)) { \
120 LOG_TEST_ERROR("%s:%d: [%s] is false", __FILE__, __LINE__, #exp); \
121 return Count(false); \
124 #define VS(e1, e2) \
125 if (!VerifySame(#e1, #e2, e1, e2)) { \
126 LOG_TEST_ERROR("%s:%d: VerifySame failed.", __FILE__, __LINE__); \
127 return Count(false); \
130 #define VC(e1, e2) \
131 if (!VerifyClose(#e1, #e2, e1, e2)) { \
132 LOG_TEST_ERROR("%s:%d: VerifyClose failed.", __FILE__, __LINE__); \
133 return Count(false); \
136 ///////////////////////////////////////////////////////////////////////////////
138 #endif // incl_HPHP_TEST_BASE_H_