1 /*
2 * Copyright (c) 2021-2025 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 "macros.h"
18 #include "public/es2panda_lib.h"
19 #include "test/utils/panda_executable_path_getter.h"
20 #include "test/utils/ast_verifier_test.h"
21 #include "util/diagnostic.h"
22
23 namespace {
24
25 using Es2PandaLibTest = test::utils::AstVerifierTest;
26
TEST_F(Es2PandaLibTest,NoError)27 TEST_F(Es2PandaLibTest, NoError)
28 {
29 CONTEXT(ES2PANDA_STATE_ASM_GENERATED, "function main() {}", "no-error.ets") {}
30 }
31
TEST_F(Es2PandaLibTest,TypeError)32 TEST_F(Es2PandaLibTest, TypeError)
33 {
34 CONTEXT(ES2PANDA_STATE_ASM_GENERATED, ES2PANDA_STATE_ERROR, "function main() { let x: int = \"\" }", "error.ets")
35 {
36 ASSERT_EQ(GetImpl()->ContextState(GetContext()), ES2PANDA_STATE_ERROR);
37 auto diagnostics = GetImpl()->GetSemanticErrors(GetContext());
38 ASSERT_EQ(reinterpret_cast<const ark::es2panda::util::DiagnosticStorage *>(diagnostics)->size(), 1);
39 }
40 }
41
TEST_F(Es2PandaLibTest,ListIdentifiers)42 TEST_F(Es2PandaLibTest, ListIdentifiers)
43 {
44 char const *text = R"XXX(
45 class C {
46 n: string = "oh"
47 }
48
49 function main() {
50 let c = new C
51 console.log(c.n + 1) // type error, but not syntax error
52 }
53 )XXX";
54
55 struct Arg {
56 es2panda_Impl const *impl;
57 es2panda_Context *ctx;
58 std::vector<std::string> ids {};
59 };
60
61 auto func = [](es2panda_AstNode *ast, void *argp) {
62 auto *a = reinterpret_cast<Arg *>(argp);
63 if (a->impl->IsIdentifier(ast)) {
64 a->ids.emplace_back(a->impl->IdentifierName(a->ctx, ast));
65 }
66 };
67 CONTEXT(ES2PANDA_STATE_PARSED, text, "list-ids.ets")
68 {
69 Arg arg {GetImpl(), GetContext()};
70 AstNodeForEach(func, &arg);
71
72 std::vector<std::string> expected {"C", "n", "string", "constructor", "constructor", "main",
73 "c", "C", "console", "log", "c", "n"};
74 ASSERT_EQ(arg.ids, expected);
75 }
76 }
77
TEST_F(Es2PandaLibTest,LogDiagnostic)78 TEST_F(Es2PandaLibTest, LogDiagnostic)
79 {
80 CONTEXT(ES2PANDA_STATE_ASM_GENERATED, "", "user-error.ets")
81 {
82 auto pos = GetImpl()->CreateSourcePosition(GetContext(), 3, 5);
83 auto diagnostics = GetImpl()->GetPluginErrors(GetContext());
84 auto diagnosticStorage = reinterpret_cast<const ark::es2panda::util::DiagnosticStorage *>(diagnostics);
85 ASSERT_EQ(diagnosticStorage->size(), 0);
86
87 auto kind = GetImpl()->CreateDiagnosticKind(GetContext(), "Test {}", ES2PANDA_PLUGIN_ERROR);
88 const char **args = new const char *[1];
89 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
90 args[0] = "1";
91 GetImpl()->LogDiagnostic(GetContext(), kind, args, 1, pos);
92 delete[] args;
93 ASSERT_EQ(diagnosticStorage->size(), 1);
94 ASSERT_EQ((*diagnosticStorage)[0]->Type(), ark::es2panda::util::DiagnosticType::PLUGIN_ERROR);
95 ASSERT_EQ((*diagnosticStorage)[0]->Message(), "Test 1");
96 }
97 }
98
99 } // namespace
100