summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/unittests/clang-move/ClangMoveTests.cpp
blob: 97c7ce0759d7a7e0b929b21d3c858164ce862dfa (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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
//===-- ClangMoveTest.cpp - clang-move unit tests -------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "ClangMove.h"
#include "unittests/Tooling/RewriterTestContext.h"
#include "clang/Format/Format.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Tooling/Refactoring.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/ADT/StringRef.h"
#include "gmock/gmock-matchers.h"
#include "gtest/gtest.h"
#include <string>
#include <vector>

namespace clang {
namespace move {
namespace {

const char TestHeader[] = "namespace a {\n"
                          "class C1; // test\n"
                          "template <typename T> class C2;\n"
                          "namespace b {\n"
                          "// This is a Foo class\n"
                          "// which is used in\n"
                          "// test.\n"
                          "class Foo {\n"
                          "public:\n"
                          "  void f();\n"
                          "\n"
                          "private:\n"
                          "  C1 *c1;\n"
                          "  static int b;\n"
                          "}; // abc\n"
                          "\n"
                          "class Foo2 {\n"
                          "public:\n"
                          "  int f();\n"
                          "};\n"
                          "} // namespace b\n"
                          "} // namespace a\n";

const char TestCC[] = "#include \"foo.h\"\n"
                      "namespace a {\n"
                      "namespace b {\n"
                      "namespace {\n"
                      "// comment1.\n"
                      "void f1() {}\n"
                      "/// comment2.\n"
                      "int kConstInt1 = 0;\n"
                      "} // namespace\n"
                      "\n"
                      "/* comment 3*/\n"
                      "static int kConstInt2 = 1;\n"
                      "\n"
                      "/** comment4\n"
                      " */\n"
                      "static int help() {\n"
                      "  int a = 0;\n"
                      "  return a;\n"
                      "}\n"
                      "\n"
                      "// comment5\n"
                      "// comment5\n"
                      "void Foo::f() {\n"
                      "  f1();\n"
                      "  kConstInt1;\n"
                      "  kConstInt2;\n"
                      "  help();\n"
                      "}\n"
                      "\n"
                      "/////////////\n"
                      "// comment //\n"
                      "/////////////\n"
                      "int Foo::b = 2;\n"
                      "int Foo2::f() {\n"
                      "  kConstInt1;\n"
                      "  kConstInt2;\n"
                      "  help();\n"
                      "  f1();\n"
                      "  return 1;\n"
                      "}\n"
                      "} // namespace b\n"
                      "} // namespace a\n";

const char ExpectedTestHeader[] = "namespace a {\n"
                                  "class C1; // test\n"
                                  "template <typename T> class C2;\n"
                                  "namespace b {\n"
                                  "\n"
                                  "class Foo2 {\n"
                                  "public:\n"
                                  "  int f();\n"
                                  "};\n"
                                  "} // namespace b\n"
                                  "} // namespace a\n";

const char ExpectedTestCC[] = "#include \"foo.h\"\n"
                              "namespace a {\n"
                              "namespace b {\n"
                              "namespace {\n"
                              "// comment1.\n"
                              "void f1() {}\n"
                              "/// comment2.\n"
                              "int kConstInt1 = 0;\n"
                              "} // namespace\n"
                              "\n"
                              "/* comment 3*/\n"
                              "static int kConstInt2 = 1;\n"
                              "\n"
                              "/** comment4\n"
                              " */\n"
                              "static int help() {\n"
                              "  int a = 0;\n"
                              "  return a;\n"
                              "}\n"
                              "\n"
                              "int Foo2::f() {\n"
                              "  kConstInt1;\n"
                              "  kConstInt2;\n"
                              "  help();\n"
                              "  f1();\n"
                              "  return 1;\n"
                              "}\n"
                              "} // namespace b\n"
                              "} // namespace a\n";

const char ExpectedNewHeader[] = "#ifndef NEW_FOO_H\n"
                                 "#define NEW_FOO_H\n"
                                 "\n"
                                 "namespace a {\n"
                                 "class C1; // test\n"
                                 "\n"
                                 "template <typename T> class C2;\n"
                                 "namespace b {\n"
                                 "// This is a Foo class\n"
                                 "// which is used in\n"
                                 "// test.\n"
                                 "class Foo {\n"
                                 "public:\n"
                                 "  void f();\n"
                                 "\n"
                                 "private:\n"
                                 "  C1 *c1;\n"
                                 "  static int b;\n"
                                 "}; // abc\n"
                                 "} // namespace b\n"
                                 "} // namespace a\n"
                                 "\n"
                                 "#endif // NEW_FOO_H\n";

const char ExpectedNewCC[] = "namespace a {\n"
                             "namespace b {\n"
                             "namespace {\n"
                             "// comment1.\n"
                             "void f1() {}\n"
                             "\n"
                             "/// comment2.\n"
                             "int kConstInt1 = 0;\n"
                             "} // namespace\n"
                             "\n"
                             "/* comment 3*/\n"
                             "static int kConstInt2 = 1;\n"
                             "\n"
                             "/** comment4\n"
                             " */\n"
                             "static int help() {\n"
                             "  int a = 0;\n"
                             "  return a;\n"
                             "}\n"
                             "\n"
                             "// comment5\n"
                             "// comment5\n"
                             "void Foo::f() {\n"
                             "  f1();\n"
                             "  kConstInt1;\n"
                             "  kConstInt2;\n"
                             "  help();\n"
                             "}\n"
                             "\n"
                             "/////////////\n"
                             "// comment //\n"
                             "/////////////\n"
                             "int Foo::b = 2;\n"
                             "} // namespace b\n"
                             "} // namespace a\n";

#ifdef _WIN32
const char WorkingDir[] = "C:\\test";
#else
const char WorkingDir[] = "/test";
#endif

const char TestHeaderName[] = "foo.h";
const char TestCCName[] = "foo.cc";

std::map<std::string, std::string>
runClangMoveOnCode(const move::MoveDefinitionSpec &Spec,
                   const char *const Header = TestHeader,
                   const char *const CC = TestCC,
                   DeclarationReporter *const Reporter = nullptr) {
  clang::RewriterTestContext Context;

  Context.InMemoryFileSystem->setCurrentWorkingDirectory(WorkingDir);

  std::map<llvm::StringRef, clang::FileID> FileToFileID;

  auto CreateFiles = [&Context, &FileToFileID](llvm::StringRef Name,
                                               llvm::StringRef Code) {
    if (!Name.empty()) {
      FileToFileID[Name] = Context.createInMemoryFile(Name, Code);
    }
  };
  CreateFiles(Spec.NewCC, "");
  CreateFiles(Spec.NewHeader, "");
  CreateFiles(TestHeaderName, Header);
  CreateFiles(TestCCName, CC);

  std::map<std::string, tooling::Replacements> FileToReplacements;
  ClangMoveContext MoveContext = {Spec, FileToReplacements, WorkingDir, "LLVM",
                                  Reporter != nullptr};

  auto Factory = llvm::make_unique<clang::move::ClangMoveActionFactory>(
      &MoveContext, Reporter);

 // std::string IncludeArg = Twine("-I" + WorkingDir;
  tooling::runToolOnCodeWithArgs(
      Factory->create(), CC, Context.InMemoryFileSystem,
      {"-std=c++11", "-fparse-all-comments", "-I."}, TestCCName, "clang-move",
      std::make_shared<PCHContainerOperations>());
  formatAndApplyAllReplacements(FileToReplacements, Context.Rewrite, "llvm");
  // The Key is file name, value is the new code after moving the class.
  std::map<std::string, std::string> Results;
  for (const auto &It : FileToReplacements) {
    // The path may come out as "./foo.h", normalize to "foo.h".
    SmallString<32> FilePath (It.first);
    llvm::sys::path::remove_dots(FilePath);
    Results[FilePath.str().str()] = Context.getRewrittenText(FileToFileID[FilePath]);
  }
  return Results;
}

TEST(ClangMove, MoveHeaderAndCC) {
  move::MoveDefinitionSpec Spec;
  Spec.Names = {std::string("a::b::Foo")};
  Spec.OldHeader = "foo.h";
  Spec.OldCC = "foo.cc";
  Spec.NewHeader = "new_foo.h";
  Spec.NewCC = "new_foo.cc";
  std::string ExpectedHeader = "#include \"" + Spec.NewHeader + "\"\n\n";
  auto Results = runClangMoveOnCode(Spec);
  EXPECT_EQ(ExpectedTestHeader, Results[Spec.OldHeader]);
  EXPECT_EQ(ExpectedTestCC, Results[Spec.OldCC]);
  EXPECT_EQ(ExpectedNewHeader, Results[Spec.NewHeader]);
  EXPECT_EQ(ExpectedHeader + ExpectedNewCC, Results[Spec.NewCC]);
}

TEST(ClangMove, MoveHeaderOnly) {
  move::MoveDefinitionSpec Spec;
  Spec.Names = {std::string("a::b::Foo")};
  Spec.OldHeader = "foo.h";
  Spec.NewHeader = "new_foo.h";
  auto Results = runClangMoveOnCode(Spec);
  EXPECT_EQ(2u, Results.size());
  EXPECT_EQ(ExpectedTestHeader, Results[Spec.OldHeader]);
  EXPECT_EQ(ExpectedNewHeader, Results[Spec.NewHeader]);
}

TEST(ClangMove, MoveCCOnly) {
  move::MoveDefinitionSpec Spec;
  Spec.Names = {std::string("a::b::Foo")};
  Spec.OldCC = "foo.cc";
  Spec.NewCC = "new_foo.cc";
  std::string ExpectedHeader = "#include \"foo.h\"\n\n";
  auto Results = runClangMoveOnCode(Spec);
  EXPECT_EQ(2u, Results.size());
  EXPECT_EQ(ExpectedTestCC, Results[Spec.OldCC]);
  EXPECT_EQ(ExpectedHeader + ExpectedNewCC, Results[Spec.NewCC]);
}

TEST(ClangMove, MoveNonExistClass) {
  move::MoveDefinitionSpec Spec;
  Spec.Names = {std::string("NonExistFoo")};
  Spec.OldHeader = "foo.h";
  Spec.OldCC = "foo.cc";
  Spec.NewHeader = "new_foo.h";
  Spec.NewCC = "new_foo.cc";
  auto Results = runClangMoveOnCode(Spec);
  EXPECT_EQ(0u, Results.size());
}

TEST(ClangMove, HeaderIncludeSelf) {
  move::MoveDefinitionSpec Spec;
  Spec.Names = {std::string("Foo")};
  Spec.OldHeader = "foo.h";
  Spec.OldCC = "foo.cc";
  Spec.NewHeader = "new_foo.h";
  Spec.NewCC = "new_foo.cc";

  const char TestHeader[] = "#ifndef FOO_H\n"
                            "#define FOO_H\n"
                            "#include \"foo.h\"\n"
                            "class Foo {};\n"
                            "#endif\n";
  const char TestCode[] = "#include \"foo.h\"";
  const char ExpectedNewHeader[] = "#ifndef FOO_H\n"
                                   "#define FOO_H\n"
                                   "#include \"new_foo.h\"\n"
                                   "class Foo {};\n"
                                   "#endif\n";
  const char ExpectedNewCC[] = "#include \"new_foo.h\"";
  auto Results = runClangMoveOnCode(Spec, TestHeader, TestCode);
  EXPECT_EQ("", Results[Spec.OldHeader]);
  EXPECT_EQ(ExpectedNewHeader, Results[Spec.NewHeader]);
  EXPECT_EQ(ExpectedNewCC, Results[Spec.NewCC]);
}

TEST(ClangMove, MoveAll) {
  std::vector<std::string> TestHeaders = {
    "class A {\npublic:\n  int f();\n};",
    // forward declaration.
    "class B;\nclass A {\npublic:\n  int f();\n};",
    // template forward declaration.
    "template <typename T> class B;\nclass A {\npublic:\n  int f();\n};",
    "namespace a {}\nclass A {\npublic:\n  int f();\n};",
    "namespace a {}\nusing namespace a;\nclass A {\npublic:\n  int f();\n};",
  };
  const char Code[] = "#include \"foo.h\"\nint A::f() { return 0; }";
  move::MoveDefinitionSpec Spec;
  Spec.Names.push_back("A");
  Spec.OldHeader = "foo.h";
  Spec.OldCC = "foo.cc";
  Spec.NewHeader = "new_foo.h";
  Spec.NewCC = "new_foo.cc";
  for (const auto& Header : TestHeaders) {
    auto Results = runClangMoveOnCode(Spec, Header.c_str(), Code);
    EXPECT_EQ(Header, Results[Spec.NewHeader]);
    EXPECT_EQ("", Results[Spec.OldHeader]);
    EXPECT_EQ("", Results[Spec.OldCC]);
  }
}

TEST(ClangMove, MoveAllMultipleClasses) {
  move::MoveDefinitionSpec Spec;
  std::vector<std::string> TestHeaders = {
    "class C;\nclass A {\npublic:\n  int f();\n};\nclass B {};",
    "class C;\nclass B;\nclass A {\npublic:\n  int f();\n};\nclass B {};",
  };
  const char Code[] = "#include \"foo.h\"\nint A::f() { return 0; }";
  Spec.Names = {std::string("A"), std::string("B")};
  Spec.OldHeader = "foo.h";
  Spec.OldCC = "foo.cc";
  Spec.NewHeader = "new_foo.h";
  Spec.NewCC = "new_foo.cc";
  for (const auto& Header : TestHeaders) {
    auto Results = runClangMoveOnCode(Spec, Header.c_str(), Code);
    EXPECT_EQ(Header, Results[Spec.NewHeader]);
    EXPECT_EQ("", Results[Spec.OldHeader]);
    EXPECT_EQ("", Results[Spec.OldCC]);
  }
}

TEST(ClangMove, DontMoveAll) {
  const char ExpectedHeader[] = "#ifndef NEW_FOO_H\n"
                                "#define NEW_FOO_H\n"
                                "\n"
                                "class A {\npublic:\n  int f();\n};\n"
                                "\n"
                                "#endif // NEW_FOO_H\n";
  const char Code[] = "#include \"foo.h\"\nint A::f() { return 0; }";
  std::vector<std::string> TestHeaders = {
    "class B {};\nclass A {\npublic:\n  int f();\n};\n",
    "void f() {};\nclass A {\npublic:\n  int f();\n};\n",
  };
  move::MoveDefinitionSpec Spec;
  Spec.Names.push_back("A");
  Spec.OldHeader = "foo.h";
  Spec.OldCC = "foo.cc";
  Spec.NewHeader = "new_foo.h";
  Spec.NewCC = "new_foo.cc";
  for (const auto& Header : TestHeaders) {
    auto Results = runClangMoveOnCode(Spec, Header.c_str(), Code);
    EXPECT_EQ(ExpectedHeader, Results[Spec.NewHeader]);
    // The expected old header should not contain class A definition.
    std::string ExpectedOldHeader = Header.substr(0, Header.size() - 32);
    EXPECT_EQ(ExpectedOldHeader, Results[Spec.OldHeader]);
  }
}

TEST(ClangMove, IgnoreMacroSymbolsAndMoveAll) {
  const char TestCode[] = "#include \"foo.h\"";
  std::vector<std::string> TestHeaders = {
    "#define DEFINE_Foo int Foo = 1;\nDEFINE_Foo;\nclass Bar {};\n",
    "#define DEFINE(x) int var_##x = 1;\nDEFINE(foo);\nclass Bar {};\n",
  };
  move::MoveDefinitionSpec Spec;
  Spec.Names.push_back("Bar");
  Spec.OldHeader = "foo.h";
  Spec.OldCC = "foo.cc";
  Spec.NewHeader = "new_foo.h";
  Spec.NewCC = "new_foo.cc";

  for (const auto& Header : TestHeaders) {
    auto Results = runClangMoveOnCode(Spec, Header.c_str(), TestCode);
    EXPECT_EQ("", Results[Spec.OldHeader]);
    EXPECT_EQ(Header, Results[Spec.NewHeader]);
  }
}

TEST(ClangMove, MacroInFunction) {
  const char TestHeader[] = "#define INT int\n"
                            "class A {\npublic:\n  int f();\n};\n"
                            "class B {};\n";
  const char TestCode[] = "#include \"foo.h\"\n"
                          "INT A::f() { return 0; }\n";
  const char ExpectedNewCode[] = "#include \"new_foo.h\"\n\n"
                                 "INT A::f() { return 0; }\n";
  move::MoveDefinitionSpec Spec;
  Spec.Names.push_back("A");
  Spec.OldHeader = "foo.h";
  Spec.OldCC = "foo.cc";
  Spec.NewHeader = "new_foo.h";
  Spec.NewCC = "new_foo.cc";
  auto Results = runClangMoveOnCode(Spec, TestHeader, TestCode);
  EXPECT_EQ(ExpectedNewCode, Results[Spec.NewCC]);
}

TEST(ClangMove, DefinitionInMacro) {
  const char TestHeader[] = "#define DEF(CLASS) void CLASS##_::f() {}\n"
                            "#define DEF2(CLASS, ...) void CLASS##_::f2() {}\n"
                            "class A_ {\nvoid f();\nvoid f2();\n};\n"
                            "class B {};\n";
  const char TestCode[] = "#include \"foo.h\"\n"
                          "DEF(A)\n\n"
                          "DEF2(A,\n"
                          "     123)\n";
  const char ExpectedNewCode[] = "#include \"new_foo.h\"\n\n"
                                 "DEF(A)\n\n"
                                 "DEF2(A, 123)\n";
  move::MoveDefinitionSpec Spec;
  Spec.Names.push_back("A_");
  Spec.OldHeader = "foo.h";
  Spec.OldCC = "foo.cc";
  Spec.NewHeader = "new_foo.h";
  Spec.NewCC = "new_foo.cc";
  auto Results = runClangMoveOnCode(Spec, TestHeader, TestCode);
  EXPECT_EQ(ExpectedNewCode, Results[Spec.NewCC]);
}

TEST(ClangMove, WellFormattedCode) {
  const std::string CommonHeader =
      "namespace a {\n"
      "namespace b {\n"
      "namespace c {\n"
      "class C;\n"
      "\n"
      "class A {\npublic:\n  void f();\n  void f2();\n};\n"
      "} // namespace c\n"
      "} // namespace b\n"
      "\n"
      "namespace d {\n"
      "namespace e {\n"
      "class B {\npublic:\n  void f();\n};\n"
      "} // namespace e\n"
      "} // namespace d\n"
      "} // namespace a\n";
  const std::string CommonCode = "\n"
                                 "namespace a {\n"
                                 "namespace b {\n"
                                 "namespace c {\n"
                                 "void A::f() {}\n"
                                 "\n"
                                 "void A::f2() {}\n"
                                 "} // namespace c\n"
                                 "} // namespace b\n"
                                 "\n"
                                 "namespace d {\n"
                                 "namespace e {\n"
                                 "void B::f() {}\n"
                                 "} // namespace e\n"
                                 "} // namespace d\n"
                                 "} // namespace a\n";
  // Add dummy class to prevent behavior of moving all declarations from header.
  const std::string TestHeader = CommonHeader + "class D {};\n";
  const std::string TestCode = "#include \"foo.h\"\n" + CommonCode;
  const std::string ExpectedNewHeader = "#ifndef NEW_FOO_H\n"
                                        "#define NEW_FOO_H\n"
                                        "\n" +
                                        CommonHeader +
                                        "\n"
                                        "#endif // NEW_FOO_H\n";
  const std::string ExpectedNewCC = "#include \"new_foo.h\"\n" + CommonCode;
  move::MoveDefinitionSpec Spec;
  Spec.Names.push_back("a::b::c::A");
  Spec.Names.push_back("a::d::e::B");
  Spec.OldHeader = "foo.h";
  Spec.OldCC = "foo.cc";
  Spec.NewHeader = "new_foo.h";
  Spec.NewCC = "new_foo.cc";
  auto Results = runClangMoveOnCode(Spec, TestHeader.c_str(), TestCode.c_str());
  EXPECT_EQ(ExpectedNewCC, Results[Spec.NewCC]);
  EXPECT_EQ(ExpectedNewHeader, Results[Spec.NewHeader]);
}

TEST(ClangMove, AddDependentNewHeader) {
  const char TestHeader[] = "class A {};\n"
                            "class B {};\n";
  const char TestCode[] = "#include \"foo.h\"\n";
  const char ExpectedOldHeader[] = "#include \"new_foo.h\"\nclass B {};\n";
  const char ExpectedNewHeader[] = "#ifndef NEW_FOO_H\n"
                                   "#define NEW_FOO_H\n"
                                   "\n"
                                   "class A {};\n"
                                   "\n"
                                   "#endif // NEW_FOO_H\n";
  move::MoveDefinitionSpec Spec;
  Spec.Names.push_back("A");
  Spec.OldHeader = "foo.h";
  Spec.OldCC = "foo.cc";
  Spec.NewHeader = "new_foo.h";
  Spec.NewCC = "new_foo.cc";
  Spec.OldDependOnNew = true;
  auto Results = runClangMoveOnCode(Spec, TestHeader, TestCode);
  EXPECT_EQ(ExpectedOldHeader, Results[Spec.OldHeader]);
  EXPECT_EQ(ExpectedNewHeader, Results[Spec.NewHeader]);
}

TEST(ClangMove, AddDependentOldHeader) {
  const char TestHeader[] = "class A {};\n"
                            "class B {};\n";
  const char TestCode[] = "#include \"foo.h\"\n";
  const char ExpectedNewHeader[] = "#ifndef NEW_FOO_H\n"
                                   "#define NEW_FOO_H\n"
                                   "\n"
                                   "#include \"foo.h\"\n"
                                   "\n"
                                   "class B {};\n"
                                   "\n"
                                   "#endif // NEW_FOO_H\n";
  const char ExpectedOldHeader[] = "class A {};\n";
  move::MoveDefinitionSpec Spec;
  Spec.Names.push_back("B");
  Spec.OldHeader = "foo.h";
  Spec.OldCC = "foo.cc";
  Spec.NewHeader = "new_foo.h";
  Spec.NewCC = "new_foo.cc";
  Spec.NewDependOnOld = true;
  auto Results = runClangMoveOnCode(Spec, TestHeader, TestCode);
  EXPECT_EQ(ExpectedNewHeader, Results[Spec.NewHeader]);
  EXPECT_EQ(ExpectedOldHeader, Results[Spec.OldHeader]);
}

TEST(ClangMove, DumpDecls) {
  const char TestHeader[] = "template <typename T>\n"
                            "class A {\n"
                            " public:\n"
                            "  void f();\n"
                            "  template <typename U> void h();\n"
                            "  static int b;\n"
                            "};\n"
                            "\n"
                            "template <typename T> void A<T>::f() {}\n"
                            "\n"
                            "template <typename T>\n"
                            "template <typename U>\n"
                            "void A<T>::h() {}\n"
                            "\n"
                            "template <typename T> int A<T>::b = 2;\n"
                            "\n"
                            "template <> class A<int> {};\n"
                            "\n"
                            "class B {};\n"
                            "\n"
                            "namespace a {\n"
                            "class Move1 {};\n"
                            "void f1() {}\n"
                            "template <typename T>"
                            "void f2(T t);\n"
                            "} // namespace a\n"
                            "\n"
                            "class ForwardClass;\n"
                            "namespace a {\n"
                            "namespace b {\n"
                            "class Move1 { public : void f(); };\n"
                            "void f() {}\n"
                            "enum E1 { Green };\n"
                            "enum class E2 { Red };\n"
                            "typedef int Int2;\n"
                            "typedef A<double> A_d;"
                            "using Int = int;\n"
                            "template <typename T>\n"
                            "using AA = A<T>;\n"
                            "extern int kGlobalInt;\n"
                            "extern const char* const kGlobalStr;\n"
                            "} // namespace b\n"
                            "} // namespace a\n"
                            "#define DEFINE_FOO class Foo {};\n"
                            "DEFINE_FOO\n";
  const char TestCode[] = "#include \"foo.h\"\n";
  move::MoveDefinitionSpec Spec;
  Spec.Names.push_back("B");
  Spec.OldHeader = "foo.h";
  Spec.OldCC = "foo.cc";
  Spec.NewHeader = "new_foo.h";
  Spec.NewCC = "new_foo.cc";
  DeclarationReporter Reporter;
  std::vector<DeclarationReporter::Declaration> ExpectedDeclarations = {
      {"A", "Class", true},
      {"B", "Class", false},
      {"a::Move1", "Class", false},
      {"a::f1", "Function", false},
      {"a::f2", "Function", true},
      {"a::b::Move1", "Class", false},
      {"a::b::f", "Function", false},
      {"a::b::E1", "Enum", false},
      {"a::b::E2", "Enum", false},
      {"a::b::Int2", "TypeAlias", false},
      {"a::b::A_d", "TypeAlias", false},
      {"a::b::Int", "TypeAlias", false},
      {"a::b::AA", "TypeAlias", true},
      {"a::b::kGlobalInt", "Variable", false},
      {"a::b::kGlobalStr", "Variable", false}};
  runClangMoveOnCode(Spec, TestHeader, TestCode, &Reporter);
  std::vector<DeclarationReporter::Declaration> Results;
  for (const auto &DelPair : Reporter.getDeclarationList())
    Results.push_back(DelPair);
  EXPECT_THAT(ExpectedDeclarations,
              testing::UnorderedElementsAreArray(Results));
}

} // namespace
} // namespce move
} // namespace clang
OpenPOWER on IntegriCloud