summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/unittests/cpp11-migrate/UniqueHeaderNameTest.cpp
blob: a2d70e03baa91485dcde9ca5d41e9bd422f9dd35 (plain)
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
//===- unittests/cpp11-migrate/UniqueHeaderNameTest.cpp -------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Test for the generateReplacementsFileName() in FileOverrides.h
//
//===----------------------------------------------------------------------===//

#include "gtest/gtest.h"
#include "Core/FileOverrides.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/system_error.h"

TEST(UniqueHeaderName, testUniqueHeaderName) {
  using namespace llvm::sys::path;

  llvm::SmallString<32> TmpDir;
  system_temp_directory(true, TmpDir);

  llvm::SmallString<128> SourceFile(TmpDir);
  append(SourceFile, "project/lib/feature.cpp");
  native(SourceFile.str().str(), SourceFile);

  llvm::SmallString<128> FullActualPath;
  llvm::SmallString<128> Error;
  bool Result =
      generateReplacementsFileName(SourceFile, FullActualPath, Error);

  ASSERT_TRUE(Result);
  EXPECT_TRUE(Error.empty());

  // We need to check the directory name and filename separately since on
  // Windows, the path separator is '\' which is a regex escape character.
  llvm::SmallString<128> ExpectedPath =
      llvm::sys::path::parent_path(SourceFile);
  llvm::SmallString<128> ActualPath =
      llvm::sys::path::parent_path(FullActualPath);
  llvm::SmallString<128> ActualName =
      llvm::sys::path::filename(FullActualPath);

  EXPECT_STREQ(ExpectedPath.c_str(), ActualPath.c_str());

  llvm::StringRef ExpectedName =
      "^feature.cpp_[0-9a-f]{2}_[0-9a-f]{2}_[0-9a-f]{2}_[0-9a-f]{2}_["
      "0-9a-f]{2}_[0-9a-f]{2}.yaml$";
  llvm::Regex R(ExpectedName);
  ASSERT_TRUE(R.match(ActualName))
      << "ExpectedName: " << ExpectedName.data()
      << "\nActualName: " << ActualName.c_str();
  ASSERT_TRUE(Error.empty()) << "Error: " << Error.c_str();
}
OpenPOWER on IntegriCloud