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

0001 // ct_lvtclp_testnamespace.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 // This file is home to tests for adding NamespaceDeclarations to the database
0021 
0022 #include <ct_lvtmdb_fileobject.h>
0023 #include <ct_lvtmdb_namespaceobject.h>
0024 #include <ct_lvtmdb_objectstore.h>
0025 
0026 #include <ct_lvtclp_testutil.h>
0027 
0028 #include <algorithm>
0029 #include <iostream>
0030 
0031 #include <catch2-local-includes.h>
0032 
0033 using namespace Codethink::lvtclp;
0034 using namespace Codethink::lvtmdb;
0035 
0036 const PyDefaultGilReleasedContext defaultGilContextForTesting;
0037 
0038 TEST_CASE("Namespace declaration")
0039 {
0040     const static char *source = "namespace foo {}";
0041     ObjectStore session;
0042     REQUIRE(Test_Util::runOnCode(session, source, "testNamespaceDeclaration.cpp"));
0043 
0044     session.withROLock([&] {
0045         NamespaceObject *nmspc = session.getNamespace("foo");
0046         REQUIRE(nmspc);
0047     });
0048 }
0049 
0050 TEST_CASE("Anon namespace")
0051 {
0052     const static char *source = "namespace {}";
0053     ObjectStore session;
0054     REQUIRE(Test_Util::runOnCode(session, source, "testAnonNamespace.cpp"));
0055 
0056     // no namespace should have been added
0057     session.withROLock([&] {
0058         REQUIRE(session.namespaces().empty());
0059     });
0060 }
0061 
0062 TEST_CASE("Parent namespace")
0063 {
0064     const static char *source = "namespace foo { namespace bar {} } namespace foo {}";
0065     ObjectStore session;
0066     REQUIRE(Test_Util::runOnCode(session, source, "testParentNamespace.cpp"));
0067 
0068     NamespaceObject *foo = nullptr;
0069     NamespaceObject *bar = nullptr;
0070     FileObject *file = nullptr;
0071 
0072     session.withROLock([&] {
0073         foo = session.getNamespace("foo");
0074         bar = session.getNamespace("foo::bar");
0075         file = session.getFile("testParentNamespace.cpp");
0076     });
0077 
0078     REQUIRE(foo);
0079     REQUIRE(bar);
0080     REQUIRE(file);
0081 
0082     foo->withROLock([&] {
0083         REQUIRE(foo->name() == "foo");
0084     });
0085 
0086     bar->withROLock([&] {
0087         REQUIRE(bar->name() == "bar");
0088         REQUIRE(bar->parent() == foo);
0089     });
0090 
0091     foo->withROLock([&] {
0092         const auto children = foo->children();
0093         const auto childIt = std::find(children.begin(), children.end(), bar);
0094         REQUIRE(childIt != children.end());
0095     });
0096 
0097     // check both namespaces were added to the file
0098     std::vector<NamespaceObject *> namespaces;
0099     file->withROLock([&] {
0100         namespaces = file->namespaces();
0101     });
0102 
0103     auto it = std::find(namespaces.begin(), namespaces.end(), foo);
0104     REQUIRE(it != namespaces.end());
0105     it = std::find(namespaces.begin(), namespaces.end(), bar);
0106     REQUIRE(it != namespaces.end());
0107 
0108     // check the second namespace foo wasn't added all over again
0109     REQUIRE(namespaces.size() == 2);
0110 }
0111 
0112 TEST_CASE("Nested namespace")
0113 {
0114     static const char *source = "namespace foo::bar {}";
0115     ObjectStore session;
0116     REQUIRE(Test_Util::runOnCode(session, source, "testNestedNamespace.cpp"));
0117     NamespaceObject *foo = nullptr;
0118     NamespaceObject *bar = nullptr;
0119 
0120     session.withROLock([&] {
0121         foo = session.getNamespace("foo");
0122         bar = session.getNamespace("foo::bar");
0123     });
0124 
0125     foo->withROLock([&] {
0126         REQUIRE(foo->name() == "foo");
0127     });
0128 
0129     bar->withROLock([&] {
0130         REQUIRE(bar->name() == "bar");
0131         REQUIRE(bar->parent() == foo);
0132     });
0133 }
0134 
0135 TEST_CASE("No unnamed namespace")
0136 {
0137     static const char *source = R"(
0138 namespace {
0139 namespace _u {
0140 }
0141 })";
0142     ObjectStore session;
0143     REQUIRE(Test_Util::runOnCode(session, source, "testNoUnnamednamespace.cpp"));
0144 
0145     // no namespace should have been added
0146     session.withROLock([&] {
0147         REQUIRE(session.namespaces().empty());
0148     });
0149 }