TianoCore EDK2 master
Loading...
Searching...
No Matches
GoogleTestLib.h
Go to the documentation of this file.
1
9#ifndef GOOGLE_TEST_LIB_H_
10#define GOOGLE_TEST_LIB_H_
11
12#include <gtest/gtest.h>
13#include <gmock/gmock.h>
14#include <cstring>
15
16using ::testing::Throws;
17using ::testing::ThrowsMessage;
18using ::testing::HasSubstr;
19
20//
21// Extended macros for testing exceptions with a specific description string
22// in the exception message. Typically used to check that the expression
23// that generates an ASSERT() matches the expected expression.
24//
25#define EXPECT_THROW_MESSAGE(statement, description) \
26 EXPECT_THAT ( \
27 []() { statement; }, \
28 ThrowsMessage<std::runtime_error>(HasSubstr (description)) \
29 )
30#define ASSERT_THROW_MESSAGE(statement, description) \
31 ASSERT_THAT ( \
32 []() { statement; }, \
33 ThrowsMessage<std::runtime_error>(HasSubstr (description)) \
34 )
35
36extern "C" {
37 #include <Uefi.h>
38}
39
41// Below are the action extensions to GoogleTest and gmock for EDK2 types.
42// These actions are intended to be used in EXPECT_CALL (and related gmock
43// macros) to support assignments to output arguments in the expected call.
44//
45
46// Action to support pointer types to a buffer (such as UINT8* or VOID*)
47ACTION_TEMPLATE (
48 SetArgBuffer,
49 HAS_1_TEMPLATE_PARAMS (size_t, ArgNum),
50 AND_2_VALUE_PARAMS (Buffer, ByteSize)
51 ) {
52 auto ArgBuffer = std::get<ArgNum>(args);
53
54 std::memcpy (ArgBuffer, Buffer, ByteSize);
55}
56
58// Below are the matcher extensions to GoogleTest and gmock for EDK2 types.
59// These matchers are intended to be used in EXPECT_CALL (and related gmock
60// macros) to support comparisons to input arguments in the expected call.
61//
62// Note that these matchers can also be used in the EXPECT_THAT or ASSERT_THAT
63// macros to compare whether two values are equal.
64//
65
66// Matcher to support pointer types to a buffer (such as UINT8* or VOID* or
67// any structure pointer)
68MATCHER_P2 (
69 BufferEq,
70 Buffer,
71 ByteSize,
72 std::string ("buffer data to ") + (negation ? "not " : "") + "be the same"
73 ) {
74 UINT8 *Actual = (UINT8 *)arg;
75 UINT8 *Expected = (UINT8 *)Buffer;
76
77 for (size_t i = 0; i < ByteSize; i++) {
78 if (Actual[i] != Expected[i]) {
79 *result_listener << "byte at offset " << i
80 << " does not match expected. [" << std::hex
81 << "Actual: 0x" << std::setw (2) << std::setfill ('0')
82 << (unsigned int)Actual[i] << ", "
83 << "Expected: 0x" << std::setw (2) << std::setfill ('0')
84 << (unsigned int)Expected[i] << "]";
85 return false;
86 }
87 }
88
89 *result_listener << "all bytes match";
90 return true;
91}
92
93// Matcher to support CHAR16* type
94MATCHER_P (
95 Char16StrEq,
96 String,
97 std::string ("strings to ") + (negation ? "not " : "") + "be the same"
98 ) {
99 CHAR16 *Actual = (CHAR16 *)arg;
100 CHAR16 *Expected = (CHAR16 *)String;
101
102 for (size_t i = 0; Actual[i] != 0; i++) {
103 if (Actual[i] != Expected[i]) {
104 *result_listener << "character at offset " << i
105 << " does not match expected. [" << std::hex
106 << "Actual: 0x" << std::setw (4) << std::setfill ('0')
107 << Actual[i];
108
109 if (std::isprint (Actual[i])) {
110 *result_listener << " ('" << (char)Actual[i] << "')";
111 }
112
113 *result_listener << ", Expected: 0x" << std::setw (4) << std::setfill ('0')
114 << Expected[i];
115
116 if (std::isprint (Expected[i])) {
117 *result_listener << " ('" << (char)Expected[i] << "')";
118 }
119
120 *result_listener << "]";
121
122 return false;
123 }
124 }
125
126 *result_listener << "strings match";
127 return true;
128}
129
130#endif