• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- unittests/Tooling/ReplacementsYamlTest.cpp - Serialization tests ---===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Tests for serialization of Replacements.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Tooling/ReplacementsYaml.h"
15 #include "gtest/gtest.h"
16 
17 using namespace llvm;
18 using namespace clang::tooling;
19 
TEST(ReplacementsYamlTest,serializesReplacements)20 TEST(ReplacementsYamlTest, serializesReplacements) {
21 
22   TranslationUnitReplacements Doc;
23 
24   Doc.MainSourceFile = "/path/to/source.cpp";
25   Doc.Context = "some context";
26   Doc.Replacements
27       .push_back(Replacement("/path/to/file1.h", 232, 56, "replacement #1"));
28   Doc.Replacements
29       .push_back(Replacement("/path/to/file2.h", 301, 2, "replacement #2"));
30 
31   std::string YamlContent;
32   llvm::raw_string_ostream YamlContentStream(YamlContent);
33 
34   yaml::Output YAML(YamlContentStream);
35   YAML << Doc;
36 
37   // NOTE: If this test starts to fail for no obvious reason, check whitespace.
38   ASSERT_STREQ("---\n"
39                "MainSourceFile:  /path/to/source.cpp\n"
40                "Context:         some context\n"
41                "Replacements:    \n" // Extra whitespace here!
42                "  - FilePath:        /path/to/file1.h\n"
43                "    Offset:          232\n"
44                "    Length:          56\n"
45                "    ReplacementText: 'replacement #1'\n"
46                "  - FilePath:        /path/to/file2.h\n"
47                "    Offset:          301\n"
48                "    Length:          2\n"
49                "    ReplacementText: 'replacement #2'\n"
50                "...\n",
51                YamlContentStream.str().c_str());
52 }
53 
TEST(ReplacementsYamlTest,deserializesReplacements)54 TEST(ReplacementsYamlTest, deserializesReplacements) {
55   std::string YamlContent = "---\n"
56                             "MainSourceFile:      /path/to/source.cpp\n"
57                             "Context:             some context\n"
58                             "Replacements:\n"
59                             "  - FilePath:        /path/to/file1.h\n"
60                             "    Offset:          232\n"
61                             "    Length:          56\n"
62                             "    ReplacementText: 'replacement #1'\n"
63                             "  - FilePath:        /path/to/file2.h\n"
64                             "    Offset:          301\n"
65                             "    Length:          2\n"
66                             "    ReplacementText: 'replacement #2'\n"
67                             "...\n";
68   TranslationUnitReplacements DocActual;
69   yaml::Input YAML(YamlContent);
70   YAML >> DocActual;
71   ASSERT_FALSE(YAML.error());
72   ASSERT_EQ(2u, DocActual.Replacements.size());
73   ASSERT_EQ("/path/to/source.cpp", DocActual.MainSourceFile);
74   ASSERT_EQ("some context", DocActual.Context);
75   ASSERT_EQ("/path/to/file1.h", DocActual.Replacements[0].getFilePath());
76   ASSERT_EQ(232u, DocActual.Replacements[0].getOffset());
77   ASSERT_EQ(56u, DocActual.Replacements[0].getLength());
78   ASSERT_EQ("replacement #1", DocActual.Replacements[0].getReplacementText());
79   ASSERT_EQ("/path/to/file2.h", DocActual.Replacements[1].getFilePath());
80   ASSERT_EQ(301u, DocActual.Replacements[1].getOffset());
81   ASSERT_EQ(2u, DocActual.Replacements[1].getLength());
82   ASSERT_EQ("replacement #2", DocActual.Replacements[1].getReplacementText());
83 }
84 
TEST(ReplacementsYamlTest,deserializesWithoutContext)85 TEST(ReplacementsYamlTest, deserializesWithoutContext) {
86   // Make sure a doc can be read without the context field.
87   std::string YamlContent = "---\n"
88                             "MainSourceFile:      /path/to/source.cpp\n"
89                             "Replacements:\n"
90                             "  - FilePath:        target_file.h\n"
91                             "    Offset:          1\n"
92                             "    Length:          10\n"
93                             "    ReplacementText: replacement\n"
94                             "...\n";
95   TranslationUnitReplacements DocActual;
96   yaml::Input YAML(YamlContent);
97   YAML >> DocActual;
98   ASSERT_FALSE(YAML.error());
99   ASSERT_EQ("/path/to/source.cpp", DocActual.MainSourceFile);
100   ASSERT_EQ(1u, DocActual.Replacements.size());
101   ASSERT_EQ(std::string(), DocActual.Context);
102   ASSERT_EQ("target_file.h", DocActual.Replacements[0].getFilePath());
103   ASSERT_EQ(1u, DocActual.Replacements[0].getOffset());
104   ASSERT_EQ(10u, DocActual.Replacements[0].getLength());
105   ASSERT_EQ("replacement", DocActual.Replacements[0].getReplacementText());
106 }
107