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 #include "plugin_conversion_rule_test.h" 17 18 #include <iostream> 19 #include <fstream> 20 #include <sstream> 21 22 namespace { HasContainRelation(const std::string & searchStr,const std::string & content)23bool HasContainRelation(const std::string &searchStr, const std::string &content) 24 { 25 return content.find(searchStr) != std::string::npos; 26 } 27 } // namespace 28 29 namespace test::utils { PluginConversionRuleTest()30PluginConversionRuleTest::PluginConversionRuleTest() 31 { 32 const std::string pluginLibImplPath {"../../../generated/es2panda_lib/es2panda_lib_impl.inc"}; 33 ReadStringFromFile(pluginLibImplPath); 34 } 35 RemoveWhitespace(const std::string & str)36std::string PluginConversionRuleTest::RemoveWhitespace(const std::string &str) 37 { 38 std::string result; 39 for (char elem : str) { 40 if (std::isspace(static_cast<unsigned char>(elem)) == 0) { 41 result += elem; 42 } 43 } 44 return result; 45 } 46 HasMatched(const std::string & searchStr) const47bool PluginConversionRuleTest::HasMatched(const std::string &searchStr) const 48 { 49 return HasContainRelation(searchStr, cApiImplFileStr_); 50 } 51 ReadStringFromFile(const std::string & filePath)52void PluginConversionRuleTest::ReadStringFromFile(const std::string &filePath) 53 { 54 std::filesystem::path cApiImplFileRelativePath {filePath}; 55 auto cApiImplFilePath = std::filesystem::current_path() / cApiImplFileRelativePath; 56 std::ifstream fileStream(cApiImplFilePath, std::ios::in); 57 if (!fileStream.is_open()) { 58 std::cerr << "Failed to open file " << cApiImplFilePath << std::endl; 59 cApiImplFileStr_ = ""; 60 return; 61 } 62 63 std::stringstream strStream; 64 strStream << fileStream.rdbuf(); 65 cApiImplFileStr_ = RemoveWhitespace(strStream.str()); 66 } 67 } // namespace test::utils 68