/* 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 #include "mozilla/DebugOnly.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 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 result = mBuffer.resizeUninitialized(aAmount); MOZ_ASSERT(result); } /** * Get a string view into the buffer, which is useful for test assertions. */ template std::basic_string_view get_string_view() { return std::basic_string_view(reinterpret_cast(data()), length()); } Vector mBuffer{}; }; } // namespace mozilla::intl #endif