File indexing completed on 2024-05-19 05:42:02

0001 // ct_lvtclp_logicaldepscanner.t.cpp                                  -*-C++-*-
0002 
0003 /*
0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk>
0005 // SPDX-License-Identifier: Apache-2.0
0006 //
0007 // Licensed under the Apache License, Version 2.0 (the "License");
0008 // you may not use this file except in compliance with the License.
0009 // You may obtain a copy of the License at
0010 //
0011 //     http://www.apache.org/licenses/LICENSE-2.0
0012 //
0013 // Unless required by applicable law or agreed to in writing, software
0014 // distributed under the License is distributed on an "AS IS" BASIS,
0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016 // See the License for the specific language governing permissions and
0017 // limitations under the License.
0018 */
0019 
0020 #include <ct_lvtmdb_functionobject.h>
0021 #include <ct_lvtmdb_methodobject.h>
0022 #include <ct_lvtmdb_namespaceobject.h>
0023 #include <ct_lvtmdb_objectstore.h>
0024 #include <ct_lvtmdb_typeobject.h>
0025 
0026 #include <ct_lvtclp_logicaldepscanner.h>
0027 #include <ct_lvtclp_testutil.h>
0028 #include <ct_lvtclp_toolexecutor.h>
0029 
0030 #include <memory>
0031 #include <unordered_set>
0032 
0033 // Auto generated by CMake / see main CMakeLists file, and
0034 // the CMakeLists on this folder.
0035 #include <catch2-local-includes.h>
0036 #include <test-project-paths.h>
0037 
0038 using namespace Codethink::lvtclp;
0039 using namespace Codethink::lvtmdb;
0040 using namespace Codethink;
0041 
0042 const PyDefaultGilReleasedContext defaultGilContextForTesting;
0043 
0044 struct FoundCommentTestData {
0045     struct HashFunc {
0046         size_t operator()(const FoundCommentTestData& data) const
0047         {
0048             return std::hash<std::string>{}(data.filename) ^ std::hash<std::string>{}(data.briefText)
0049                 ^ std::hash<unsigned>{}(data.startLine) ^ std::hash<unsigned>{}(data.endLine);
0050         }
0051     };
0052 
0053     std::string filename;
0054     std::string briefText;
0055     unsigned startLine;
0056     unsigned endLine;
0057 
0058     bool operator==(const FoundCommentTestData& other) const
0059     {
0060         return this->filename == other.filename && this->briefText == other.briefText
0061             && this->startLine == other.startLine && this->endLine == other.endLine;
0062     }
0063 
0064     friend auto operator<<(std::ostream& os, FoundCommentTestData const& self) -> std::ostream&
0065     {
0066         return os << self.filename << " comment: '" << self.briefText << "' from line " << self.startLine << " to "
0067                   << self.endLine;
0068     }
0069 };
0070 
0071 TEST_CASE("Optional comment callbacks")
0072 {
0073     auto const PREFIX = std::string{TEST_PRJ_PATH};
0074 
0075     auto const prjAPath = PREFIX + "/project_with_includes_outside_src/prjA";
0076     auto const prjBPath = PREFIX + "/project_with_includes_outside_src/prjB";
0077     auto cdb = StaticCompilationDatabase{{{"groups/one/oneaaa/oneaaa_comp.cpp", "oneaaa_comp.o"},
0078                                           {"groups/one/oneaaa/oneaaa_comp.h", "oneaaa_comp.h.o"}},
0079                                          "placeholder",
0080                                          {"-I" + prjAPath + "/groups/one/oneaaa/",
0081                                           "-I" + prjBPath + "/groups/two/twoaaa/",
0082                                           "-fparse-all-comments",
0083                                           "-std=c++17"},
0084                                          prjAPath};
0085     auto memDb = lvtmdb::ObjectStore{};
0086 
0087     auto foundComments = std::unordered_set<FoundCommentTestData, FoundCommentTestData::HashFunc>{};
0088     auto saveCommentsCallback =
0089         [&](const std::string& filename, const std::string& briefText, unsigned startLine, unsigned endLine) {
0090             foundComments.insert({filename, briefText, startLine, endLine});
0091         };
0092 
0093     auto executor = ToolExecutor{cdb, 1, [](auto&& _1, auto&& _2) {}, memDb};
0094     (void) executor.execute(std::make_unique<LogicalDepActionFactory>(
0095         memDb,
0096         PREFIX,
0097         std::vector<std::filesystem::path>{},
0098         std::vector<std::pair<std::string, std::string>>{},
0099         [](const std::string&) {},
0100         std::nullopt,
0101         false,
0102         saveCommentsCallback));
0103     REQUIRE(foundComments.size() == 4);
0104     REQUIRE(foundComments.find(FoundCommentTestData{"oneaaa_comp.h", "klass", 7, 7}) != foundComments.end());
0105     REQUIRE(foundComments.find(FoundCommentTestData{"oneaaa_comp.h", "Test only include", 4, 4})
0106             != foundComments.end());
0107     REQUIRE(foundComments.find(FoundCommentTestData{"oneaaa_comp.cpp", "Some other comment...?", 4, 4})
0108             != foundComments.end());
0109     REQUIRE(foundComments.find(FoundCommentTestData{"oneaaa_comp.cpp", "Main include", 1, 1}) != foundComments.end());
0110 }