1 //===- unittests/Tooling/ReplacementsYamlTest.cpp - Serialization tests ---===//
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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Tests for serialization of Replacements.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/Tooling/ReplacementsYaml.h"
14 #include "gtest/gtest.h"
15 
16 using namespace llvm;
17 using namespace clang::tooling;
18 
TEST(ReplacementsYamlTest,serializesReplacements)19 TEST(ReplacementsYamlTest, serializesReplacements) {
20 
21   TranslationUnitReplacements Doc;
22 
23   Doc.MainSourceFile = "/path/to/source.cpp";
24   Doc.Replacements.emplace_back("/path/to/file1.h", 232, 56, "replacement #1");
25   Doc.Replacements.emplace_back("/path/to/file2.h", 301, 2, "replacement #2");
26 
27   std::string YamlContent;
28   llvm::raw_string_ostream YamlContentStream(YamlContent);
29 
30   yaml::Output YAML(YamlContentStream);
31   YAML << Doc;
32 
33   // NOTE: If this test starts to fail for no obvious reason, check whitespace.
34   ASSERT_STREQ("---\n"
35                "MainSourceFile:  '/path/to/source.cpp'\n"
36                "Replacements:\n"
37                "  - FilePath:        '/path/to/file1.h'\n"
38                "    Offset:          232\n"
39                "    Length:          56\n"
40                "    ReplacementText: 'replacement #1'\n"
41                "  - FilePath:        '/path/to/file2.h'\n"
42                "    Offset:          301\n"
43                "    Length:          2\n"
44                "    ReplacementText: 'replacement #2'\n"
45                "...\n",
46                YamlContentStream.str().c_str());
47 }
48 
TEST(ReplacementsYamlTest,serializesNewLines)49 TEST(ReplacementsYamlTest, serializesNewLines) {
50   TranslationUnitReplacements Doc;
51 
52   Doc.MainSourceFile = "/path/to/source.cpp";
53   Doc.Replacements.emplace_back("/path/to/file1.h", 0, 0, "#include <utility>\n");
54 
55   std::string YamlContent;
56   llvm::raw_string_ostream YamlContentStream(YamlContent);
57 
58   yaml::Output YAML(YamlContentStream);
59   YAML << Doc;
60 
61   // NOTE: If this test starts to fail for no obvious reason, check whitespace.
62   ASSERT_STREQ("---\n"
63                "MainSourceFile:  '/path/to/source.cpp'\n"
64                "Replacements:\n"
65                "  - FilePath:        '/path/to/file1.h'\n"
66                "    Offset:          0\n"
67                "    Length:          0\n"
68                "    ReplacementText: \"#include <utility>\\n\"\n"
69                "...\n",
70                YamlContentStream.str().c_str());
71 }
72 
TEST(ReplacementsYamlTest,deserializesReplacements)73 TEST(ReplacementsYamlTest, deserializesReplacements) {
74   std::string YamlContent = "---\n"
75                             "MainSourceFile:      /path/to/source.cpp\n"
76                             "Replacements:\n"
77                             "  - FilePath:        /path/to/file1.h\n"
78                             "    Offset:          232\n"
79                             "    Length:          56\n"
80                             "    ReplacementText: 'replacement #1'\n"
81                             "  - FilePath:        /path/to/file2.h\n"
82                             "    Offset:          301\n"
83                             "    Length:          2\n"
84                             "    ReplacementText: 'replacement #2'\n"
85                             "...\n";
86   TranslationUnitReplacements DocActual;
87   yaml::Input YAML(YamlContent);
88   YAML >> DocActual;
89   ASSERT_FALSE(YAML.error());
90   ASSERT_EQ(2u, DocActual.Replacements.size());
91   ASSERT_EQ("/path/to/source.cpp", DocActual.MainSourceFile);
92   ASSERT_EQ("/path/to/file1.h", DocActual.Replacements[0].getFilePath());
93   ASSERT_EQ(232u, DocActual.Replacements[0].getOffset());
94   ASSERT_EQ(56u, DocActual.Replacements[0].getLength());
95   ASSERT_EQ("replacement #1", DocActual.Replacements[0].getReplacementText());
96   ASSERT_EQ("/path/to/file2.h", DocActual.Replacements[1].getFilePath());
97   ASSERT_EQ(301u, DocActual.Replacements[1].getOffset());
98   ASSERT_EQ(2u, DocActual.Replacements[1].getLength());
99   ASSERT_EQ("replacement #2", DocActual.Replacements[1].getReplacementText());
100 }
101 
TEST(ReplacementsYamlTest,deserializesWithoutContext)102 TEST(ReplacementsYamlTest, deserializesWithoutContext) {
103   // Make sure a doc can be read without the context field.
104   std::string YamlContent = "---\n"
105                             "MainSourceFile:      /path/to/source.cpp\n"
106                             "Replacements:\n"
107                             "  - FilePath:        target_file.h\n"
108                             "    Offset:          1\n"
109                             "    Length:          10\n"
110                             "    ReplacementText: replacement\n"
111                             "...\n";
112   TranslationUnitReplacements DocActual;
113   yaml::Input YAML(YamlContent);
114   YAML >> DocActual;
115   ASSERT_FALSE(YAML.error());
116   ASSERT_EQ("/path/to/source.cpp", DocActual.MainSourceFile);
117   ASSERT_EQ(1u, DocActual.Replacements.size());
118   ASSERT_EQ("target_file.h", DocActual.Replacements[0].getFilePath());
119   ASSERT_EQ(1u, DocActual.Replacements[0].getOffset());
120   ASSERT_EQ(10u, DocActual.Replacements[0].getLength());
121   ASSERT_EQ("replacement", DocActual.Replacements[0].getReplacementText());
122 }
123