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