1 /** 2 * Copyright (c) 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 #ifndef ES2PANDA_TEST_UTILS_ASM_TEST_H 17 #define ES2PANDA_TEST_UTILS_ASM_TEST_H 18 19 #include "compiler/lowering/phase.h" 20 #include "util/options.h" 21 #include <gtest/gtest.h> 22 23 namespace util_alias = ark::es2panda::util; 24 25 using AnnotationMap = std::map<std::string, std::vector<std::pair<std::string, std::string>>>; 26 using AnnotationValueType = std::variant<bool, uint8_t, uint16_t, uint32_t, uint64_t, float, double, std::string>; 27 28 namespace test::utils { 29 30 class AsmTest : public testing::Test { 31 public: 32 AsmTest(); 33 ~AsmTest() override; 34 GetProgram(int argc,char const * const * argv,std::string_view fileName,std::string_view src)35 static std::unique_ptr<ark::pandasm::Program> GetProgram(int argc, char const *const *argv, 36 std::string_view fileName, std::string_view src) 37 { 38 auto de = util_alias::DiagnosticEngine(); 39 auto options = std::make_unique<util_alias::Options>( 40 argv[0], de); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 41 if (!options->Parse(ark::Span(argv, argc))) { 42 return nullptr; 43 } 44 45 ark::Logger::ComponentMask mask {}; 46 mask.set(ark::Logger::Component::ES2PANDA); 47 ark::Logger::InitializeStdLogging(options->LogLevel(), mask); 48 49 ark::es2panda::Compiler compiler(options->GetExtension(), options->GetThread()); 50 ark::es2panda::SourceFile input(fileName, src, options->IsModule()); 51 52 return std::unique_ptr<ark::pandasm::Program>(compiler.Compile(input, *options, de)); 53 } 54 55 ark::pandasm::Function *GetFunction(std::string_view functionName, 56 const std::map<std::string, ark::pandasm::Function> &table); 57 ark::pandasm::Record *GetRecord(std::string_view recordName, const std::unique_ptr<ark::pandasm::Program> &program); 58 void CompareActualWithExpected(const std::string &expectedValue, ark::pandasm::ScalarValue *scalarValue, 59 const std::string &field); 60 61 void CheckAnnoDecl(ark::pandasm::Program *program, const std::string &annoName, 62 const std::vector<std::pair<std::string, std::string>> &expectedAnnotations); 63 64 void CheckLiteralArrayTable( 65 ark::pandasm::Program *program, 66 const std::vector<std::pair<std::string, std::vector<AnnotationValueType>>> &expectedLiteralArrayTable); 67 68 void CheckAnnotation(const std::vector<std::pair<std::string, std::string>> &expectedValues, 69 const ark::pandasm::AnnotationData &annotation); 70 71 void CheckRecordAnnotations(ark::pandasm::Program *program, const std::string &recordName, 72 const AnnotationMap &expectedAnnotations); 73 74 void CheckModuleAnnotation(ark::pandasm::Program *program, const std::string &recordName, bool isModule, 75 const std::vector<std::string> &expectedAnnotations = {}); 76 77 void CheckRecordWithoutAnnotations(ark::pandasm::Program *program, const std::string &recordName, 78 bool isModule = false); 79 80 void CheckFunctionAnnotations(ark::pandasm::Program *program, const std::string &functionName, bool isStatic, 81 const AnnotationMap &expectedAnnotations); 82 83 void CheckFunctionWithoutAnnotations(ark::pandasm::Program *program, const std::string &functionName, 84 bool isStatic); 85 86 void CheckFunctionParameterAnnotations(ark::pandasm::Program *program, const std::string &functionName, 87 bool isStatic, const uint32_t ¶mIndex, 88 const AnnotationMap &expectedAnnotations); 89 90 void CheckFunctionParameterWithoutAnnotations(ark::pandasm::Program *program, const std::string &functionName, 91 bool isStatic, const uint32_t ¶mIndex); 92 93 void CheckClassFieldAnnotations(ark::pandasm::Program *program, const std::string &recordName, 94 const std::string &fieldName, const AnnotationMap &expectedAnnotations); 95 96 void CheckClassFieldWithoutAnnotations(ark::pandasm::Program *program, const std::string &recordName, 97 const std::string &fieldName); 98 99 void SetCurrentProgram(std::string_view src); 100 101 std::unique_ptr<ark::pandasm::Program> GetCurrentProgram(std::string_view src); 102 103 NO_COPY_SEMANTIC(AsmTest); 104 NO_MOVE_SEMANTIC(AsmTest); 105 106 protected: 107 // NOLINTBEGIN(misc-non-private-member-variables-in-classes) 108 std::unique_ptr<ark::pandasm::Program> program_ {}; 109 // NOLINTEND(misc-non-private-member-variables-in-classes) 110 }; 111 112 } // namespace test::utils 113 114 #endif // ES2PANDA_TEST_UTILS_ASM_TEST_H 115