1 /** 2 * Copyright (c) 2024-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 18 #include "util/string_util.h" 19 20 using namespace testing::ext; 21 using namespace panda::guard; 22 23 /** 24 * @tc.name: string_util_test_001 25 * @tc.desc: test string util split function 26 * @tc.type: FUNC 27 * @tc.require: 28 */ 29 HWTEST(StringUtilUnitTest, string_util_test_001, TestSize.Level4) 30 { 31 std::string delimiter = "#"; 32 33 std::string str = "##a#"; 34 auto result = StringUtil::Split(str, delimiter); 35 EXPECT_EQ(result.size(), 1); 36 EXPECT_EQ(result[0], "a"); 37 38 str = "error"; 39 result = StringUtil::Split(str, delimiter); 40 EXPECT_EQ(result.size(), 1); 41 EXPECT_EQ(result[0], "error"); 42 } 43 44 /** 45 * @tc.name: string_util_test_002 46 * @tc.desc: test string util strict split function 47 * @tc.type: FUNC 48 * @tc.require: 49 */ 50 HWTEST(StringUtilUnitTest, string_util_test_002, TestSize.Level4) 51 { 52 std::string str = "##a#"; 53 std::string delimiter = "#"; 54 auto result = StringUtil::StrictSplit(str, delimiter); 55 EXPECT_EQ(result.size(), 4); 56 EXPECT_EQ(result[0], ""); 57 EXPECT_EQ(result[1], ""); 58 EXPECT_EQ(result[2], "a"); 59 EXPECT_EQ(result[3], ""); 60 } 61 62 /** 63 * @tc.name: string_util_test_003 64 * @tc.desc: test string util is anonymous function 65 * @tc.type: FUNC 66 * @tc.require: 67 */ 68 HWTEST(StringUtilUnitTest, string_util_test_003, TestSize.Level4) 69 { 70 auto result = StringUtil::IsAnonymousFunctionName(""); 71 EXPECT_EQ(result, true); 72 73 result = StringUtil::IsAnonymousFunctionName("^1"); 74 EXPECT_EQ(result, true); 75 76 result = StringUtil::IsAnonymousFunctionName("^11"); 77 EXPECT_EQ(result, true); 78 79 result = StringUtil::IsAnonymousFunctionName("^a"); 80 EXPECT_EQ(result, true); 81 82 result = StringUtil::IsAnonymousFunctionName("^a1"); 83 EXPECT_EQ(result, true); 84 85 result = StringUtil::IsAnonymousFunctionName("^1a"); 86 EXPECT_EQ(result, true); 87 88 result = StringUtil::IsAnonymousFunctionName("^aa"); 89 EXPECT_EQ(result, true); 90 91 result = StringUtil::IsAnonymousFunctionName("^g"); 92 EXPECT_EQ(result, false); 93 94 result = StringUtil::IsAnonymousFunctionName("^1g"); 95 EXPECT_EQ(result, false); 96 97 result = StringUtil::IsAnonymousFunctionName("^g1"); 98 EXPECT_EQ(result, false); 99 100 result = StringUtil::IsAnonymousFunctionName("^gg"); 101 EXPECT_EQ(result, false); 102 }