• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <algorithm>
18 #include "macros.h"
19 
20 #include "assembler/assembly-program.h"
21 #include "ir/astDump.h"
22 #include "ir/expressions/literals/stringLiteral.h"
23 #include "bytecode_optimizer/bytecodeopt_options.h"
24 #include "compiler/compiler_logger.h"
25 #include "mem/arena_allocator.h"
26 #include "mem/pool_manager.h"
27 #include "es2panda.h"
28 #include "util/arktsconfig.h"
29 #include "util/generateBin.h"
30 #include "libpandabase/mem/mem.h"
31 #include "test/utils/panda_executable_path_getter.h"
32 #include "test/utils/asm_test.h"
33 
34 namespace {
35 
36 class TestParams {
37 public:
TestParams(std::string_view src,std::initializer_list<const char * > argsList)38     TestParams(std::string_view src, std::initializer_list<const char *> argsList) : src_ {src}
39     {
40         argsList_.push_back(test::utils::PandaExecutablePathGetter::Get()[0]);
41         argsList_.insert(argsList_.end(), argsList.begin(), argsList.end());
42     }
GetSrc()43     auto GetSrc()
44     {
45         return src_;
46     };
GetExec()47     auto GetExec()
48     {
49         return argsList_[0];
50     };
GetFilename()51     auto GetFilename()
52     {
53         return "dummy.ets";
54     };
GetArgs() const55     auto GetArgs() const
56     {
57         return argsList_;
58     };
59 
60 private:
61     std::string_view src_;
62     std::vector<const char *> argsList_;
63 };
64 
DumpJsonSimple()65 TestParams DumpJsonSimple()
66 {
67     static constexpr std::string_view SRC =
68         "\
69         function main(args: FixedArray<String>): int {\
70             let a: int = 2;\
71             let b: int = 3;\
72             return a + b;\
73         }";
74 
75     return TestParams {SRC, {}};
76 }
77 
DumpJsonUTF16Char()78 TestParams DumpJsonUTF16Char()
79 {
80     static constexpr std::string_view SRC =
81         "\
82         function main(args: FixedArray<String>): int {\
83             let a: char = c'\\uDBFF';\
84             let b: char = c'\\uDC00';\
85             console.log(a);\
86             console.log(b);\
87             return 0;\
88         }";
89 
90     return TestParams {SRC, {}};
91 }
92 
DumpEtsSrcSimple()93 TestParams DumpEtsSrcSimple()
94 {
95     static constexpr std::string_view SRC =
96         "\
97         function main(args: FixedArray<String>): int {\
98             let a: int = 2;\
99             let b: int = 3;\
100             return a + b;\
101         }";
102 
103     return TestParams {SRC,
104                        {"--extension=ets",
105                         "--dump-ets-src-before-phases=plugins-after-parse,plugins-after-check,plugins-after-lowering"}};
106 }
107 
108 class ASTDumperTest : public testing::TestWithParam<TestParams> {
109 public:
ASTDumperTest()110     ASTDumperTest()
111     {
112         ark::mem::MemConfig::Initialize(0, 0, ark::es2panda::COMPILER_SIZE, 0, 0, 0);
113         ark::PoolManager::Initialize(ark::PoolType::MMAP);
114     }
115 
~ASTDumperTest()116     ~ASTDumperTest() override
117     {
118         ark::PoolManager::Finalize();
119         ark::mem::MemConfig::Finalize();
120     };
121 
122     NO_COPY_SEMANTIC(ASTDumperTest);
123     NO_MOVE_SEMANTIC(ASTDumperTest);
124 };
125 
TEST_P(ASTDumperTest,CheckJsonDump)126 TEST_P(ASTDumperTest, CheckJsonDump)
127 {
128     auto param = GetParam();
129     auto argsList = param.GetArgs();
130     auto program = std::unique_ptr<ark::pandasm::Program> {
131         test::utils::AsmTest::GetProgram(argsList.size(), argsList.data(), param.GetFilename(), param.GetSrc())};
132     ASSERT(program);
133 
134     auto dumpStr = program->JsonDump();
135     ASSERT(!dumpStr.empty());
136 }
137 
138 INSTANTIATE_TEST_SUITE_P(ASTDumperTestParamList, ASTDumperTest,
139                          ::testing::Values(DumpJsonSimple(), DumpJsonUTF16Char()));
140 
TEST_F(ASTDumperTest,CheckSrcDump)141 TEST_F(ASTDumperTest, CheckSrcDump)
142 {
143     std::stringstream dumpStr;
144     std::streambuf *prevcoutbuf = std::cout.rdbuf(dumpStr.rdbuf());
145 
146     auto param = DumpEtsSrcSimple();
147     auto argsList = param.GetArgs();
148     auto program = std::unique_ptr<ark::pandasm::Program> {
149         test::utils::AsmTest::GetProgram(argsList.size(), argsList.data(), param.GetFilename(), param.GetSrc())};
150     ASSERT(program);
151 
152     std::cout.rdbuf(prevcoutbuf);
153 
154     ASSERT(program);
155     ASSERT(!dumpStr.str().empty());
156 }
157 
158 }  // namespace
159