76 lines · c
1//===- TestUtilities.h ------------------------------------------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLDB_UNITTESTS_TESTINGSUPPORT_TESTUTILITIES_H10#define LLDB_UNITTESTS_TESTINGSUPPORT_TESTUTILITIES_H11 12#include "lldb/Core/ModuleSpec.h"13#include "lldb/Utility/DataBuffer.h"14#include "llvm/ADT/Twine.h"15#include "llvm/Support/Error.h"16#include "llvm/Support/FileSystem.h"17#include "llvm/Support/JSON.h"18#include "llvm/Support/raw_ostream.h"19#include <string>20 21#define ASSERT_NO_ERROR(x) \22 if (std::error_code ASSERT_NO_ERROR_ec = x) { \23 llvm::SmallString<128> MessageStorage; \24 llvm::raw_svector_ostream Message(MessageStorage); \25 Message << #x ": did not return errc::success.\n" \26 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \27 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \28 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \29 } else { \30 }31 32namespace lldb_private {33 34/// Returns a pretty printed json string of a `llvm::json::Value`.35std::string PrettyPrint(const llvm::json::Value &E);36 37std::string GetInputFilePath(const llvm::Twine &name);38 39class TestUtilities {40public:41 static std::once_flag g_debugger_initialize_flag;42};43 44class TestFile {45public:46 static llvm::Expected<TestFile> fromYaml(llvm::StringRef Yaml);47 static llvm::Expected<TestFile> fromYamlFile(const llvm::Twine &Name);48 49 ModuleSpec moduleSpec() {50 return ModuleSpec(FileSpec(), UUID(), dataBuffer());51 }52 53 llvm::Expected<llvm::sys::fs::TempFile> writeToTemporaryFile();54 55private:56 TestFile(std::string &&Buffer) : Buffer(std::move(Buffer)) {}57 58 lldb::DataBufferSP dataBuffer() {59 auto *Data = reinterpret_cast<const uint8_t *>(Buffer.data());60 return std::make_shared<DataBufferUnowned>(const_cast<uint8_t *>(Data),61 Buffer.size());62 }63 64 std::string Buffer;65};66 67template <typename T> static llvm::Expected<T> roundtripJSON(const T &input) {68 std::string encoded;69 llvm::raw_string_ostream OS(encoded);70 OS << toJSON(input);71 return llvm::json::parse<T>(encoded);72}73} // namespace lldb_private74 75#endif76