0
0
mirror of https://github.com/OpenVPN/openvpn3.git synced 2024-09-20 12:12:15 +02:00

unittests: work around some limitations in googletest

Signed-off-by: James Yonan <james@openvpn.net>
This commit is contained in:
James Yonan 2020-07-18 17:51:57 -06:00 committed by David Sommerseth
parent 31ec17645a
commit 159a64e338
No known key found for this signature in database
GPG Key ID: 86CF944C9671FDF2

View File

@ -21,8 +21,9 @@
#pragma once
#include <openvpn/log/logbase.hpp>
#include <openvpn/random/mtrandapi.hpp>
#include <openvpn/common/exception.hpp>
#include <openvpn/common/hexstr.hpp>
#include <openvpn/random/mtrandapi.hpp>
#include <iostream>
#include <gtest/gtest.h>
@ -269,3 +270,44 @@ private:
unsigned char next;
};
// googletest is missing the ability to test for specific
// text inside a thrown exception, so we implement it here
#define JY_EXPECT_THROW(statement, expected_exception, expected_text) \
try { \
statement; \
OPENVPN_THROW_EXCEPTION("JY_EXPECT_THROW: no exception was thrown " << __FILE__ << ':' << __LINE__); \
} \
catch (const expected_exception& e) \
{ \
if (std::string(e.what()).find(expected_text) == std::string::npos) \
OPENVPN_THROW_EXCEPTION("JY_EXPECT_THROW: did not find expected text in exception at " << __FILE__ << ':' << __LINE__); \
}
// googletest ASSERT macros can't be used inside constructors
// or non-void-returning functions, so implement workaround here
#define JY_ASSERT_TRUE(value) \
{ \
if (!(value)) \
OPENVPN_THROW_EXCEPTION("JY_ASSERT_TRUE: failure at " << __FILE__ << ':' << __LINE__); \
}
#define JY_ASSERT_FALSE(value) \
{ \
if (value) \
OPENVPN_THROW_EXCEPTION("JY_ASSERT_FALSE: failure at " << __FILE__ << ':' << __LINE__); \
}
#define JY_ASSERT_EQ(v1, v2) \
{ \
if ((v1) != (v2)) \
OPENVPN_THROW_EXCEPTION("JY_ASSERT_EQ: failure at " << __FILE__ << ':' << __LINE__); \
}
#define JY_ASSERT_NE(v1, v2) \
{ \
if ((v1) == (v2)) \
OPENVPN_THROW_EXCEPTION("JY_ASSERT_NE: failure at " << __FILE__ << ':' << __LINE__); \
}