reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    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
//===- unittests/Tooling/ReplacementsYamlTest.cpp - Serialization tests ---===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Tests for serialization of Replacements.
//
//===----------------------------------------------------------------------===//

#include "clang/Tooling/ReplacementsYaml.h"
#include "gtest/gtest.h"

using namespace llvm;
using namespace clang::tooling;

TEST(ReplacementsYamlTest, serializesReplacements) {

  TranslationUnitReplacements Doc;

  Doc.MainSourceFile = "/path/to/source.cpp";
  Doc.Replacements.emplace_back("/path/to/file1.h", 232, 56, "replacement #1");
  Doc.Replacements.emplace_back("/path/to/file2.h", 301, 2, "replacement #2");

  std::string YamlContent;
  llvm::raw_string_ostream YamlContentStream(YamlContent);

  yaml::Output YAML(YamlContentStream);
  YAML << Doc;

  // NOTE: If this test starts to fail for no obvious reason, check whitespace.
  ASSERT_STREQ("---\n"
               "MainSourceFile:  '/path/to/source.cpp'\n"
               "Replacements:\n"
               "  - FilePath:        '/path/to/file1.h'\n"
               "    Offset:          232\n"
               "    Length:          56\n"
               "    ReplacementText: 'replacement #1'\n"
               "  - FilePath:        '/path/to/file2.h'\n"
               "    Offset:          301\n"
               "    Length:          2\n"
               "    ReplacementText: 'replacement #2'\n"
               "...\n",
               YamlContentStream.str().c_str());
}

TEST(ReplacementsYamlTest, serializesNewLines) {
  TranslationUnitReplacements Doc;

  Doc.MainSourceFile = "/path/to/source.cpp";
  Doc.Replacements.emplace_back("/path/to/file1.h", 0, 0, "#include <utility>\n");

  std::string YamlContent;
  llvm::raw_string_ostream YamlContentStream(YamlContent);

  yaml::Output YAML(YamlContentStream);
  YAML << Doc;

  // NOTE: If this test starts to fail for no obvious reason, check whitespace.
  ASSERT_STREQ("---\n"
               "MainSourceFile:  '/path/to/source.cpp'\n"
               "Replacements:\n"
               "  - FilePath:        '/path/to/file1.h'\n"
               "    Offset:          0\n"
               "    Length:          0\n"
               "    ReplacementText: '#include <utility>\n\n'\n"
               "...\n",
               YamlContentStream.str().c_str());
}

TEST(ReplacementsYamlTest, deserializesReplacements) {
  std::string YamlContent = "---\n"
                            "MainSourceFile:      /path/to/source.cpp\n"
                            "Replacements:\n"
                            "  - FilePath:        /path/to/file1.h\n"
                            "    Offset:          232\n"
                            "    Length:          56\n"
                            "    ReplacementText: 'replacement #1'\n"
                            "  - FilePath:        /path/to/file2.h\n"
                            "    Offset:          301\n"
                            "    Length:          2\n"
                            "    ReplacementText: 'replacement #2'\n"
                            "...\n";
  TranslationUnitReplacements DocActual;
  yaml::Input YAML(YamlContent);
  YAML >> DocActual;
  ASSERT_FALSE(YAML.error());
  ASSERT_EQ(2u, DocActual.Replacements.size());
  ASSERT_EQ("/path/to/source.cpp", DocActual.MainSourceFile);
  ASSERT_EQ("/path/to/file1.h", DocActual.Replacements[0].getFilePath());
  ASSERT_EQ(232u, DocActual.Replacements[0].getOffset());
  ASSERT_EQ(56u, DocActual.Replacements[0].getLength());
  ASSERT_EQ("replacement #1", DocActual.Replacements[0].getReplacementText());
  ASSERT_EQ("/path/to/file2.h", DocActual.Replacements[1].getFilePath());
  ASSERT_EQ(301u, DocActual.Replacements[1].getOffset());
  ASSERT_EQ(2u, DocActual.Replacements[1].getLength());
  ASSERT_EQ("replacement #2", DocActual.Replacements[1].getReplacementText());
}

TEST(ReplacementsYamlTest, deserializesWithoutContext) {
  // Make sure a doc can be read without the context field.
  std::string YamlContent = "---\n"
                            "MainSourceFile:      /path/to/source.cpp\n"
                            "Replacements:\n"
                            "  - FilePath:        target_file.h\n"
                            "    Offset:          1\n"
                            "    Length:          10\n"
                            "    ReplacementText: replacement\n"
                            "...\n";
  TranslationUnitReplacements DocActual;
  yaml::Input YAML(YamlContent);
  YAML >> DocActual;
  ASSERT_FALSE(YAML.error());
  ASSERT_EQ("/path/to/source.cpp", DocActual.MainSourceFile);
  ASSERT_EQ(1u, DocActual.Replacements.size());
  ASSERT_EQ("target_file.h", DocActual.Replacements[0].getFilePath());
  ASSERT_EQ(1u, DocActual.Replacements[0].getOffset());
  ASSERT_EQ(10u, DocActual.Replacements[0].getLength());
  ASSERT_EQ("replacement", DocActual.Replacements[0].getReplacementText());
}