r370825 - [libclang][test][NFC] Split off fixture from tests.
Jan Korous via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 3 15:01:47 PDT 2019
Author: jkorous
Date: Tue Sep 3 15:01:46 2019
New Revision: 370825
URL: http://llvm.org/viewvc/llvm-project?rev=370825&view=rev
Log:
[libclang][test][NFC] Split off fixture from tests.
Added:
cfe/trunk/unittests/libclang/TestUtils.h
Modified:
cfe/trunk/unittests/libclang/LibclangTest.cpp
Modified: cfe/trunk/unittests/libclang/LibclangTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/libclang/LibclangTest.cpp?rev=370825&r1=370824&r2=370825&view=diff
==============================================================================
--- cfe/trunk/unittests/libclang/LibclangTest.cpp (original)
+++ cfe/trunk/unittests/libclang/LibclangTest.cpp Tue Sep 3 15:01:46 2019
@@ -13,6 +13,7 @@
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
+#include "TestUtils.h"
#include <fstream>
#include <functional>
#include <map>
@@ -352,77 +353,6 @@ TEST(libclang, ModuleMapDescriptor) {
clang_ModuleMapDescriptor_dispose(MMD);
}
-class LibclangParseTest : public ::testing::Test {
- std::set<std::string> Files;
- typedef std::unique_ptr<std::string> fixed_addr_string;
- std::map<fixed_addr_string, fixed_addr_string> UnsavedFileContents;
-public:
- std::string TestDir;
- CXIndex Index;
- CXTranslationUnit ClangTU;
- unsigned TUFlags;
- std::vector<CXUnsavedFile> UnsavedFiles;
-
- void SetUp() override {
- llvm::SmallString<256> Dir;
- ASSERT_FALSE(llvm::sys::fs::createUniqueDirectory("libclang-test", Dir));
- TestDir = Dir.str();
- TUFlags = CXTranslationUnit_DetailedPreprocessingRecord |
- clang_defaultEditingTranslationUnitOptions();
- Index = clang_createIndex(0, 0);
- ClangTU = nullptr;
- }
- void TearDown() override {
- clang_disposeTranslationUnit(ClangTU);
- clang_disposeIndex(Index);
- for (const std::string &Path : Files)
- llvm::sys::fs::remove(Path);
- llvm::sys::fs::remove(TestDir);
- }
- void WriteFile(std::string &Filename, const std::string &Contents) {
- if (!llvm::sys::path::is_absolute(Filename)) {
- llvm::SmallString<256> Path(TestDir);
- llvm::sys::path::append(Path, Filename);
- Filename = Path.str();
- Files.insert(Filename);
- }
- llvm::sys::fs::create_directories(llvm::sys::path::parent_path(Filename));
- std::ofstream OS(Filename);
- OS << Contents;
- assert(OS.good());
- }
- void MapUnsavedFile(std::string Filename, const std::string &Contents) {
- if (!llvm::sys::path::is_absolute(Filename)) {
- llvm::SmallString<256> Path(TestDir);
- llvm::sys::path::append(Path, Filename);
- Filename = Path.str();
- }
- auto it = UnsavedFileContents.insert(std::make_pair(
- fixed_addr_string(new std::string(Filename)),
- fixed_addr_string(new std::string(Contents))));
- UnsavedFiles.push_back({
- it.first->first->c_str(), // filename
- it.first->second->c_str(), // contents
- it.first->second->size() // length
- });
- }
- template<typename F>
- void Traverse(const F &TraversalFunctor) {
- CXCursor TuCursor = clang_getTranslationUnitCursor(ClangTU);
- std::reference_wrapper<const F> FunctorRef = std::cref(TraversalFunctor);
- clang_visitChildren(TuCursor,
- &TraverseStateless<std::reference_wrapper<const F>>,
- &FunctorRef);
- }
-private:
- template<typename TState>
- static CXChildVisitResult TraverseStateless(CXCursor cx, CXCursor parent,
- CXClientData data) {
- TState *State = static_cast<TState*>(data);
- return State->get()(cx, parent);
- }
-};
-
TEST_F(LibclangParseTest, AllSkippedRanges) {
std::string Header = "header.h", Main = "main.cpp";
WriteFile(Header,
Added: cfe/trunk/unittests/libclang/TestUtils.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/libclang/TestUtils.h?rev=370825&view=auto
==============================================================================
--- cfe/trunk/unittests/libclang/TestUtils.h (added)
+++ cfe/trunk/unittests/libclang/TestUtils.h Tue Sep 3 15:01:46 2019
@@ -0,0 +1,94 @@
+//===- unittests/libclang/TestUtils.h -------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TEST_TESTUTILS_H
+#define LLVM_CLANG_TEST_TESTUTILS_H
+
+#include "clang-c/Index.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Path.h"
+
+#include <fstream>
+#include <memory>
+#include <string>
+#include <vector>
+#include "gtest/gtest.h"
+
+class LibclangParseTest : public ::testing::Test {
+ std::set<std::string> Files;
+ typedef std::unique_ptr<std::string> fixed_addr_string;
+ std::map<fixed_addr_string, fixed_addr_string> UnsavedFileContents;
+public:
+ std::string TestDir;
+ CXIndex Index;
+ CXTranslationUnit ClangTU;
+ unsigned TUFlags;
+ std::vector<CXUnsavedFile> UnsavedFiles;
+
+ void SetUp() override {
+ llvm::SmallString<256> Dir;
+ ASSERT_FALSE(llvm::sys::fs::createUniqueDirectory("libclang-test", Dir));
+ TestDir = Dir.str();
+ TUFlags = CXTranslationUnit_DetailedPreprocessingRecord |
+ clang_defaultEditingTranslationUnitOptions();
+ Index = clang_createIndex(0, 0);
+ ClangTU = nullptr;
+ }
+ void TearDown() override {
+ clang_disposeTranslationUnit(ClangTU);
+ clang_disposeIndex(Index);
+ for (const std::string &Path : Files)
+ llvm::sys::fs::remove(Path);
+ llvm::sys::fs::remove(TestDir);
+ }
+ void WriteFile(std::string &Filename, const std::string &Contents) {
+ if (!llvm::sys::path::is_absolute(Filename)) {
+ llvm::SmallString<256> Path(TestDir);
+ llvm::sys::path::append(Path, Filename);
+ Filename = Path.str();
+ Files.insert(Filename);
+ }
+ llvm::sys::fs::create_directories(llvm::sys::path::parent_path(Filename));
+ std::ofstream OS(Filename);
+ OS << Contents;
+ assert(OS.good());
+ }
+ void MapUnsavedFile(std::string Filename, const std::string &Contents) {
+ if (!llvm::sys::path::is_absolute(Filename)) {
+ llvm::SmallString<256> Path(TestDir);
+ llvm::sys::path::append(Path, Filename);
+ Filename = Path.str();
+ }
+ auto it = UnsavedFileContents.insert(std::make_pair(
+ fixed_addr_string(new std::string(Filename)),
+ fixed_addr_string(new std::string(Contents))));
+ UnsavedFiles.push_back({
+ it.first->first->c_str(), // filename
+ it.first->second->c_str(), // contents
+ it.first->second->size() // length
+ });
+ }
+ template<typename F>
+ void Traverse(const F &TraversalFunctor) {
+ CXCursor TuCursor = clang_getTranslationUnitCursor(ClangTU);
+ std::reference_wrapper<const F> FunctorRef = std::cref(TraversalFunctor);
+ clang_visitChildren(TuCursor,
+ &TraverseStateless<std::reference_wrapper<const F>>,
+ &FunctorRef);
+ }
+private:
+ template<typename TState>
+ static CXChildVisitResult TraverseStateless(CXCursor cx, CXCursor parent,
+ CXClientData data) {
+ TState *State = static_cast<TState*>(data);
+ return State->get()(cx, parent);
+ }
+};
+
+#endif // LLVM_CLANG_TEST_TESTUTILS_H
\ No newline at end of file
More information about the cfe-commits
mailing list