1 /**
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17 #include <cstring>
18
19 #include "libabckit/include/c/abckit.h"
20 #include "helpers/helpers.h"
21 #include "libabckit/include/c/metadata_core.h"
22
23 namespace libabckit::test {
24
25 static auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
26 static auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
27 static auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
28
29 class LibAbcKitInspectApiNamespacesTests : public ::testing::Test {};
30 static constexpr auto INPUT_PATH = ABCKIT_ABC_DIR "ut/metadata_core/inspect_api/namespaces/namespaces_dynamic.abc";
31
32 enum TestTypes {
33 CLASS,
34 FUNCTION,
35 NAMESPACE,
36 };
37
TestNamespaces(const std::string & namespaceName,const std::string & expectedNamespaceName)38 static void TestNamespaces(const std::string &namespaceName, const std::string &expectedNamespaceName)
39 {
40 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
41 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
42
43 auto *n = helpers::FindNamespaceByName(file, namespaceName);
44 ASSERT_NE(n, nullptr);
45 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
46 auto *parent = g_implI->namespaceGetParentNamespace(n);
47 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
48 if (expectedNamespaceName.empty()) {
49 ASSERT_EQ(parent, nullptr);
50 } else {
51 ASSERT_NE(parent, nullptr);
52 auto parentName = helpers::AbckitStringToString(g_implI->namespaceGetName(parent));
53 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
54 ASSERT_EQ(parentName, expectedNamespaceName);
55 }
56
57 g_impl->closeFile(file);
58 }
59
TestFunctions(const std::string & functionName,const std::string & expectedNamespaceName)60 static void TestFunctions(const std::string &functionName, const std::string &expectedNamespaceName)
61 {
62 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
63 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
64
65 auto *func = helpers::FindMethodByName(file, functionName);
66 ASSERT_NE(func, nullptr);
67 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
68 auto *parent = g_implI->functionGetParentNamespace(func);
69 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
70 if (expectedNamespaceName.empty()) {
71 ASSERT_EQ(parent, nullptr);
72 } else {
73 ASSERT_NE(parent, nullptr);
74 auto parentName = helpers::AbckitStringToString(g_implI->namespaceGetName(parent));
75 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
76 ASSERT_EQ(parentName, expectedNamespaceName);
77 }
78
79 g_impl->closeFile(file);
80 }
81
TestClasses(const std::string & className,const std::string & expectedNamespaceName)82 static void TestClasses(const std::string &className, const std::string &expectedNamespaceName)
83 {
84 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
85 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
86
87 auto *func = helpers::FindMethodByName(file, className);
88 ASSERT_NE(func, nullptr);
89 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
90 auto *klass = g_implI->functionGetParentClass(func);
91 ASSERT_NE(klass, nullptr);
92 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
93 auto *parent = g_implI->classGetParentNamespace(klass);
94 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
95 if (expectedNamespaceName.empty()) {
96 ASSERT_EQ(parent, nullptr);
97 } else {
98 ASSERT_NE(parent, nullptr);
99 auto parentName = helpers::AbckitStringToString(g_implI->namespaceGetName(parent));
100 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
101 ASSERT_EQ(parentName, expectedNamespaceName);
102 }
103
104 g_impl->closeFile(file);
105 }
106
107 template <TestTypes TYPE>
GeneralTestNamespaces(const std::string & entityName,const std::string & expectedNamespaceName="")108 static void GeneralTestNamespaces(const std::string &entityName, const std::string &expectedNamespaceName = "")
109 {
110 if constexpr (TYPE == NAMESPACE) {
111 TestNamespaces(entityName, expectedNamespaceName);
112 } else if (TYPE == CLASS) {
113 TestClasses(entityName, expectedNamespaceName);
114 } else {
115 TestFunctions(entityName, expectedNamespaceName);
116 }
117 }
118
119 // Test: test-kind=api, api=InspectApiImpl::namespaceGetParentNamespace, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitInspectApiNamespacesTests,NamespaceGetParentNamespace_1)120 TEST_F(LibAbcKitInspectApiNamespacesTests, NamespaceGetParentNamespace_1)
121 {
122 GeneralTestNamespaces<NAMESPACE>("N1");
123 }
124
125 // Test: test-kind=api, api=InspectApiImpl::namespaceGetParentNamespace, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitInspectApiNamespacesTests,NamespaceGetParentNamespace_2)126 TEST_F(LibAbcKitInspectApiNamespacesTests, NamespaceGetParentNamespace_2)
127 {
128 GeneralTestNamespaces<NAMESPACE>("N2", "N1");
129 }
130
131 // Test: test-kind=api, api=InspectApiImpl::functionGetParentNamespace, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitInspectApiNamespacesTests,FunctionGetParentNamespace_1)132 TEST_F(LibAbcKitInspectApiNamespacesTests, FunctionGetParentNamespace_1)
133 {
134 GeneralTestNamespaces<FUNCTION>("f0");
135 }
136
137 // Test: test-kind=api, api=InspectApiImpl::functionGetParentNamespace, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitInspectApiNamespacesTests,FunctionGetParentNamespace_2)138 TEST_F(LibAbcKitInspectApiNamespacesTests, FunctionGetParentNamespace_2)
139 {
140 GeneralTestNamespaces<FUNCTION>("f1", "N1");
141 }
142
143 // Test: test-kind=api, api=InspectApiImpl::functionGetParentNamespace, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitInspectApiNamespacesTests,FunctionGetParentNamespace_3)144 TEST_F(LibAbcKitInspectApiNamespacesTests, FunctionGetParentNamespace_3)
145 {
146 GeneralTestNamespaces<FUNCTION>("f2", "N2");
147 }
148
149 // Test: test-kind=api, api=InspectApiImpl::functionGetParentNamespace, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitInspectApiNamespacesTests,FunctionGetParentNamespace_4)150 TEST_F(LibAbcKitInspectApiNamespacesTests, FunctionGetParentNamespace_4)
151 {
152 GeneralTestNamespaces<FUNCTION>("M1");
153 }
154
155 // Test: test-kind=api, api=InspectApiImpl::functionGetParentNamespace, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitInspectApiNamespacesTests,FunctionGetParentNamespace_5)156 TEST_F(LibAbcKitInspectApiNamespacesTests, FunctionGetParentNamespace_5)
157 {
158 GeneralTestNamespaces<FUNCTION>("M2");
159 }
160
161 // Test: test-kind=api, api=InspectApiImpl::classGetParentNamespace, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitInspectApiNamespacesTests,ClassGetParentNamespace_1)162 TEST_F(LibAbcKitInspectApiNamespacesTests, ClassGetParentNamespace_1)
163 {
164 GeneralTestNamespaces<CLASS>("C2", "N2");
165 }
166
167 // Test: test-kind=api, api=InspectApiImpl::classGetParentNamespace, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitInspectApiNamespacesTests,ClassGetParentNamespace_2)168 TEST_F(LibAbcKitInspectApiNamespacesTests, ClassGetParentNamespace_2)
169 {
170 GeneralTestNamespaces<CLASS>("C1", "N1");
171 }
172
173 // Test: test-kind=api, api=InspectApiImpl::classGetParentNamespace, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitInspectApiNamespacesTests,ClassGetParentNamespace_3)174 TEST_F(LibAbcKitInspectApiNamespacesTests, ClassGetParentNamespace_3)
175 {
176 GeneralTestNamespaces<CLASS>("C0");
177 }
178
179 } // namespace libabckit::test
180