1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef intl_components_gtest_TestBuffer_h_
#define intl_components_gtest_TestBuffer_h_
#include <string_view>
#include "mozilla/DebugOnly.h"
#include "mozilla/Utf8.h"
#include "mozilla/Vector.h"
namespace mozilla::intl {
/**
* A test buffer for interfacing with unified intl classes.
* Closely resembles the FormatBuffer class, but without
* JavaScript-specific implementation details.
*/
template <typename C, size_t inlineCapacity = 0>
class TestBuffer {
public:
using CharType = C;
// Only allow moves, and not copies, as this class owns the mozilla::Vector.
TestBuffer(TestBuffer&& other) noexcept = default;
TestBuffer& operator=(TestBuffer&& other) noexcept = default;
explicit TestBuffer(const size_t aSize = 0) { reserve(aSize); }
/**
* Ensures the buffer has enough space to accommodate |aSize| elemtns.
*/
bool reserve(const size_t aSize) { return mBuffer.reserve(aSize); }
/**
* Returns the raw data inside the buffer.
*/
CharType* data() { return mBuffer.begin(); }
/**
* Returns the count of elements in written to the buffer.
*/
size_t length() const { return mBuffer.length(); }
/**
* Returns the buffer's overall capacity.
*/
size_t capacity() const { return mBuffer.capacity(); }
/**
* Resizes the buffer to the given amount of written elements.
* This is necessary because the buffer gets written to across
* FFI boundaries, so this needs to happen in a separate step.
*/
void written(size_t aAmount) {
MOZ_ASSERT(aAmount <= mBuffer.capacity());
mozilla::DebugOnly<bool> result = mBuffer.resizeUninitialized(aAmount);
MOZ_ASSERT(result);
}
/**
* Get a string view into the buffer, which is useful for test assertions.
*/
std::basic_string_view<CharType> get_string_view() {
return std::basic_string_view<CharType>(data(), length());
}
/**
* Clear the buffer, allowing it to be re-used.
*/
void clear() { mBuffer.clear(); }
/**
* A utility function to convert UTF-16 strings to UTF-8 strings so that they
* can be logged to stderr.
*/
static std::string toUtf8(mozilla::Span<const char16_t> input) {
size_t buff_len = input.Length() * 3;
std::string result(buff_len, ' ');
result.reserve(buff_len);
size_t result_len =
ConvertUtf16toUtf8(input, mozilla::Span(result.data(), buff_len));
result.resize(result_len);
return result;
}
/**
* String buffers, especially UTF-16, do not assert nicely, and are difficult
* to debug. This function is verbose in that it prints the buffer contents
* and expected contents to stderr when they do not match.
*
* Usage:
* ASSERT_TRUE(buffer.assertStringView(u"9/23/2002, 8:07:30 PM"));
*
* Here is what gtests output:
*
* Expected equality of these values:
* buffer.get_string_view()
* Which is: { '0' (48, 0x30), '9' (57, 0x39), '/' (47, 0x2F), ... }
* "9/23/2002, 8:07:30 PM"
* Which is: 0x11600afb9
*
* Here is what this method outputs:
*
* The buffer did not match:
* Buffer:
* u"9/23/2002, 8:07:30 PM"
* Expected:
* u"09/23/2002, 08:07:30 PM"
*/
[[nodiscard]] bool verboseMatches(const CharType* aExpected) {
std::basic_string_view<CharType> actualSV(data(), length());
std::basic_string_view<CharType> expectedSV(aExpected);
if (actualSV.compare(expectedSV) == 0) {
return true;
}
static_assert(std::is_same_v<CharType, char> ||
std::is_same_v<CharType, char16_t>);
std::string actual;
std::string expected;
const char* startQuote;
if constexpr (std::is_same_v<CharType, char>) {
actual = std::string(actualSV);
expected = std::string(expectedSV);
startQuote = "\"";
}
if constexpr (std::is_same_v<CharType, char16_t>) {
actual = toUtf8(actualSV);
expected = toUtf8(expectedSV);
startQuote = "u\"";
}
fprintf(stderr, "The buffer did not match:\n");
fprintf(stderr, " Buffer:\n %s%s\"\n", startQuote, actual.c_str());
fprintf(stderr, " Expected:\n %s%s\"\n", startQuote, expected.c_str());
return false;
}
Vector<C, inlineCapacity> mBuffer{};
};
} // namespace mozilla::intl
#endif
|