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_LSP_API_TEST_H 17 #define ES2PANDA_TEST_LSP_API_TEST_H 18 19 #include "lsp/include/api.h" 20 #include "lsp/include/internal_api.h" 21 #include "public/public.h" 22 #include <gtest/gtest.h> 23 24 class LSPAPITests : public testing::Test { 25 public: 26 LSPAPITests() = default; 27 ~LSPAPITests() override = default; 28 29 NO_COPY_SEMANTIC(LSPAPITests); 30 NO_MOVE_SEMANTIC(LSPAPITests); 31 ContextState(es2panda_Context * context)32 es2panda_ContextState ContextState(es2panda_Context *context) 33 { 34 auto *s = reinterpret_cast<ark::es2panda::public_lib::Context *>(context); 35 return s->state; 36 } 37 38 template <typename Ast> GetAstFromContext(es2panda_Context * context)39 Ast *GetAstFromContext(es2panda_Context *context) 40 { 41 auto ctx = reinterpret_cast<ark::es2panda::public_lib::Context *>(context); 42 auto ast = reinterpret_cast<Ast *>(ctx->parserProgram->Ast()); 43 return ast; 44 } 45 CreateTempFile(std::vector<std::string> files,std::vector<std::string> texts)46 std::vector<std::string> CreateTempFile(std::vector<std::string> files, std::vector<std::string> texts) 47 { 48 std::vector<std::string> result = {}; 49 auto tempDir = testing::TempDir(); 50 for (size_t i = 0; i < files.size(); i++) { 51 auto outPath = tempDir + files[i]; 52 std::ofstream outStream(outPath); 53 if (outStream.fail()) { 54 std::cerr << "Failed to open file: " << outPath << std::endl; 55 return result; 56 } 57 outStream << texts[i]; 58 outStream.close(); 59 result.push_back(outPath); 60 } 61 return result; 62 } 63 CreateTestRange()64 Range CreateTestRange() 65 { 66 int const endPos = 10; 67 Position start(1, 0); 68 Position end(1, endPos); 69 return Range(start, end); 70 } 71 SetUp()72 void SetUp() override 73 { 74 range_ = CreateTestRange(); 75 message_ = R"(Test message)"; 76 } 77 78 protected: 79 // NOLINTBEGIN(misc-non-private-member-variables-in-classes) 80 Range range_; 81 std::string message_; 82 // NOLINTEND(misc-non-private-member-variables-in-classes) 83 }; 84 85 #endif 86