2 +----------------------------------------------------------------------+
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"
31 ///////////////////////////////////////////////////////////////////////////////
35 virtual ~TestBase() {}
37 virtual bool RunTests(const std::string
&which
) = 0;
42 std::string error_messages
;
43 std::string test_name
;
46 bool Count(bool result
);
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
);
56 bool runTestImpl(T test
, const std::string
& which
, const std::string
& name
) {
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); };
72 } catch (const std::exception
& e
) {
73 fprintf(stderr
, "%s threw %s: '%s'\n",
74 name
.c_str(), typeid(e
).name(), e
.what());
77 fprintf(stderr
, "%s threw unknown object\n", name
.c_str());
87 explicit WithOption(bool& option
) :
88 m_option(&option
), m_save(option
) {
91 ~WithOption() { *m_option
= m_save
; }
97 typedef WithOption
<true> WithOpt
;
98 typedef WithOption
<false> WithNoOpt
;
100 ///////////////////////////////////////////////////////////////////////////////
103 #define RUN_TEST(test) do { \
104 if (!runTestImpl([=] { return test(); }, which, #test)) ret = false; \
107 #define LOG_TEST_ERROR(...) do { \
109 string_printf(msg, __VA_ARGS__); \
110 printf("%s\n", msg.c_str()); \
111 error_messages += "\n\n" + msg; \
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); \
125 if (!VerifySame(#e1, #e2, e1, e2)) { \
126 LOG_TEST_ERROR("%s:%d: VerifySame failed.", __FILE__, __LINE__); \
127 return Count(false); \
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_